Esempio n. 1
0
def test_KnowledgeEngine_reset_resets_agenda():
    from experta.engine import KnowledgeEngine
    ke = KnowledgeEngine()
    ke.agenda = None

    ke.reset()
    assert ke.agenda is not None
Esempio n. 2
0
def test_KnowledgeEngine_reset_resets_facts():
    from experta.engine import KnowledgeEngine
    ke = KnowledgeEngine()
    ke.facts = None

    ke.reset()
    assert ke.facts is not None
Esempio n. 3
0
def test_KnowledgeEngine_reset_declare_initialfact():
    from experta.engine import KnowledgeEngine

    ke = KnowledgeEngine()
    ke.reset()

    assert len(ke.facts) == 1
Esempio n. 4
0
def test_KnowledgeEngine_retract_retracts_fact():
    from experta.engine import KnowledgeEngine
    from unittest.mock import patch

    ke = KnowledgeEngine()
    with patch('experta.factlist.FactList') as mock:
        ke.facts = mock
        ke.retract({'__factid__': (0, )})
        assert mock.retract.called
Esempio n. 5
0
def test_KnowledgeEngine_get_activations_returns_a_list():
    from experta.engine import KnowledgeEngine
    ke = KnowledgeEngine()

    res = ke.get_activations()

    assert isinstance(res, tuple)
    assert isinstance(res[0], list)
    assert isinstance(res[1], list)
Esempio n. 6
0
def test_KnowledgeEngine_declare_define_fact():
    from experta.engine import KnowledgeEngine
    from experta import Fact
    from unittest.mock import patch

    ke = KnowledgeEngine()
    with patch('experta.factlist.FactList') as mock:
        ke.facts = mock
        ke.declare(Fact())
        assert mock.declare.called
Esempio n. 7
0
def test_duplicate_declare():
    from experta import KnowledgeEngine, Fact

    ke = KnowledgeEngine()

    f1 = ke.declare(Fact(color='red'))
    assert len(ke.facts) == 1

    f2 = ke.duplicate(f1, color='yellow', blink=True)
    assert len(ke.facts) == 2
    assert f2 is not f1
    assert f2['color'] == 'yellow'
    assert f2['blink']
Esempio n. 8
0
def test_modify_retracts_and_declare():
    from experta import KnowledgeEngine, Fact

    ke = KnowledgeEngine()

    f1 = ke.declare(Fact())
    assert len(ke.facts) == 1

    f2 = ke.modify(f1, _0='test_pos', key='test_key')
    assert f1 != f2
    assert len(ke.facts) == 1
    assert f2.__factid__ in ke.facts
    assert f2[0] == 'test_pos'
    assert f2['key'] == 'test_key'
Esempio n. 9
0
def test_double_underscore_raise_on_declare():
    ke = KnowledgeEngine()
    ke.reset()

    ke.declare(Fact(__startwithdoubleunderscore__=True))

    with pytest.raises(KeyError):
        ke.declare(Fact(key__with__double__underscores=True))
Esempio n. 10
0
def test_KnowledgeEngine_agenda_is_Agenda():
    from experta.engine import KnowledgeEngine
    from experta.agenda import Agenda

    ke = KnowledgeEngine()

    assert isinstance(ke.agenda, Agenda)
Esempio n. 11
0
def test_retematcher_changes_are_propagated(TestNode):
    from experta.engine import KnowledgeEngine
    from experta.fact import Fact
    from experta.matchers.rete import ReteMatcher
    from experta.matchers.rete.token import Token

    matcher = ReteMatcher(KnowledgeEngine())
    tn1 = TestNode()
    tn2 = TestNode()

    matcher.root_node.add_child(tn1, tn1.activate)
    matcher.root_node.add_child(tn2, tn2.activate)

    f1 = Fact(__factid__=1)
    f2 = Fact(__factid__=2)
    f3 = Fact(__factid__=3)
    f4 = Fact(__factid__=4)

    matcher.changes(adding=[f1, f2], deleting=[f3, f4])

    assert Token.valid(f1) in tn1.added
    assert Token.valid(f1) in tn2.added
    assert Token.valid(f2) in tn1.added
    assert Token.valid(f2) in tn2.added
    assert Token.invalid(f3) in tn1.added
    assert Token.invalid(f3) in tn2.added
    assert Token.invalid(f4) in tn1.added
    assert Token.invalid(f4) in tn2.added
Esempio n. 12
0
def test_retematcher_has_root_node():
    from experta.matchers.rete import ReteMatcher
    from experta.engine import KnowledgeEngine
    from experta.matchers.rete.nodes import BusNode

    matcher = ReteMatcher(KnowledgeEngine())
    assert hasattr(matcher, 'root_node')
    assert isinstance(matcher.root_node, BusNode)
Esempio n. 13
0
def test_fact_setitem_do_raise_after_declare():
    f = Fact()
    ke = KnowledgeEngine()
    ke.reset()
    ke.declare(f)

    with pytest.raises(RuntimeError):
        f[0] = 1
Esempio n. 14
0
def test_retematcher_changes_return_activations_if_csn():
    from experta.engine import KnowledgeEngine
    from experta.fact import Fact
    from experta.rule import Rule
    from experta.activation import Activation
    from experta.matchers.rete.nodes import ConflictSetNode
    from experta.matchers.rete import ReteMatcher

    matcher = ReteMatcher(KnowledgeEngine())
    rule = Rule()
    csn = ConflictSetNode(rule)
    matcher.root_node.add_child(csn, csn.activate)

    added, removed = matcher.changes(
        adding=[Fact(__factid__=1), Fact(__factid__=2)])

    assert len(added) == 2
    assert all(isinstance(a, Activation) for a in added)
Esempio n. 15
0
def test_KnowledgeEngine_get_rules_return_empty_list():
    from experta.engine import KnowledgeEngine

    ke = KnowledgeEngine()

    assert ke.get_rules() == []
Esempio n. 16
0
def test_retematcher_is_not_abstract():
    from experta.matchers.rete import ReteMatcher
    from experta.engine import KnowledgeEngine

    # MUST NOT RAISE
    ReteMatcher(KnowledgeEngine())
Esempio n. 17
0
def test_declare_raises_typeerror_if_conditionalelement_found():
    from experta import KnowledgeEngine, L, W, P, Fact

    ke = KnowledgeEngine()

    with pytest.raises(TypeError):
        ke.declare(Fact(L(1)))

    with pytest.raises(TypeError):
        ke.declare(Fact(W()))

    with pytest.raises(TypeError):
        ke.declare(Fact(P(lambda _: True)))

    with pytest.raises(TypeError):
        ke.declare(Fact(~L(1)))

    with pytest.raises(TypeError):
        ke.declare(Fact(L(1) | L(2)))

    with pytest.raises(TypeError):
        ke.declare(Fact(L(1) & L(2)))
Esempio n. 18
0
def test_KnowledgeEngine_default_strategy_is_Depth_instance():
    from experta.engine import KnowledgeEngine
    from experta.strategies import DepthStrategy

    assert isinstance(KnowledgeEngine().strategy, DepthStrategy)
Esempio n. 19
0
def test_KnowledgeEngine_has__facts():
    from experta.engine import KnowledgeEngine
    ke = KnowledgeEngine()
    assert hasattr(ke, 'facts')
Esempio n. 20
0
def test_KnowledgeEngine_has_agenda():
    from experta.engine import KnowledgeEngine
    ke = KnowledgeEngine()
    assert hasattr(ke, 'agenda')
Esempio n. 21
0
def test_KnowledgeEngine__facts_is_FactList():
    from experta.engine import KnowledgeEngine
    from experta.factlist import FactList

    ke = KnowledgeEngine()
    assert isinstance(ke.facts, FactList)
Esempio n. 22
0
def test_KnowledgeEngine_has_declare():
    from experta.engine import KnowledgeEngine
    ke = KnowledgeEngine()
    assert hasattr(ke, 'declare')
Esempio n. 23
0
def test_KnowledgeEngine_has_initialfacts():
    from experta.engine import KnowledgeEngine
    from experta import InitialFact

    assert list(KnowledgeEngine()._declare_initial_fact()) == [InitialFact()]