Implemented a first and basic version of a Rete rule engine
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from typing import List
|
||||
from sheerkarete.join_node import JoinNode
|
||||
from sheerkarete.common import WME
|
||||
|
||||
|
||||
class AlphaMemory:
|
||||
|
||||
def __init__(self, key, condition, items=None, successors=None):
|
||||
"""
|
||||
Stores a set of WMEs (items). If activating an activated wme does not
|
||||
exist, then it addes it. It also right activates all of its successors,
|
||||
which correspond to beta nodes.
|
||||
:param items: set of WMEs that match this memory
|
||||
:param successors: list of JoinNode
|
||||
"""
|
||||
self.key = key
|
||||
self.condition = condition
|
||||
self.items: List[WME] = items if items else []
|
||||
self.successors: List[JoinNode] = successors if successors else []
|
||||
self.reference_count = 0
|
||||
|
||||
def activation(self, wme: WME) -> None:
|
||||
"""
|
||||
Adds the WME to the alpha memory and then right activates the children
|
||||
in the beta network. Note, these are activated in reversed order to
|
||||
prevent duplicate matches.
|
||||
"""
|
||||
if not self.condition.test(wme):
|
||||
return
|
||||
|
||||
self.items.append(wme)
|
||||
wme.amems.append(self)
|
||||
for child in reversed(self.successors):
|
||||
child.right_activation(wme)
|
||||
Reference in New Issue
Block a user