execute ocl query that is not boolean expression [message #66787] |
Sat, 07 February 2009 16:41  |
Eclipse User |
|
|
|
Hi group!
I need to execute an ocl query from java code, so I create this method
execute pasted below but this method just execute query if the constraint
be boolean.
If I run this query "self.name = 'className'" the method return correctly.
But i want to execute a query like that:
"self.allOwnedElements() -> select(e | e.oclIsKindOf(Package))
->collect(e | e.oclAsType(Package))->select(e | e.name = 'domain')"
when i run this query I obtain this message:
java.lang.IllegalArgumentException: Query must be a boolean-valued
constraint.
someone can help me?
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.query.conditions.Condition;
import org.eclipse.emf.query.conditions.DataTypeCondition;
import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
import org.eclipse.emf.query.ocl.conditions.BooleanOCLCondition;
import org.eclipse.emf.query.statements.FROM;
import org.eclipse.emf.query.statements.IQueryResult;
import org.eclipse.emf.query.statements.SELECT;
import org.eclipse.emf.query.statements.WHERE;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.ecore.OCL;
import org.eclipse.uml2.uml.resource.UMLResource;
public class QueryExecutor {
/**
* The method thats execute ocl queries
* @param query the ocl query that must be executed
*/
public static IQueryResult execute( UMLResource uml2Resource, String query
){
OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();
Condition condition;
DataTypeCondition<org.eclipse.uml2.uml.Class> dtCondition;
IQueryResult results = null;
try {
condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(
ocl.getEnvironment(),query,null);
SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
new FROM(uml2Resource.getContents()), new WHERE((EObjectCondition)
condition) );
results = statement.execute();
} catch (ParserException e) {
e.printStackTrace();
}
return results;
}
}
BEST REGARDS!
|
|
|
Re: execute ocl query that is not boolean expression [message #67358 is a reply to message #66787] |
Mon, 09 February 2009 11:12  |
Eclipse User |
|
|
|
Originally posted by: cdamus.zeligsoft.com
Hi, Victor,
The Query framework's Conditions all check boolean predicates, because
they use them to determine whether an object is in the result set or
not. The OCL condition objects are no different.
If you want to execute a non-boolean OCL query expression, you can just
use the evaluate(...) API of the OCL object, or use
OCL::createQuery(...) to create a Query object that can be evaluated
repeatedly on different model elements.
HTH,
Christian
Victor Hugo wrote:
> Hi group!
>
> I need to execute an ocl query from java code, so I create this method
> execute pasted below but this method just execute query if the
> constraint be boolean.
> If I run this query "self.name = 'className'" the method return correctly.
> But i want to execute a query like that:
> "self.allOwnedElements() -> select(e | e.oclIsKindOf(Package))
> ->collect(e | e.oclAsType(Package))->select(e | e.name = 'domain')"
>
> when i run this query I obtain this message:
> java.lang.IllegalArgumentException: Query must be a boolean-valued
> constraint.
>
> someone can help me?
>
>
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EClassifier;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.query.conditions.Condition;
> import org.eclipse.emf.query.conditions.DataTypeCondition;
> import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
> import org.eclipse.emf.query.ocl.conditions.BooleanOCLCondition;
> import org.eclipse.emf.query.statements.FROM;
> import org.eclipse.emf.query.statements.IQueryResult;
> import org.eclipse.emf.query.statements.SELECT;
> import org.eclipse.emf.query.statements.WHERE;
> import org.eclipse.ocl.ParserException;
> import org.eclipse.ocl.ecore.OCL;
> import org.eclipse.uml2.uml.resource.UMLResource;
>
> public class QueryExecutor {
>
> /**
> * The method thats execute ocl queries
> * @param query the ocl query that must be executed
> */
> public static IQueryResult execute( UMLResource uml2Resource, String
> query ){
>
> OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();
> Condition condition;
> DataTypeCondition<org.eclipse.uml2.uml.Class> dtCondition;
> IQueryResult results = null;
> try {
>
> condition = new BooleanOCLCondition<EClassifier, EClass,
> EObject>(
> ocl.getEnvironment(),query,null);
>
> SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
> new FROM(uml2Resource.getContents()), new
> WHERE((EObjectCondition) condition) );
>
> results = statement.execute();
>
> } catch (ParserException e) {
> e.printStackTrace();
> }
>
> return results;
> }
>
> }
>
>
>
> BEST REGARDS!
>
|
|
|
Powered by
FUDForum. Page generated in 0.09763 seconds