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)
class TestConsistent: 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"), ) small = Fact(predicate=p_large_weight, terms=Entity(name="Alice")) big = Fact(predicate=p_small_weight, terms=Entity(name="Bob")) 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 def test_factor_consistent_with_none(self): assert self.small.consistent_with(None)
def test_factor_pairs(self): register = ContextRegister.from_lists( [Entity(name="apple"), Entity(name="lemon")], [Entity(name="pear"), Entity(name="orange")], ) gen = register.factor_pairs() assert next(gen)[0].name == "apple"
def test_import_to_mapping_conflict(self): old_mapping = ContextRegister.from_lists([Entity(name="Al")], [Entity(name="Li")]) new_mapping = ContextRegister.from_lists([Entity(name="Al")], [Entity(name="Al")]) merged = old_mapping.merged_with(new_mapping) assert merged is None
def test_cannot_repeat_term_in_termsequence(self, make_predicate): with pytest.raises(DuplicateTermError): Statement( predicate="$person1 shot $person2", terms=[Entity(name="Al"), Entity(name="Al")], )
def test_include_of_in_string(self): fact = Statement(predicate="$suspect stole bread", terms=Entity(name="Valjean")) accusation = Assertion(statement=fact, authority=Entity(name="Javert")) assert ( "the assertion, by <Javert>, of the statement that <Valjean> stole bread" in str(accusation))
def test_possible_context_different_terms(self): left = Statement(predicate=self.bird, terms=Entity(name="Foghorn")) right = Statement(predicate=self.bird, terms=Entity(name="Woody")) contexts = list(left.possible_contexts(right)) assert len(contexts) == 1 assert contexts[0].check_match(Entity(name="Foghorn"), Entity(name="Woody"))
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)
def test_load_factor_marked_reciprocal(self): fact = Fact( predicate=Comparison( content="the distance between $place1 and $place2 was", sign="<", expression="5 miles", ), terms=TermSequence( [Entity(name="the apartment"), Entity(name="the office")]), ) assert hasattr(fact.predicate.quantity, "dimensionality") data = { "type": "fact", "content": "the distance between ${place1} and ${place2} was", "sign": "<", "expression": "5 miles", "terms": [ { "type": "entity", "name": "the office" }, { "type": "entity", "name": "the apartment" }, ], } loaded_fact = Fact(**data) assert loaded_fact.means(fact)
def test_likely_context_two_by_two(self, make_statement): left = FactorGroup( [make_statement["murder"], make_statement["large_weight"]]) right = FactorGroup((make_statement["murder_entity_order"], make_statement["small_weight_bob"])) context = next(left.likely_contexts(right)) assert context.check_match(Entity(name="Alice"), Entity(name="Bob"))
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
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)
def test_implication_complex_explain(self, make_complex_fact, make_context_register): complex_true = make_complex_fact["relevant_murder"] complex_whether = make_complex_fact[ "relevant_murder_whether"].new_context(make_context_register) explanation = complex_true.explain_implication(complex_whether) assert str( Entity(name="Alice")), Entity(name="Craig") in explanation.items()
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)
def test_error_predicate_contradict_factor(self, make_comparison): with pytest.raises(TypeError): make_comparison["exact"].contradicts( Statement( make_comparison["exact"], terms=[Entity(name="thing"), Entity(name="place")], ) )
def test_new_context(self): changes = ContextRegister.from_lists( [Entity(name="Death Star 3"), Entity(name="Kylo Ren")], [Entity(name="Death Star 1"), Entity(name="Darth Vader")], ) place = Entity(name="Death Star 3") assert place.new_context(changes) == changes.get_factor(place)
def test_string_no_truth_value(self): predicate = Predicate(content="$bird came before $ovum", truth=None) statement = Fact( predicate=predicate, terms=[Entity(name="the chicken"), Entity(name="the egg")], ) assert "whether <the chicken> came before <the egg>" in str(statement)
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)
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
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()
def test_context_from_lists_wrong_length(self): context = ContextRegister.from_lists( [Entity(name="Death Star 3"), Entity(name="Kylo Ren")], [Entity(name="Death Star 1")], ) assert "Kylo Ren" not in str(context)
def test_wrong_type_in_input_list(self, make_statement): explanation = Explanation(reasons=[]) with pytest.raises(TypeError): ContextRegister.from_lists( to_replace=[Entity(name="Al"), explanation], replacements=[Entity(name="Bo"), Entity(name="Cid")], )
def test_import_to_mapping_no_change(self): old_mapping = ContextRegister.from_lists([Entity(name="Al")], [Entity(name="Li")]) new_mapping = ContextRegister.from_lists([Entity(name="Al")], [Entity(name="Li")]) merged = old_mapping.merged_with(new_mapping) assert len(merged) == 1 assert merged["<Al>"].name == "Li"
def test_no_duplicate_explanations_consistent(self): large_payments = FactorGroup([ Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=10000, ), terms=[Entity(name="Alice"), Entity(name="Bob")], ), Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=1000, ), terms=[Entity(name="Dan"), Entity(name="Eve")], ), ]) small_payments = FactorGroup([ Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=100, ), terms={ "payer": Entity(name="Fred"), "payee": Entity(name="Greg") }, ), Statement( predicate=Comparison( content= "the number of dollars that $payer paid to $payee was", sign=">", expression=10, ), terms={ "payer": Entity(name="Jim"), "payee": Entity(name="Kim") }, ), ]) assert large_payments.consistent_with(small_payments) all_explanations = list( large_payments.explanations_consistent_with(small_payments)) assert len(all_explanations) == 24 limited_explanations = list( large_payments.explanations_consistent_with( small_payments, context=([Entity(name="Alice")], [Entity(name="Jim")]))) assert len(limited_explanations) == 6
def test_all_possible_contexts_identical_factor(self): left = Statement(predicate=self.paid, terms=[Entity(name="Irene"), Entity(name="Bob")]) contexts = list(left.possible_contexts(left)) assert len(contexts) == 2 assert any( context.check_match(Entity(name="Irene"), Entity(name="Bob")) for context in contexts)
def test_factor_contradiction_absent_predicate(self): predicate = Predicate(content="$person was a person") fact = Fact(predicate=predicate, terms=Entity(name="Alice")) absent_fact = Fact(predicate=predicate, terms=Entity(name="Alice"), absent=True) assert fact.contradicts(absent_fact) assert absent_fact.contradicts(fact)
def test_length_with_specific_term(self): statement = Statement( predicate="$person paid tax to $state", terms=[ Entity(name="Alice"), Entity(name="the State of Texas", generic=False), ], ) assert len(statement) == 1
def test_equal_referencing_diffent_generic_terms(self): predicate = Predicate(content="$speaker greeted $listener") fact = Fact(predicate=predicate, terms=[Entity(name="Al"), Entity(name="Meg")]) fact_b = Fact(predicate=predicate, terms=[Entity(name="Jim"), Entity(name="Ned")]) assert fact.means(fact_b)
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
def test_equality_because_factors_are_generic_entities(self): predicate = Predicate(content="$speaker greeted $listener") fact = Fact(predicate=predicate, terms=[Entity(name="Al"), Entity(name="Meg")]) fact_b = Fact(predicate=predicate, terms=[Entity(name="Ed"), Entity(name="Imogene")]) assert fact.means(fact_b)