forked from singnet/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm-mealy.scm
186 lines (163 loc) · 5.87 KB
/
fsm-mealy.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;
; Mealy Finite State Machine (FSM) Demo.
;
; Based on fsm-full.scm, this alters the general FSM defintion to
; include a dependency on the external state.
;
; To run this demo, load this file:
; (add-to-load-path ".")
; (load-from-path "fsm-mealy.scm")
;
; Then, scroll to the bottom, and try some of the commented-out examples.
(use-modules (opencog))
(use-modules (opencog query))
(define my-trans (ConceptNode "My FSM's Transition Rule"))
(define my-state (AnchorNode "My FSM's Current State"))
(define halt-state (ConceptNode "halt state"))
(define red-state (ConceptNode "red state"))
(define green-state (ConceptNode "green state"))
(define blue-state (ConceptNode "blue state"))
(define cyan-state (ConceptNode "cyan state"))
(define magenta-state (ConceptNode "magenta state"))
(define yellow-state (ConceptNode "yellow state"))
; External state will control the behavior of the FSM.
(define extern-anchor (PredicateNode "External State"))
; External, environmental commands.
(define go-forward (ConceptNode "forward"))
(define go-reverse (ConceptNode "reverse"))
(define halt (ConceptNode "halt"))
;; The inital state of the FSM
(ListLink my-state halt-state)
;; The current envoronment
(EvaluationLink extern-anchor halt)
;; The set of allowed state transistions. The transitions depend on
;; both the current state, and the external state; thus, this is
;; effectively a Mealy machine.
;;
;; Three cycles are implemented: a unit-length no-op cycle, a "forward"
;; cycle going through red-green-blue, and a "reverse" cycle going
;; through cyan-magenta-yellow.
;;
;; Each rule is labelled with the "my-trans", so that rules for
;;
; All states transition to halt upon halt.
(ContextLink (AndLink halt-state halt) (ListLink my-trans halt-state))
(ContextLink (AndLink red-state halt) (ListLink my-trans halt-state))
(ContextLink (AndLink green-state halt) (ListLink my-trans halt-state))
(ContextLink (AndLink blue-state halt) (ListLink my-trans halt-state))
(ContextLink (AndLink cyan-state halt) (ListLink my-trans halt-state))
(ContextLink (AndLink magenta-state halt) (ListLink my-trans halt-state))
(ContextLink (AndLink yellow-state halt) (ListLink my-trans halt-state))
; The forward cycle
(ContextLink (AndLink halt-state go-forward) (ListLink my-trans red-state))
(ContextLink (AndLink red-state go-forward) (ListLink my-trans green-state))
(ContextLink (AndLink green-state go-forward) (ListLink my-trans blue-state))
(ContextLink (AndLink blue-state go-forward) (ListLink my-trans red-state))
; A reversed state halts before moving forward
(ContextLink (AndLink cyan-state go-forward) (ListLink my-trans halt-state))
(ContextLink (AndLink magenta-state go-forward) (ListLink my-trans halt-state))
(ContextLink (AndLink yellow-state go-forward) (ListLink my-trans halt-state))
; The reverse cycle
(ContextLink (AndLink halt-state go-reverse) (ListLink my-trans cyan-state))
(ContextLink (AndLink cyan-state go-reverse) (ListLink my-trans magenta-state))
(ContextLink (AndLink magenta-state go-reverse) (ListLink my-trans yellow-state))
(ContextLink (AndLink yellow-state go-reverse) (ListLink my-trans cyan-state))
; Stop before reversing.
(ContextLink (AndLink red-state go-reverse) (ListLink my-trans halt-state))
(ContextLink (AndLink green-state go-reverse) (ListLink my-trans halt-state))
(ContextLink (AndLink blue-state go-reverse) (ListLink my-trans halt-state))
;;; A Universal Deterministic Finite State Machine Constructor.
;;;
;;; This will create a deterministic FSM; that is, a rule that will
;;; transition any arbitrary deterministic FSM from state to state,
;;; given only its name, and the name given to the transition rules.
;;;
;;; Create a BindLink that can take an FSM with the name `fsm-name`
;;; and stores it's state in `fsm-state`. After the BindLink is
;;; created, each invocation of it will advance the FSM bu one step.
;;;
(define (create-fsm fsm-name fsm-state extern-state)
(BindLink
;; We will need to find the current and the next state
(VariableList
(VariableNode "$extern-state")
(VariableNode "$curr-state")
(VariableNode "$next-state")
)
(AndLink
;; If we are in the current state ...
(ListLink
fsm-state
(VariableNode "$curr-state")
)
;; ... and the external environment is in the given state
(EvaluationLink
extern-state
(VariableNode "$extern-state")
)
;; ... and there is a transition to another state...
(ContextLink
(AndLink
(VariableNode "$curr-state")
(VariableNode "$extern-state")
)
(ListLink
fsm-name
(VariableNode "$next-state")
)
)
)
(AndLink
;; ... Then, leave the current state ...
(DeleteLink
(ListLink
fsm-state
(VariableNode "$curr-state")
)
)
;; ... and transistion to the next state.
(ListLink
fsm-state
(VariableNode "$next-state")
)
)
)
)
;;; Create "my-fsm"
(define my-fsm (create-fsm my-trans my-state extern-anchor))
;;; A utility to take a step, and display the new state
(define (take-step) (gar (gar (cog-bind-single my-fsm))))
;;; A utility to show the current FSM state
(define (show-fsm-state)
(car (cog-chase-link 'ListLink 'ConceptNode my-state)))
;;; As above, but show the environment
(define (show-environment-state)
(car (cog-chase-link 'EvaluationLink 'ConceptNode extern-anchor)))
; Set the direction
(define (move-dir dir)
; First, delete the current external state
(cog-delete (EvaluationLink extern-anchor (show-environment-state)))
; Next, set the new direction
(EvaluationLink extern-anchor dir))
; Set the direction
(define (move-forward) (move-dir go-forward))
(define (move-reverse) (move-dir go-reverse))
(define (move-halt) (move-dir halt))
;;; Take one step.
;(take-step)
; View the current state:
(show-fsm-state)
;;; Take three steps.
;;; Try it!
; (take-step)
; (take-step)
; (take-step)
; (move-forward)
; (take-step)
; (take-step)
; (take-step)
; (move-reverse)
; (take-step)
; (take-step)
; (take-step)
; (move-halt)