Saving Java objects during execution

Moderator: Moderators

Saving Java objects during execution

Postby brickwall » Tue Jan 29, 2013 1:09 pm

I'm having problems saving JDK properties while executing a state machine in 17.0.2. I'm trying to create and initialize objects in one state and re-use them in other states without redoing all of the initialization over and over again.

In my test case, I created a class with a single property (m_file) of type java::io::File. In an OpaqueBehavior, I can execute the following BeanShell commands:

Code: Select all
File file = new File("C:\\");
print ("Created new file object of type: " + file.getClass());
ALH.setValue(self, "m_file", file);
print("m_file.getClass() = " + m_file.getClass());
print("ALH.getValue(self, \"m_file\").getClass() = " + ALH.getValue(self, "m_file").getClass());

The output of this script in the simulation console is:
Code: Select all
Created new file object of type: class java.io.File
m_file.getClass() = class java.lang.String
ALH.getValue(self, "m_file").getClass() = class java.lang.String

Using ALH.setValue to save the File object appears to save it as a String. Is this the expected behavior?

In a different class, I also tried the following variation on the script to avoid using ALH:
Code: Select all
File file = new File("C:\\");
print ("Created new file object of type: " + file.getClass());
this.m_file = file;
print("m_file.getClass() = " + m_file.getClass());

Setting m_file this way kept it as a java.io.File (m_file.getClass() = class java.io.File). However, after moving to a different state and checking on m_file, it had reverted to java.lang.String again.

Is saving a Java object between states possible in MD? If so, what am I doing wrong? If not, is there a different approach that I can use to achieve the same goal? I've attached my project file for reference.

Also, in my project, I'm consistently having issues with final state's behavior (Check Java Object). If I move it to the Do Activity, the behavior is not executed. If it's in Entry or Exit, then it executes. What is the issue here?

Thank you for any assistance.
You do not have the required permissions to view the files attached to this post.
brickwall
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Tue Jun 05, 2012 1:39 pm

Re: Saving Java objects during execution

Postby lonniev » Tue Feb 12, 2013 11:56 am

Be careful and be explicit: you mix references to "self", "this", and to default instance references freely in your code. Depending on the scripting language(s) involved within a runtime context, these variables may or may not refer to the object(s) that you assume they do. Here, for example, is a little print out from your model:

Entering behaviorCreateObj...
self is (com.nomagic.magicdraw.simulation.fuml.a.u@572fa0a: TestBlockThis)
this is 'this' reference (XThis) to Bsh object: NameSpace: javax_script_context (bsh.ExternalNameSpace@7ade72f2)

I suspect that you are assuming that self == this.
lonniev
Customer Support
Customer Support
 
Posts: 12
Posts Rating:1
Joined: Mon Nov 01, 2010 12:53 pm

Re: Saving Java objects during execution

Postby brickwall » Tue Feb 12, 2013 1:07 pm

Thank you for the response.

I actually was not assuming "self" and "this" were the same; I only used self as a parameter to pass to ALH.getValue and ALH.setValue. I did mix setting/getting values using ALH and "this" to see if the results were the same. Is it a bad idea to use them interchangeably?

Anyway, I tried reducing the example to explicitly use only "this", and it gives the same results.

Here is the code that gets executed in one state:
print ("Entering behaviorCreateObj...");
print("this.m_file initial state = " + this.m_file);
 
File file = new File("C:\\");
print ("Created new file object of type: " + file.getClass());
 
print ("Setting property m_file to newly created file object..."); 
this.m_file = file;

print("this.m_file.getClass() = " + this.m_file.getClass());
print ("Exiting behaviorCreateObj...");

behaviorCreateObj prints the following:
Entering behaviorCreateObj...
this.m_file initial state = void
Created new file object of type: class java.io.File
Setting property m_file to newly created file object...
this.m_file.getClass() = class java.io.File
Exiting behaviorCreateObj...

When checking on the property in the next state:
print("Entering behaviorCheckObj...");
print("this.m_file.getClass() = " + this.m_file.getClass());
print("Exiting behaviorCheckObj...");

behaviorCheckObj prints the following:
Entering behaviorCheckObj...
this.m_file.getClass() = class java.lang.String
Exiting behaviorCheckObj...
You do not have the required permissions to view the files attached to this post.
brickwall
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Tue Jun 05, 2012 1:39 pm


Return to Cameo Simulation Toolkits

Who is online

Users browsing this forum: No registered users and 1 guest