コード例 #1
0
def test_Rule_get_activations_needs_factlist(data):
    from pyknow.rule import Rule

    r = Rule()

    with pytest.raises(ValueError):
        r.get_activations(data)
コード例 #2
0
def test_Rule_get_activations_needs_factlist(data):
    from pyknow.rule import Rule

    r = Rule()

    with pytest.raises(ValueError):
        r.get_activations(data)
コード例 #3
0
def test_Rule_with_NOT_DEFINED():
    from pyknow.rule import Rule, NOT
    from pyknow.factlist import FactList
    from pyknow.fact import Fact, InitialFact, L, W

    r = Rule(Fact(a=L(1)), NOT(Fact(b=W(True))))

    fl = FactList()
    fl.declare(InitialFact())
    fl.declare(Fact(a=L(1)))

    activations = r.get_activations(fl)
    assert len(list(activations)) == 1

    fl.declare(Fact(b=L('SOMETHING')))
    activations = r.get_activations(fl)
    assert len(list(activations)) == 0
コード例 #4
0
def test_Rule_empty_doesnt_match_empty_factlist():
    from pyknow.rule import Rule
    from pyknow.factlist import FactList

    r = Rule()
    fl = FactList()

    assert r.get_activations(fl) == tuple()
コード例 #5
0
def test_Rule_empty_doesnt_match_empty_factlist():
    from pyknow.rule import Rule
    from pyknow.factlist import FactList

    r = Rule()
    fl = FactList()

    assert tuple(r.get_activations(fl)) == tuple()
コード例 #6
0
def test_Rule_with_only_one_NOT_match_InitialFact_if_fact_is_not_present():
    from pyknow.rule import NOT, Rule
    from pyknow.factlist import FactList
    from pyknow.fact import Fact, InitialFact, L
    from pyknow.match import Capturation

    r = Rule(NOT(Fact(something=L(True))))
    fl = FactList()
    fl.declare(InitialFact())

    assert next(r.get_activations(fl, Capturation()), None) is not None
コード例 #7
0
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

    r = Rule()
    fl = FactList()
    idx = fl.declare(InitialFact())

    assert Activation(r, (0,)) in r.get_activations(fl)
コード例 #8
0
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()))
コード例 #9
0
def test_Rule_multiple_criteria_generates_activation_with_matching_facts():
    from pyknow.rule import Rule
    from pyknow.factlist import FactList
    from pyknow.fact import Fact, L

    r = Rule(Fact(a=L(1)), Fact(b=L(2)))
    fl = FactList()
    fl.declare(Fact(a=L(1)))
    fl.declare(Fact(b=L(2)))

    activations = list(r.get_activations(fl))
    assert len(activations) == 1
    assert {0, 1} == set(activations[0].facts)
コード例 #10
0
def test_Rule_and_AND_nesting():
    from pyknow.rule import Rule, AND
    from pyknow.factlist import FactList
    from pyknow.fact import Fact, L

    r = Rule(AND(Fact(a=L(2)), Fact(b=L(1))))

    fl = FactList()
    fl.declare(Fact(a=L(2)))
    fl.declare(Fact(b=L(1)))

    activations = list(r.get_activations(fl))
    assert len(activations) == 1
    assert {0, 1} == set(activations[0].facts)
コード例 #11
0
def test_rule_with_NOT_testce():
    from pyknow.rule import Rule, NOT
    from pyknow.factlist import FactList
    from pyknow.fact import Fact, InitialFact, L, T

    r = Rule(Fact(a=L(1)), NOT(Fact(b=T(lambda c, x: x.startswith('D')))))

    fl = FactList()
    fl.declare(InitialFact())
    fl.declare(Fact(a=L(1)))

    activations = r.get_activations(fl)
    assert len(list(activations)) == 1

    fl.declare(Fact(b=L('David')))
    activations = r.get_activations(fl)
    assert len(list(activations)) == 0

    fl = FactList()
    fl.declare(InitialFact())
    fl.declare(Fact(a=L(1)))
    fl.declare(Fact(b=L('Penelope')))
    activations = r.get_activations(fl)
    assert len(list(activations)) == 1
コード例 #12
0
def test_Rule_with_empty_Fact_matches_all_Facts():
    from pyknow.rule import Rule
    from pyknow.factlist import FactList
    from pyknow.fact import Fact
    from pyknow.activation import Activation

    r = Rule(Fact())
    fl = FactList()
    fl.declare(Fact(something=True))
    fl.declare(Fact(something=False))
    fl.declare(Fact(something=3))

    activations = r.get_activations(fl)
    assert len(activations) == 3
    for i in range(3):
        assert Activation(r, (i, )) in activations
コード例 #13
0
def test_Rule_simple_testce():
    from pyknow.rule import Rule
    from pyknow.fact import Fact, T, L
    from pyknow.factlist import FactList

    r = Rule(Fact(a=T(lambda c, x: x.startswith('D'))))

    fl = FactList()
    fl.declare(Fact(a=L("David")))
    fl.declare(Fact(a=L("Penelope")))

    activations = list(r.get_activations(fl))

    assert len(activations) == 1

    assert {0} == set(activations[0].facts)
コード例 #14
0
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
コード例 #15
-1
def test_Rule_multiple_criteria_generates_activation_with_matching_facts():
    from pyknow.rule import Rule
    from pyknow.factlist import FactList
    from pyknow.fact import Fact

    r = Rule(Fact(a=1), Fact(b=2))
    fl = FactList()
    fl.declare(Fact(a=1))
    fl.declare(Fact(b=2))

    activations = r.get_activations(fl)
    assert len(activations) == 1
    assert {0, 1} == set(activations[0].facts)