forked from opencog/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm-mealy.scm
185 lines (162 loc) · 5.57 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
;
; fsm-mealy.scm -- Mealy Finite State Machine (FSM) Demo.
;
; Based on fsm-full.scm, this alters the general FSM definition 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))
(define my-trans (Concept "My FSM's Transition Rule"))
(define my-state (Anchor "My FSM's Current State"))
(define halt-state (Concept "halt state"))
(define red-state (Concept "red state"))
(define green-state (Concept "green state"))
(define blue-state (Concept "blue state"))
(define cyan-state (Concept "cyan state"))
(define magenta-state (Concept "magenta state"))
(define yellow-state (Concept "yellow state"))
; External state will control the behavior of the FSM.
(define extern-anchor (Predicate "External State"))
; External, environmental commands.
(define go-forward (Concept "forward"))
(define go-reverse (Concept "reverse"))
(define halt (Concept "halt"))
;; The initial state of the FSM
(List my-state halt-state)
;; The current environment
(Evaluation extern-anchor halt)
;; The set of allowed state transitions. 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 (And halt-state halt) (List my-trans halt-state))
(ContextLink (And red-state halt) (List my-trans halt-state))
(ContextLink (And green-state halt) (List my-trans halt-state))
(ContextLink (And blue-state halt) (List my-trans halt-state))
(ContextLink (And cyan-state halt) (List my-trans halt-state))
(ContextLink (And magenta-state halt) (List my-trans halt-state))
(ContextLink (And yellow-state halt) (List my-trans halt-state))
; The forward cycle
(ContextLink (And halt-state go-forward) (List my-trans red-state))
(ContextLink (And red-state go-forward) (List my-trans green-state))
(ContextLink (And green-state go-forward) (List my-trans blue-state))
(ContextLink (And blue-state go-forward) (List my-trans red-state))
; A reversed state halts before moving forward
(ContextLink (And cyan-state go-forward) (List my-trans halt-state))
(ContextLink (And magenta-state go-forward) (List my-trans halt-state))
(ContextLink (And yellow-state go-forward) (List my-trans halt-state))
; The reverse cycle
(ContextLink (And halt-state go-reverse) (List my-trans cyan-state))
(ContextLink (And cyan-state go-reverse) (List my-trans magenta-state))
(ContextLink (And magenta-state go-reverse) (List my-trans yellow-state))
(ContextLink (And yellow-state go-reverse) (List my-trans cyan-state))
; Stop before reversing.
(ContextLink (And red-state go-reverse) (List my-trans halt-state))
(ContextLink (And green-state go-reverse) (List my-trans halt-state))
(ContextLink (And blue-state go-reverse) (List 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 but one step.
;;;
(define (create-fsm fsm-name fsm-state extern-state)
(BindLink
;; We will need to find the current and the next state
(VariableList
(Variable "$extern-state")
(Variable "$curr-state")
(Variable "$next-state")
)
(And
;; If we are in the current state ...
(List
fsm-state
(Variable "$curr-state")
)
;; ... and the external environment is in the given state
(EvaluationLink
extern-state
(Variable "$extern-state")
)
;; ... and there is a transition to another state...
(ContextLink
(And
(Variable "$curr-state")
(Variable "$extern-state")
)
(List
fsm-name
(Variable "$next-state")
)
)
)
(And
;; ... Then, leave the current state ...
(DeleteLink
(List
fsm-state
(Variable "$curr-state")
)
)
;; ... and transition to the next state.
(List
fsm-state
(Variable "$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-execute! 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 (Evaluation extern-anchor (show-environment-state)))
; Next, set the new direction
(Evaluation 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)