Creating variable in OCL [message #809075] |
Tue, 28 February 2012 13:03 |
Piotr J. Puczynski Messages: 17 Registered: January 2012 |
Junior Member |
|
|
I am trying to set additional variable for OCL in UML evaluation. As you can see in the code I add variable "c" with value 3 to evalEnv and later evaluate "c = 3".
What I am doing wrong that I am getting ParserException: Unrecognized variable: (c)?
UMLEnvironmentFactory envFactory = new UMLEnvironmentFactory(constraint.getModel().eResource().getResourceSet());
UMLEnvironment env = envFactory.createEnvironment();
org.eclipse.ocl.uml.OCL myOCL = org.eclipse.ocl.uml.OCL.newInstance(env);
EvaluationEnvironment<?, ?, ?, ?, ?> evalEnv = myOCL.getEvaluationEnvironment();
EvaluationOptions.setOption(evalEnv, UMLEvaluationOptions.EVALUATION_MODE, EvaluationMode.RUNTIME_OBJECTS);
evalEnv.add("c", 3);
OCLExpression<?> oclConstraint = null;
OCLHelper<?, ?, ?, ?> helper = myOCL.createOCLHelper();
helper.setInstanceContext(executor.getInstanceSpecification());
String body = "c = 3";
try {
oclConstraint = helper.createQuery(body);
} catch (ParserException e) {
//exception
}
@SuppressWarnings("unchecked")
Object result = myOCL.evaluate(executor.getInstanceSpecification(), (OCLExpression<Classifier>) oclConstraint);
if (result instanceof Boolean) {
return (Boolean) result;
}
Regards,
Piotr
|
|
|
Re: Creating variable in OCL [message #809128 is a reply to message #809075] |
Tue, 28 February 2012 14:07 |
Ed Willink Messages: 7670 Registered: July 2009 |
Senior Member |
|
|
Hi
The code seems reasonable. If you want me to debug it, please provide
something that is runnable (a zipped project); I do not have time to
redevelop the surrounding context.
Regards
Ed Willink
On 28/02/2012 13:03, Piotr J. Puczynski wrote:
> I am trying to set additional variable for OCL in UML evaluation. As
> you can see in the code I add variable "c" with value 3 to evalEnv and
> later evaluate "c = 3".
> What I am doing wrong that I am getting ParserException: Unrecognized
> variable: (c)?
>
> UMLEnvironmentFactory envFactory = new
> UMLEnvironmentFactory(constraint.getModel().eResource().getResourceSet());
> UMLEnvironment env = envFactory.createEnvironment();
> org.eclipse.ocl.uml.OCL myOCL =
> org.eclipse.ocl.uml.OCL.newInstance(env);
> EvaluationEnvironment<?, ?, ?, ?, ?> evalEnv =
> myOCL.getEvaluationEnvironment();
> EvaluationOptions.setOption(evalEnv,
> UMLEvaluationOptions.EVALUATION_MODE, EvaluationMode.RUNTIME_OBJECTS);
> evalEnv.add("c", 3);
> OCLExpression<?> oclConstraint = null;
> OCLHelper<?, ?, ?, ?> helper = myOCL.createOCLHelper();
> helper.setInstanceContext(executor.getInstanceSpecification());
>
> String body = "c = 3";
> try {
> oclConstraint = helper.createQuery(body);
> } catch (ParserException e) {
> //exception
> }
> @SuppressWarnings("unchecked")
> Object result =
> myOCL.evaluate(executor.getInstanceSpecification(),
> (OCLExpression<Classifier>) oclConstraint);
> if (result instanceof Boolean) {
> return (Boolean) result;
> }
>
> Regards,
> Piotr
|
|
|
|
Re: Creating variable in OCL [message #809150 is a reply to message #809140] |
Tue, 28 February 2012 14:32 |
Ed Willink Messages: 7670 Registered: July 2009 |
Senior Member |
|
|
Hi
To be honest, I've no idea. What you are doing seems quite mad. OCL
works in a defined model context. You seem to be creating a context on
the fly.
I recommend that you take a step back and decide what you are really
trying to do, and why you cannot do that using, rather than fighting, a
modeling technology. Generally, once you start fighting, you just
encounter more and more problems as you use tools in ways that were not
intended, or at least in ways that have never been exercised.
Regards
Ed Willink
On 28/02/2012 14:22, Piotr J. Puczynski wrote:
> Hi Ed, thanks for answering.
> I debugged the OCL parser code and I put breakpoints on
> EvaluationEnvironment.getValueOf(varName) function.
> It is clear that the parser do not check for this value during call
> myOCL.createOCLHelper() from my code. It looks like only during
> evaluation (that will never occurred since the exception is thrown
> before it) this values are checked and that the parser only sees model
> elements; not additional variables defined in EvaluationEnvironment.
> Is there any other way to define variables so that parser will see
> them? Or to ignore parser errors in this case. I tried
> OCLHelper.setValidating(false) - didn't work.
>
> If the preceding questions do not solve the problem, then I will
> create the project for you.
>
> Thanks for help.
>
> Regards,
> Piotr
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07186 seconds