[ragel-users] Re: Nested machines to make a HSM?

Chuck Remes cremes.devl... at mac.com
Sat Jul 12 10:42:57 UTC 2008

On Jul 11, 2008, at 1:08 PM, B. Estrade wrote:

> On Fri, Jul 11, 2008 at 12:16:26PM -0500, Chuck Remes wrote:
>> I'm currently reading "Practical Statecharts in C/C++" by Miro Samek
>> which was suggested on this list some months ago. It is blowing my  
>> mind.
>> I'm curious to know if it is possible to use ragel to nest multiple
>> machines so that behavior is inherited as described in that book.
>> If you haven't read the book, I'll give a quickie explanation.  
>> Imagine
>> an oven that has 4 states:
>> Heating
>> 	Baking
>> 	Toasting
>> Door Open
>> The Baking and Toasting states are substates of the Heating  
>> superstate
>> because they exhibit different behavior though both require the
>> heating element to be on. If the machine is in the Toasting state and
>> it receives a door opened event, it transitions to Door Open.  
>> Toasting
>> has no exit action of its own; it inherits an exit action
>> (TurnOffHeatingElement) from its superstate, Heating.
>> So, much like we have encapsulation and inheritance in OOP, a
>> hierarchical state machine allows substates to inherit behavior from
>> superstates. Here endeth the quickie overview. I hope it made some
>> sense.
>> Is this kind of inheritance (or chain of responsibility-ish) behavior
>> possible in ragel? If so, how would one achieve it?
> I admittedly don't know how to do this in Ragel (it's been a while  
> since I played with it), but you can set up the heirarchy from a  
> symbol/transition view using a regular expression.
> So, if you look at it as a regex, it is something like:
> H(B+T)D + D
> This accepts:
> D
> Of course, you have to define what the valid order of things might  
> be, otherwise you could get something super general like:
> ((H(B+T))*D)*

That's an interesting idea. I've played with it a bit but can't seem  
to get it to work in anything other than the simplest cases. Plus, the  
regex syntax isn't well suited for doing this kind of state chart  
translation. Using the state chart syntax with epsilon transitions  
would be ideal.

It looks like the fundamental issue with ragel is its inability for a  
nested state chart block to transition to an outer/parent label; it  
can only reference labels in its own scope or lower.

No big deal. I'm going to take some time today to hack out a ruby  
version of Samek's behavioral inheritance meta-pattern. Ruby's (or  
python's or javascript's or ...) dynamic typing will clean up a lot of  
the nasty & ugly casting he did in the C and C++ versions. If anyone  
is interested I could post some code here (or release a gem if it  
works well).


More information about the ragel-users mailing list