This repository has been archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-rete.lisp
1214 lines (1008 loc) · 45.5 KB
/
05-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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- mode:lisp; coding:utf-8 -*-
;;; This file is part of LISA, the Lisp-based Intelligent Software
;;; Agents platform.
;;; Copyright (C) 2000 David E. Young (de.young@computer.org)
;;; Modification for JSCL under Electron platform
;;; Copyright (C) 2021 Vladimir K. Mezentsev (@vlad-km)
;;; $Id: node-tests.lisp,v 1.24 2007/09/17 22:42:39 youngde Exp $
(defvar *node-test-table*)
(defun find-test (key constructor)
(let ((test (gethash key *node-test-table*)))
(when (null test)
(setq test
(setf (gethash key *node-test-table*)
(funcall constructor))))
test))
(defun clear-node-test-table ()
(clrhash *node-test-table*))
(defgeneric class-matches-p (instance fact class))
(defmethod class-matches-p ((instance inference-engine-object) fact class)
(eq (fact-name fact) class))
(defmethod class-matches-p ((instance t) fact class)
(or (eq (fact-name fact) class)
(has-superclass fact class)))
(defun make-class-test (class)
(find-test class
#'(lambda ()
(function
(lambda (token)
;;(declare (optimize (speed 3) (debug 1) (safety 0)))
(let ((fact (token-top-fact token)))
(class-matches-p
(find-instance-of-fact fact) fact class)))))))
(defun make-simple-slot-test-aux (slot-name value negated-p)
(find-test
`(,slot-name ,value ,negated-p)
#'(lambda ()
(let ((test
(function
(lambda (token)
(equal value
(get-slot-value (token-top-fact token) slot-name))))))
(if negated-p
(complement test)
test)))))
(defun make-simple-slot-test (slot)
(make-simple-slot-test-aux (pattern-slot-name slot)
(pattern-slot-value slot)
(pattern-slot-negated slot)))
#+nul
(defmacro make-variable-test (slot-name binding)
`(function
(lambda (tokens)
(equal (get-slot-value (token-top-fact tokens) ,slot-name)
(get-slot-value
(token-find-fact tokens (binding-address ,binding))
(binding-slot-name ,binding))))))
(defun make-inter-pattern-test (slot)
(let* ((binding (pattern-slot-slot-binding slot))
(test
(function
(lambda (tokens)
(equal (get-slot-value (token-top-fact tokens) (pattern-slot-name slot))
(get-slot-value (token-find-fact tokens (binding-address binding))
(binding-slot-name binding)))))))
(if (negated-slot-p slot) (complement test) test)))
(defun maklet-predicate-test (predicate-forms bindings)
(let* ((special-vars (mapcar #'binding-variable bindings))
(valuator-form '(valuator (lambda (binding)
(if (pattern-binding-p binding)
(token-find-fact tokens (binding-address binding))
(get-slot-value (token-find-fact tokens (binding-address binding))
(binding-slot-name binding))))))
(binding-pairs
(loop for symbol-name in special-vars
for binding in bindings
append (list (list symbol-name (list 'funcall 'valuator (list 'quote binding))))))
(binding-form (push valuator-form binding-pairs))
(let-form (append (list binding-form) predicate-forms)))
(push 'let* let-form)
let-form))
(defun make-predicate-test (forms bindings &optional (negated-p nil))
(let* ((special-vars
(mapcar #'binding-variable bindings))
(body
(if (consp (first forms))
forms
(list forms)))
(test
(eval
`(lambda (tokens)
,(maklet-predicate-test `,body `,bindings)))))
(if negated-p (complement test) test)))
;;; original make-predicate-test with PROGV
#+nil
(defun make-predicate-test (forms bindings &optional (negated-p nil))
(let* ((special-vars
(mapcar #'binding-variable bindings))
(body
(if (consp (first forms))
forms
(list forms)))
(predicate
(compile nil `(lambda ()
;;(declaim ,@special-vars)
,@body)))
(test
(function
(lambda (tokens)
(progv
`(,@special-vars)
`(,@(mapcar #'(lambda (binding)
(if (pattern-binding-p binding)
(token-find-fact tokens (binding-address binding))
(get-slot-value (token-find-fact tokens (binding-address binding))
(binding-slot-name binding))))
bindings))
(funcall predicate))))))
(if negated-p (complement test) test)))
(defun maklet-pattern-predicate-test (predicate-forms bindings)
(let* ((special-vars (mapcar #'binding-variable bindings))
(valuator-form '(valuator (lambda (binding)
(if (pattern-binding-p binding)
(token-find-fact tokens (binding-address binding))
(get-slot-value (token-top-fact tokens) (binding-slot-name binding))))))
(binding-pairs
(loop for symbol-name in special-vars
for binding in bindings
append (list (list symbol-name (list 'funcall 'valuator (list 'quote binding))))))
(binding-form (push valuator-form binding-pairs))
(let-form (append (list binding-form) predicate-forms)))
(push 'let* let-form)
let-form))
(defun make-intra-pattern-predicate (forms bindings negated-p)
(let* ((special-vars
(mapcar #'binding-variable bindings))
(body
(if (consp (first forms))
forms
(list forms)))
(test
(eval
`(lambda (tokens)
,(maklet-pattern-predicate-test `,body `,bindings)))))
(if negated-p (complement test) test)))
;;; original make-intra-pattern-predicate with PROGV
#+nil
(defun make-intra-pattern-predicate (forms bindings negated-p)
(let* ((special-vars
(mapcar #'binding-variable bindings))
(body
(if (consp (first forms))
forms
(list forms)))
(predicate
(compile nil `(lambda ()
;;(declare (special ,@special-vars))
,@body)))
(test
(function
(lambda (tokens)
(progv
`(,@special-vars)
`(,@(mapcar #'(lambda (binding)
(if (pattern-binding-p binding)
(token-find-fact
tokens (binding-address binding))
(get-slot-value
(token-top-fact tokens)
(binding-slot-name binding))))
bindings))
(funcall predicate))))))
(if negated-p (complement test) test)))
(defun make-intra-pattern-constraint-test (slot)
(make-intra-pattern-predicate
(pattern-slot-constraint slot)
(pattern-slot-constraint-bindings slot)
(negated-slot-p slot)))
(defun make-intra-pattern-test (slot)
(let ((test
(function
(lambda (tokens)
(equal (get-slot-value (token-top-fact tokens) (pattern-slot-name slot))
(get-slot-value (token-top-fact tokens)
(binding-slot-name (pattern-slot-slot-binding slot))))))))
(if (negated-slot-p slot) (complement test) test)))
(defun make-behavior (function bindings)
(make-predicate-test function bindings))
;;; File: node2-test.lisp
;;; GENERIC
(defgeneric accept-token (node1 token))
(defgeneric accept-token-from-right (join-node reset-token))
(defgeneric accept-tokens-from-left (node token))
(defgeneric add-successor (node1 node1 connector))
(defgeneric clear-memories (join-node))
(defgeneric combine-tokens (token token))
(defgeneric increment-use-count (shared-node))
(defgeneric join-node-add-test (join-node test))
(defgeneric node-referenced-p (shared-node))
(defgeneric node-use-count (shared-node))
(defgeneric pass-token-to-successors (shared-node token))
(defgeneric remove-successor (node1 successor-node))
(defgeneric test-against-left-memory (node2 add-token))
(defgeneric test-against-right-memory (node2 left-tokens))
(defgeneric test-tokens (join-node left-tokens right-token))
;;;(defgeneric accept-token (terminal-node add-token))
;;;(defgeneric accept-token-from-left (join-node reset-token))
;;;(defgeneric add-node-set (parent node &optional count-p ))
;;;(defgeneric add-successor (join-node successor-node connector))
;;;(defgeneric add-successor (parent new-node connector))
;;;(defgeneric decrement-use-count (join-node))
;;;(defgeneric decrement-use-count (shared-node))
;;;(defgeneric find-existing-successor (shared-node node1))
(defgeneric pass-tokens-to-successor (join-node left-tokens))
(defgeneric remove-node-from-parent (rete-network parent child))
;;; CLASSES
(defclass shared-node ()
((successors :initform (make-hash-table :test #'equal)
:reader shared-node-successors)
(refcnt :initform 0
:accessor shared-node-refcnt)))
(defclass terminal-node ()
((rule :initarg :rule
:initform nil
:reader terminal-node-rule)))
(defclass node1 (shared-node)
((test :initarg :test
:reader node1-test)))
(defclass join-node ()
((successor :initform nil
:accessor join-node-successor)
(logical-block :initform nil
:reader join-node-logical-block)
(tests :initform (list)
:accessor join-node-tests)
(left-memory :initform (make-hash-table :test #'equal)
:reader join-node-left-memory)
(right-memory :initform (make-hash-table :test #'equal)
:reader join-node-right-memory)))
(defclass node2 (join-node) ())
(defclass node2-not (join-node) ())
(defclass node2-test (join-node) ())
(defclass node2-exists (join-node) ())
(defclass rete-network ()
((root-nodes :initform (make-hash-table)
:initarg :root-nodes
:reader rete-roots)
(node-test-cache :initform (make-hash-table :test #'equal)
:initarg :node-test-cache
:reader node-test-cache)))
(defgeneric mak-hash-successor-node (node))
(defmethod mak-hash-successor-node (node)
(write-to-string node))
(defmethod mak-hash-successor-node ((node shared-node))
(list 'shared-node
'successors (hash-table-count (shared-node-successors node))
'refcnt (shared-node-refcnt node)) )
(defmethod mak-hash-successor-node ((node node1))
(list 'node1
'successors (hash-table-count (shared-node-successors node))
'refcnt (shared-node-refcnt node)) )
(defmethod mak-hash-successor-node ((node node2))
(list 'node2
'successors (join-node-successor node)
'logical (join-node-logical-block node)
'left (hash-table-count (join-node-left-memory node))
'right (hash-table-count (join-node-right-memory node))) )
(defmethod mak-hash-successor-node ((node node2-not))
(list 'node2-not
'successors (join-node-successor node)
'logical (join-node-logical-block node)
'left (hash-table-count (join-node-left-memory node))
'right (hash-table-count (join-node-right-memory node))))
(defmethod mak-hash-successor-node ((node node2-test))
(list 'node2-test
'successors (join-node-successor node)
'logical (join-node-logical-block node)
'left (hash-table-count (join-node-left-memory node))
'right (hash-table-count (join-node-right-memory node))))
(defmethod mak-hash-successor-node ((node node2-exists))
(list 'node2-exists
'successors (join-node-successor node)
'logical (join-node-logical-block node)
'left (hash-table-count (join-node-left-memory node))
'right (hash-table-count (join-node-right-memory node))))
;;;;;; METHODS
(defmethod accept-tokens-from-left ((self node2-test) (left-tokens add-token))
(add-tokens-to-left-memory self left-tokens)
(when (every #'(lambda (test)
(funcall test left-tokens))
(join-node-tests self))
(pass-tokens-to-successor self (combine-tokens left-tokens self))))
(defmethod accept-tokens-from-left ((self node2-test) (left-tokens remove-token))
(when (remove-tokens-from-left-memory self left-tokens)
(pass-tokens-to-successor self (combine-tokens left-tokens self))))
(defun make-node2-test ()
(make-instance 'node2-test))
;;; File: shared-node.lisp
#+nil
(defclass shared-node ()
((successors :initform (make-hash-table :test #'equal)
:reader shared-node-successors)
(refcnt :initform 0
:accessor shared-node-refcnt)))
(defmethod increment-use-count ((self shared-node))
(incf (shared-node-refcnt self)))
#+nil
(defmethod decrement-use-count ((self shared-node))
(decf (shared-node-refcnt self)))
(defmethod node-use-count ((self shared-node))
(shared-node-refcnt self))
(defmethod node-referenced-p ((self shared-node))
(plusp (node-use-count self)))
#+nil
(defmethod pass-token-to-successors ((self shared-node) token)
(loop for successor being the hash-values of (shared-node-successors self)
do (funcall (successor-connector successor)
(successor-node successor)
token)))
(defmethod pass-token-to-successors ((self shared-node) token)
(loop for successor in (jscl::hash-table-values (shared-node-successors self))
do (funcall (successor-connector successor)
(successor-node successor)
token)))
#+nil
(defun shared-node-successor-nodes (shared-node)
(loop for successor being the hash-values of (shared-node-successors shared-node)
collect (successor-node successor)))
(defun shared-node-successor-nodes (shared-node)
(loop for successor in (jscl::hash-table-values (shared-node-successors shared-node))
collect (successor-node successor)))
#+nil
(defun shared-node-all-successors (shared-node)
(loop for successor being the hash-values of (shared-node-successors shared-node)
collect successor))
(defun shared-node-all-successors (shared-node)
(loop for successor in (jscl::hash-table-values (shared-node-successors shared-node))
collect successor))
;;; File: successor.lisp
(defun make-successor (node connector)
(cons node connector))
(defun successor-node (successor)
(car successor))
(defun successor-connector (successor)
(cdr successor))
(defun call-successor (successor &rest args)
(apply #'funcall
(successor-connector successor)
(successor-node successor)
args))
;;; File: node-pair.lisp
(defun make-node-pair (child parent)
(cons child parent))
(defun node-pair-child (node-pair)
(car node-pair))
(defun node-pair-parent (node-pair)
(cdr node-pair))
;;; File: terminal-node.lisp
#+nil
(defclass terminal-node ()
((rule :initarg :rule
:initform nil
:reader terminal-node-rule)))
(defmethod accept-token ((self terminal-node) (tokens add-token))
(let* ((rule (terminal-node-rule self))
(activation (make-activation rule tokens)))
(add-activation (rule-engine rule) activation)
(bind-rule-activation rule activation tokens)
t))
(defmethod accept-token ((self terminal-node) (tokens remove-token))
(let* ((rule (terminal-node-rule self))
(activation (find-activation-binding rule tokens)))
(unless (null activation)
(disable-activation (rule-engine rule) activation)
(unbind-rule-activation rule tokens))
t))
(defmethod accept-token ((self terminal-node) (token reset-token))
(clear-activation-bindings (terminal-node-rule self))
t)
#+nil
(defmethod print-object ((self terminal-node) strm)
(print-unreadable-object (self strm :type t)
(format strm "~A" (rule-name (terminal-node-rule self)))))
(defun make-terminal-node (rule)
(make-instance 'terminal-node :rule rule))
;;; File: node1.lisp
#+nil
(defclass node1 (shared-node)
((test :initarg :test
:reader node1-test)))
(defmethod add-successor ((self node1) (new-node node1) connector)
(with-slots ((successor-table successors)) self
(let ((successor (gethash (node1-test new-node) successor-table)))
(when (null successor)
(setf successor
(setf (gethash (node1-test new-node) successor-table)
(make-successor new-node connector))))
(successor-node successor))))
;;; original add-successor
#+nil
(defmethod add-successor ((self node1) (new-node t) connector)
(print 'add-successor-setf-key)
(setf (gethash `(,new-node ,connector) (shared-node-successors self))
(make-successor new-node connector))
new-node)
(defun mak-hash-successor (node connector)
(let ((h (list (mak-hash-successor-node node) (write-to-string connector))))
h))
(defmethod add-successor ((self node1) (new-node t) connector)
(setf (gethash (mak-hash-successor new-node connector) (shared-node-successors self))
(make-successor new-node connector))
new-node)
(defmethod remove-successor ((self node1) successor-node)
(let ((successors (shared-node-successors self)))
(maphash #'(lambda (key successor)
(when (eq successor-node (successor-node successor))
(remhash key successors)))
successors)
successor-node))
(defmethod accept-token ((self node1) token)
(if (funcall (node1-test self) token)
(pass-token-to-successors self token)
nil))
(defmethod accept-token ((self node1) (token reset-token))
(pass-token-to-successors self (token-push-fact token t)))
#+nil
(defmethod print-object ((self node1) strm)
(print-unreadable-object (self strm :type t :identity t)
(format strm "~S ; ~D" (node1-test self) (node-use-count self))))
(defun make-node1 (test)
(make-instance 'node1 :test test))
;;; File: join-node.lisp
(defun mark-as-logical-block (join-node marker)
(setf (slot-value join-node 'logical-block) marker))
(defun logical-block-p (join-node)
(numberp (join-node-logical-block join-node)))
(defun remember-token (memory token)
(setf (gethash (hash-key token) memory) token))
(defun forget-token (memory token)
(remhash (hash-key token) memory))
(defun add-tokens-to-left-memory (join-node tokens)
(remember-token (join-node-left-memory join-node) tokens))
(defun add-token-to-right-memory (join-node token)
(remember-token (join-node-right-memory join-node) token))
(defun remove-tokens-from-left-memory (join-node tokens)
(forget-token (join-node-left-memory join-node) tokens))
(defun remove-token-from-right-memory (join-node token)
(forget-token (join-node-right-memory join-node) token))
(defun left-memory-count (join-node)
(hash-table-count (join-node-left-memory join-node)))
(defun right-memory-count (join-node)
(hash-table-count (join-node-right-memory join-node)))
(defmethod test-tokens ((self join-node) left-tokens right-token)
(token-push-fact left-tokens (token-top-fact right-token))
(prog1
(every #'(lambda (test)
(funcall test left-tokens))
(join-node-tests self))
(token-pop-fact left-tokens)))
(defmethod pass-tokens-to-successor ((self join-node) left-tokens)
(call-successor (join-node-successor self) left-tokens))
(defmethod combine-tokens ((left-tokens token) (right-token token))
(token-push-fact (replicate-token left-tokens) (token-top-fact right-token)))
(defmethod combine-tokens ((left-tokens token) (right-token t))
(token-push-fact (replicate-token left-tokens) right-token))
(defmethod add-successor ((self join-node) successor-node connector)
(setf (join-node-successor self) (make-successor successor-node connector)))
(defmethod join-node-add-test ((self join-node) test)
(push test (join-node-tests self)))
(defmethod clear-memories ((self join-node))
(clrhash (join-node-left-memory self))
(clrhash (join-node-right-memory self)))
(defmethod accept-tokens-from-left ((self join-node) (left-tokens reset-token))
(clear-memories self)
(pass-tokens-to-successor self left-tokens))
(defmethod accept-token-from-right ((self join-node) (left-tokens reset-token))
nil)
#+nil
(defmethod print-object ((self join-node) strm)
(print-unreadable-object (self strm :type t :identity t)
(format strm "left ~S ; right ~S ; tests ~S"
(left-memory-count self)
(right-memory-count self)
(length (join-node-tests self)))))
;;; File: node2.lisp
#+nil
(defmethod test-against-right-memory ((self node2) left-tokens)
(loop for right-token being the hash-values of (join-node-right-memory self)
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
(defmethod test-against-right-memory ((self node2) left-tokens)
(loop for right-token in (jscl::hash-table-values (join-node-right-memory self))
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
#+nil
(defmethod test-against-left-memory ((self node2) (right-token add-token))
(loop for left-tokens being the hash-values of (join-node-left-memory self)
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
(defmethod test-against-left-memory ((self node2) (right-token add-token))
(loop for left-tokens in (jscl::hash-table-values (join-node-left-memory self))
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
#+nil
(defmethod test-against-left-memory ((self node2) (right-token remove-token))
(loop for left-tokens being the hash-values of (join-node-left-memory self)
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens
(make-remove-token left-tokens) right-token)))))
(defmethod test-against-left-memory ((self node2) (right-token remove-token))
(loop for left-tokens in (jscl::hash-table-values (join-node-left-memory self))
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens
(make-remove-token left-tokens) right-token)))))
(defmethod accept-tokens-from-left ((self node2) (left-tokens add-token))
(add-tokens-to-left-memory self left-tokens)
(test-against-right-memory self left-tokens))
(defmethod accept-token-from-right ((self node2) (right-token add-token))
(add-token-to-right-memory self right-token)
(test-against-left-memory self right-token))
(defmethod accept-tokens-from-left ((self node2) (left-tokens remove-token))
(when (remove-tokens-from-left-memory self left-tokens)
(test-against-right-memory self left-tokens)))
(defmethod accept-token-from-right ((self node2) (right-token remove-token))
(when (remove-token-from-right-memory self right-token)
(test-against-left-memory self right-token)))
(defun make-node2 ()
(make-instance 'node2))
;;; File: node2-not.lisp
#+nil
(defmethod test-against-right-memory ((self node2-not) left-tokens)
(loop for right-token being the hash-values of (join-node-right-memory self)
do (when (test-tokens self left-tokens right-token)
(token-increment-not-counter left-tokens)))
(unless (token-negated-p left-tokens)
(pass-tokens-to-successor
self (combine-tokens left-tokens self))))
(defmethod test-against-right-memory ((self node2-not) left-tokens)
(loop for right-token in (jscl::hash-table-values (join-node-right-memory self))
do (when (test-tokens self left-tokens right-token)
(token-increment-not-counter left-tokens)))
(unless (token-negated-p left-tokens)
(pass-tokens-to-successor
self (combine-tokens left-tokens self))))
#+nil
(defmethod test-against-left-memory ((self node2-not) (right-token add-token))
(loop for left-tokens being the hash-values of (join-node-left-memory self)
do (when (test-tokens self left-tokens right-token)
(token-increment-not-counter left-tokens)
(pass-tokens-to-successor
self (combine-tokens (make-remove-token left-tokens) self)))))
(defmethod test-against-left-memory ((self node2-not) (right-token add-token))
(loop for left-tokens in (jscl::hash-table-values (join-node-left-memory self))
do (when (test-tokens self left-tokens right-token)
(token-increment-not-counter left-tokens)
(pass-tokens-to-successor
self (combine-tokens (make-remove-token left-tokens) self)))))
#+nil
(defmethod test-against-left-memory ((self node2-not)(right-token remove-token))
(loop for left-tokens being the hash-values of (join-node-left-memory self)
do (when (and (test-tokens self left-tokens right-token)
(not (token-negated-p
(token-decrement-not-counter left-tokens))))
(pass-tokens-to-successor
self (combine-tokens left-tokens self)))))
(defmethod test-against-left-memory ((self node2-not)(right-token remove-token))
(loop for left-tokens in (jscl::hash-table-values (join-node-left-memory self))
do (when (and (test-tokens self left-tokens right-token)
(not (token-negated-p
(token-decrement-not-counter left-tokens))))
(pass-tokens-to-successor
self (combine-tokens left-tokens self)))))
(defmethod accept-tokens-from-left ((self node2-not) (left-tokens add-token))
(add-tokens-to-left-memory self left-tokens)
(test-against-right-memory self left-tokens))
(defmethod accept-tokens-from-left ((self node2-not) (left-tokens remove-token))
(when (remove-tokens-from-left-memory self left-tokens)
(pass-tokens-to-successor self (combine-tokens left-tokens self))))
(defmethod accept-token-from-right ((self node2-not) (right-token add-token))
(add-token-to-right-memory self right-token)
(test-against-left-memory self right-token))
(defmethod accept-token-from-right ((self node2-not) (right-token remove-token))
(when (remove-token-from-right-memory self right-token)
(test-against-left-memory self right-token)))
(defun make-node2-not ()
(make-instance 'node2-not))
;;; File: node2-test.lisp
(defmethod accept-tokens-from-left ((self node2-test) (left-tokens add-token))
(add-tokens-to-left-memory self left-tokens)
(when (every #'(lambda (test)
(funcall test left-tokens))
(join-node-tests self))
(pass-tokens-to-successor self (combine-tokens left-tokens self))))
(defmethod accept-tokens-from-left ((self node2-test) (left-tokens remove-token))
(when (remove-tokens-from-left-memory self left-tokens)
(pass-tokens-to-successor self (combine-tokens left-tokens self))))
(defun make-node2-test ()
(make-instance 'node2-test))
;;; File: node2-exists.lisp
#+nil
(defmethod test-against-right-memory ((self node2-exists) (left-tokens add-token))
(loop for right-token being the hash-values of (join-node-right-memory self)
do (when (test-tokens self left-tokens right-token)
(token-increment-exists-counter left-tokens)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
(defmethod test-against-right-memory ((self node2-exists) (left-tokens add-token))
(maphash
(lambda (ignore right-token)
(when (test-tokens self left-tokens right-token)
(token-increment-exists-counter left-tokens)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token))))
(join-node-right-memory self)))
#+nil
(defmethod test-against-right-memory ((self node2-exists) (left-tokens remove-token))
(loop for right-token being the hash-values of (join-node-right-memory self)
do (when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
(defmethod test-against-right-memory ((self node2-exists) (left-tokens add-token))
(maphash
(lambda (ignore right-token)
(when (test-tokens self left-tokens right-token)
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token))))
(join-node-right-memory self)))
#+nil
(defmethod test-against-left-memory ((self node2-exists) (right-token add-token))
(loop for left-tokens being the hash-values of (join-node-left-memory self)
do (when (and (test-tokens self left-tokens right-token)
(= (token-increment-exists-counter left-tokens) 1))
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token)))))
(defmethod test-against-left-memory ((self node2-exists) (right-token add-token))
(maphash
(lambda (ignore left-tokens)
(when (and (test-tokens self left-tokens right-token)
(= (token-increment-exists-counter left-tokens) 1))
(pass-tokens-to-successor
self (combine-tokens left-tokens right-token))))
(join-node-left-memory self)))
#+nil
(defmethod test-against-left-memory ((self node2-exists) (right-token remove-token))
(loop for left-tokens being the hash-values of (join-node-left-memory self)
do (when (test-tokens self left-tokens right-token)
(token-decrement-exists-counter left-tokens)
(pass-tokens-to-successor
self (combine-tokens
(make-remove-token left-tokens) right-token)))))
(defmethod test-against-left-memory ((self node2-exists) (right-token remove-token))
(maphash
(lambda (ignore left-tokens)
(when (test-tokens self left-tokens right-token)
(token-decrement-exists-counter left-tokens)
(pass-tokens-to-successor
self (combine-tokens
(make-remove-token left-tokens) right-token))))
(join-node-left-memory self)))
(defmethod accept-tokens-from-left ((self node2-exists) (left-tokens add-token))
(add-tokens-to-left-memory self left-tokens)
(test-against-right-memory self left-tokens))
(defmethod accept-token-from-right ((self node2-exists) (right-token add-token))
(add-token-to-right-memory self right-token)
(test-against-left-memory self right-token))
(defmethod accept-tokens-from-left ((self node2-exists) (left-tokens remove-token))
(when (remove-tokens-from-left-memory self left-tokens)
(test-against-right-memory self left-tokens)))
(defmethod accept-token-from-right ((self node2-exists) (right-token remove-token))
(when (remove-token-from-right-memory self right-token)
(test-against-left-memory self right-token)))
(defun make-node2-exists ()
(make-instance 'node2-exists))
;;; File: rete-compiler.lisp
(defvar *root-nodes* nil)
(defvar *rule-specific-nodes* nil)
(defvar *leaf-nodes* nil)
(defvar *logical-block-marker*)
(defun set-leaf-node (node address) (setf (aref *leaf-nodes* address) node))
(defun leaf-node () (aref *leaf-nodes* (1- (length *leaf-nodes*))))
(defun left-input (address) (aref *leaf-nodes* (1- address)))
(defun right-input (address) (aref *leaf-nodes* address))
(defun logical-block-marker () *logical-block-marker*)
#+nil
(defclass rete-network ()
((root-nodes :initform (make-hash-table)
:initarg :root-nodes
:reader rete-roots)
(node-test-cache :initform (make-hash-table :test #'equal)
:initarg :node-test-cache
:reader node-test-cache)))
(defun record-node (node parent)
(when (typep parent 'shared-node) (increment-use-count parent))
(push (make-node-pair node parent) *rule-specific-nodes*)
node)
(defmethod remove-node-from-parent ((self rete-network)(parent t) child)
(remhash (node1-test child) (rete-roots self)))
(defmethod remove-node-from-parent ((self rete-network)(parent shared-node) child)
(remove-successor parent child))
(defun make-root-node (class)
(let* ((test (make-class-test class))
(root (gethash test *root-nodes*)))
(when (null root)
(setf root (make-node1 test))
(setf (gethash test *root-nodes*) root))
(record-node root t)))
#+nil
(defmethod add-successor ((parent t) new-node connector)
(print (list 'add-successor-t new-node))
;;(declare (ignore connector))
new-node)
;;; bug: the method never call
(defmethod add-successor (parent new-node connector)
new-node)
;;; method with bug:
(defmethod add-successor :around ((parent shared-node) new-node connector)
(record-node (call-next-method) parent))
(defun make-intra-pattern-node (slot)
(let ((test
(cond ((simple-slot-p slot)
(make-simple-slot-test slot))
((constrained-slot-p slot)
(make-intra-pattern-constraint-test slot))
(t
(make-intra-pattern-test slot)))))
(make-node1 test)))
#+nil
(defun distribute-token (rete-network token)
(loop for root-node being the hash-values
of (rete-roots rete-network)
do (accept-token root-node token)))
(defun distribute-token (rete-network token)
(loop for root-node in (jscl::hash-table-values (rete-roots rete-network))
do (accept-token root-node token)))
(defmethod make-rete-network (&rest args)
(apply #'make-instance 'rete-network args))
;;; The following functions serve as "connectors" between any two
;;; nodes. PASS-TOKEN connects two pattern (one-input) nodes, or a join node
;;; to a terminal node; ENTER-JOIN-NETWORK-FROM-LEFT connects a pattern node
;;; to a join node; ENTER-JOIN-NETWORK-FROM-RIGHT also connects a pattern node
;;; to a join node; both PASS-TOKENS-ON-LEFT and PASS-TOKEN-ON-RIGHT connect
;;; two join nodes.
(defun pass-token (node token)
(accept-token node token))
(defun pass-tokens-on-left (node2 tokens)
(accept-tokens-from-left node2 tokens))
(defun pass-token-on-right (node2 token)
(accept-token-from-right node2 token))
(defun enter-join-network-from-left (node2 tokens)
(pass-tokens-on-left node2 (replicate-token tokens)))
(defun enter-join-network-from-right (node2 token)
(pass-token-on-right node2 (replicate-token token)))
;;; end connector functions
;;; "The alpha memory nodes and tests"
(defun add-intra-pattern-nodes (patterns)
(dolist (pattern patterns)
(cond ((test-pattern-p pattern)
(set-leaf-node t (parsed-pattern-address pattern)))
(t
(let ((node (make-root-node (parsed-pattern-class pattern)))
(address (parsed-pattern-address pattern)))
(set-leaf-node node address)
(dolist (slot (parsed-pattern-slots pattern))
(when (intra-pattern-slot-p slot)
(setf node
(add-successor node (make-intra-pattern-node slot)
#'pass-token))
(set-leaf-node node address))))))))
(defun add-join-node-tests (join-node pattern)
(labels ((add-simple-join-node-test (slot)
(unless (= (binding-address (pattern-slot-slot-binding slot))
(parsed-pattern-address pattern))
(join-node-add-test join-node
(make-inter-pattern-test slot))))
(add-slot-constraint-test (slot)
(join-node-add-test join-node
(make-predicate-test
(pattern-slot-constraint slot)
(pattern-slot-constraint-bindings slot)
(negated-slot-p slot))))
(add-test-pattern-predicate ()
(join-node-add-test join-node
(make-predicate-test
(parsed-pattern-test-forms pattern)
(parsed-pattern-test-bindings pattern))))
(add-generic-pattern-tests ()
(dolist (slot (parsed-pattern-slots pattern))
(cond ((simple-bound-slot-p slot)
(add-simple-join-node-test slot))
((constrained-slot-p slot)
(add-slot-constraint-test slot))))))
(if (test-pattern-p pattern)
(add-test-pattern-predicate)
(add-generic-pattern-tests))
join-node))
(defun make-join-node (pattern)
(let ((join-node
(cond ((negated-pattern-p pattern)
(make-node2-not))
((test-pattern-p pattern)
(make-node2-test))
((existential-pattern-p pattern)
(make-node2-exists))
(t (make-node2)))))
(when (eql (parsed-pattern-address pattern) (logical-block-marker))
(mark-as-logical-block join-node (logical-block-marker)))
join-node))
(defun make-left-join-connection (join-node node)
(if (typep node 'shared-node)
(add-successor node join-node #'enter-join-network-from-left)
(add-successor node join-node #'pass-tokens-on-left))
join-node)
(defun make-right-join-connection (join-node node)
(if (typep node 'shared-node)
(add-successor node join-node #'enter-join-network-from-right)
(add-successor node join-node #'pass-token-on-right))