def test_input_or_despite_factors(self): """Test part of the process of checking contradiction.""" rural = Entity(name="Rural's telephone directory") compilation = Predicate( content="${rural_s_telephone_directory} was a compilation of facts" ) idea = Predicate(content="${rural_s_telephone_directory} was an idea") copyrightable = Fact( predicate=Predicate( content="${rural_s_telephone_directory} was copyrightable" ), terms=rural, ) left = Procedure( outputs=(copyrightable), inputs=(Fact(predicate=compilation, terms=rural)), ) right = Procedure( outputs=(copyrightable), inputs=(Fact(predicate=idea, terms=rural)), ) context = ContextRegister() explanation = Explanation.from_context(context) gen = left._has_input_or_despite_factors_implied_by_all_inputs_of( right, explanation ) with pytest.raises(StopIteration): next(gen)
def test_new_context_replace_fact(self): predicate_shot = Predicate(content="$shooter shot $victim") predicate_no_gun = Predicate(content="$suspect had a gun", truth=False) predicate_told = Predicate(content="$speaker told $hearer $statement") shot = Fact(predicate=predicate_shot, terms=[Entity(name="Alice"), Entity(name="Bob")]) told = Fact( predicate=predicate_told, terms=[Entity(name="Henry"), Entity(name="Jenna"), shot], ) no_gun = Fact(predicate=predicate_no_gun, terms=Entity(name="Dan")) changes = ContextRegister.from_lists( [ Entity(name="Alice"), Entity(name="Henry"), Entity(name="Jenna"), shot ], [ Entity(name="Dan"), Entity(name="Leslie"), Entity(name="Mike"), no_gun ], ) result = told.new_context(changes) assert ( "told <Mike> the fact it was false that <Dan> had a gun".lower() in result.short_string.lower())
def test_does_not_share_all_factors(self, make_statement): left = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="Oakland"), Entity(name="San Francisco"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) right = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="San Francisco"), Entity(name="Oakland"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) assert not left.shares_all_factors_with(right)
def test_not_same_nonmatching_terms(self, make_statement): left = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="Oakland"), Entity(name="San Francisco"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) right = FactorGroup([ Statement( predicate=Predicate(content="$suburb was a suburb of $city"), terms=( Entity(name="San Francisco"), Entity(name="Oakland"), ), ), make_statement["more"], Statement(predicate="$city was sunny", terms=Entity(name="Oakland")), ]) assert not left.means(right)
def test_term_placeholders_do_not_change_result(self): left = Predicate( content="$organizer1 and $organizer2 planned for $player1 to play $game with $player2." ) right = Predicate( content="$promoter1 and $promoter2 planned for $player1 to play $chess with $player2." ) assert left.means(right)
def test_term_positions_change_result(self): left = Predicate( content="$organizer1 and $organizer2 planned for $player1 to play $game with $player2." ) right = Predicate( content="$organizer1 and $organizer2 planned for $organizer1 to play $game with $organizer2." ) assert not left.means(right)
def test_equality_factor_from_equal_predicate(self): predicate = Predicate(content="$speaker greeted $listener") equal_predicate = Predicate(content="$speaker greeted $listener") fact = Fact(predicate=predicate, terms=[Entity(name="Al"), Entity(name="Meg")]) fact_b = Fact(predicate=equal_predicate, terms=[Entity(name="Al"), Entity(name="Meg")]) assert fact.means(fact_b)
def test_add_unrelated_factors(self): murder = Fact( predicate=Predicate(content="$person committed a murder"), terms=Entity(name="Al"), ) crime = Fact( predicate=Predicate(content="$person committed a crime"), terms=Entity(name="Al"), ) assert murder + crime is None
def test_means_despite_plural(self): directory = Entity(name="Rural's telephone directory", plural=False) listings = Entity(name="Rural's telephone listings", plural=True) directory_original = Statement( predicate=Predicate(content="$thing was original"), terms=directory) listings_original = Statement( predicate=Predicate(content="$thing were original"), terms=listings) assert directory_original.means(listings_original)
def test_is_beard_implied( self, facial_hair_over_5mm, facial_hair_on_or_below_chin, facial_hair_uninterrupted, outcome, fake_beard_client, make_beard_rule, ): beard = Entity(name="a facial feature") sec_4 = fake_beard_client.read("/test/acts/47/4/") was_facial_hair = Predicate(content="$thing was facial hair") fact_was_facial_hair = Fact(predicate=was_facial_hair, terms=beard) hypothetical = Rule( procedure=Procedure( inputs=[ fact_was_facial_hair, Fact( predicate=Comparison( content="the length of $thing was", sign=">=", expression=Q_("5 millimeters"), truth=facial_hair_over_5mm, ), terms=beard, ), Fact( predicate=Predicate( content="$thing occurred on or below the chin", truth=facial_hair_on_or_below_chin, ), terms=beard, ), Fact( predicate=Predicate( content= "$thing existed in an uninterrupted line from the front " "of one ear to the front of the other ear below the nose", truth=facial_hair_uninterrupted, ), terms=beard, ), ], outputs=Fact(predicate=Predicate(content="$thing was a beard"), terms=beard), ), enactments=sec_4, ) meets_chin_test = make_beard_rule[0].implies(hypothetical) meets_ear_test = make_beard_rule[1].implies(hypothetical) assert outcome == meets_chin_test or meets_ear_test
def test_factor_no_contradiction_no_truth_value(self): fact = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) fact_no_truth = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) assert not fact.contradicts(fact_no_truth) assert not fact_no_truth.contradicts(fact)
def test_factor_different_predicate_truth_unequal(self): predicate = Predicate(content="$shooter shot $victim") false_predicate = Predicate(content="$shooter shot $victim", truth=False) fact = Fact(predicate=predicate, terms=[Entity(name="Al"), Entity(name="Meg")]) fact_b = Fact(predicate=false_predicate, terms=[Entity(name="Al"), Entity(name="Meg")]) assert not fact.means(fact_b)
def test_same_predicate_true_vs_false(self): fact = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) fiction = Fact( predicate=Predicate(content="$person was a person", truth=False), terms=Entity(name="Alice"), ) assert fact.contradicts(fiction) assert fact.truth != fiction.truth
def test_statement_implies_no_truth_value(self): fact = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) whether = Fact( predicate=Predicate(content="$person was a person", truth=None), terms=Entity(name="Alice"), ) assert fact >= whether assert not whether > fact
def test_specific_implies_generic_form_of_another_fact(self): concrete = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) generic_merperson = Fact( predicate=Predicate(content="$person was a merperson"), terms=Entity(name="Alice"), generic=True, ) assert concrete > generic_merperson
def test_specific_statement_implies_generic(self): concrete = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) generic = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), generic=True, ) assert concrete > generic assert not generic > concrete
def test_get_factor_from_recursive_search(self): predicate_shot = Predicate(content="$shooter shot $victim") predicate_told = Predicate(content="$speaker told $hearer $statement") shot = Fact(predicate=predicate_shot, terms=[Entity(name="Alice"), Entity(name="Bob")]) told = Fact( predicate=predicate_told, terms=[Entity(name="Henry"), Entity(name="Jenna"), shot], ) factors = told.recursive_terms assert factors["<Alice>"].compare_keys(Entity(name="Alice"))
def test_specific_implies_generic_explain(self): concrete = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), ) generic = Fact( predicate=Predicate(content="$person was a person"), terms=Entity(name="Alice"), generic=True, ) answer = concrete.explain_implication(generic) assert (str(concrete), generic) in answer.context.items()
def test_contradicts_if_present_one_absent(self): shot_fact = Fact( predicate=Predicate(content="$shooter shot $victim"), terms=[Entity(name="Alice"), Entity(name="Bob")], ) shot_false = Fact( predicate=Predicate(content="$shooter shot $victim", truth=False), terms=[Entity(name="Alice"), Entity(name="Bob")], absent=True, ) assert shot_fact._contradicts_if_present(shot_false, ContextRegister()) assert shot_false._contradicts_if_present(shot_fact, ContextRegister())
def test_generic_terms_with_different_text_equal(self): predicate = Predicate(content="$speaker greeted $listener") different_predicate = Predicate(content="$speaker attacked $listener") fact = Fact( predicate=predicate, terms=[Entity(name="Al"), Entity(name="Meg")], generic=True, ) different_fact = Fact( predicate=different_predicate, terms=[Entity(name="Al"), Entity(name="Meg")], generic=True, ) assert fact.means(different_fact)
def test_union_with_string_fails(self): murder = Fact( predicate=Predicate(content="$person committed a murder"), terms=Entity(name="Al"), ) with pytest.raises(TypeError): murder | "a string"
def test_false_does_not_contradict_absent(self): absent_fact = Statement( predicate=Predicate( content="${rural_s_telephone_directory} was copyrightable", truth=True), terms=[Entity(name="Rural's telephone directory")], absent=True, ) false_fact = Statement( predicate=Predicate(content="${the_java_api} was copyrightable", truth=False), terms=[Entity(name="the Java API", generic=True, plural=False)], absent=False, ) assert not false_fact.contradicts(absent_fact) assert not absent_fact.contradicts(false_fact)
def test_no_sign_allowed_for_predicate(self): with pytest.raises(ValidationError): Predicate( content="the date when $work was created was", sign=">=", expression=date(1978, 1, 1), )
def test_generic_terms_equal(self): generic = Statement(predicate="something happened", generic=True) generic_false = Statement(predicate=Predicate( content="something happened", truth=False), generic=True) assert generic.means(generic_false) assert generic_false.means(generic)
def test_true_and_false_generic_terms_equal(self): predicate = Predicate(content="$speaker greeted $listener") false_predicate = Predicate(content="$speaker greeted $listener", truth=False) fact = Fact( predicate=predicate, terms=[Entity(name="Al"), Entity(name="Meg")], generic=True, ) false_fact = Fact( predicate=false_predicate, terms=[Entity(name="Ed"), Entity(name="Imogene")], generic=True, ) assert fact.means(false_fact)
def test_make_str_plural(self, context, expected): phrase = ( "$thing were names, towns, and telephone numbers of telephone subscribers" ) predicate = Predicate(content=phrase) with_context = predicate._content_with_terms(context) assert with_context.startswith(expected)
def test_contradiction_of_group(self): lived_at = Predicate(content="$person lived at $residence") bob_lived = Fact( predicate=lived_at, terms=[Entity(name="Bob"), Entity(name="Bob's house")] ) carl_lived = Fact( predicate=lived_at, terms=[Entity(name="Carl"), Entity(name="Carl's house")] ) distance_long = Comparison( content="the distance from the center of $city to $residence was", sign=">=", expression="50 miles", ) statement_long = Fact( predicate=distance_long, terms=[Entity(name="Houston"), Entity(name="Bob's house")], ) distance_short = Comparison( content="the distance from the center of $city to $residence was", sign="<=", expression="10 kilometers", ) statement_short = Fact( predicate=distance_short, terms=[Entity(name="El Paso"), Entity(name="Carl's house")], ) left = FactorGroup([bob_lived, statement_long]) right = FactorGroup([carl_lived, statement_short]) explanation = left.explain_contradiction(right) assert explanation.context["<Houston>"].name == "El Paso" assert contradicts(left, right)
def test_cannot_add_statement_to_enactment(self, e_search_clause): statement = Fact( predicate=Predicate(content="$person committed a murder"), terms=Entity(name="Al"), ) with pytest.raises(ValidationError): e_search_clause + statement
def nest_predicate_fields(cls, values): """Move fields passed to the Fact model that really belong to the Predicate model.""" type_str = values.pop("type", "") if type_str and type_str.lower() != "fact": raise ValidationError(f"type {type_str} was passed to Fact model") if isinstance(values.get("predicate"), str): values["predicate"] = Predicate(content=values["predicate"]) if "truth" in values: values["predicate"].truth = values.pop("truth") for field_name in ["content", "truth", "sign", "expression"]: if field_name in values: values["predicate"] = values.get("predicate", {}) values["predicate"][field_name] = values.pop(field_name) if isinstance(values.get("predicate"), dict) and values["predicate"].get("content"): for sign in { **QuantityRange.opposite_comparisons, **QuantityRange.normalized_comparisons, }: if sign in values["predicate"]["content"]: content, quantity_text = values["predicate"][ "content"].split(sign) values["predicate"]["content"] = content.strip() values["predicate"]["expression"] = quantity_text.strip() values["predicate"]["sign"] = sign break return values
def test_add_simple_rules(self): """ A simple form of two Rules from Feist, with no Enactments. Even though the rules have different generic Factors (i.e. Entities), the __add__ function will make one Rule using the generic Factors from the operand on the left, but will give it the output from the operand on the right. """ context = Entity(name="the Pythagorean theorem") three = Entity(name="the number three") fact_not_original = Rule( procedure=Procedure( inputs=Fact(predicate=Predicate(content="$work was a fact"), terms=context), outputs=Fact( predicate=Predicate(content="$work was an original work", truth=False), terms=context, ), ), universal=True, ) unoriginal_not_copyrightable = Rule( procedure=Procedure( inputs=Fact( predicate=Predicate(content="$work was an original work", truth=False), terms=three, ), outputs=Fact( predicate=Predicate(content="${work} was copyrightable", truth=False), terms=three, ), ), universal=True, ) facts_not_copyrightable = fact_not_original + unoriginal_not_copyrightable assert len(facts_not_copyrightable.inputs) == 1 assert str(facts_not_copyrightable.inputs[0]).endswith( "act that <the Pythagorean theorem> was a fact") assert len(facts_not_copyrightable.outputs) == 2 assert "false that <the Pythagorean theorem> was copyrightable" in str( facts_not_copyrightable)