Trying to create an execution listener

Moderator: Moderators

Trying to create an execution listener

Postby max.byers@ngc.com » Wed Oct 25, 2017 6:41 pm

Hi,

I was reading through documentation and saw that simulations can have execution listeners. I was able to run a simulation through a macro but I didn't know how to create an execution listener. I then created my own java class and compiled it into a jar, but I could not import it into a macro after add it as an external library to the simulation script engine. Is this the correct approach or should is there a better way to create an execution listener?

I attached the java file for the listener. I compiled it using the classpath given in the cameoea.properties file. Then I created the jar file and added that to external libraries. However, when I try to import the jar file in a script using either "var CollectionsAndFiles = new JavaImporter(Packages.myjar)" or Javascript's importClass(Packages.myjar) I get error messages. For the importClass call in an OpaqueAction I get the error: TypeError: [object JavaPackage] is not a Java class in nashorn:mozilla_compat.js at line number 373 at column number 16 expression body: importClass(Packages.ExecListener);

Any help getting a listener working would be greatly appreciated!!

Thank you,
Max
You do not have the required permissions to view the files attached to this post.
max.byers@ngc.com
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Wed Sep 27, 2017 4:24 pm

Re: Trying to create an execution listener

Postby chanon_s » Tue Oct 31, 2017 2:58 am

Dear Customer

Could you please tell us what CST version are you using?

The external jar files, which are specified in the External Libraries project options, are loaded and can be used when running simulation only. For example, when running an activity diagram with an opaque action.

If you would like to register your custom execution listener globally, we suggest you to create a custom plugin and have that custom execution listener loaded and registered when the plugin is initializing. This way, your custom execution listener will be called every time you run a simulation for any project. Please see the following attachment and extract it into the <install.root>/plugins folder.

MaxExternalPlugin.zip


The folder structure should look like this:
    <install.root>/plugins/MaxExternalPlugin/MaxExternalPlugin.jar
    <install.root>/plugins/MaxExternalPlugin/plugin.xml

Then you can try to run simulation with any sample project and your custom execution listener will be called.

If you would like to register your custom execution listener for only a single project, we suggest you to see the following sample project:

register_when_running_simulation.zip


Please extract this zip file and open the Test project to run the Model activity diagram.

Sincerely

Chanon S.
No Magic Customer Support
You do not have the required permissions to view the files attached to this post.
chanon_s
Customer Support
Customer Support
 
Posts: 91
Posts Rating:7
Joined: Thu Dec 04, 2014 2:21 am

Re: Trying to create an execution listener

Postby max.byers@ngc.com » Tue Oct 31, 2017 6:11 pm

I am using Cameo Enterprise Architecture 18.5. I was able to get a plugin working. Compiling java code and testing plugins takes a long time, so I was hoping it was possible to create one in an opaqueAction script.

However, your solution of importing the listener is very helpful. I don't have to restart the tool each time I want to test the plugin which is nice. For the attached test project, the class is not being recognized. I have the CustomExecListener.jar file on my Desktop and I made sure under Project -> Simulation Script Engine -> External Libraries that the jar is attached. I restarted Cameo after adding the path, but the import statement continues to fail. I notice the import says Packages.max.ExecListener. Am I supposed to put the jar in folder called max? Does the jar have to be in the installation directory?
max.byers@ngc.com
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Wed Sep 27, 2017 4:24 pm

Re: Trying to create an execution listener

Postby chanon_s » Tue Oct 31, 2017 11:26 pm

Dear Customer

Could you please upgrade your CEA to the latest service pack, which is 18.5 sp2? We have fixed an issue into 18.5 sp2 which solved the issue that it could not run the Test project.

I notice the import says Packages.max.ExecListener.


Regarding the import statement, if the package of your classes in your jar file do not start with com, edu, or net; you need to prepend the Packages word to your import statement.

Am I supposed to put the jar in folder called max?

No, you don't need to. max is a package which is inside of the jar file. The structure of the jar file is as follows:
max/ExecListener.class

Does the jar have to be in the installation directory?

No, it does not need to. You just need to make sure that it is put at the same place as the one you have specified in the External Libraries project option.

Sincerely

Chanon S.
No Magic Customer Support
chanon_s
Customer Support
Customer Support
 
Posts: 91
Posts Rating:7
Joined: Thu Dec 04, 2014 2:21 am

Re: Trying to create an execution listener

Postby max.byers@ngc.com » Thu Nov 02, 2017 2:32 pm

Hi,

I am not able to upgrade my version of MagicDraw at this time. However, I was able to create the plugin to run a simulation. Once I register an execution listener I am unable to get rid of it. In the documentation (https://docs.nomagic.com/display/CST185 ... n+listener) it says "All registered listeners will be cleared and removed automatically when the execution is terminated." I use
SimulationSession sess = SimulationManager.execute(element)
When the simulation ends, I run
SimulationManager.terminate(sess).
But the next time I run any diagram in the model by hand, I still see my print statements from the execution listener I registered. If I run a simulation using my plugin again, then I get double the print statements because the listener is registered a second time.

Am I missing something in how registering works?

Thanks for the help!

Max
max.byers@ngc.com
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Wed Sep 27, 2017 4:24 pm

Re: Trying to create an execution listener

Postby chanon_s » Fri Nov 03, 2017 2:25 am

Dear Customer

As described in the documentation (the same link from your previous post), as quoted below:

Once you have created the execution listener, you can register it to a list of global execution listeners.
To register a new execution listener to a global list, for example, type:
ExecutionManager.registerListener(new MyExecutionListener());



This means that your custom execution listener will be registered to the global execution listener list. At the time that the ExecutionManager.registerListener() method is called, the given execution listener is just added to the global list. It is not really added to any simulation execution even though it is being called when a simulation execution is running. In addition, the given execution listener will be retrieved from the global list and used to register in the simulation execution of the next runs. So, you just need to register your custom execution listener only once (maybe in the init() method of your custom plugin, as well as we did in our plugin example). Your listener will be automatically registered in the simulation execution (when simulation is going to run) and unregistered from the simulation execution (when simulation is going to terminate).

All registered listeners will be cleared and removed automatically when the execution is terminated.

By this sentence, it means that your registered custom execution listener will be removed from the simulation execution, not from the global listener list.

Sincerely

Chanon S.
No Magic Customer Support
chanon_s
Customer Support
Customer Support
 
Posts: 91
Posts Rating:7
Joined: Thu Dec 04, 2014 2:21 am

Re: Trying to create an execution listener

Postby max.byers@ngc.com » Mon Nov 06, 2017 12:55 pm

Hi,

Thanks for the clear response! I did incorrectly interpret the sentence in the documentation. I am guessing there is not a way to disable a listener once it has been registered.

When I look at the JavaDoc for the Cameo Simulation Toolkit plugin it doesn't have a class called Execution Manager. I assumed the documentation was just outdated and the new class name is Simulation Manager. Is this correct or is there another class called Execution Manager that I can look into. I am also unable to see some other classes that show up in the SimulationManager methods like "com.nomagic.magicdraw.simulation.execution.session.SimulationSession". Does the JavaDoc only show API methods and classes?

Thanks,
Max
max.byers@ngc.com
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Wed Sep 27, 2017 4:24 pm

Re: Trying to create an execution listener

Postby chanon_s » Mon Nov 06, 2017 9:25 pm

Dear Customer

I am guessing there is not a way to disable a listener once it has been registered.

Yes, you are correct. Currently, there is no way to unregister any execution listeners.

When I look at the JavaDoc for the Cameo Simulation Toolkit plugin it doesn't have a class called Execution Manager. I assumed the documentation was just outdated and the new class name is Simulation Manager. Is this correct or is there another class called Execution Manager that I can look into.

There are both ExecutionManager and SimulationManager classes. But, the difference is that SimulationManager is open API, while ExecutionManager is not. So, please change to use SimulationManager instead, because all classes and methods which are not open API are subjected to change at any time.

I am also unable to see some other classes that show up in the SimulationManager methods like "com.nomagic.magicdraw.simulation.execution.session.SimulationSession". Does the JavaDoc only show API methods and classes?

SimulationSession is not yet an open API in CST 18.5. However, we have changed this class as open API and will be released with CST 19.0 beta.

Sincerely

Chanon S.
No Magic Customer Support
chanon_s
Customer Support
Customer Support
 
Posts: 91
Posts Rating:7
Joined: Thu Dec 04, 2014 2:21 am


Return to Cameo Simulation Toolkits

Who is online

Users browsing this forum: No registered users and 0 guests