forked from buguroo/pyknow
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hypothesis rule based state machine test case for testing strategies …
…behavior. Fix #10.
- Loading branch information
1 parent
a14de22
commit 1dac991
Showing
2 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pytest | ||
|
||
from hypothesis import strategies as st | ||
from hypothesis import assume, settings | ||
from hypothesis.database import DirectoryBasedExampleDatabase | ||
from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule, invariant, consumes | ||
|
||
from experta.activation import Activation | ||
from experta.agenda import Agenda | ||
from experta.engine import KnowledgeEngine | ||
from experta.fact import Fact | ||
from experta.factlist import FactList | ||
from experta.rule import Rule | ||
from experta.strategies import DepthStrategy | ||
|
||
|
||
def get_rule_stm(strategy): | ||
class StrategyStateMachine(RuleBasedStateMachine): | ||
def __init__(self): | ||
super(StrategyStateMachine, self).__init__() | ||
self.model = set() | ||
self.agenda = Agenda() | ||
self.strategy = strategy() | ||
self.fss = set() | ||
|
||
activations = Bundle("activations") | ||
|
||
@rule(target=activations, | ||
r=st.integers(min_value=0), | ||
fs=st.sets(st.integers(min_value=0), min_size=1)) | ||
def declare(self, r, fs): | ||
assume((r, frozenset(fs)) not in self.fss) | ||
self.fss.add((r, frozenset(fs))) | ||
|
||
fs = [Fact(i, __factid__=i) for i in fs] | ||
act = Activation(Rule(Fact(r)), facts=tuple(fs)) | ||
|
||
# Update agenda | ||
self.strategy.update_agenda(self.agenda, [act], []) | ||
|
||
# Update model | ||
self.model |= set([act]) | ||
|
||
return act | ||
|
||
@rule(act=consumes(activations)) | ||
def retract(self, act): | ||
# Update agenda | ||
self.strategy.update_agenda(self.agenda, [], [act]) | ||
|
||
# Update model | ||
self.model -= set([act]) | ||
|
||
@invariant() | ||
def values_agree(self): | ||
assert set(self.agenda.activations) == self.model | ||
|
||
return StrategyStateMachine | ||
|
||
|
||
test_depthstrategy_state_machine = get_rule_stm(DepthStrategy).TestCase |