forked from singnet/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm-basic.scm
130 lines (120 loc) · 3.47 KB
/
fsm-basic.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
;
; Very Simple Finite State Machine (FSM) Demo.
;
; This defines a very simple four-state finite state machine: an initial
; state, and a cycle of three state: green, yellow, red. It then uses a
; BindLink to take one step at a time. The BindLink is more or less
; "universal", in that it can run any FSM, not just this one. However,
; here it has been a bit simplified, to keep the demo as simple as
; possible. See the file `fsm-full.scm` for the correct way to define a
; truly general-purpose FSM, in such a way that multiple FSM's can be run
; at the same time.
;
; The run this, you probably need to do this:
;
; OCDIR=home/home/yourname/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 "fsm-basic.scm")
;
; Then, scroll to the bottom, and some of the commented-out
; examples.
(use-modules (opencog))
(use-modules (opencog query))
;; Set of possible states of the state machine.
;; The definition of this set is not strictly needed; it is not used
;; for anything in the demo code below.
(SetLink
(ConceptNode "initial state")
(ConceptNode "green")
(ConceptNode "yellow")
(ConceptNode "red")
)
;; The inital state of the FSM
(ListLink
(AnchorNode "Current State")
(ConceptNode "initial state")
)
;; The set of allowed state transistions. Its a triangular cycle,
;; of green goint to yellow going to red going back to green.
;; The intial state transitions into green (and is never visted again).
(ListLink
(ConceptNode "initial state")
(ConceptNode "green")
)
(ListLink
(ConceptNode "green")
(ConceptNode "yellow")
)
(ListLink
(ConceptNode "yellow")
(ConceptNode "red")
)
(ListLink
(ConceptNode "red")
(ConceptNode "green")
)
;;; Take one step of a finite state machine.
;;; Note that the algorithm below is "universal": it can run any FSM,
;;; it does not care about the specific states or state transition
;;; rules.
;;;
;;; To define two or more machines that run at the same time, each
;;; should use a different AnchorNode, so that the states of the
;;; various machines would not get confused with one-another.
;;; It would also probably be a good idea to include the machine name
;;; (i.e. the AnchorNode) as part of the transition rules, so that
;;; the transition rules for one machine would not get used accidentally
;;; for another machine.
(define take-one-step
(BindLink
;; We will need to find the current and the next state
(VariableList
(VariableNode "$curr-state")
(VariableNode "$next-state")
)
(AndLink
;; If we are in the current state ...
(ListLink
(AnchorNode "Current State")
(VariableNode "$curr-state")
)
;; ... and there is a transition to another state...
(ListLink
(VariableNode "$curr-state")
(VariableNode "$next-state")
)
)
(AndLink
;; ... then transistion to the next state ...
(ListLink
(AnchorNode "Current State")
(VariableNode "$next-state")
)
;; ... and leave the current state.
(DeleteLink
(ListLink
(AnchorNode "Current State")
(VariableNode "$curr-state")
)
)
)
)
)
;; Take on step of the FSM
;(cog-bind-single take-one-step)
;
;;; Take three more steps;
;;; Try it! See what appens!
;(cog-bind-single take-one-step)
;(cog-bind-single take-one-step)
;(cog-bind-single take-one-step)