Exemplo n.º 1
0
    def test_no_contradiction_quantity_outputs(self, make_rule):
        """
        Added to try to break Procedure.contradiction_between_outputs.

        "the distance between {the stockpile of trees} and a parking area
        used by personnel and patrons of {Hideaway Lodge} was <= 5 feet"
        does not contradict
        "the distance between {circus} and a parking area used by personnel
        and patrons of {Hideaway Lodge} was > 5 feet"

        Given that the context parameter indicates the "circus" is not the
        same place as "the stockpile of trees", there's no contradiction.
        """
        stockpile_means_stockpile = ContextRegister()
        stockpile_means_stockpile.insert_pair(
            key=Entity(name="the stockpile of trees"),
            value=Entity(name="the stockpile of trees"),
        )
        assert not make_rule["h_output_distance_less"].contradicts(
            make_rule["h_output_farther_different_entity"],
            context=stockpile_means_stockpile,
        )
        assert not make_rule["h_output_farther_different_entity"].contradicts(
            make_rule["h_output_distance_less"],
            context=stockpile_means_stockpile)
Exemplo n.º 2
0
 def test_opinion_posits_holding_dict_context(self, make_entity,
                                              make_response):
     """
     Having the Watt case posit a holding from the Brad
     case, but replacing one generic factor with a factor
     from Watt.
     """
     mock_client = FakeClient(responses=make_response)
     to_read = load_holdings("holding_brad.yaml")
     holdings = readers.read_holdings(to_read, client=mock_client)
     breading = OpinionReading()
     breading.clear_holdings()
     breading.posit(holdings)
     expectation_not_reasonable = breading.holdings[6]
     changes = ContextRegister()
     changes.insert_pair(
         key=expectation_not_reasonable.generic_terms()[0],
         value=make_entity["watt"],
     )
     context_holding = expectation_not_reasonable.new_context(changes)
     wreading = OpinionReading()
     wreading.clear_holdings()
     wreading.posit(context_holding)
     string = str(context_holding)
     assert "<Wattenburg> lived at <Bradley's house>" in string
     assert "<Wattenburg> lived at <Bradley's house>" in str(
         wreading.holdings[-1])
Exemplo n.º 3
0
 def test_consistent_factor_groups_with_context(self):
     alice_like_craig = ContextRegister()
     alice_like_craig.insert_pair(alice, craig)
     assert FactorGroup([alice_rich, bob_poor]).consistent_with(
         FactorGroup([dan_poor, craig_rich]),
         context=alice_like_craig,
     )
Exemplo n.º 4
0
 def test_group_inconsistent_with_one_statement(self):
     group = FactorGroup(
         [self.slower_specific_statement, self.farm_statement])
     register = ContextRegister()
     register.insert_pair(Entity(name="the car"), Entity(name="the pickup"))
     assert not group.consistent_with(self.faster_statement,
                                      context=register)
Exemplo n.º 5
0
 def test_one_statement_consistent_with_group(self):
     group = FactorGroup(
         [self.slower_general_statement, self.farm_statement])
     register = ContextRegister()
     register.insert_pair(Entity(name="the pickup"),
                          Entity(name="the pickup"))
     assert self.faster_statement.consistent_with(group, context=register)
Exemplo n.º 6
0
 def test_contradictory_facts_about_same_entity(self, make_statement):
     left = make_statement["less_than_20"]
     right = make_statement["more_meters"]
     register = ContextRegister()
     register.insert_pair(left.generic_terms()[0], right.generic_terms()[0])
     assert not left.consistent_with(right, register)
     assert left.explain_consistent_with(right, register) is None
Exemplo n.º 7
0
 def test_context_register_valid(self, make_statement):
     expected = ContextRegister()
     expected.insert_pair(Entity(name="Alice"), Entity(name="Bob"))
     generated = next(make_statement["no_crime"]._context_registers(
         make_statement["no_crime_entity_order"], operator.le))
     assert len(generated) == len(expected)
     assert generated["<Alice>"].name == expected["<Alice>"].name
Exemplo n.º 8
0
    def test_explain_consistency(self):
        register = ContextRegister()
        register.insert_pair(Entity(name="Al"), Entity(name="Alice"))
        explanation = self.fact_al.explain_consistent_with(
            self.fact_alice, register)

        assert "<the bull> is like <the cow>" in explanation.context.reason
        assert "=[Entity(name='Al'" in repr(explanation)
Exemplo n.º 9
0
 def test_new_context(self):
     context = ContextRegister()
     context.insert_pair(
         Entity(name="Twitter user"),
         Entity(name="Python Software Foundation", generic=False),
     )
     new = self.generic_authority.new_context(context)
     assert "by Python" in str(new)
Exemplo n.º 10
0
 def test_explanations_consistent_with(self, make_statement):
     left = make_statement["less_than_20"]
     right = make_statement["more_meters"]
     register = ContextRegister()
     register.insert_pair(left.generic_terms()[0], right.generic_terms()[0])
     explanations = list(
         left.explanations_consistent_with(right, context=register))
     assert not explanations
Exemplo n.º 11
0
 def test_contradictory_facts_about_same_entity(self):
     register = ContextRegister()
     register.insert_pair(Entity(name="Alice"), Entity(name="Bob"))
     assert not self.small.consistent_with(self.big, register)
     explanations = list(
         self.small.explanations_consistent_with(self.big,
                                                 context=register))
     assert not explanations
Exemplo n.º 12
0
 def test_not_internally_consistent_with_context(self, make_statement):
     context = ContextRegister()
     context.insert_pair(Entity(name="Alice"), Entity(name="Alice"))
     context.insert_pair(Entity(name="Bob"), Entity(name="Bob"))
     group = FactorGroup(
         [make_statement["shooting"], make_statement["no_shooting"]])
     with pytest.raises(ValueError):
         group.internally_consistent()
Exemplo n.º 13
0
 def test_import_to_context_register(self, make_entity, watt_factor):
     left = ContextRegister.from_lists(
         to_replace=[watt_factor["f7"], make_entity["motel"]],
         replacements=[watt_factor["f7_swap_entities"], make_entity["trees"]],
     )
     right = ContextRegister()
     right.insert_pair(make_entity["trees"], make_entity["motel"])
     assert len(left.merged_with(right)) == 3
Exemplo n.º 14
0
 def test_limited_possible_contexts_identical_factor(self, watt_factor, make_entity):
     left = watt_factor["f2"]
     right = watt_factor["f2"]
     context = ContextRegister()
     context.insert_pair(make_entity["motel"], make_entity["motel"])
     contexts = list(left.possible_contexts(right, context=context))
     assert len(contexts) == 1
     assert contexts[0].check_match(make_entity["watt"], make_entity["watt"])
Exemplo n.º 15
0
 def test_context_register_valid(self, make_entity, watt_factor):
     expected = ContextRegister()
     expected.insert_pair(make_entity["motel"], make_entity["watt"])
     generated = next(
         watt_factor["f1"]._context_registers(
             watt_factor["f1_entity_order"], operator.le
         )
     )
     assert generated == expected
Exemplo n.º 16
0
    def test_register_for_matching_entities(self):
        known = ContextRegister()
        alice = Entity(name="Alice")
        craig = Entity(name="Craig")
        known.insert_pair(alice, craig)

        gen = alice._context_registers(other=craig, comparison=means, context=known)
        register = next(gen)
        assert register.get("<Alice>") == craig
Exemplo n.º 17
0
    def test_entity_consistency_identity_not_equality(self, make_statement):

        register = ContextRegister()
        register.insert_pair(key=Entity(name="Dan"), value=Entity(name="Dan"))
        update = make_statement["irrelevant_3"].update_context_register(
            make_statement["irrelevant_3_new_context"],
            context=register,
            comparison=means,
        )
        assert all(register is None for register in update)
Exemplo n.º 18
0
 def test_check_entity_consistency_false(self, make_statement):
     context = ContextRegister()
     context.insert_pair(key=Entity(name="circus"),
                         value=Entity(name="Alice"))
     update = make_statement["irrelevant_3"].update_context_register(
         make_statement["irrelevant_3_new_context"],
         comparison=means,
         context=context,
     )
     assert all(register is None for register in update)
Exemplo n.º 19
0
 def test_group_inconsistent_with_single_factor(self):
     group = FactorGroup(
         [self.slower_specific_statement, self.farm_statement])
     register = ContextRegister()
     register.insert_pair(Entity(name="the car"), Entity(name="the pickup"))
     assert not group.consistent_with(self.faster_statement,
                                      context=register)
     assert not consistent_with(
         group, self.faster_statement, context=register)
     assert repr(group).startswith("FactorGroup([Statement")
     assert "30.0 mile / hour" in repr(group)
Exemplo n.º 20
0
 def test_limited_possible_contexts_identical_factor(self):
     statement = Statement(predicate=self.paid,
                           terms=[Entity(name="Al"),
                                  Entity(name="Xu")])
     context = ContextRegister()
     context.insert_pair(Entity(name="Al"), Entity(name="Xu"))
     contexts = list(statement.possible_contexts(statement,
                                                 context=context))
     assert len(contexts) == 1
     assert contexts[0].check_match(Entity(name="Al"), Entity(name="Xu"))
     assert "Entity(name='Xu'" in repr(contexts)
Exemplo n.º 21
0
    def test_context_registers_for_complex_comparison(self):
        context_names = ContextRegister()
        context_names.insert_pair(key=Entity(name="Alice"),
                                  value=Entity(name="Bob"))
        context_names.insert_pair(key=Entity(name="Bob"),
                                  value=Entity(name="Alice"))

        swapped_entities = self.relevant_fact.new_context(context_names)
        gen = swapped_entities._context_registers(self.relevant_fact,
                                                  operator.ge)
        register = next(gen)
        assert register.matches.get("<Alice>").compare_keys(Entity(name="Bob"))
Exemplo n.º 22
0
 def test_all_generic_terms_match_in_statement(self):
     predicate = Predicate(content="the telescope pointed at $object")
     morning = Statement(predicate=predicate,
                         terms=Entity(name="Morning Star"))
     evening = Statement(predicate=predicate,
                         terms=Entity(name="Evening Star"))
     left = FactorGroup(morning)
     right = FactorGroup(evening)
     context = ContextRegister()
     context.insert_pair(Entity(name="Morning Star"),
                         Entity(name="Evening Star"))
     assert left._all_generic_terms_match(right, context=context)
Exemplo n.º 23
0
    def test_inconsistent_factor_groups(self):
        """
        If Alice is considered analagous to Dan the two sets of
        statements would be inconsistent, but not if
        Alice is considered analagous to Craig.
        """
        alice_like_dan = ContextRegister()
        alice_like_dan.insert_pair(alice, dan)

        assert not FactorGroup([alice_rich, bob_poor]).consistent_with(
            FactorGroup([dan_poor, craig_rich]), context=alice_like_dan
        )
Exemplo n.º 24
0
    def test_not_contradictory_factor_groups(self):
        """
        Because the ContextRegister matches up the two contexts
        consistently, it's impossible to reach a contradiction.
        """
        alice_like_craig = ContextRegister()
        alice_like_craig.insert_pair(alice, craig)

        assert not FactorGroup((alice_rich, bob_poor)).contradicts(
            FactorGroup((dan_poor, craig_rich)),
            context=alice_like_craig,
        )
Exemplo n.º 25
0
    def test_context_register(self):
        """
        There will be a match because both object are :class:`.Term`.
        """
        left = Entity(name="peanut butter")
        right = Entity(name="jelly")
        update = left._context_registers(right, operator.ge)
        assert any(register is not None for register in update)

        update = left._context_registers(right, operator.le)
        expected = ContextRegister()
        expected.insert_pair(left, right)
        assert any(register == expected for register in update)
Exemplo n.º 26
0
    def test_import_to_context_register(self, make_statement):

        left = ContextRegister.from_lists(
            to_replace=[make_statement["shooting"],
                        Entity(name="Alice")],
            replacements=[
                make_statement["shooting_entity_order"],
                Entity(name="Bob")
            ],
        )
        right = ContextRegister()
        right.insert_pair(Entity(name="Bob"), Entity(name="Alice"))
        assert len(left.merged_with(right)) == 3
Exemplo n.º 27
0
 def test_holding_with_non_generic_value(self, make_entity, make_response):
     """
     This test originally required a ValueError, but why should it?
     """
     mock_client = FakeClient(responses=make_response)
     reading = OpinionReading()
     to_read = load_holdings("holding_brad.yaml")
     holdings = readers.read_holdings(to_read, client=mock_client)
     reading.posit(holdings)
     expectation_not_reasonable = reading.holdings[6]
     generic_patch = expectation_not_reasonable.generic_terms()[1]
     changes = ContextRegister()
     changes.insert_pair(generic_patch, make_entity["trees_specific"])
     context_change = expectation_not_reasonable.new_context(changes)
     string = context_change.short_string
     assert "plants in the stockpile of trees was at least 3" in string
Exemplo n.º 28
0
    def test_implication_complex_explain(self):
        """
        Check that when .implies() provides a ContextRegister as an "explanation",
        it uses elements only from the left as keys and from the right as values.
        """

        context_names = ContextRegister()
        context_names.insert_pair(key=Entity(name="Alice"),
                                  value=Entity(name="Craig"))
        context_names.insert_pair(key=Entity(name="Bob"),
                                  value=Entity(name="Dan"))

        complex_whether = self.relevant_whether.new_context(context_names)
        explanation = self.relevant_fact.explain_implication(complex_whether)
        assert explanation.context.get("<Alice>").compare_keys(
            Entity(name="Craig"))
        assert "<Alice> is like <Craig>, and <Bob> is like <Dan>" in str(
            explanation)
        assert explanation.context.get(Entity(name="Craig").key) is None
        assert explanation.context.get(Entity(name="Alice").key).compare_keys(
            Entity(name="Craig"))
Exemplo n.º 29
0
 def test_inconsistent_statements_about_corresponding_entities(self):
     """
     Even though Alice and Bob are both generics, it's known that
     Alice in the first context corresponds with Alice in the second.
     So there's no contradiction.
     """
     p_small_weight = Comparison(
         content="the amount of gold $person possessed was",
         sign="<",
         expression=Q_("1 gram"),
     )
     p_large_weight = Comparison(
         content="the amount of gold $person possessed was",
         sign=">=",
         expression=Q_("100 kilograms"),
     )
     alice = Entity(name="Alice")
     bob = Entity(name="Bob")
     alice_rich = Statement(predicate=p_large_weight, terms=alice)
     bob_poor = Statement(predicate=p_small_weight, terms=bob)
     register = ContextRegister()
     register.insert_pair(alice, alice)
     assert not alice_rich.contradicts(bob_poor, context=register)
Exemplo n.º 30
0
 def test_group_contradicts_single_factor(self):
     group = FactorGroup([self.slower_specific_statement, self.farm_statement])
     register = ContextRegister()
     register.insert_pair(Entity(name="the car"), Entity(name="the pickup"))
     assert group.contradicts(self.faster_statement, context=register)