Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » problem with parsing ocl expression in a standalone environment
problem with parsing ocl expression in a standalone environment [message #21827] Fri, 04 May 2007 09:29 Go to next message
Eclipse UserFriend
Originally posted by: lepton.gmx.net

Hello ,

I've a problem with parsing OCL constraints for a UML2 model in a
standalone environment. The creation of the model works well and a
subsequent serilization to XMI work well too.

The created UML2 model consists of a root package ( called "Root" ) ,
another package ( called "eclipse" ). The "eclipse"-package consists of
one class (called "test" with one attribute). I created this model just
for testing purposes.

I wanted to parse the following simple constraint :
"package eclipse context test inv : true endpackage "

The code is:
String exp = "package eclipse context test inv : true endpackage ";
model = UMLFactory.eINSTANCE.createModel();
createModel(model);
rset = new org.eclipse.emf.ecore.resource.impl.ResourceSetImpl();
URI uri = URI.createURI("test.uml");
res = rset.createResource(uri);
res.getContents().add(model);
res.save(null); // works well
org.eclipse.ocl.uml.OCL ocl =
org.eclipse.ocl.uml.OCL.newInstance(res.getResourceSet());
org.eclipse.ocl.uml.OCL.Helper helper = ocl.createOCLHelper();
helper.setContext(org.eclipse.ocl.uml.util.OCLUMLUtil.getMet aclass(model));
org.eclipse.ocl.uml.OCLExpression expr = helper.createQuery(exp);

Everything is fine until the createQuery method is called, because the
following exception is always thrown :

org.eclipse.ocl.ParserException: Invalid OCL
at org.eclipse.ocl.internal.parser.OCLParser.runParser(OCLParse r.java:740)
at
org.eclipse.ocl.internal.parser.OCLParser.parseInvOrDefCS(OC LParser.java:779)
at
org.eclipse.ocl.internal.helper.HelperUtil.parseQuery(Helper Util.java:159)
at
org.eclipse.ocl.internal.helper.OCLHelperImpl.createQuery(OC LHelperImpl.java:166)
at org.eclipse.ocl.uml.OCLHelperImpl.createQuery(OCLHelperImpl. java:85)

I don't know what to do anymore , so any help would be appreciated ! THX
!!!

best regards,
lepton
Re: problem with parsing ocl expression in a standalone environment [message #21872 is a reply to message #21827] Fri, 04 May 2007 12:16 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, lepton,

I think perhaps that "Invalid OCL" message could use some improvement ;-)

There are a couple of problems in your code snippet:

1. The Model metaclass is not appropriate as the context classifier for an
invariant constraint on the test class. You would rather do:

helper.setContext(test);

where test is the "test" Class in your model.

2. The OCLHelper API is intended for parsing constraints embedded in models.
In that scenario, a constraint does not use the concrete syntax for context
declarations, but is just an unadorned OCL expression (the context is
implied by its placement in the model). So, you would do this to parse the
invariant in the "test" context:

helper.setContext(test);
Constraint invariant = helper.createConstraint("true");

because "true" is the constraint in your example; the rest is just context
declaration.

Using the OCL document parsing API, you could alternatively do:

Constraint invariant = ocl.parse(new OCLInput(
"package eclipse context test inv: true endpackage")).get(0);

Yesterday's OCL build includes a first draft of an improved Programmer's
Guide in the on-line help, which explains more fully the usage of the OCL
and OCLHelper APIs.

HTH,

Christian


lepton wrote:

> Hello ,
>
> I've a problem with parsing OCL constraints for a UML2 model in a
> standalone environment. The creation of the model works well and a
> subsequent serilization to XMI work well too.
>
> The created UML2 model consists of a root package ( called "Root" ) ,
> another package ( called "eclipse" ). The "eclipse"-package consists of
> one class (called "test" with one attribute). I created this model just
> for testing purposes.
>
> I wanted to parse the following simple constraint :
> "package eclipse context test inv : true endpackage "
>
> The code is:
> String exp = "package eclipse context test inv : true endpackage ";
> model = UMLFactory.eINSTANCE.createModel();
> createModel(model);
> rset = new org.eclipse.emf.ecore.resource.impl.ResourceSetImpl();
> URI uri = URI.createURI("test.uml");
> res = rset.createResource(uri);
> res.getContents().add(model);
> res.save(null); // works well
> org.eclipse.ocl.uml.OCL ocl =
> org.eclipse.ocl.uml.OCL.newInstance(res.getResourceSet());
> org.eclipse.ocl.uml.OCL.Helper helper = ocl.createOCLHelper();
>
helper.setContext(org.eclipse.ocl.uml.util.OCLUMLUtil.getMet aclass(model));
> org.eclipse.ocl.uml.OCLExpression expr = helper.createQuery(exp);
>
> Everything is fine until the createQuery method is called, because the
> following exception is always thrown :
>
> org.eclipse.ocl.ParserException: Invalid OCL
> at org.eclipse.ocl.internal.parser.OCLParser.runParser(OCLParse r.java:740)
> at
>
org.eclipse.ocl.internal.parser.OCLParser.parseInvOrDefCS(OC LParser.java:779)
> at
> org.eclipse.ocl.internal.helper.HelperUtil.parseQuery(Helper Util.java:159)
> at
>
org.eclipse.ocl.internal.helper.OCLHelperImpl.createQuery(OC LHelperImpl.java:166)
> at org.eclipse.ocl.uml.OCLHelperImpl.createQuery(OCLHelperImpl. java:85)
>
> I don't know what to do anymore , so any help would be appreciated ! THX
> !!!
>
> best regards,
> lepton
Previous Topic:[Announce] MDT OCL 1.1.0 I200705031420 is available
Next Topic:error in creating expression using EMF enumeration
Goto Forum:
  


Current Time: Fri Mar 29 01:29:19 GMT 2024

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

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

Back to the top