예제 #1
0
    def _validate_terms(cls, v, values, **kwargs):
        """Normalize ``terms`` to initialize Statement."""

        # make TermSequence for validation, then ignore it
        TermSequence.validate_terms(v)

        if len(v) != len(values["predicate"]):
            message = (
                "The number of items in 'terms' must be " +
                f"{len(values['predicate'])}, not {len(v)}, " +
                f"to match predicate.context_slots for '{values['predicate']}'"
            )
            raise ValueError(message)
        return v
예제 #2
0
 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)
예제 #3
0
    def new_context(self, changes: Dict[Comparable, Comparable]) -> Comparable:
        """
        Create new :class:`Factor`, replacing keys of ``changes`` with values.

        :returns:
            a version of ``self`` with the new context.
        """
        result = deepcopy(self)
        new_terms = TermSequence([
            factor.new_context(changes) for factor in self.terms_without_nulls
        ])
        result.terms = list(new_terms)
        return result
예제 #4
0
    def terms(self) -> TermSequence:
        r"""
        Get :class:`Factor`\s used in comparisons with other :class:`Factor`\s.

        :returns:
            a tuple of attributes that are designated as the ``terms``
            for whichever subclass of :class:`Factor` calls this method. These
            can be used for comparing objects using :meth:`consistent_with`
        """
        result: List[Term] = []
        result.extend(self.outputs)
        result.extend(self.inputs)
        result.extend(self.despite)
        return TermSequence(result)
예제 #5
0
    def test_cannot_put_string_in_term_sequence(self):

        entity = Entity(name="Austin")
        with pytest.raises(TypeError):
            TermSequence([entity, "Dallas as a string"])
예제 #6
0
    def test_term_sequence_from_one_term(self):

        entity = Entity(name="Austin")
        sequence = TermSequence(entity)
        assert sequence[0].name == entity.name
예제 #7
0
 def term_sequence(self) -> TermSequence:
     """Return a TermSequence of the terms in this Statement."""
     return TermSequence(self.terms)
예제 #8
0
 def term_permutations(self) -> Iterator[TermSequence]:
     """Generate permutations of context factors that preserve same meaning."""
     for pattern in self.predicate.term_index_permutations():
         sorted_terms = [x for _, x in sorted(zip(pattern, self.terms))]
         yield TermSequence(sorted_terms)
예제 #9
0
 def get_term_sequence_from_mapping(
         self, term_mapping: Mapping[str, Term]) -> TermSequence:
     """Get an ordered list of terms from a mapping of placeholder names to terms."""
     placeholders = self.placeholders
     result = [term_mapping[placeholder] for placeholder in placeholders]
     return TermSequence(result)