Practical Statecharts in C/C++ 121
Practical Statecharts in C/C++: Quantum Programming for Embedded Systems | |
author | Miro Samek |
pages | 389 |
publisher | CMP Books |
rating | 10/10 |
reviewer | Jonathan Kaye |
ISBN | 1578201101 |
summary | Practical and methodologically sound approach to improving software design using statecharts |
As the title indicates, this book brings the topic of statecharts from the realm of expensive design tools to the practical realm, illustrating its points with full examples and extensive commentary.
Essentially Samek postulates that the slow adoption by developers of best practices by statechart design is due to lack of understanding of the fundamental nature of statecharts and how it is perceived as requiring expensive tools to use well. Samek insightfully discusses how statecharts as a best practice embody "behavioral inheritance" as a fundamental design concept that stands as a peer alongside the conventional pillars of object-oriented programming, namely inheritance, encapsulation, and polymorphism.
The book is very technical and written in an academic style, with ample references to original sources as well as detailed code reviews and many reader exercises. I would caution anyone from approaching this book as a quick or light read. For me, it took a seriousness and good understanding of C and C++ to follow Samek's examples and achieve the "a-ha", which was always worth it in the end. The book contains full, working code to incorporate statecharts into my own work, implemented both in C and C++.
The two basic parts of the text are (1) an explanation of statecharts and their methodological implications, and (2) a description of how to apply statecharts as a data structure in real applications, namely embedded as control strategies for "active objects." In several places in the text, Samek makes an analogy between statechart (and active object) semantics and quantum mechanics. This parallel was an interesting philosophical argument, but didn't add much for me in terms of accepting his "quantum framework" as a best practice -- I was sold by his methodological arguments he had presented already.
Speaking from experience in writing a book about using statecharts to build simulations (www.FlashSim.com), I can say Samek is a visionary who extended my perception of statecharts several steps. I know I will be quoting from it and referring to it in my work to come. This book has earned a prominent place on my bookshelf, and I would heartily recommend it to any other developer who wants to create correct, verifiable, scaleable, and solid designs (which should be ALL developers!)
You can purchase Reviewing Practical Statecharts in C/C++ from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
currently reading it (Score:4, Informative)
Most other programmers I meet in the workplace have a hard time understanding state machines. All of those programmers are self taught or picked up "learn programming in 21 days" type of books.
I recommend this book.
This is distrubing... (Score:2)
I seem to recall that state machines were the first thing that I learned when I started learning about computer architecture. They are, after all, the basis. What I find disturbing is that programmers that you meet in the workplace have a hard time understanding state machines and they're still in the workplace.
Isn't that a like knowing how for loops work? Or how to do recursion? It's a basic funda
Re:currently reading it (Score:2)
Re:currently reading it (Score:2)
Zing .... (Score:3, Interesting)
I know, I know, you can just google it yourself. But why have hundreds of people searching for exactly the same thing when one person could save a lot of people some time.
Re:Zing .... (Score:5, Informative)
Re:Zing .... (Score:1, Insightful)
Re:Zing .... (Score:2)
Even if the result can be thought of as a state machine, it's often better not to model it as one.
Re:Karnaugh maps. not granularity (Score:3, Interesting)
If the system uses a single byte that means you have 256 possible states. If you have two bytes it's 65,536 states and so on. I don't think you want to draw a Karnaugh map for something like Emacs, you won't live long enough.
That's what I mean by fine-grained state, a single state of the system is defined by the state of all the bits in the system.
A c
Re:Zing .... (Score:1)
And, these are really informal "state machine diagrams", so I can't say whether they conform to UML "statechart" standards (I don't think UML was around in the mid 80's when I was learning this stuff).
Real world examples. (Score:4, Interesting)
I learned about state diagrams in college years ago. The examples that we used at the time were all the sorts of things that could be done as stand-alone projects. They typically involved parsing strings that had to match a description given by a state diagram. If you are building a lexical scanner by hand or a regular expression library, it's good stuff to know. However, I wondered how often I'd see it in the real world.
Well folks, this can be real world stuff if you apply it. It's all about retaining a state that summarizes inputs that you have seen so far. This is about communication between autonomous devices (or with a user). If nothing else, state information is useful in retaining position within a set of search results when a user is paging through them. It can be used in the session layer of a communication protocol to track handshaking. If you have to asynchronously do two things concurrently within your program, unless each of them involves completely separate transactions that are completely atomic, one or both can be modelled as state machines.
If you write GUI code where you have buttons being toggled, check boxes checked or fields filled, you have a state machine, whether you coded it or not.
If you are writing embedded systems, you don't need me to tell you that you are maintaining real time state information about the devices you are talking to.
Doom using the zombie trick....Re:Zing .... (Score:2, Interesting)
For instance. When the player crosses a line I want the following to happen. I don't really know state semantics so it's in english which for a programmer is very poor. This can be as complex as you want, this is just a simple one:
Set up some moving floors that will carry an object (zombie doll).
Player crosses line.
Open a door and release zombie doll.
Zombie doll 1 moves
Re:Doom using the zombie trick....Re:Zing .... (Score:2)
Zing? Odd... (Score:2)
switch(statevar) {
case STATE1:
outvar = boolfunc1(invar1, invar2...);
statevar = STATE2;
break;
case STATE2:
outvar = boolfunc2(invar1, invar2...);
statevar = STATE3;
break;
. .
}
Am I missing something here? I'll be first to admit I'm far from a C guru but I fail to see what is groundbreaking about the concept. Of course, I'm an oddball and tend to mentally envision a program as
oh really? (Score:1)
I take offense to your to your boolean statement of programmers being one or the other. You may have to revise your logic to include an indeterminate state: those of us who have the same background as you and traditionally prefer to code the way you describe for the most part, but have to dumb our
what ever happened to Zone Logic? (Score:4, Interesting)
Zone Logic is a nice combination of state charts and autononmous agent design. It was used in the 80s and early 90s for industrial automation, and was particularly good at recovery from hardware failures.
Real-time systems depend on sensors to report the state of the outside world. These sensors often fail, putting the system in an illogical state. Zone Logic offered a nice structured way to deal with this.
R. Roberts. Zone Logic: A Unique Method of Practical Artificial Intelligence. Radnor, PA, Compute! Books, 1989. citeseer [nec.com]
What I use... (Score:4, Informative)
It only has 1 chapter on UML statecharts, but after reading through it, I was able to describe in 1 diagram LCD behavior that used to take ~20 weakly worded requirements. (Shall do this, except when this, etc...)
If you're having trouble being explicit and clear in requirements, I would highly recommend looking at statecharts. (Picture speaks a thousand words, eh?)
BUY THIS BOOK NOW! (Score:1, Funny)
Re:A question on the book... (Score:1)
gee, thanks (Score:5, Informative)
Ah, well, google to the rescue: here's good example [dotnetcoders.com] of a statechart.
Re:gee, thanks (Score:3, Informative)
Re:gee, thanks (Score:2)
What Statecharts are (Score:3, Informative)
Alsthom Alcatel, for example, used our tool to discover flaws in the high-speed TGF before they started actually building it. They thereby save
Re:gee, thanks (Score:1)
It only looks like a flowchart to the untrained and horribly ignorant eye. :)
Looks a lot like... (Score:2)
Explanation of [degenerate]:
Markov chains can be represented
Re:Looks a lot like... (Score:1)
The UML crowd discovers finite state automata (Score:5, Insightful)
State machines aren't that complicated. The UML people are just burying them under a mess of jargon. This is probably not helpful.
UML is a reasonable idea that's turning into a management fad and is being used to sell overpriced tools. Such fads come around from time to time. Anyone remember decision logic tables? "Bubtangles?" The Kepner-Tregoe method? "Business Objects?"
Re: Kepner Tregoe (Score:1)
Re:The UML crowd discovers finite state automata (Score:2)
Here's a book [amazon.com] that presents a good case for using statecharts in one particular problem domain. Like any
Re:The UML crowd discovers finite state automata (Score:2)
Re:Why, You're RIGHT! (Score:2)
Re:The UML crowd discovers finite state automata (Score:1)
well, as a notation it's not bad (Score:2)
Re:The UML crowd discovers finite state automata (Score:1)
Excuse me, hardgrok, but you might say my PhD in Computer Science is kind of a "formal CS background." I learned about FSA in my algorithms class both as an undergrad and grad student, and I can clearly see that statecharts are not the same thing by any means. I find that p
Re:The UML crowd discovers finite state automata (Score:2)
Rather,
Re:The UML crowd discovers finite state automata (Score:1)
Sorry -- I didn't mean to go off on you or anyone in particular, and I understood your comments in the non-personal context.
I am no 'statechart-does-everything' zealot, but I've found even among CS educated people, that they tend to dismiss things because the value is not immediately obvious, especially as I infer from you,
Re:The UML crowd discovers finite state automata (Score:2)
They do?
Funny, every UML book where i have read something about statecharts mentions they're just state diagrams for finite automata.
"State machines aren't that complicated. The UML people are just burying them under a mess of jargon."
They are?
It seems to me this is necessary, as it is in other UMLized diagrams, in order to generalize it so it can be useful out of the context they got it from in the first place. It's part of the "universalizing" process.
For example
Re:The UML crowd discovers finite state automata (Score:2)
Re:The UML crowd discovers finite state automata (Score:2)
Don't discount UML...yet (Score:1)
Plus, it's rooted in object-oriented design. That, at the very least, is a step in the right direction (i.e. away from the long-term logistics nightmare of functional decomposition [wikipedia.org]) . Keep in mind, however, it's not a method [softed.com]. What you do wi
State charts and C (Score:3, Funny)
"While I can see that author Miro Samek has a directed target for his audience, I strongly feel that this book is a 'must read' for technical developers that wish to eliminate their chances at successfully mating within the next three to four days. In most cases, the knowledge gleaned from this book will also allow the reader to avoid sexual intercourse indefinately (excluding mating rituals that involve the transfer of monetary units first)."
Ok, state machines (Score:5, Informative)
A state machine is a way of representing a computational task. The machine or program can be in precisely one of a number of possible states at a given time. Different kinds of events cause the system to move from one state to another state (called a transition). Each transition can have an associated side effect. Therefore, the model of computation is:
The FSM is sort of the bottom rung of the computational ladder. FSMs can process regular languages (i.e. the languages describable by regular expressions). PDAs can process context-free languages (like most programming languages). Even more powerful than a PDA is a Turing machine, the theoretical "ultimate" model of a computing machine.
It might seem odd to think of the input events to the program as comprising sentences in some "language," but it makes sense when you consider it. Input events have an inherent structure to them, and this structure can often be described in the form of a regular language. In these situations, using an FSM model of the code is very natural since the code is directly, structurally related to the events it is processing.
Ok, there's your dose of CS for the day.
Re:Ok, state machines (Score:1)
When I heard the author speak at a meeting of the ACCU [accu-usa.org] recently, he described statecharts somewhat differently from the basic FSMs I knew.
Basically, statecharts allow some extensions to FSM: nested states, init/entry/exit actions for states, and probably some others. Apparently these are all defined in UML.
The author's basic approach is to represent the state as a pointer-to-member-function. Except that he's working on embedded systems, which means he uses C, which means he hacks his own OO in C. I m
Re:Ok, state machines (Score:1)
Statecharts are actually used to fully describe beasts like factory process control systems (the tools then generate C
Re:Ok, state machines (Score:3, Informative)
Without knowing more detail, I can only speculate here but from this statement, it appears this allows you to "call" another FSM from a particular state. Ok, now you have a PDA, the next step up from a FSM.
You have parallelism: if a statechart is subdivided with dashed lines (or whatever the fashionable notation for this is now), you have two (or more) control flows.
This is known as nondeterminism in computer science. Any nondeterministic
Re:Ok, state machines (Score:2)
Statecharts.pdf [weizmann.ac.il]
Re:Ok, state machines (Score:2)
I'd be curious to hear if I'm mistaken and there's more to it than that.
Hierarchical State Machines (Score:1)
I'm a little surprised that the poster didn't use the term, it's not like Samek doesn't use it everywhere in his book.
The first half of the book is just exploring different ways of implementing FSMs/HSMs in C and C++. It doesn't seem like a hard thing to do, but a simplistic version with nested switch/case stat
Re:Ok, state machines, but... (Score:3, Informative)
First and absolutely foremost: an FSM is an adstract concept born of graph theory. A statechart is a diagram of an FSM, which complies to a rigorous specification for what each notation means.
The statechart spec adds a few semantically trivial notations (i.e. they can be easily translated into standard digraph style FSM), which are nonetheless extremely useful for presenting clear, easy to understand diagrams.
Most of those additions have been mentioned already
Re:Ok, state machines (Score:1)
I wss a little confused by the term "statechart," but it turns out he's talking about finite state machines
The term statechart has been in the Comp Sci and then OO literature for a number of years, it was later adopted by the UML group. IIRC they often refer to David Harel as a source of much of the background on the concepts.
A quick google search shows the following reference:
Re:Ok, state machines (Score:1)
Is there a good visual representation of states that is more efficient than listing them out in words? (sort of like flowcharts are easier to look at than "IF (blah) GOTO (blah) ELSE (blah)")
Huh, you finally figured it out? (Score:5, Insightful)
It's not new. We've been doing state diagrams over here in Engineerland for decades.
Re:Huh, you finally figured it out? (Score:2)
mmm!!! pop machine!!! (Score:2)
Of couse, these days thanks to Microsoft, we have "popmachine.NET". What once was implemented with a smattering of TTL logic must now posess a Pentium chip, megabytes of RAM and embedded Windows with the
Not New In CS-Land, either (Score:2)
And the tech specs were described with states. Beautiful, simple, clean. Yep. Good stuff.
Re:Huh, you finally figured it out? (Score:1)
A little background information... (Score:3, Informative)
Los Alamos National Lab has some good info (overview mostly) [lanl.gov]
Lecture notes from MIT [mit.edu]
An interesting research project from The Beast [microsoft.com]
Some info on how FSMs are used for AI in computer games [gameai.com]
Statecharts Driven (Score:3, Insightful)
Re:Statecharts Driven (Score:1)
hmmm... my experience has been it depends on several factors (problem/solution/environment, etc.) that might govern "advantage". e.g. Kabira's [kabira.com] Design Center utilizes a really sweet plugin to Rose - almost 100% driven by UML/statecharts. I can't imagine a better tool for deriving solutions for the types of problems its best suited to solve. But as you point these tools are costly $$$!.
Re:Statecharts Driven (Score:1)
bumsword of the month - certifiably (Score:2, Interesting)
... methinks SlashDot may be spammed by m$monkeys. Many of my Google searches on this topic ended me up at the msdev site.I resist temptation to drop US$15000 to be m$certified.
State machines, statecharts, all seems a tad modal to me. State, to me, is simply a piece of information and calling it "state" and giving it this whole modal metaphor simply confuses. I hear a FUDding sound
progging by example, examples, lots of examples oh, and we're "modelling" here we don't actually need to do any coding
Reviewed in Dr. Dobb's Journal (Score:2, Informative)
Another reason (Score:2)
Additional tools (Score:1)
My new hammer in the state modelling department is petri nets; I would recommend any developer looking to learn about state modelling investigate petri nets as a supplement and/or replacement for state charts.
The translation of state chart to petri net (and vice-versa) is fairly straight-forw
Some good readings ... (Score:2, Interesting)
It is very interesting to see Statecharts mentioned on Slashdot. Statecharts are very useful for many reasons. I know that at NASA, in Europe and in the embedded world, generating code from directly from statecharts is very useful in having a very effective way of producing good code such that
Statecharts, UI, tools (Score:1)
Ian Horrocks
Constructing the User Interface with Statecharts
Addison-Wesley 1999
Robert Martin's state map compiler for/in C(++) and Java
http://www.objectmentor.com/resources/downl
Charles Rapp's state machine compiler
http://smc.sourceforge.net/
Michael
What Statecharts Are (Score:4, Insightful)
Statecharts are not a fancy way to do state machines. They are an extremely sophisticated graphics based notation with a formal mathematical semantics for modelling reactive systems. The ideas behind statecharts were developed by David Harel and Amir Pnueli (a winner of the prestigious Touring award and a world expert in Temporal Logic which heavily influenced the semantics of Statecharts).
I was one of the early programmers (later manager) in the company that Harel and Pnueli founded in the early 80s. Because of its mathematical basis, we were able to build a tool that "executed" the statechart designs - not a probabilistic execution, but more akin to a mathematical proof done via a computer program.
This isn't just a nice toy. It was used by companies like Alsthom-Alcatel to build the high-speed TGF train. The reactive model of the train was designed in Statecharts and the model executed. In this way they were able to discover serious flaws in the design (e.g. situations where the doors would open while travelling at high speeds). The only alternative to doing this is to build prototypes and do real world testing. By finding flaws in the very earliest design stage, the company was able to save millions of dollars and perhaps some lives.
For more online information see:
http://ilogix.com/quick_links/white_papers/inde
There is also a paper about Statechart semantics which I am listed as an author out there, but I have only found citations online. UML adopted Harel's Statecharts with some minor modifications, as part of the standard. They never claimed to invent it.
Disclaimer: I still have some stock options in i-Logix which was founded in 1984 but never did go public.
Re:Just another hammer (Score:1)
Boy, I had to jump in on this one.
True, not all projects benefit from statecharts. If you have something simple to do, you do it simply. However, the poster's comment about fluid designs actually makes a strong case for using a rigorous design methodology. There's nothing wrong with coding a little bit and then revising designs, but i
The original statechart sources (Score:2)
He is an ACM Fellow [acm.org], whose citation reads
If you are interested at all in theoretical computer science, you should read his book, which is
Re:The original statechart sources (Score:1)
conventional state machines and state diagrams. They are different.
AI Interface Standards: Report of the GDC 2003 (Score:1)
The roundtable discussion from the Game Developers Conference was recently
posted. One area that may be of interest is Session 6.
Session 6:
* Finite State Machines
Objectives
Identify how FSMs are commonly embedded in games
Define a common language to describe finite state machines
Define an XML description of FSMs for interoperability between tools
Define an API whereby FSMs can be efficiently and easily interfaced with
other
This is a great book (Score:1)
The illustrated examples of how hierarchical state machines really work are great for anyone looking to really utilize the concepts embraced by among others the UML to their full potential.
That said, the approach used by the supplied framework is not without flaws (exception safety versus Run-To-Completion discrete steps, etc), as pointed out in discussions on USENET. Whether it fits your
how does it relate to "Executable UML"? (Score:1)
I have a lot of time for Schlaer Mellor. As a methodology coming from the real-time world, it takes my softer async requirements (eg client-and-server or browser-and-application) well in its stride.
It's also very much based on UML statecharts, and if anyone has re
Re:C and C++ (Score:1, Informative)
Re:C and C++ (Score:2)
And he never said STL. He said *a* standard library. But anyway...
Re:C and C++ (Score:3, Insightful)
Re:C and C++ (Score:1, Insightful)
Re:C and C++ (Score:1)
I've never seen assembly that's as horrible to read as C++.
Maybe if you look at the bits in the binary form of machine language, and then poke a pencil in your eye at the same time.
-Kevin
Re:C and C++ (Score:1)
I’ve been using statecharts since using FORTH (Score:1)
Create a buffer, fill it with input and call the function for the inital "state", which processes the input and passes it off to the next state in the array, till you hit a function which "finishes" the transformation of the input string.