Implemented a first and basic version of a Rete rule engine

This commit is contained in:
2021-02-09 16:06:32 +01:00
parent 821dbed189
commit a2a8d5c5e5
110 changed files with 7301 additions and 1654 deletions
+44
View File
@@ -0,0 +1,44 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from core.rule import Rule
from sheerkarete.beta import BetaMemory
from sheerkarete.common import ReteToken
if TYPE_CHECKING: # pragma: no cover
from typing import List
from typing import Dict
from typing import Any
from sheerkarete.common import WME
from sheerkarete.common import V
class PNode(BetaMemory):
"""
A beta network node that stores the matches for productions.
"""
def __init__(self, rule: Rule, **kwargs):
super(PNode, self).__init__(**kwargs)
self.rules = [rule]
self.new: List[ReteToken] = []
def left_activation(self, token: ReteToken, wme: WME, binding: Dict[V, Any]):
new_token = ReteToken(token, wme, node=self, binding=binding)
self.items.append(new_token)
self.new.append(new_token)
def pop_new_token(self):
if self.new:
return self.new.pop()
def new_activations(self):
while self.new:
t = self.new.pop()
yield t
@property
def activations(self):
for t in self.items:
yield t