Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Creating variable in OCL
Creating variable in OCL [message #809075] Tue, 28 February 2012 13:03 Go to next message
Piotr J. Puczynski is currently offline Piotr J. PuczynskiFriend
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 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4186
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 #809140 is a reply to message #809128] Tue, 28 February 2012 14:22 Go to previous messageGo to next message
Piotr J. Puczynski is currently offline Piotr J. PuczynskiFriend
Messages: 17
Registered: January 2012
Junior Member
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
Re: Creating variable in OCL [message #809150 is a reply to message #809140] Tue, 28 February 2012 14:32 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4186
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
Re: Creating variable in OCL [message #809197 is a reply to message #809150] Tue, 28 February 2012 15:37 Go to previous messageGo to next message
Piotr J. Puczynski is currently offline Piotr J. PuczynskiFriend
Messages: 17
Registered: January 2012
Junior Member
Hi Ed,
I can understand your confusion but in fact I cannot agree with you.
From my code it is clearly see that the context of OCL expression is set to existing InstanceSpecification.
My problem is not with context but with adding more local variables (that are created in my simulator and cannot be stored in InstanceSpecification because they do not have corresponding Class feature). The functionality to add new variable in OCL is provided by EvaluationEnvironment and it is described in this tutorial (so I hope I am not doing some nonstandard operation): http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.ocl.doc%2Fhelp%2FEvaluatingConstraints.html. I hope it is clear now.

I attach the plugin project with one test "src/OCLTest.java" that should give you an error during parsing.

Cheers,
Piotr
  • Attachment: TestOCL.zip
    (Size: 275.45KB, Downloaded 118 times)
Re: Creating variable in OCL [message #809239 is a reply to message #809197] Tue, 28 February 2012 16:21 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4186
Registered: July 2009
Senior Member
Hi

Thanks for the excellent repro. [Unfortunately it doesn't work on Juno.
Hopefully https://bugs.eclipse.org/bugs/show_bug.cgi?id=372752 will be
fixed to avoid a widespread migration issue.]

The problem is that you don't have a model context, OCL does not support
global variables and that is what your "c" is. The parser will only find
"c" with in the scope of some implicit source.

I found this API unhelpful when trying to use it for QVT, which at least
had a model context. The problem is that the API introduces unmodeled
concepts that then require non-standard lookup mechanisms. The revised
API for the new Pivot binding is much simpler since everything is
modeled, but of course still requires a model context.

If you abuse the modeling technology, I'm afraid you will keep
encountering this kind of problem.

Regards

Ed Willink.

On 28/02/2012 15:37, Piotr J. Puczynski wrote:
> Hi Ed,
> I can understand your confusion but in fact I cannot agree with you.
> From my code it is clearly see that the context of OCL expression is set to existing InstanceSpecification.
> My problem is not with context but with adding more local variables (that are created in my simulator and cannot be stored in InstanceSpecification because they do not have corresponding Class feature). The functionality to add new variable in OCL is provided by EvaluationEnvironment and it is described in this tutorial (so I hope I am not doing some nonstandard operation): http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.ocl.doc%2Fhelp%2FEvaluatingConstraints.html. I hope it is clear now.
>
> I attach the plugin project with one test "src/OCLTest.java" that should give you an error during parsing.
>
> Cheers,
> Piotr
Previous Topic:Error detail
Next Topic:OCL for Extended ECORE model
Goto Forum:
  


Current Time: Fri Dec 19 01:42:12 GMT 2014

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

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