Custom operation implementation on a statechart

339 views
Skip to first unread message

Benedek Horváth

unread,
Mar 16, 2014, 10:34:24 AM3/16/14
to yakind...@googlegroups.com
Dear All,

I would like to add a custom operation call to my statechart. I have added the operation call to the statechart, but I cannot give any implementation to this operation.

Although I was trying to do it according to the guidelines written in the Documentation (http://statecharts.org/documentation.html#SimulatingOperationsWithCustomJavaCode), I do not find any Operation class option in the run configuration. I can only find the Eclipse run configurations popup, where there is not any option, like on the screenshot.

May someone help me?

Thank you in advance!

Best regards,
Ben

Benedek Horváth

unread,
Mar 16, 2014, 11:21:43 AM3/16/14
to yakind...@googlegroups.com
Dear All,

Sorry for the dummy question, I have found the answer myself.

Here you find the same window as the printscreen: Run Configurations -> YAKINDU Statechart -> <sct_file_name>.sct

I would modify my question: Is there a way, that the statemachine will call that operation in my generated Java code? I have created a simple Java application based on the generated statemachine code, and when I raise the event that calls the given operation (that I have implemented previously), I get a NullPointerException.

Here is the StackTrace:

Exception in thread "main" java.lang.NullPointerException
    at org.yakindu.scr.simple.SimpleStatemachine.reactMain_region_Train_moving(SimpleStatemachine.java:240)
    at org.yakindu.scr.simple.SimpleStatemachine.runCycle(SimpleStatemachine.java:303)

Is there any way that the operation will be called as it was when I simulated the model in Yakindu? (I ran the *.sct file in Eclipse, after I modified the run configurations according.)

Thank you for help in advance!

Best regards,
Ben

Benedek Horváth

unread,
Mar 26, 2014, 8:16:03 PM3/26/14
to yakind...@googlegroups.com
Dear All,

I have found the answer for my previous question.

You should not only register the class in the Run configurations for the *.sct file (as shown in the homepage's documentation section), but you should register an operation callback for that method.

So for example if you have this on the statechart:

interface Train:
operation go(): void
operation stop(): void

Then you should register an Operation Class in the Run configurations for your YAKINDU Statechart. That operation class should have got a void go(), and a void stop() methods. (Referring for your operation class by its qualified name is a good practice.)
Then you generate the Java code by a Java GeneratorModel (sgen), from your statechart.
Then if you would like to call that method programetically from a Java code, you should register an operation callback for this purpose:

ss.getSCITrain().setSCITrainOperationCallback(new Train());       // ss is the instance of the Statechart class, that represents the statechart generated by the sgen.

Where ITrain means "interface Train", and "new Train()" is an object, whose class (Train) implements interface called SCITrainOperationCallback (ITrain means again "interface Train"). That interface has got the void go(), and void stop() methods, that you have declared on the statechart.

I hope someone may find these pieces of information useful one time.

Best regards,
Ben

pHiLoX pHiLoX

unread,
Jul 31, 2017, 8:34:39 AM7/31/17
to YAKINDU User
Hello,

your info is very useful up to this point! currently I am trying to do the same thing.
I want to call a custom method within the diagram.
Either with return-type of void in order to do stuff in the certain state.
Or the better solution would be a method which returns an integer. Then the events in the diagram listen on the integer
and transition appropriately.

Can you explain the way to use a non-generated method within the generated code(in the model without simulation) again or in
a different way. I did everything to the point where I have:

ss.getSCITrain().setSCITrainOperationCallback(new Train());

Just with my classes instead of Train. What exacly is happening there and how does this help me?

Just to let you know, currently I have the same error-message as you did have: Exception in thread "main" java.lang.NullPointerException
    at org.yakindu.scr.simple.SimpleStatemachine.reactMain_region_Train_moving(SimpleStatemachine.java:240)
    at org.yakindu.scr.simple.SimpleStatemachine.runCycle(SimpleStatemachine.java:303)

Another little question. Is the code of one state executed until one transition can be fired?
In every cycle the code of a state is executed and all events are checked right?
And when an event is true the state transisions.

Then I would like to have an "int light = getLight()" in the state and events like "light > 100", "light < 100".

Thank you very much for your help with this.

Jonathan Thöne

unread,
Aug 1, 2017, 3:25:19 AM8/1/17
to YAKINDU User
Hi philox,

I think our documentation can help you with your problem : https://www.itemis.com/en/yakindu/state-machine/documentation/user-guide/#java-code-generator
Section 11.2.10 provides some little sample code, which should help with your problem.

Best regards,

Jonathan

Reply all
Reply to author
Forward
0 new messages