473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Larger structured state machine; implementation issue

This may seem like an abstract question on behavioral inheritance. Anyway, I'm building a hierarchical state machine in C++ (with gcc for target platform Gentoo Linux). More precisely, I'm using this framework: Practical Statecharts in C/C++: Quantum Programming for Embedded Systems (hopefully, you don't have to know it to answer my question). Summary is that a state is represented as a function and state changes are handled by member-of-functions pointers. Very structured and efficient! The hard part is how the (somewhat abstract) behavioral inheritance is implemented through C++ polymorphism)

The base class, "the core", has all the important functions for dispatching the state machine and organizing the state changes. This class must be inherited by all other classes to make them executable as state machines (through the behavioral inheritance).

Let's make an simple illustration of a state machine.
Expand|Select|Wrap|Line Numbers
  1. [core]--+--[state 1]--+--[state 11]--+--[state 111]
  2.                       |              +--[state 112]--+--[state 1121]
  3.                       |              |               +--[state 1122]
  4.                       |              +--[state 113]
  5.                       | 
  6.                       +--[state 12]--+--[state 121]
  7.                       |              +--[state 122]--+--[state 1221]
  8.                       |                              +--[state 1222]
  9.                       |                              +--[state 1223]
  10.                       |  
  11.                       +--[state 13]--+--[state 131]
  12.                                      +--[state 132]
  13.  
  14.  
As you see, it will get very messy to have all these states defined in one class, even in one file! My idea is to keep every "layer" (vertically paired states in illustration, e.g. state 1x, 11x, 12x and so on) in separate classes.
But, how do i define a such inheritance (the first level is simple, I have it working already)? All classed have to be in the same state machine, and inherit the "core" functionality directly or indirectly, but without actually creating a new instance of the core! Since there are a lot of branches, I cannot simply create the "highest" layer and let it recursively create all superstates down to the core as the usual way to go would say.
Should I maybe use:
* Friend classes?
* Inheriting without calling super's constructor?

Bonus feature: Substates like '1121' and '1221' (in the illustration) will be very similar in behavior, so it will be great if it's possible to make them descend from the same same (virtual) class and just define the differences for each branch. Will this be as easy as making pancakes thanks to multiple inheritance?

If you don't understand what I'm talking about this far, some reference to "advanced" class inheritance in C++ with examples would probably help me very much :)

Thank you for your time!
Jul 20 '08 #1
4 2379
JosAH
11,448 Expert 8TB
Just a thought: can a state be a Composite (see the GoF book for a definition)
so that a state *is-a* state (trivial) and also *has* states? I don't know how the
entire machine changes its state but maybe such a Composite could do the job.

kind regards,

Jos
Jul 20 '08 #2
Banfa
9,065 Expert Mod 8TB
Have you looked at the State Design Pattern, descibed here?
Jul 20 '08 #3
Thanks for good answers guys!

@ JosAH
I've looked at some composite examples, and that is probably what I need to initialize all state classes. Unfortunately, I can't really for the moment think of any way this pattern can solve all my problems due to the predefined framework from the book.


@ Banfa
Yes, I had a look at some examples from bytes' library just after I posted. They're good, but doesn't quite solve my multi-state-per-class-problem, but I got some ideas to work on.

And I added GoF's Design Patterns to my wishlist. I'll solve this problem in some more or less elegant way after reading some more articles about chain-of-responsibility and template patterns.

Thanks
Stefan
Jul 22 '08 #4
Banfa
9,065 Expert Mod 8TB
Yes, I had a look at some examples from bytes' library just after I posted. They're good, but doesn't quite solve my multi-state-per-class-problem, but I got some ideas to work on.
The thing to do would be 1 state per class not multiple states per class. Each class will then have very simplistic behaviour I think trying to have multiple states per class will result in complex class behaviour and will basically negate most of the advantage you would get from using classes.

Don't make the mistake of thinking your class hierarchy will inherit down your state tree. Classes and states are 2 separate things. What I normally do is have 1 abstract base class defining the interface to the states and then have each state as a sub-class of that base.

The example I linked to is using a state machine that deals with a synchronous operation, reading a file, but where I have implemented it the operation has been asynchronous dealing with events from 2 different sources (user interface and comms interface) so where the example has a DoProcess function I have implemented a number of different functions to handle different events with different attached data. The base class then holds default handlers for these events for states in which they should not occur (because there is a difference between should not and can not).
Jul 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Leonard J. Reder | last post by:
Hello list, I have been searching on the web for a while now for a specific Python implementation of an FSM. More specifically what I am looking for is a Python implementation of the so called...
0
by: Leonard J. Reder | last post by:
Hello, Although posted this a few weeks ago I still have gotten much feedback so I want to put this out again and see if the response gets a bit more interesting. I have been searching on the...
4
by: Tobin Harris | last post by:
Hi there, I was just wondering if anyone out there has done any apps with lots of UI Wizards? I'm currently finding myself doing loads of these, and am trying to find a nice way of making things...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
5
by: Stu Carter | last post by:
Hi, ENV: Windows 2003 Server SP1 (+all updates), VS 2003, .Net 1.1 SP1 We've got an ASP.Net web application using State Service. All is fine until we tried to use the app through a virtual...
6
by: nilavya | last post by:
HI, I have got a C++ application involving State machine. Now I have got too many states and too many events which changes the state of the Staet machine. So my code is somewhat unmanageable....
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
30
by: Peter Ammon | last post by:
Howdy, I have four unsigned long longs a, b, c, d. I want to determine whether a+b or c+d is larger, as if there were no overflow. That is, ULLONG_MAX + 5 should be considered larger than 10 +...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.