Javadoc link

A Finite State Machine, or FSM, is a computation model that can represent and control execution flow. Finite State Machines can be used to model problems in many fields, including mathematics, artificial intelligence, games or linguistics.

The FiniteStateMachine Service is a general purpose service which can create and maintain multiple FSMs of arbitrary complexity.  The "work" involved in making a FSM within the service is primarily defining the States & Transitions.

Example code (from branch develop):
# TODO: Implement this script fpr
fsm = runtime.start("fsm","FiniteStateMachine")
 
# add 4 states
fsm.addState("neutral")
fsm.addState("ill")
fsm.addState("sick")
fsm.addState("vomiting")
 
# woah - can handle multiple states - it propegates
# events across all of them
# fsm.setStates("neutral", "ill", "sick", "vomiting");
# fsm.setStates("neutral")
 
# add 8 transitions of 2 types
fsm.addTransition("neutral","ill-event","ill")
fsm.addTransition("ill","ill-event","sick")
fsm.addTransition("sick","ill-event","vomiting")
fsm.addTransition("vomiting","ill-event","vomiting")
 
fsm.addTransition("vomiting","clear-event","sick")
fsm.addTransition("sick","clear-event","ill")
fsm.addTransition("ill","clear-event","neutral")
fsm.addTransition("neutral","clear-event","neutral")
 
fsm.fire("clear-event");
print(fsm.getCurrentState())
sleep(1)
 
fsm.fire("ill-event");
print(fsm.getCurrentState())
sleep(1)
 
fsm.fire("ill-event");
print(fsm.getCurrentState())
sleep(1)
 
fsm.fire("ill-event");
print(fsm.getCurrentState())
sleep(1)
 
fsm.fire("clear-event");
print(fsm.getCurrentState())
sleep(1)
 
fsm.fire("clear-event");
print(fsm.getCurrentState())
sleep(1)
 
fsm.fire("clear-event");
print(fsm.getCurrentState())
sleep(1)
 
 
print(fsm.getCurrentState())
print(fsm.getCurrentStates())
 
# cleare the diagram
# fsm.clear()

Ray.Edgley

3 years 4 months ago

But how do we use it?

If I understand it, we need to create a number of states, then we create the triggers to move from one state to the next.

Depending on the current state, the type of trigger might move up a state, or down a state, or just to mix things up sideways to another state :-)

But how do we create the states and define the triggers.
In this case the JavaDocs didn't really help :-)

 

GroG

3 years 4 months ago

In reply to by Ray.Edgley

You and your logical questions !

Uh, ya ... I need to document that part after I look at how I did it ;)

Example configuration (from branch develop):
!!org.myrobotlab.service.config.FiniteStateMachineConfig
current: null
listeners: null
messageListeners: [
  ]
peers: null
transitions: [
  ]
type: FiniteStateMachine