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))
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
def test_activation_eq(): """ Check if we can compare two activations """ from pyknow.activation import Activation act1 = Activation(rule=None, facts=(1, 2, 3, 3)) act2 = Activation(rule=None, facts=(1, 2, 3, 4)) act3 = Activation(rule=None, facts=(1, 2, 3, 3)) assert act1 != act2 assert act1 == act3
def test_activation_sum(): """ Check if we can sum two activations and the result is correct """ from pyknow.activation import Activation act1 = Activation(rule=None, facts=(1, 2, 3, 3)) act2 = Activation(rule=None, facts=(1, 2, 3, 4)) act3 = act1 + act2 assert act3.facts == (1, 2, 3, 4)
def test_activation_facts_only_iterable(): """ Check if activation facts are required to be a tuple """ from pyknow.activation import Activation from pyknow import Rule import pytest # SHOULD NOT RAISE Activation(rule=Rule(), facts=tuple()) Activation(rule=Rule(), facts=list()) Activation(rule=Rule(), facts=dict()) with pytest.raises(TypeError): Activation(rule=Rule(), facts=None)
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
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
def _activate(self, token): """Activate this node for the given token.""" info = token.to_info() activation = Activation(self.rule, frozenset( info.data), {k: v for k, v in info.context if isinstance(k, str)}) if token.is_valid(): if info not in self.memory: self.memory.add(info) if activation in self.removed: self.removed.remove(activation) else: self.added.add(activation) else: try: self.memory.remove(info) except ValueError: pass else: if activation in self.added: self.added.remove(activation) else: self.removed.add(activation)
def test_activation_facts_only_tuple(): """ Check if activation facts are required to be a tuple """ from pyknow.activation import Activation from pyknow.rule import Rule import pytest with pytest.raises(ValueError): Activation(rule=Rule(), facts=list())
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))
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))
def test_Rule_empty_matches_with_initial_fact(): from pyknow.rule import Rule from pyknow.factlist import FactList from pyknow.fact import InitialFact from pyknow.activation import Activation from pyknow.match import Capturation r = Rule() fl = FactList() fl.declare(InitialFact()) assert Activation(None, (0, )) in list(r.get_activations(fl, Capturation()))
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))
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
def get_activations(self, factlist, capturations): """ Extract activations from a given fact. Will be aggregated later by a ``Rule`` """ FACT_WATCHER.debug("Getting activations") if not capturations: FACT_WATCHER.debug("No capturations found") for idx, fact in factlist.facts.items(): if self.matches(fact, Context()): act = Activation(rule=None, facts=(idx, )) FACT_WATCHER.debug("Yielding uncontexted act: %s", act) yield act else: for (idx, fact), caps in product(factlist.facts.items(), capturations.items()): cap_facts, ctx = caps if self.matches(fact, ctx): facts = tuple(set([int(a) for a in cap_facts] + [idx])) act = Activation(rule=None, facts=facts, context=ctx) FACT_WATCHER.debug("Yielding contexted act: %s", act) yield act FACT_WATCHER.debug("Got all activations")
def test_Rule_with_empty_Fact_matches_all_Facts(): from pyknow.rule import Rule from pyknow.factlist import FactList from pyknow.fact import Fact, L from pyknow.activation import Activation r = Rule(Fact()) fl = FactList() fl.declare(Fact(something=L(True))) fl.declare(Fact(something=L(False))) fl.declare(Fact(something=L(3))) activations = list(r.get_activations(fl)) assert len(activations) == 3 for i in range(3): assert Activation(None, (i, )) in activations
def test_activation_has_not_rule(): """ Check if we can create an activation without rule """ from pyknow.activation import Activation assert Activation(rule=None, facts=tuple())
def get_activations(self, factlist, capturations): if next(super().get_activations(factlist, capturations), None) is None: if factlist.facts: yield Activation(rule=None, facts=(0, ))
def test_activation_eq(): from pyknow.activation import Activation assert Activation(None, []) == Activation(None, []) assert (None, []) != Activation(None, [])
def test_activation_has_facts(): """ Check if activation has facts property """ from pyknow.activation import Activation from pyknow import Rule assert hasattr(Activation(rule=Rule(), facts=[]), 'facts')
def test_activation_in_set(): from pyknow.activation import Activation assert Activation(None, []) in {Activation(None, [])}