I have a working state machine that sends similar messages from several states. At present they are all hard coded, so I have fragments in my .scxml file like:
<state id="state1">
<transition event="event_1">
<send event="unexpectedEvent1FromState1"/>
</transition>
</state>
and
<state id="state2">
<transition event="event_2">
<send event="unexpectedEventEvent2FromState2"/>
</transition>
</state>
and I have to catch multiple unexpectedEventXxxxFromYyyy
messages elsewhere in my C++ code.
I'd like to standardise these message so that I just have to catch a single, parameterised unexpectedEvent
signal in my code, which will examine the QScxmlEvent object to find the transition and source state that caused the signal to be emitted.
Having looked at the Qt documentation, I believe I need to add a data model. I don't use these anywhere so have no familiarity. I have previously experimented fairly successfully with an EcmaScript data model but found that the application crashes on my machine if I try to create more than around 150 machines, apparently because of the memory required for 150+ V8 JavaScript engines. Since I need to run 1000+ copies of the state machine, an EcmaScript data model is ruled out and I need to use a C++ data model.
I've had no luck with this and the program crashes when the first machine I instantiate first tries to process events. I have reduced the code in the data model to the barest of bare bones as below, and it still crashes.
Please can someone tell me what to do to get my data model working? I've looked at the Qt examples and they all seem too trivial to be helpful, can anyone point me to any meatier examples? Many thanks.
Bare bones code changes
Added to the root element in the .scxml file:
datamodel="cplusplus:FooDatamodel:foodatamodel.h"
foodatamodel.h:
#ifndef FOODATAMODEL_H
#define FOODATAMODEL_H
#include "qscxmlcppdatamodel.h"
class FooDatamodel : public QScxmlCppDataModel
{
Q_OBJECT
Q_SCXML_DATAMODEL
public:
FooDatamodel();
};
#endif // FOODATAMODEL_H
foodatamodel.cpp
#include "foodatamodel.h"
FooDatamodel::FooDatamodel()
{
}
Disclaimers:
- I'm using the state machine editor in Qt Creator and I may well have left out something vital in the hand-written SCXML fragment at the top. I'm pretty sure that the real file is syntactically and semantically valid - although the
datamodel
attribute above is pretty accurate. - The real filenames and state and transition names are different, and I may have failed to change something in the C++ fragments above. The real files do not contain any substantial code.
Thanks again, apologies for the length of the question.