Home » Modeling » OCL » How to define collection type of variable
How to define collection type of variable [message #64250] |
Wed, 05 November 2008 05:29  |
Eclipse User |
|
|
|
Originally posted by: zzhangh.cn.ibm.com
Hi,
I'm trying to define a variable by following the help document, it is OK with normal variable, but how to define a
collection type of variable, say sequence type.
Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
appContextVar.setName("myvar");
appContextVar.setType(myType);//What to write here if "myType" is of collection?
ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
Thanks!
Regards,
Hao
|
|
|
Re: How to define collection type of variable [message #64317 is a reply to message #64250] |
Wed, 05 November 2008 08:37   |
Eclipse User |
|
|
|
Originally posted by: cdamus.zeligsoft.com
Hi, Hao,
Try this:
TypeUtil.resolveSequenceType(ocl.getEnvironment(), myType)
HTH,
Christian
Hao Zhang wrote:
> Hi,
> I'm trying to define a variable by following the help document, it is OK with normal variable, but how to define a
> collection type of variable, say sequence type.
>
> Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
> appContextVar.setName("myvar");
> appContextVar.setType(myType);//What to write here if "myType" is of collection?
> ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
>
> Thanks!
>
> Regards,
> Hao
|
|
|
Re: How to define collection type of variable [message #64451 is a reply to message #64317] |
Wed, 05 November 2008 21:14   |
Eclipse User |
|
|
|
Originally posted by: zzhangh.cn.ibm.com
Hi Christian,
Thanks for your instruction, I think I've found the correct type, however when I evaluate this expression
"myVar->size()", I always get result of 1, which is expected to be a number greater than 1. And if I evaluate "myVar", I
found it is an instance of EContainmentList having multiple elements, which is expected.
Could you shed me some light on this, thanks!
//Customize this OCL environment
Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
appContextVar.setName("myVar");
appContextVar.setType(EcorePackage.Literals.ORDERED_SET_TYPE );
ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
try {
OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
helper.setContext(SpmsPackage.Literals.SP_SERVICE_PLAN);
OCLExpression<EClassifier> exp = helper.createQuery(metric.getFormula());//Seems this call is time costly
Query<EClassifier, EClass, EObject> query = ocl.createQuery(exp);
query.getEvaluationEnvironment().add(appContextVar.getName() ,
SpmsResourceManager.getInstance().getCustomerModel().getCust omers());
////////////This line is evaluation result////////////////
Object object = query.evaluate(servicePlan);
System.out.println(object);
if (object instanceof Number) {
result = ((Number) object).doubleValue();
}
} catch (ParserException e) {
e.printStackTrace();
}
Regards,
Hao
Christian W. Damus wrote:
> Hi, Hao,
>
> Try this:
>
> TypeUtil.resolveSequenceType(ocl.getEnvironment(), myType)
>
> HTH,
>
> Christian
>
> Hao Zhang wrote:
>> Hi,
>> I'm trying to define a variable by following the help document, it is OK with normal variable, but how to define a
>> collection type of variable, say sequence type.
>>
>> Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
>> appContextVar.setName("myvar");
>> appContextVar.setType(myType);//What to write here if "myType" is of collection?
>> ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
>>
>> Thanks!
>>
>> Regards,
>> Hao
|
|
|
Re: How to define collection type of variable [message #64474 is a reply to message #64451] |
Thu, 06 November 2008 12:16   |
Eclipse User |
|
|
|
Originally posted by: cdamus.zeligsoft.com
Hi, Hao,
See some comments in-line, below.
HTH,
Christian
Hao Zhang wrote:
> Hi Christian,
>
> Thanks for your instruction, I think I've found the correct type, however when I evaluate this expression
> "myVar->size()", I always get result of 1, which is expected to be a number greater than 1. And if I evaluate "myVar", I
> found it is an instance of EContainmentList having multiple elements, which is expected.
Interesting that this is a containment list. Is it possible that the
contents of the list are changing during the execution of this code?
Containment lists sometimes surprise by losing elements when they are
added to some other containment ...
>
> Could you shed me some light on this, thanks!
>
> //Customize this OCL environment
> Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
> appContextVar.setName("myVar");
> appContextVar.setType(EcorePackage.Literals.ORDERED_SET_TYPE );
Hmmm ... this could explain it. You have set the variable's type to the
OrderedSetType *metaclass* instead of an instance of it, as I indicated
in my previous reply. You should use
TypeUtil.resolveOrderedSetType(...) to get the OrderedSet(T) type for
the particular element type of your collection value.
By assigning the OrderedSetType type to your variable, it is a scalar,
so OCL expects only a single value and actually sees the collection as
one collection-object, not as a collection-of-objects. Then, the
->size() operation coerces the scalar value to a Set (because of the
arrow operator) and takes its size, which is one, because the set
contains the single scalar value of the myVar variable.
> ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
>
> try {
> OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
> helper.setContext(SpmsPackage.Literals.SP_SERVICE_PLAN);
> OCLExpression<EClassifier> exp = helper.createQuery(metric.getFormula());//Seems this call is time costly
The first parsing of an expression in an Environment has a lot of
overhead, possibly even loading plug-ins by searching the
EPackage.Registry for packages in looking up names. I would be
concerned if subsequent parsing of expressions using the same
Environment is anything like as costly.
> Query<EClassifier, EClass, EObject> query = ocl.createQuery(exp);
> query.getEvaluationEnvironment().add(appContextVar.getName() ,
> SpmsResourceManager.getInstance().getCustomerModel().getCust omers());
>
> ////////////This line is evaluation result////////////////
> Object object = query.evaluate(servicePlan);
> System.out.println(object);
>
> if (object instanceof Number) {
> result = ((Number) object).doubleValue();
> }
> } catch (ParserException e) {
> e.printStackTrace();
> }
>
>
> Regards,
> Hao
>
> Christian W. Damus wrote:
>> Hi, Hao,
>>
>> Try this:
>>
>> TypeUtil.resolveSequenceType(ocl.getEnvironment(), myType)
>>
>> HTH,
>>
>> Christian
>>
>> Hao Zhang wrote:
>>> Hi,
>>> I'm trying to define a variable by following the help document, it is OK with normal variable, but how to define a
>>> collection type of variable, say sequence type.
>>>
>>> Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
>>> appContextVar.setName("myvar");
>>> appContextVar.setType(myType);//What to write here if "myType" is of collection?
>>> ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
>>>
>>> Thanks!
>>>
>>> Regards,
>>> Hao
|
|
|
Re: How to define collection type of variable [message #64544 is a reply to message #64474] |
Thu, 06 November 2008 22:32  |
Eclipse User |
|
|
|
Originally posted by: zzhangh.cn.ibm.com
Christian,
You are right, using resolved type did solve my problem, thanks a lot!
Regards,
Hao
Christian W. Damus wrote:
> Hi, Hao,
>
> See some comments in-line, below.
>
> HTH,
>
> Christian
>
> Hao Zhang wrote:
>> Hi Christian,
>>
>> Thanks for your instruction, I think I've found the correct type, however when I evaluate this expression
>> "myVar->size()", I always get result of 1, which is expected to be a number greater than 1. And if I evaluate "myVar", I
>> found it is an instance of EContainmentList having multiple elements, which is expected.
>
> Interesting that this is a containment list. Is it possible that the
> contents of the list are changing during the execution of this code?
> Containment lists sometimes surprise by losing elements when they are
> added to some other containment ...
>
>> Could you shed me some light on this, thanks!
>>
>> //Customize this OCL environment
>> Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
>> appContextVar.setName("myVar");
>> appContextVar.setType(EcorePackage.Literals.ORDERED_SET_TYPE );
>
> Hmmm ... this could explain it. You have set the variable's type to the
> OrderedSetType *metaclass* instead of an instance of it, as I indicated
> in my previous reply. You should use
> TypeUtil.resolveOrderedSetType(...) to get the OrderedSet(T) type for
> the particular element type of your collection value.
>
> By assigning the OrderedSetType type to your variable, it is a scalar,
> so OCL expects only a single value and actually sees the collection as
> one collection-object, not as a collection-of-objects. Then, the
> ->size() operation coerces the scalar value to a Set (because of the
> arrow operator) and takes its size, which is one, because the set
> contains the single scalar value of the myVar variable.
>
>> ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
>>
>> try {
>> OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
>> helper.setContext(SpmsPackage.Literals.SP_SERVICE_PLAN);
>> OCLExpression<EClassifier> exp = helper.createQuery(metric.getFormula());//Seems this call is time costly
>
> The first parsing of an expression in an Environment has a lot of
> overhead, possibly even loading plug-ins by searching the
> EPackage.Registry for packages in looking up names. I would be
> concerned if subsequent parsing of expressions using the same
> Environment is anything like as costly.
>
>> Query<EClassifier, EClass, EObject> query = ocl.createQuery(exp);
>> query.getEvaluationEnvironment().add(appContextVar.getName() ,
>> SpmsResourceManager.getInstance().getCustomerModel().getCust omers());
>>
>> ////////////This line is evaluation result////////////////
>> Object object = query.evaluate(servicePlan);
>> System.out.println(object);
>>
>> if (object instanceof Number) {
>> result = ((Number) object).doubleValue();
>> }
>> } catch (ParserException e) {
>> e.printStackTrace();
>> }
>>
>>
>> Regards,
>> Hao
>>
>> Christian W. Damus wrote:
>>> Hi, Hao,
>>>
>>> Try this:
>>>
>>> TypeUtil.resolveSequenceType(ocl.getEnvironment(), myType)
>>>
>>> HTH,
>>>
>>> Christian
>>>
>>> Hao Zhang wrote:
>>>> Hi,
>>>> I'm trying to define a variable by following the help document, it is OK with normal variable, but how to define a
>>>> collection type of variable, say sequence type.
>>>>
>>>> Variable appContextVar = ExpressionsFactory.eINSTANCE.createVariable();
>>>> appContextVar.setName("myvar");
>>>> appContextVar.setType(myType);//What to write here if "myType" is of collection?
>>>> ocl.getEnvironment().addElement(appContextVar.getName(), appContextVar, true);
>>>>
>>>> Thanks!
>>>>
>>>> Regards,
>>>> Hao
|
|
|
Goto Forum:
Current Time: Wed May 07 15:52:08 EDT 2025
Powered by FUDForum. Page generated in 0.05046 seconds
|