473,804 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Larger structured state machine; implementation issue

2 New Member
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 2405
JosAH
11,448 Recognized Expert MVP
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 Recognized Expert Moderator Expert
Have you looked at the State Design Pattern, descibed here?
Jul 20 '08 #3
laktofil
2 New Member
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 Recognized Expert Moderator Expert
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
5075
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 UML Harel State Machine notion of a state machine. These are multi-threaded capable state machines with hierarchical representation capability (nested instances of state machines). They also have a unique conditional trigger symbol scheme for...
0
1468
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 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 UML Harel State Machine notion of a state machine. These are multi-threaded capable state
4
1276
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 easier for myself. I reckon that a wizard is basically a state machine, so it would be great if anyone knows of any software for quickly wiring up state machines for ASP.NET/c# for use as a GUI controller.
6
3811
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 one day): \\FFDS24\ASP.NET Applications(_LM_W3SVC_1_Root_ATV2004)\Errors During Execution: 7 \\FFDS24\ASP.NET Apps v1.1.4322(_LM_W3SVC_1_Root_ATV2004)\Compilations
5
2942
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 directory with a space in it. e.g. 'http://localhost/My%20App'. The following exception is thrown: ----------
6
12534
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. It is : switch(nState) { case state1:
4
4388
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 machines in C# where the machine can use the enumerated 'state' to execute (thus the term 'machine' as in 'state machine') the machine operations as this requires boolean operations to be performed on the state. .....
67
7402
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 constantly cursing at the goto statement, accusing it of being some sort of spawn of satan. So it seems we have a pickle here. The thing is, at first thought it seems that there aren't any viable alternatives which are better suited for this...
30
1915
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 + 20. The best approach I have thought of so far is to check for overflow in both sums; if exactly one overflows, that one is larger. Otherwise, compare the sums normally. Is there a better way?
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.