Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » How to define collection type of variable
How to define collection type of variable [message #64250] Wed, 05 November 2008 10:29 Go to next message
Eclipse UserFriend
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 13:37 Go to previous messageGo to next message
Eclipse UserFriend
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] Thu, 06 November 2008 02:14 Go to previous messageGo to next message
Eclipse UserFriend
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 17:16 Go to previous messageGo to next message
Eclipse UserFriend
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] Fri, 07 November 2008 03:32 Go to previous message
Eclipse UserFriend
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
Previous Topic:Non lazy Ocl Validation through Dynamic Constaint Provider
Next Topic:OCL, profiles and namespaces
Goto Forum:
  


Current Time: Wed Nov 26 08:16:37 GMT 2014

Powered by FUDForum. Page generated in 0.01858 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software