Inconsisten Evaluation of OCL Constraint [message #62783] |
Fri, 10 October 2008 14:09 |
sth2000 Messages: 10 Registered: July 2009 |
Junior Member |
|
|
Hi,
I stumbled across the following peculiar effect. In the following code
snippet two integer variables 'nl' and 'np' (both have value '1') are
compared with the expression 'nl <= np' which should be true obviously.
However, evaluating the expression with query.check() results in false while
ocl.check() returns true. Is this a bug?
Best Regards,
Stephan
Diagram diagram = (Diagram) inputModelResource.getContents().get(0);
OCL ocl = OCL.newInstance();
Helper helper = ocl.createOCLHelper();
helper.setContext(EcorePackage.eINSTANCE.getEObject());
org.eclipse.ocl.expressions.Variable<EClassifier, EParameter> v =
ocl.getEnvironment().getOCLFactory().createVariable();
v.setName("nl");
v.setType(ocl.getEnvironment().getOCLStandardLibrary().getIn teger());
ocl.getEnvironment().addElement(v.getName(), v, true);
ocl.getEvaluationEnvironment().add(v.getName(), 1);
v = ocl.getEnvironment().getOCLFactory().createVariable();
v.setName("np");
v.setType(ocl.getEnvironment().getOCLStandardLibrary().getIn teger());
ocl.getEnvironment().addElement(v.getName(), v, true);
ocl.getEvaluationEnvironment().add(v.getName(), 1);
try
{
Constraint oclExpression = helper.createInvariant("nl <= np");
Query query = ocl.createQuery(oclExpression);
Boolean b = query.check(diagram); //Evaluates to false
System.out.println("Query successful.\nResult: " + b.toString());
b = ocl.check(diagram, oclExpression); //Evaluates to true
System.out.println("Query successful.\nResult: " + b.toString());
}
catch (ParserException e)
{
System.out.println("Query failed.");
// TODO Auto-generated catch block
e.printStackTrace();
}
|
|
|
Re: Inconsisten Evaluation of OCL Constraint [message #62802 is a reply to message #62783] |
Fri, 10 October 2008 16:58 |
Eclipse User |
|
|
|
Originally posted by: cdamus.zeligsoft.com
Hi, Stephan,
The query.check() invocation is failing with an exception, which results
in an OclInvalid token, which is not equal to the Boolean "true" value,
so the Java method returns "false."
The problem is that the Query object creates its own evaluation
environment, in which you have not provided variable bindings. That's
one of the benefits of the queries: you can create several, assign them
different variable bindings, and evaluate them repeatedly.
HTH,
Christian
Stephan wrote:
> Hi,
>
> I stumbled across the following peculiar effect. In the following code
> snippet two integer variables 'nl' and 'np' (both have value '1') are
> compared with the expression 'nl <= np' which should be true obviously.
> However, evaluating the expression with query.check() results in false while
> ocl.check() returns true. Is this a bug?
>
> Best Regards,
>
> Stephan
>
>
>
> Diagram diagram = (Diagram) inputModelResource.getContents().get(0);
>
> OCL ocl = OCL.newInstance();
>
>
> Helper helper = ocl.createOCLHelper();
>
>
> helper.setContext(EcorePackage.eINSTANCE.getEObject());
>
>
> org.eclipse.ocl.expressions.Variable<EClassifier, EParameter> v =
> ocl.getEnvironment().getOCLFactory().createVariable();
>
> v.setName("nl");
>
> v.setType(ocl.getEnvironment().getOCLStandardLibrary().getIn teger());
>
>
> ocl.getEnvironment().addElement(v.getName(), v, true);
>
>
> ocl.getEvaluationEnvironment().add(v.getName(), 1);
>
>
> v = ocl.getEnvironment().getOCLFactory().createVariable();
>
> v.setName("np");
>
> v.setType(ocl.getEnvironment().getOCLStandardLibrary().getIn teger());
>
>
> ocl.getEnvironment().addElement(v.getName(), v, true);
>
>
> ocl.getEvaluationEnvironment().add(v.getName(), 1);
>
> try
>
> {
>
> Constraint oclExpression = helper.createInvariant("nl <= np");
>
>
> Query query = ocl.createQuery(oclExpression);
>
> Boolean b = query.check(diagram); //Evaluates to false
>
>
> System.out.println("Query successful.\nResult: " + b.toString());
>
>
> b = ocl.check(diagram, oclExpression); //Evaluates to true
>
>
> System.out.println("Query successful.\nResult: " + b.toString());
>
> }
>
> catch (ParserException e)
>
> {
>
> System.out.println("Query failed.");
>
> // TODO Auto-generated catch block
>
> e.printStackTrace();
>
> }
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.02637 seconds