Exemple #1
0
    def triggers_next_procedure(
        self,
        other: Procedure,
        context: Optional[Union[ContextRegister, Explanation]] = None,
    ) -> Iterator[Explanation]:
        r"""
        Test if :class:`.Factor`\s from firing ``self`` would trigger ``other``.

        .. Note::
            To be confident that ``self`` actually can trigger ``other``,
            we would have to assume that self or other has ``universal: True``
            since otherwise there could be a mismatch between what is provided
            and what is needed to trigger ``other``.

        :param other:
            another :class:`Procedure` to test to see whether it can
            be triggered by triggering ``self``

        :returns:
            whether the set of :class:`Factor`\s that exist after ``self``
            is fired could trigger ``other``
        """
        self_despite_or_input = FactorGroup((*self.despite, *self.inputs))
        self_output_or_input = FactorGroup((*self.outputs, *self.inputs))

        if not isinstance(context, Explanation):
            context = Explanation.from_context(context)

        for explanation_1 in self_output_or_input.explanations_implication(
                other.inputs_group, context=context):
            yield from self_despite_or_input.explanations_implication(
                other.despite_group, context=explanation_1)
Exemple #2
0
 def test_implication_no_repeated_explanations(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.implies(small_payments)
     all_explanations = list(
         large_payments.explanations_implication(small_payments))
     assert len(all_explanations) == 2
     limited_explanations = list(
         large_payments.explanations_implication(
             small_payments,
             context=([Entity(name="Alice")], [Entity(name="Jim")])))
     assert len(limited_explanations) == 1
Exemple #3
0
    def test_interchangeable_terms_in_factorgroup(self):
        """Fails whether the method is 'comparison' or '_verbose_comparison'"""
        left = FactorGroup([
            Statement(
                predicate=Comparison(
                    content="the distance between $place1 and $place2 was",
                    truth=True,
                    sign="<=",
                    expression="35 foot",
                ),
                terms=(
                    Entity(name="Scylla", generic=True, plural=False),
                    Entity(name="Charybdis", generic=True, plural=False),
                ),
            ),
            Statement(
                predicate=Comparison(
                    content=
                    "the distance between ${monster} and a boat used by ${hero} was",
                    truth=True,
                    sign="<=",
                    expression="5 foot",
                ),
                terms=(
                    Entity(name="Scylla", generic=True, plural=False),
                    Entity(name="Ulysses", generic=True, plural=False),
                ),
            ),
        ])

        right = FactorGroup([
            Statement(
                predicate=Comparison(
                    content="the distance between $place1 and $place2 was",
                    truth=True,
                    sign="<=",
                    expression="35 foot",
                ),
                terms=(
                    Entity(name="Charybdis", generic=True, plural=False),
                    Entity(name="Scylla", generic=True, plural=False),
                ),
            ),
            Statement(
                predicate=Comparison(
                    content=
                    "the distance between $thing and a boat used by $person was",
                    truth=True,
                    sign="<=",
                    expression="5 foot",
                ),
                terms=(
                    Entity(name="Scylla", generic=True, plural=False),
                    Entity(name="Ulysses", generic=True, plural=False),
                ),
            ),
        ])
        gen = left.explanations_implication(right)
        result = next(gen)
        assert result
Exemple #4
0
    def test_interchangeable_implication_no_repeated_explanations(self):
        nafta = FactorGroup([
            Statement(
                predicate="$country1 signed a treaty with $country2",
                terms=[Entity(name="Mexico"),
                       Entity(name="USA")],
            ),
            Statement(
                predicate="$country2 signed a treaty with $country3",
                terms=[Entity(name="USA"),
                       Entity(name="Canada")],
            ),
            Statement(
                predicate="$country3 signed a treaty with $country1",
                terms=[Entity(name="Canada"),
                       Entity(name="Mexico")],
            ),
        ])
        nato = FactorGroup([
            Statement(
                predicate="$country1 signed a treaty with $country2",
                terms=[Entity(name="USA"),
                       Entity(name="UK")],
            ),
            Statement(
                predicate="$country2 signed a treaty with $country3",
                terms=[Entity(name="UK"),
                       Entity(name="Germany")],
            ),
            Statement(
                predicate="$country3 signed a treaty with $country1",
                terms=[Entity(name="Germany"),
                       Entity(name="USA")],
            ),
        ])
        assert nafta.implies(nato)
        all_answers = list(nafta.explanations_implication(nato))
        assert len(all_answers) == 6

        limited_answers = list(
            nafta.explanations_implication(nato,
                                           context=([Entity(name="USA")],
                                                    [Entity(name="UK")])))
        assert len(limited_answers) == 2
Exemple #5
0
 def other_despite_implied(context: Explanation):
     despite_or_input = FactorGroup((*self.despite, *self.inputs))
     yield from despite_or_input.explanations_implication(
         other.despite_group,
         context=context,
     )