forked from singnet/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.scm
167 lines (155 loc) · 4.28 KB
/
sequence.scm
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
;
; sequence.scm
;
; Demonstrate using the GroundedPredicateNode to provide a simple
; behavior sequence: i.e. a set of steps that are conditionally played
; out, depending on whether the predicate node returns true of false.
; There are no variables in this demo; thus, the sequence is not
; a graph search, but is rather a straight-forward if-else sequence
; evaluation.
;
; This demo also demonstrates the use of a SatisfactionLink: there is no
; output, and no graph-rewriting occurs, thus a BindLink is not needed,
; and the simpler SatisfactionLink is enough.
;
; The SatisfactionLink, combined with the SequentialAndLink, implement
; the concept of a behavior tree "sequence node". See
; https://en.wikipedia.org/wiki/Behavior_Trees_(artificial_intelligence,_robotics_and_control)
;
; The run this, you might need to do this:
;
; OCDIR=/home/yourname/where/you/put/opencog
; export LTDL_LIBRARY_PATH=$OCDIR/build/opencog/guile:$OCDIR/build/opencog/query
;
; Add the following to your ~/.guile file:
; (add-to-load-path "/home/yourname/opencog/build")
; (add-to-load-path "/home/yourname/opencog/opencog/scm")
; (add-to-load-path ".")
;
; Start guile:
; guile
;
; and then load this file:
; (load-from-path "sequence.scm")
;
; Then, scroll to the bottom, and try some of the commented-out
; examples.
;
(use-modules (opencog) (opencog query) (opencog exec))
(define green-light (ConceptNode "green light"))
(define red-light (ConceptNode "red light"))
; Counts of how many times the green and red lights were seen.
(define num-green 0)
(define num-red 0)
; Return SimpleTV of true if green light, false if red light, and
; throw an exception if neither. Increment counters so that we
; can verify that this was invoked.
(define (stop-go atom)
(cond
((equal? atom green-light) (begin (set! num-green (+ 1 num-green)) (stv 1 1)))
((equal? atom red-light) (begin (set! num-red (+ 1 num-red)) (stv 0 1)))
(else (throw 'not-a-stoplight "stop-go" "you're busted"))
)
)
; Should throw an exception in all cases. Shouldn't do donuts on
; corn fields.
(define off-road
(SatisfactionLink
(VariableList) ; no variables
(SequentialAndLink
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink
(ConceptNode "corn field")
)
)
)
)
)
;; Should see two green lights, and one red light, after which
;; the matching should stop. There should be no exceptions or
;; errors when evaluating this.
(define traffic-lights
(SatisfactionLink
(VariableList) ; no variables
(SequentialAndLink
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink green-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink green-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink red-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink
(ConceptNode "traffic ticket")
)
)
)
)
)
(define (start-again)
(cog-evaluate! traffic-lights)
(simple-format #t "Went through ~A green lights and ~A red lights\n"
num-green num-red)
)
;;; Try the below. This should result in an exception being thrown.
;;;
; (cog-evaluate! off-road)
;
;;; The below should result in the green light being seen twice, and
;;; the red light once, and no exceptions or errors.
;;;
; (start-again)
; (start-again)
; (start-again)
;
;
;;; The below is very similar to the above, except that this uses
;;; a SequentialOrLink that halts after the first TRUE value.
;;;
(define hot-rodding
(SatisfactionLink
(VariableList) ; no variables
(SequentialOrLink ; <==== unlike before, this it OR
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink red-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink red-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink red-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink green-light)
)
(EvaluationLink
(GroundedPredicateNode "scm: stop-go")
(ListLink
(ConceptNode ".... And they're off!")
)
)
)
)
)
(define (drag-race)
(cog-evaluate! hot-rodding)
(simple-format #t "Waited on ~A red lights\n" num-red)
)
;;; The below should result in the three red lights before it turns
;;; green.
;;;
; (drag-race)
; (drag-race)
; (drag-race)