How to transition when user input matter?

46 views
Skip to first unread message

Jonathan Zhou

unread,
Feb 9, 2015, 9:34:28 AM2/9/15
to squirrel-st...@googlegroups.com
Hi,

I have two questions for help. 

1. Suppose that I need to monitor user input from console. When the input is "Love", the current state moves to the Love state. When it is Hate, the current state moves to the Hate state. My question is: How to link the user input to the transition? 

I would like to use Context to represent the user input. But how can I set up a listener to monitor the input from console? Please note that it isn't a Java Swing project.

2. Suppose that the current state machine is performing a function (as below), which can generate a random double. Depending on the double value, the next state varies.

              protected void ontoQuestion(FSMState from, FSMState to, FSMEvent event, String context) {

double move = new RandomBetween(0, 1);

if (move>0.5) {

                       //     need to move to State A

} else {

                       //     need to move to State B
}
}

My question is how to move to A or B but the supposed state to.

Thanks!

He Henry

unread,
Feb 9, 2015, 9:54:16 AM2/9/15
to squirrel-st...@googlegroups.com
1, Well I think "Love" event(user keyboard input) will trigger state machine transfer to "Love" state, while "Hate" event(user keyboard input) will transfer state machine into "Hate" state. Event can trigger state machine changing states while context is only used for transition condition. 
2, You can refer to Decision State Sample.


在 2015年2月9日星期一 UTC+8下午10:34:28,Jonathan Zhou写道:

Cheng

unread,
Feb 9, 2015, 11:49:47 AM2/9/15
to He Henry, squirrel-st...@googlegroups.com
Which event is used in this case? I don't use Swing here. The user inputs may come from COM3 port.

--
You received this message because you are subscribed to a topic in the Google Groups "squirrel state machine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/squirrel-state-machine/h_wOsvbpjws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to squirrel-state-ma...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cheng

unread,
Feb 9, 2015, 8:55:29 PM2/9/15
to He Henry, squirrel-st...@googlegroups.com
Hi Mr. Henry,

Could you respond to my follow-on email? I don't know how to use COM3 port input to interact with state machine. The input is continuously monitored and will define which event is. 

Some example will do the trick.

Thanks.

He Henry

unread,
Feb 9, 2015, 9:30:33 PM2/9/15
to squirrel-st...@googlegroups.com, hekai...@gmail.com
No matter what the source of your event, keyboard inputs, COM3, network and so on, you always need to have a buffer to hold bytes data read from IO devices, and then a decoder translates these raws bytes into meaningful events. After that, you need to dispatch your event to proper state machine instance to do whatever you want.

在 2015年2月10日星期二 UTC+8上午9:55:29,Jonathan Zhou写道:
To unsubscribe from this group and all its topics, send an email to squirrel-state-machine+unsub...@googlegroups.com.

Cheng

unread,
Feb 9, 2015, 10:01:04 PM2/9/15
to He Henry, squirrel-st...@googlegroups.com
My question is how to dispatch indeed. Is listener involved?

Otherwise, how to involve the events in the transition of states. Should I check the inputs whenever enter or leave the states?

To unsubscribe from this group and all its topics, send an email to squirrel-state-ma...@googlegroups.com.

vishal gaurav

unread,
Feb 10, 2015, 2:51:01 AM2/10/15
to squirrel-st...@googlegroups.com
Hi Jonathan,

If I understand it correctly, you have to capture the input from COM3 port and fire an event into state machine instance. You have to define the transition definition first. 
Lets assume if have already define the transition like this:

From Love state To Hate state On Hate Action.

Now you can grab the user input and say 
statemachine.fire(usrInput, context);


Now if you want an internal transition on Hate Action , we have to pre define that in state machine definiton. 
for e.g. From Love State to Hate State On Hate Action with internal transition to HateA if context has A. 

I hope I understand your question correctly.

Cheng

unread,
Feb 10, 2015, 11:48:24 AM2/10/15
to vishal gaurav, squirrel-st...@googlegroups.com
Hi vishal,

Thanks for the information. One thing that I am still not certain about is:

statemachine.fire(usrInput, context);

As I know, the first parameter of fire method is Event type variable. Can we use userInput as Event?

Thanks


--

vishal gaurav

unread,
Feb 11, 2015, 6:26:05 AM2/11/15
to squirrel-st...@googlegroups.com
Yes you can. There are numerous ways you can do that. Simplest is use string based events or lookup able event by some name (user input string).


On Monday, 9 February 2015 20:04:28 UTC+5:30, Jonathan Zhou wrote:

Cheng

unread,
Feb 11, 2015, 7:03:35 PM2/11/15
to vishal gaurav, squirrel-st...@googlegroups.com
Thanks a lot.

--
Reply all
Reply to author
Forward
0 new messages