Skip to main content



      Home
Home » Modeling » OCL » Type casting of query.evaluate() and ocl.evaluate() (type casting of query.evaluate() for Set(uml::Element) as resultType)
Type casting of query.evaluate() and ocl.evaluate() [message #1229066] Wed, 08 January 2014 11:34 Go to next message
Eclipse UserFriend
Hi,

I'm trying to use OCL Standalone to parse OCL query in UML State Machine Model by following examples in eclipse OCL help. According to the help we have to type case the query evaluation result to be a set

@SuppressWarnings("unchecked")
		    Set<BookCategory> categories = (Set<BookCategory>) queryEval.evaluate(next);


I follow same idea
@SuppressWarnings("unchecked")
			Set<Element> resultSet =  (Set<Element>) oclQueryEval.evaluate(umlModel);


but I got the following error

java.lang.ClassCastException: org.eclipse.ocl.examples.domain.values.impl.SetValueImpl$Accumulator cannot be cast to java.util.Set


I ran resultType() method
System.out.println(oclQueryEval.resultType());


and I got Set(uml::Element)

how to type cast of the evaluate method of OCL or Query class with this result type?

any a advice please

thanks

[Updated on: Wed, 08 January 2014 11:37] by Moderator

Re: Type casting of query.evalauet() and ocl.evalauet() [message #1229086 is a reply to message #1229066] Wed, 08 January 2014 12:30 Go to previous messageGo to next message
Eclipse UserFriend
Hi

OCL Collections are immutable, or at least only mutable by those who
really know what they are doing, so offering the Collection or Set
interfaces is not really appropriate.

In the new Pivot implementation SetValueImpl is immutable and
SetValueImpl$Accumulator is internally mutable for use as an iterate
accumulator.

I took a quick look at adding the Java interfaces; two methods
size()/isEmpty() have slightly different return types - probably fixable,
but many mutating methods are missing.

The CollectionValue classes also wrap the Java Collection classes to
ensure that OCL rather than Java equality/numeric semantics are observed
again making direct use as Java collections inappropriate.

[You may however gain access to the underlying List by using asObject().
But if you modify the List don't be surprised if nasty things happen.]

The correct conversion is provided by Value.asEcoreObject(idResolver) or
IdResolver.unboxedValueOf(Object).

This should be done for you.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=425116 raised.

For now try:

IdResolver idResolver =
oclQueryEval.getMetaModelManager().getIdResolver();
Object result = (Set<Element>)
oclQueryEval.evaluate(umlModel);
Set<Element> results;
if (result instanceof Value) {
results = (Set<Element>)idResolver.unboxedValueOf(result);
} else {
results = (Set<Element>)result;
}

Once the bug is fixed the then part of the if can be removed.

Regards

Ed Willink







On 08/01/2014 16:34, Naif Mokhayesh wrote:
> Hi,
>
> I'm trying to use OCL Standalone to parse OCL query in UML State
> Machine Model by following examples in eclipse OCL help. According to
> the help we have to type case the query evaluation result to be a set
>
> @SuppressWarnings("unchecked")
> Set<BookCategory> categories = (Set<BookCategory>)
> queryEval.evaluate(next);
>
>
> I follow same idea
> @SuppressWarnings("unchecked")
> Set<Element> resultSet = (Set<Element>)
> oclQueryEval.evaluate(umlModel);
>
>
> but I got the following error
>
> java.lang.ClassCastException:
> org.eclipse.ocl.examples.domain.values.impl.SetValueImpl$Accumulator
> cannot be cast to java.util.Set
>
>
> I ran resultType() method
> System.out.println(oclQueryEval.resultType());
>
>
> and I got Set(uml::Element)
> how to type cast of the evaluate method of OCL or Query class with
> this result type?
> any a advice please
> thanks
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1229110 is a reply to message #1229086] Wed, 08 January 2014 13:38 Go to previous messageGo to next message
Eclipse UserFriend
Thanks Mr.Ed for your reply

I added your suggested work around solution but still I'm getting same exception !

// IdResolver idResolver = oclQueryEval.getMetaModelManager().getIdResolver();// in Query class we do not have getMetaModelManager()
			IdResolver idResolver = ocl.getMetaModelManager().getIdResolver();
			
			//Object result = (Set<Element>) oclQueryEval.evaluate(umlModel);
			Object result = (Set<Element>) ocl.evaluate(umlModel, oclQuery);
		
			Set<Element> results;
			if (result instanceof Value) {
				results = (Set<Element>)idResolver.unboxedValueOf(result);
				} else {
					results = (Set<Element>)result;
					}
			
			System.out.println("Final OCL query on UML-SM Model :" + results);// to see the output !


I will keep trying and following the raised bug

thanks again.
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1229121 is a reply to message #1229110] Wed, 08 January 2014 14:11 Go to previous messageGo to next message
Eclipse UserFriend
Hi

Of course you need to remove the original bad cast....

Object result = ocl.evaluate(umlModel, oclQuery);

then you hit the problem that the Ecore representation of a Set is a
UniqueEList so you then need

results = new
HashSet<Element>((Collection<Element>)idResolver.unboxedValueOf(result));

Regards

Ed Willink

On 08/01/2014 18:38, Naif Mokhayesh wrote:
> Thanks Mr.Ed for your reply
> I added your suggested work around solution but still I'm getting same
> exception !
>
>
> // IdResolver idResolver =
> oclQueryEval.getMetaModelManager().getIdResolver();// in Query class
> we do not have getMetaModelManager()
> IdResolver idResolver =
> ocl.getMetaModelManager().getIdResolver();
>
> //Object result = (Set<Element>)
> oclQueryEval.evaluate(umlModel);
> Object result = (Set<Element>) ocl.evaluate(umlModel,
> oclQuery);
>
> Set<Element> results;
> if (result instanceof Value) {
> results =
> (Set<Element>)idResolver.unboxedValueOf(result);
> } else {
> results = (Set<Element>)result;
> }
>
> System.out.println("Final OCL query on UML-SM Model :" +
> results);// to see the output !
>
>
> I will keep trying and following the raised bug
>
> thanks again.
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1229162 is a reply to message #1229121] Wed, 08 January 2014 16:09 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ed,

YES, at least I got some results like the following

[org.eclipse.uml2.uml.internal.impl.StateImpl@2555e3ab (name: State2, visibility: public) (isLeaf: false), org.eclipse.uml2.uml.internal.impl.StateImpl@3850620f (name: State1, visibility: public) (isLeaf: false)]


The above result is comes in 2 field of HashSet<Element> object.

actually I need only the name proper of each state i.e. State1 and Stat2

but I'm still figuring out how to get just name property as new set ? any suggestion please

Thanks a lot
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1229166 is a reply to message #1229162] Wed, 08 January 2014 16:21 Go to previous messageGo to next message
Eclipse UserFriend
state.name


On 08/01/2014 21:09, Naif Mokhayesh wrote:
> Hi Ed,
>
> YES, at least I got some results like the following
>
> [org.eclipse.uml2.uml.internal.impl.StateImpl@2555e3ab (name: State2,
> visibility: public) (isLeaf: false),
> org.eclipse.uml2.uml.internal.impl.StateImpl@3850620f (name: State1,
> visibility: public) (isLeaf: false)]
>
>
> The above result is comes in 2 field of HashSet<Element> object.
>
> actually I need only the name proper of each state i.e. State1 and Stat2
>
> but I'm still figuring out how to get just name property as new set ?
> any suggestion please
> Thanks a lot
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1231041 is a reply to message #1229166] Mon, 13 January 2014 12:24 Go to previous messageGo to next message
Eclipse UserFriend
Hello,

A new error raised when I import it to qvto. actually the above code is part of black box plugin implementation of qvto. the error message is :

" Can't resolve 'interface java.util.Set' as OCL type in java method ...... (method name that return Set<Element> ) "

I modify the code as follow but still keep getting same error when I import it to qvto

any advice please


IdResolver idResolver = ocl.getMetaModelManager().getIdResolver();

			Object result = ocl.evaluate(umlModel, oclQuery);

			//Set<Element> results;
			Set<Element> results = CollectionUtil.createNewSet();

			if (result instanceof Value) {
				
				results = new HashSet<Element>((Collection<Element>) idResolver.unboxedValueOf(result));
					
			} else {
				// results = (Set<Element>)result;
				results = (HashSet<Element>) result;
			}
			
			//selectedElements = selectedElements

			//selectedElements.addAll(results); 
			
			selectedElements = CollectionUtil.createNewSet(results);


thanks a lot
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1231110 is a reply to message #1231041] Mon, 13 January 2014 16:19 Go to previous messageGo to next message
Eclipse UserFriend
Hui

QVTo still uses the Classic OCL (Ecore-binding). You certainly shouldn't
be using MetaModelManager.

Beyond that you really provide very little to go on.

Regards

Ed Willink


On 13/01/2014 17:24, Naif Mokhayesh wrote:
> Hello,
>
> A new error raised when I import it to qvto. actually the above code
> is part of black box plugin implementation of qvto. the error message
> is :
> " Can't resolve 'interface java.util.Set' as OCL type in java method
> ...... (method name that return Set<Element> ) "
>
> I modify the code as follow but still keep getting same error when I
> import it to qvto
>
> any advice please
>
>
>
> IdResolver idResolver = ocl.getMetaModelManager().getIdResolver();
>
> Object result = ocl.evaluate(umlModel, oclQuery);
>
> //Set<Element> results;
> Set<Element> results = CollectionUtil.createNewSet();
>
> if (result instanceof Value) {
>
> results = new HashSet<Element>((Collection<Element>)
> idResolver.unboxedValueOf(result));
>
> } else {
> // results = (Set<Element>)result;
> results = (HashSet<Element>) result;
> }
>
> //selectedElements = selectedElements
>
> //selectedElements.addAll(results);
> selectedElements = CollectionUtil.createNewSet(results);
>
>
> thanks a lot
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1231119 is a reply to message #1231110] Mon, 13 January 2014 16:45 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ed,

Now I'm confused. Please correct me if I'm wrong.

I'm using Pivot as standalone OCL parser. I created plugin that has Pivot as standalone OCL parser in order to import it in qvto as black box. Then from qvto I can call method to parse OCL string on a UML model. However, based on your recent reply, this will NOT work and I should use classical not Pivot since qvto still use classical OCL !

please advice


elaboration on my recent question

I have the following method in one of back box plugin classes
public static Set<Element> parse(String Path, String ocl) {... OCL pivot parser... }


it return set of Element which is the result of OCL query parsing on the passed model. but when i import the plugin in the qvto transformation I got the rolling Error " Can't resolve 'interface java.util.Set' as OCL type in java method ...... (method name that return Set<Element> ) "


thank you

[Updated on: Mon, 13 January 2014 17:28] by Moderator

Re: Type casting of query.evalauet() and ocl.evalauet() [message #1231128 is a reply to message #1231119] Mon, 13 January 2014 17:18 Go to previous messageGo to next message
Eclipse UserFriend
Hi

Since you give me so little clue as to what you are doing you can hardly
be surprised that my answers are of limited utility.
http://wiki.eclipse.org/OCL/ForumNetiquette

Regards

Ed Willink


On 13/01/2014 21:45, Naif Mokhayesh wrote:
> Hi Ed,
>
> I'm confused now. Please correct me if I'm wrong.
>
> I'm using Pivot as standalone OCL parser. I created plugin that has
> Pivot as standalone OCL parser in order to import it in qvto as black
> box. Then from qvto I can call method to parse OCL string on a UML
> model. However, based on your recent reply, this will NOT work and I
> should use classical not Pivot since qvto still use classical OCL !
>
> thank you
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1231140 is a reply to message #1231128] Mon, 13 January 2014 18:05 Go to previous messageGo to next message
Eclipse UserFriend
Sorry Mr. Ed for any inconvenience, but the whole project was uploaded in
https://bugs.eclipse.org/bugs/show_bug.cgi?id=425116

and I need to use that code in a plugin in order to import it in qvto

thanks
Re: Type casting of query.evalauet() and ocl.evalauet() [message #1231242 is a reply to message #1231140] Tue, 14 January 2014 01:25 Go to previous message
Eclipse UserFriend
Hi

I see no QVTo in that project.

Regards

Ed Willink


On 13/01/2014 23:05, Naif Mokhayesh wrote:
> Sorry Mr. Ed for any inconvenience, but the whole project was uploaded in
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=425116
>
> and I need to use that code in a plugin in order to import it in qvto
> thanks
Previous Topic:oclinecore editor - removes genmodel annotation entries
Next Topic:union two sets of different classifier
Goto Forum:
  


Current Time: Fri Nov 07 15:00:09 EST 2025

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

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

Back to the top