Esempio n. 1
0
def test_agenda_has_activations():
    """ Agenda object has activations property """

    from pyknow.agenda import Agenda
    from collections import deque
    assert hasattr(Agenda(), "activations")
    assert isinstance(Agenda().activations, deque)
Esempio n. 2
0
def test_Depth_update_agenda_different_salience():
    from random import shuffle

    from pyknow.strategies import Depth
    from pyknow.activation import Activation
    from pyknow.rule import Rule
    from pyknow.agenda import Agenda

    act1 = Activation(rule=Rule(salience=1), facts=(1, ))
    act2 = Activation(rule=Rule(salience=2), facts=(2, ))
    act3 = Activation(rule=Rule(salience=3), facts=(3, ))
    act4 = Activation(rule=Rule(salience=4), facts=(4, ))

    acts = [act1, act2, act3, act4]
    shuffle(acts)

    st = Depth()
    a = Agenda()

    for act in acts:
        st.update_agenda(a, [act])

    order = list(a.activations)
    assert (order.index(act4) < order.index(act3) < order.index(act2) <
            order.index(act1))
Esempio n. 3
0
def test_Depth_update_agenda_asertion_order_affects_agenda_order_1():
    from pyknow.strategies import Depth
    from pyknow.activation import Activation
    from pyknow.rule import Rule
    from pyknow.agenda import Agenda

    act1 = Activation(rule=Rule(), facts=(1, ))
    act2 = Activation(rule=Rule(), facts=(2, ))
    first = {act1, act2}

    act3 = Activation(rule=Rule(), facts=(3, ))
    act4 = Activation(rule=Rule(), facts=(4, ))
    second = {act3, act4}

    a = Agenda()

    st = Depth()

    st.update_agenda(a, first)
    st.update_agenda(a, second)

    left, right = set(list(a.activations)[:2]), set(list(a.activations)[2:])

    assert left == second
    assert right == first
Esempio n. 4
0
    def reset(self, **kwargs):
        """
        Performs a reset as per CLIPS behaviour (resets the
        agenda and factlist and declares InitialFact())

        Any keyword argument passed to `reset` will be passed to @DefFacts
        which have those arguments on their signature.

        .. note:: If persistent facts have been added, they'll be
                  re-declared.
        """

        self.agenda = Agenda()
        self.facts = FactList()

        self.matcher.reset()

        deffacts = []
        for deffact in self.get_deffacts():
            signature = inspect.signature(deffact)
            if not any(p.kind == inspect.Parameter.VAR_KEYWORD
                       for p in signature.parameters.values()):
                # There is not **kwargs defined. Pass only the defined
                # names.
                args = set(signature.parameters.keys())
                deffacts.append(
                    deffact(**{k: v
                               for k, v in kwargs.items() if k in args}))
            else:
                deffacts.append(deffact(**kwargs))

        # Declare all facts yielded by deffacts
        self.__declare(*chain.from_iterable(deffacts))

        self.running = False
Esempio n. 5
0
 def __init__(self):
     self.context = Context()
     self._fixed_facts = []
     self.facts = FactList()
     self.running = False
     self.agenda = Agenda()
     self.strategy = self.__strategy__()
     self._parent = False
     self.shared_attributes = {}
Esempio n. 6
0
def test_DepthStrategy_update_agenda_no_facts_returns_empty_agenda():
    from pyknow.strategies import DepthStrategy
    from pyknow.agenda import Agenda

    st = DepthStrategy()
    a = Agenda()

    st.update_agenda(a, [], [])

    assert not a.activations
Esempio n. 7
0
def test_agenda_get_next():
    """
    Agenda has a get_next method that gets from activations and inserts
    into executed
    """

    from pyknow.agenda import Agenda
    agenda = Agenda()

    agenda.activations.append("Foo")
    assert agenda.get_next() == "Foo"
    assert "Foo" not in agenda.activations
Esempio n. 8
0
    def reset(self):
        """
        Performs a reset as per CLIPS behaviour (resets the
        agenda and factlist and declares InitialFact())

        .. note:: If persistent facts have been added, they'll be
                  re-declared.
        """

        self.agenda = Agenda()
        self.facts = FactList()
        self.__declare(InitialFact())
        self.load_initial_facts()
        self.strategy.update_agenda(self.agenda, self.get_activations())
Esempio n. 9
0
def test_Strategy_update_agenda_update_executed(strategy):
    from pyknow import strategies
    from pyknow.activation import Activation
    from pyknow.rule import Rule
    from pyknow.agenda import Agenda

    act1 = Activation(rule=Rule(), facts=(1, ))
    act2 = Activation(rule=Rule(), facts=(2, ))

    st = getattr(strategies, strategy)()
    a = Agenda()
    a.executed.add(act1)

    st.update_agenda(a, [act2])

    assert act1 not in a.executed
Esempio n. 10
0
def test_Depth_update_agenda_activations_to_agenda():
    from pyknow.strategies import Depth
    from pyknow.activation import Activation
    from pyknow.rule import Rule
    from pyknow.agenda import Agenda

    act1 = Activation(rule=Rule(), facts=(1, ))
    act2 = Activation(rule=Rule(), facts=(2, ))

    a = Agenda()

    st = Depth()
    st.update_agenda(a, {act1, act2})

    assert act1 in a.activations
    assert act2 in a.activations
Esempio n. 11
0
    def __init__(self):
        self.running = False
        self.facts = FactList()
        self.agenda = Agenda()

        if (isinstance(self.__matcher__, type)
                and issubclass(self.__matcher__, abstract.Matcher)):
            self.matcher = self.__matcher__(self)
        else:
            raise TypeError("__matcher__ must be a subclass of Matcher")

        if (isinstance(self.__strategy__, type)
                and issubclass(self.__strategy__, abstract.Strategy)):
            self.strategy = self.__strategy__()
        else:
            raise TypeError("__strategy__ must be a subclass of Strategy")
def test_DepthStrategy_update_agenda_asertion_order_affects_agenda_order_3():
    """

    From Clips docs on Depth Strategy::

      Newly activated rules are placed above all rules of the same salience.
      For example, given that facta activates rule1 and rule2 and factb
      activates rule3 and rule4, then if facta is asserted before factb, rule3
      and rule4 will be above rule1 and rule2 on the agenda. However, the
      position of rule1 relative to rule2 and rule3 relative to rule4 will be
      arbitrary.

    """
    from pyknow.strategies import DepthStrategy
    from pyknow.activation import Activation
    from pyknow import Rule
    from pyknow.agenda import Agenda
    from pyknow import Fact
    from pyknow.factlist import FactList

    fl = FactList()

    f1 = Fact(1)
    fl.declare(f1)

    f2 = Fact(2)
    fl.declare(f2)

    act1 = Activation(rule=Rule(), facts=(f1, ))
    act2 = Activation(rule=Rule(), facts=(f1, ))
    act3 = Activation(rule=Rule(), facts=(f2, ))
    act4 = Activation(rule=Rule(), facts=(f2, ))

    a = Agenda()

    st = DepthStrategy()

    st.update_agenda(a, [act1, act2, act3, act4], [])
    order = list(a.activations)

    assert (order.index(act4) > order.index(act1)
            and order.index(act4) > order.index(act2))
    assert (order.index(act3) > order.index(act1)
            and order.index(act3) > order.index(act2))
Esempio n. 13
0
    def reset(self):
        """
        Performs a reset as per CLIPS behaviour (resets the
        agenda and factlist and declares InitialFact())

        .. note:: If persistent facts have been added, they'll be
                  re-declared.
        """

        self.agenda = Agenda()
        self.facts = FactList()

        self.matcher.reset()

        # Declare all deffacts
        for deffact in self.get_deffacts():
            for fact in deffact():
                self.__declare(fact)

        self.running = False
Esempio n. 14
0
def test_DepthStrategy_update_agenda_different_salience():
    from random import shuffle

    from pyknow.strategies import DepthStrategy
    from pyknow.activation import Activation
    from pyknow import Rule
    from pyknow import Fact
    from pyknow.agenda import Agenda
    from pyknow.factlist import FactList

    flist = FactList()

    f1 = Fact(1)
    flist.declare(f1)

    f2 = Fact(2)
    flist.declare(f2)

    f3 = Fact(3)
    flist.declare(f3)

    f4 = Fact(4)
    flist.declare(f4)

    act1 = Activation(rule=Rule(salience=1), facts=(f1, ))
    act2 = Activation(rule=Rule(salience=2), facts=(f2, ))
    act3 = Activation(rule=Rule(salience=3), facts=(f3, ))
    act4 = Activation(rule=Rule(salience=4), facts=(f4, ))

    acts = [act1, act2, act3, act4]
    shuffle(acts)

    st = DepthStrategy()
    a = Agenda()

    for act in acts:
        st.update_agenda(a, acts, [])

    order = list(a.activations)
    assert (order.index(act4) < order.index(act3) < order.index(act2) <
            order.index(act1))
Esempio n. 15
0
def test_DepthStrategy_update_agenda_assertion_order_affects_agenda_order_1():
    from pyknow.strategies import DepthStrategy
    from pyknow.activation import Activation
    from pyknow import Rule
    from pyknow.agenda import Agenda
    from pyknow import Fact
    from pyknow.factlist import FactList

    fl = FactList()

    f1 = Fact(1)
    fl.declare(f1)

    f2 = Fact(2)
    fl.declare(f2)

    f3 = Fact(3)
    fl.declare(f3)

    f4 = Fact(4)
    fl.declare(f4)

    act1 = Activation(rule=Rule(), facts=(f1, ))
    act2 = Activation(rule=Rule(), facts=(f2, ))
    first = [act1, act2]

    act3 = Activation(rule=Rule(), facts=(f3, ))
    act4 = Activation(rule=Rule(), facts=(f4, ))
    second = [act3, act4]

    a = Agenda()

    st = DepthStrategy()

    oldact = a.activations
    st.update_agenda(a, first, [])
    assert list(a.activations) == list(reversed(first))

    st.update_agenda(a, second, first)
    assert list(a.activations) == list(reversed(second))
Esempio n. 16
0
def test_DepthStrategy_update_agenda_activations_to_agenda():
    from pyknow.strategies import DepthStrategy
    from pyknow.activation import Activation
    from pyknow import Rule
    from pyknow.agenda import Agenda
    from pyknow import Fact
    from pyknow.factlist import FactList

    fl = FactList()
    f1 = Fact(1)
    fl.declare(f1)
    f2 = Fact(2)
    fl.declare(f2)

    act1 = Activation(rule=Rule(), facts=(f1, ))
    act2 = Activation(rule=Rule(), facts=(f2, ))

    a = Agenda()

    st = DepthStrategy()
    st.update_agenda(a, [act1, act2], [])

    assert act1 in a.activations
    assert act2 in a.activations
Esempio n. 17
0
def test_agenda_has_executed_set():
    """ Agenda object has executed property """

    from pyknow.agenda import Agenda
    assert hasattr(Agenda(), "executed")
    assert isinstance(Agenda().executed, set)