Accessing structural features that are indexed

Moderator: Moderators

Accessing structural features that are indexed

Postby tpen3219 » Thu Feb 07, 2013 10:14 pm

I'm trying to access parts of an object that have a multiplicity of more than one.
For single values parts I use ReadStructuralFeature.
How do I apply an index to the lookup?
Last edited by tpen3219 on Fri Feb 08, 2013 7:05 pm, edited 1 time in total.
tpen3219
Forum Beginner
Forum Beginner
 
Posts: 93
Posts Rating:3
Joined: Fri Nov 04, 2011 11:57 am

Re: Accessing structural that are indexed

Postby kritsana_u » Fri Feb 08, 2013 1:43 am

Hi,

You cannot read the structural feature value from a specific index by ReadStructuralFeatureAction. It always returns all structural feature values. You have to create an opaqueBehavior for such action.
1. Create new OpaqueBehavior (or OpaqueFunction) to the project.
2. Add two input parameter for object, and the read index.
3. Add one return parameter.
001.png

4. Enter the script to the Body of the OpaqueBehavior or OpaqueFunction.
Code: Select all
if(values != null) {  // values should not be null
  result = values.get(index);  // Get the run-time value at the specified index.
  // If the result is not a run-time object (Object_) , it can be used by other run-time object directly.
  // But if the result is a run-time object, you need to create a Reference to it.
  if (result instanceof fUML.Semantics.Classes.Kernel.Object_) {
    // The reference must be created with the owner of the run-time object
    fUML.Semantics.Classes.Kernel.Reference r = new fUML.Semantics.Classes.Kernel.Reference(result.getOwner());
    // Then, set the run-time object to be referenced to the Reference.
    r.setReferent(result);
    // Set the create reference to the be the result
    result = r;
  }
}
else {
  result = null;
}

5. Specify the Language of the OpaqueBehavior to bsh.
6. Drag the OpaqueBehavior and drop to the activity diagram to create a callBehaviorAction.
002.png


Yours sincerely,

Kritsana U.
You do not have the required permissions to view the files attached to this post.
kritsana_u
Forum Expert
Forum Expert
 
Posts: 116
Posts Rating:3
Joined: Wed Jan 27, 2010 3:02 am

Re: Accessing structural features that are indexed

Postby tpen3219 » Fri Feb 08, 2013 7:15 pm

First, thank you very much for this help!!!!

Can you confirm that I understand the code by checking my notes next to each line?


4. Enter the script to the Body of the OpaqueBehavior or OpaqueFunction.

Code: Select all
if(values != null) { Tests to see if the list is empty
result = values.get(index); Retrieves the element in the structured feature list that matches the index value
if (result instanceof fUML.Semantics.Classes.Kernel.Object_) { Makes certain that the value is an object ?????????????????????????
fUML.Semantics.Classes.Kernel.Reference r = new fUML.Semantics.Classes.Kernel.Reference(result.getOwner()); Obtains the object using the reference (?)
r.setReferent(result); ?????????????
result = r; The result of the previous line is assigned to the output parameter
}

}
else {
result = null;
}[quote="kritsana_u"]Hi,
tpen3219
Forum Beginner
Forum Beginner
 
Posts: 93
Posts Rating:3
Joined: Fri Nov 04, 2011 11:57 am

Re: Accessing structural features that are indexed

Postby kritsana_u » Wed Feb 13, 2013 3:46 am

Hi,

I updated my previous post by adding the code comment to it.
So, please see my previous post.

Best regards,

Kritsana U.
kritsana_u
Forum Expert
Forum Expert
 
Posts: 116
Posts Rating:3
Joined: Wed Jan 27, 2010 3:02 am

Re: Accessing structural features that are indexed

Postby tpen3219 » Tue Feb 26, 2013 5:35 am

I used the technique you provided.
I am getting an error:

2013-02-26 03:40:47,633 INFO : Opaque behavior evaluation. Context is : Call Behavior Action Get One Plan
2013-02-26 03:40:47,638 ERROR: sun.org.mozilla.javascript.internal.EvaluatorException: missing ; before statement (<Unknown source>#4) in <Unknown source> at line number 4 expression body:
if(facilityPlans != null) {
facilityPlan = facilityPlans.get(index);
if (facilityPlan instanceof fUML.Semantics.Classes.Kernel.Object_) {
fUML.Semantics.Classes.Kernel.Reference r = new fUML.Semantics.Classes.Kernel.Reference(facilityPlan.getOwner());
r.setReferent(facilityPlan);
facilityPlan = r;
}
} else {
facilityPlan = null;
}
2013-02-26 03:40:47,638 INFO : Results : facilityPlan = []

The input is an object attribute with a multiplicity of 0..*. There is only object in the set.
The index value is 1.
tpen3219
Forum Beginner
Forum Beginner
 
Posts: 93
Posts Rating:3
Joined: Fri Nov 04, 2011 11:57 am

Re: Accessing structural features that are indexed

Postby tpen3219 » Tue Feb 26, 2013 6:19 am

I removed most of the logic and still get the following error:

2013-02-26 04:17:26,484 INFO : Opaque behavior evaluation. Context is : Call Behavior Action Get One Plan
2013-02-26 04:17:26,490 ERROR: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function get in object (fUML.Semantics.Classes.Kernel.Object_@5c0146a7: FacilityPlan         area[0] =         portal[0] =         index[0] =         name[0] =         planType[0] =         incident[0] =). (<Unknown source>#2) in <Unknown source> at line number 2 expression body:

if(facilityPlans != null) {
facilityPlan = facilityPlans.get(index);
}
else { facilityPlan = null;
}
2013-02-26 04:17:26,490 INFO : Results : facilityPlan = []
tpen3219
Forum Beginner
Forum Beginner
 
Posts: 93
Posts Rating:3
Joined: Fri Nov 04, 2011 11:57 am


Return to Cameo Simulation Toolkits

Who is online

Users browsing this forum: No registered users and 0 guests

cron