forked from kraison/vivace-graph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rete.lisp
89 lines (76 loc) · 2.82 KB
/
rete.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(in-package #:vivace-graph)
(defconstant +rete-wildcard+ :*)
(defconstant +beta-memory+ :beta)
(defconstant +join-node+ :join)
(defconstant +p-node+ :p-node)
(defstruct (rete-net
(:predicate rete-net?)
(:conc-name rn-))
(alpha-memory (make-hash-table :synchronized t :test 'equal))
(beta-memory nil)
(beta-memory-index (make-hash-table :synchronized t :test 'equal)))
(defstruct (alpha-memory
(:predicate alpha-memory?)
(:conc-name alpha-))
triples children)
(defstruct (token
(:predicate token?)
(:conc-name token-))
parent triple)
(defstruct (rete-node
(:predicate rete-node?)
(:conc-name rete-node-))
type children parent tokens alpha-memory tests)
(defstruct (join-node-test
(:conc-name nil))
arg1-field arg2-field levels-up)
(defgeneric add-rule (rule))
(defun join-test (tests token wme)
)
; (dolist (test tests)
; (let ((arg1 (funcall (arg1-field test) wme)))
; (dolist
(defmethod left-activate ((node rete-node) (token token) &optional (wme triple))
(case (rete-node-type node)
(+beta-memory+
(let ((token (make-token :parent token :triple wme)))
(push token (rete-node-tokens node))
(dolist (child (rete-node-children node))
(left-activate child token))))
(+join-node+
(dolist (wme (alpha-triples (rete-node-alpha-memory node)))
(when (join-test (rete-node-tests node) token wme)
(dolist (child (rete-node-children node))
(left-activate child token wme)))))
(+p-node+ nil)))
(defmethod right-activate ((node rete-node) (wme triple))
(case (rete-node-type node)
(+beta-memory+ nil)
(+join-node+
(dolist (token (rete-node-tokens (rete-node-parent node)))
(when (join-test (rete-node-tests node) token wme)
(dolist (child (rete-node-children node))
(left-activate child token wme)))))
(+p-node+ nil)))
(defmethod activate-alpha-memory ((am alpha-memory) (triple triple))
(push triple (alpha-triples am))
(dolist (child (alpha-children am))
(right-activate child triple)))
(defmethod add-wme ((triple triple))
(flet ((add-wme1 (wme)
(let ((am (gethash wme (rete-net *graph*))))
(when (alpha-memory? am)
(activate-alpha-memory am triple)))))
(let ((wme (as-list triple)))
(add-wme1 wme)
(add-wme1 (list (predicate wme) (subject wme) +rete-wildcard+))
(add-wme1 (list (predicate wme) +rete-wildcard+ (object wme)))
(add-wme1 (list (predicate wme) +rete-wildcard+ +rete-wildcard+))
(add-wme1 (list +rete-wildcard+ (subject wme) (object wme)))
(add-wme1 (list +rete-wildcard+ (subject wme) +rete-wildcard+))
(add-wme1 (list +rete-wildcard+ +rete-wildcard+ (object wme)))
(add-wme1 (list +rete-wildcard+ +rete-wildcard+ +rete-wildcard+)))
))
;(defmethod add-rule ((rule rule))
; (dolist (premise (rule-premises rule))
; ))