Home » Modeling » OCL » Acceleo invoking a Java Service Wrapping OCL
Acceleo invoking a Java Service Wrapping OCL [message #900691] |
Wed, 08 August 2012 07:58 |
|
I need to get the OCL model of conditions contained in UML edges from an ACCELEO script navigating the main UML model. To this end I have defined the following Java class:
public class GetOCLModel {
public Constraint getOCLModel(Classifier context, String expression){
OCL<Package, Classifier, Operation, Property, EnumerationLiteral, Parameter,
State, CallOperationAction, SendSignalAction, Constraint, Class, EObject> ocl;
//CL.newInstance(EcoreEnvironmentFactory.INSTANCE);
UMLEnvironmentFactory uef = new UMLEnvironmentFactory();
ocl = OCL.newInstance(uef.createEnvironment());
OCLHelper<Classifier, Operation, Property, Constraint> helper = ocl.createOCLHelper();
helper.setContext(context);
Constraint expr= null;
try {
expr= (Constraint) helper.createInvariant(expression);
System.out.println("Hunky Dory!");
} catch (ParserException e) {
e.printStackTrace();
}
return expr;
}
}
This is the ACCELEO Module wrapping it:
[module generateOclModel('[url]http://www.eclipse.org/ocl/1.1.0/UML[/url]','[url]http://www.eclipse.org/uml2/2.1.0/UML[/url]')/]
[query public getOclModel(cl:Classifier, str:String): Constraint = invoke('sfg.baleno.src.services.GetOCLModel',
'getOCLModel(org.eclipse.uml2.uml.Classifier, java.lang.String)',Sequence{cl,str}) /]
And here is how I am trying to invoke it from the main ACCELEO module:
[c.getOclModel('self.name=\'Testclass\'')._context.name/]
the helper outputs this exception
org.eclipse.ocl.SemanticException: Unrecognized variable: (name)
what am I doing wrong? is it about the context?
[Updated on: Wed, 08 August 2012 07:59] Report message to a moderator
|
|
|
Re: Acceleo invoking a Java Service Wrapping OCL [message #900727 is a reply to message #900691] |
Wed, 08 August 2012 10:12 |
Ed Willink Messages: 7669 Registered: July 2009 |
Senior Member |
|
|
Hi
The Eclipse OCL project provides 3 meta-model bindings.
You are using the UML binding.
Acceleo currently uses the Ecore binding, so you are passing an EClass
as a Class.
Regards
Ed Willink
On 08/08/2012 08:58, Andrea Sindico wrote:
> I need to get the OCL model of conditions contained in UML edges from
> an ACCELEO script navigating the main UML model. To this end I have
> defined the following Java class:
>
> public class GetOCLModel {
> public Constraint getOCLModel(Classifier context, String expression){
>
> OCL<Package, Classifier, Operation, Property,
> EnumerationLiteral, Parameter, State, CallOperationAction,
> SendSignalAction, Constraint, Class, EObject> ocl;
> //CL.newInstance(EcoreEnvironmentFactory.INSTANCE);
> UMLEnvironmentFactory uef = new UMLEnvironmentFactory();
> ocl = OCL.newInstance(uef.createEnvironment());
> OCLHelper<Classifier, Operation, Property, Constraint> helper
> = ocl.createOCLHelper();
> helper.setContext(context);
> Constraint expr= null;
> try {
> expr= (Constraint) helper.createInvariant(expression);
> System.out.println("Hunky Dory!");
>
> } catch (ParserException e) {
> e.printStackTrace();
> }
> return expr;
> }
> }
>
> This is the ACCELEO Module wrapping it:
>
> [module
> generateOclModel('http://www.eclipse.org/ocl/1.1.0/UML','http://www.eclipse.org/uml2/2.1.0/UML')/]
> [query public getOclModel(cl:Classifier, str:String): Constraint
> = invoke('sfg.baleno.src.services.GetOCLModel',
> 'getOCLModel(org.eclipse.uml2.uml.Classifier,
> java.lang.String)',Sequence{cl,str}) /]
>
>
> And here is how I am trying to invoke it from the main ACCELEO module:
> [c.getOclModel('self.name=\'Testclass\'')._context.name/]
>
> the helper outputs this exception
>
> org.eclipse.ocl.SemanticException: Unrecognized variable: (name)
>
>
> what am I doing wrong? is it about the context?
|
|
| | |
Re: Acceleo invoking a Java Service Wrapping OCL [message #900778 is a reply to message #900768] |
Wed, 08 August 2012 13:02 |
|
Thank you Ed
I have changed the Java class this way:
package sfg.baleno.src.services;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.ocl.ecore.*;
import org.eclipse.ocl.ecore.Constraint;
import org.eclipse.ocl.ecore.OCLExpression;
import org.eclipse.ocl.*;
import org.eclipse.ocl.OCL;
import org.eclipse.ocl.uml.*;
import org.eclipse.ocl.helper.ConstraintKind;
import org.eclipse.ocl.helper.OCLHelper;
import org.eclipse.uml2.uml.*;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Package;
import org.eclipse.emf.ecore.*;
public class GetOCLModel {
public Constraint getOCLModel(EClassifier context, String expression){
OCL<?,EClassifier, ?,?, ?, ?, ?, ?, ?, ?, ?, ?> ocl;
ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
OCLHelper<EClassifier, ?, ?, ?> helper = ocl.createOCLHelper();
helper.setContext(context);
String res = null;
Constraint expr= null;
try {
expr= (Constraint) helper.createInvariant(expression);
} catch (ParserException e) {
e.printStackTrace();
}
return expr;
}
}
but I have not changed the ACCELEO wrapper. It no longer throws the exception but I can't see how to use the returned constraint (if any) in the ACCELEO script, is it a UML or ECORE Constraint? what is its structure? I'd like to explore it as a model in ACCELEO and produce text from it
Follow me on Twitter @andreasindico
|
|
|
Re: Acceleo invoking a Java Service Wrapping OCL [message #900803 is a reply to message #900778] |
Wed, 08 August 2012 14:13 |
Ed Willink Messages: 7669 Registered: July 2009 |
Senior Member |
|
|
Hi
The Java declaration tells you that it is an
org.eclipse.ocl.ecore.Constraint and if you trace the EClass and
EPackage you will find that it is http://www.eclipse.org/ocl/1.1.0/Ecore
in /org.eclipse.ocl.ecore/model/OCLEcore.ecore.
You didn't say why you wanted an OCL model so that it's difficult to say
what to do with it.
The model is a proprietary Ecore-flavouring of the OCL specification.
If you want something that prototypes a resolution of the XMI issues in
the OCL specification you might want to use the Pivot OCL binding. You
can find examples of Acceleo templates using the Piviot OCL model in GIT
org.eclipse.ocl examples/org.eclipse.ocl.examples.build.
But generally it is best to avoid using the OCL model directly.
Regards
Ed Willink
On 08/08/2012 14:02, Andrea Sindico wrote:
> Thank you Ed
> I have changed the Java class this way:
>
> package sfg.baleno.src.services;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.ocl.ecore.*;
> import org.eclipse.ocl.ecore.Constraint;
> import org.eclipse.ocl.ecore.OCLExpression;
> import org.eclipse.ocl.*;
> import org.eclipse.ocl.OCL;
> import org.eclipse.ocl.uml.*;
> import org.eclipse.ocl.helper.ConstraintKind;
> import org.eclipse.ocl.helper.OCLHelper;
> import org.eclipse.uml2.uml.*;
> import org.eclipse.uml2.uml.Class;
> import org.eclipse.uml2.uml.Package;
> import org.eclipse.emf.ecore.*;
>
> public class GetOCLModel {
> public Constraint getOCLModel(EClassifier context, String
> expression){
>
> OCL<?,EClassifier, ?,?, ?, ?, ?, ?, ?, ?, ?, ?> ocl;
> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
> OCLHelper<EClassifier, ?, ?, ?> helper = ocl.createOCLHelper();
> helper.setContext(context);
> String res = null;
> Constraint expr= null;
> try {
> expr= (Constraint) helper.createInvariant(expression);
> } catch (ParserException e) {
> e.printStackTrace();
> }
> return expr;
>
> }
> }
>
>
> but I have not changed the ACCELEO wrapper. It no longer throws the
> exception but I can't see how to use the returned constraint (if any)
> in the ACCELEO script, is it a UML or ECORE Constraint? what is its
> structure? I'd like to explore it as a model in ACCELEO and produce
> text from it
|
|
|
Goto Forum:
Current Time: Thu Sep 12 15:40:39 GMT 2024
Powered by FUDForum. Page generated in 0.04595 seconds
|