Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » OCL Interpreter in Java (Helios)
OCL Interpreter in Java (Helios) [message #973434] Tue, 06 November 2012 10:26 Go to next message
Ger Garrigan is currently offline Ger Garrigan
Messages: 2
Registered: October 2012
Junior Member
Hello,

I'm trying to run OCL in Java and have been following the OCL Interpreter tutorial provided with Helios. When I try to evaluate my expressions it seems that the context is not being picked up and if I try to navigate through my model eg self.lowerLevel, it does not seem to work either. The code is below and the result of the print is:

Evaluation: org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl@1c10945d uri='file:/Users/Ger/sep12-group6/east-adl-validator/src/process/Timing.xmi'

I had expected it to print the model element associated with the context I set.

Am I missing some steps from my code?

Thanks very much,

Ger

public void doOcl(XMIResource model) {

		boolean valid;
		OCLExpression<EClassifier> query = null;

		try {
			// create an OCL instance for Ecore
			OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl;
			
			ResourceSet resourceSet = new ResourceSetImpl();
	resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
			
			resourceSet.getPackageRegistry().put(ElementsPackage.eNS_URI, ElementsPackage.eINSTANCE);
			
			Resource res = resourceSet.createResource(
				    URI.createFileURI(new File("/Users/Ger/sep12-group6/east-adl-validator/src/process/Timing.xmi").toString()));
			
			try {
				res.load(null);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
			
			ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE, res);
			
			// create an OCL helper object
			OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl
					.createOCLHelper();
			
			// set the OCL context classifier
			helper.setContext(TimingPackage.Literals.TIMING_CONSTRAINT);
			
			query = helper.createQuery("self");
		
			System.out.println("Evaluation: " + ocl.evaluate(res, query));
	
			// record success
			valid = true;
			
			
		} catch (Exception e) {
			// record failure to parse
			valid = false;
			System.err.println("OCL Error: " + e.getLocalizedMessage());
		}
		
	}

Re: OCL Interpreter in Java (Helios) [message #973709 is a reply to message #973434] Tue, 06 November 2012 15:01 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4115
Registered: July 2009
Senior Member
Hi

You have passed an XMIResource as the context to OCL.evaluate, and in
your example this is what has been returned as self. This usage is
ill-formed compared to the TimingPackage.Literals.TIMING_CONSTRAINT
defined as the type of self. A stronger well-formedness check could have
rejected the bad self, but the behaviour is not wrong.

Regards

Ed Willink


On 06/11/2012 13:43, Ger Garrigan wrote:
> Hello,
>
> I'm trying to run OCL in Java and have been following the OCL
> Interpreter tutorial provided with Helios. When I try to evaluate my
> expressions it seems that the context is not being picked up and if I
> try to navigate through my model eg self.lowerLevel, it does not seem
> to work either. The code is below and the result of the print is:
>
> Evaluation:
> mailto:org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl@1c10945d
> uri='file:/Users/Ger/sep12-group6/east-adl-validator/src/process/Timing.xmi'
>
> I had expected it to print the model element associated with the
> context I set.
> Am I missing some steps from my code?
>
> Thanks very much,
>
> Ger
>
>
> public void doOcl(XMIResource model) {
>
> boolean valid;
> OCLExpression<EClassifier> query = null;
>
> try {
> // create an OCL instance for Ecore
> OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint,
> EClass, EObject> ocl;
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMIResourceFactoryImpl());
>
> resourceSet.getPackageRegistry().put(ElementsPackage.eNS_URI,
> ElementsPackage.eINSTANCE);
>
> Resource res = resourceSet.createResource(
> URI.createFileURI(new
> File("/Users/Ger/sep12-group6/east-adl-validator/src/process/Timing.xmi").toString()));
>
> try {
> res.load(null);
>
> } catch (IOException e) {
> e.printStackTrace();
> }
>
>
>
> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE, res);
>
> // create an OCL helper object
> OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl
> .createOCLHelper();
>
> // set the OCL context classifier
> helper.setContext(TimingPackage.Literals.TIMING_CONSTRAINT);
>
> query = helper.createQuery("self");
>
> System.out.println("Evaluation: " + ocl.evaluate(res,
> query));
>
> // record success
> valid = true;
>
>
> } catch (Exception e) {
> // record failure to parse
> valid = false;
> System.err.println("OCL Error: " + e.getLocalizedMessage());
> }
>
> }
>
>
Re: OCL Interpreter in Java (Helios) [message #973741 is a reply to message #973709] Tue, 06 November 2012 15:31 Go to previous messageGo to next message
Ger Garrigan is currently offline Ger Garrigan
Messages: 2
Registered: October 2012
Junior Member
Thanks very much for the quick reply Ed!

So the type of resource that I am creating is incorrect. Is it possible to load the correct model from the xmi file I have and use that as a resource?

Thanks again,

Ger
Re: OCL Interpreter in Java (Helios) [message #973842 is a reply to message #973741] Tue, 06 November 2012 17:07 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4115
Registered: July 2009
Senior Member
Hi

There may be nothing wrong with your resource.

Structurally your resource comprises an XMIResource containing a
hierarchy of EObjects, each of which has an EClass defining its type.

You must pass the EClass to helper.setContext() so that the OCL parser
knows what the type of self is.

You must pass an EObject that conforms to the EClass in order to
evaluate upon that object.

You were passing an XMIResopurce whixch is not an EObject.

Regards

Ed Willink


On 06/11/2012 15:31, Ger Garrigan wrote:
> Thanks very much for the quick reply Ed!
> So the type of resource that I am creating is incorrect. Is it
> possible to load the correct model from the xmi file I have and use
> that as a resource?
>
> Thanks again,
>
> Ger
Re: OCL Interpreter in Java (Helios) [message #977693 is a reply to message #973842] Fri, 09 November 2012 13:50 Go to previous messageGo to next message
Paschalis Tsolakidis is currently offline Paschalis Tsolakidis
Messages: 5
Registered: October 2012
Junior Member
Hi,

Ed I am working on this with Ger and I tried in the code above to pass the resource contents to a tree iterator and then parse through the EObject inside as you advised. After that I call evaluate() for every one of them(The model is fairly small and I understand that it is not applicable to larger models).

The code is the following:

TreeIterator ti = res.getAllContents();
Object ob;
while(ti.hasNext()) {
ob = ti.next();
System.out.println("TI " + ob.toString());
System.out.println("Evaluation: " + ocl.isInvalid(ocl.evaluate(ob, query)));
}

This evaluates the Objects. It works fine on attribute level but I cannot manage to navigate to child elements.

Also I print my query "self.upper.value > 0" (I want to navigate to the child "upper") and it is transformed into this: Query: self.upper.value.>(0)
Why does this happen?Does this have an impact on the result?

Regards,
Paschalis.

[Updated on: Fri, 09 November 2012 15:00]

Report message to a moderator

Re: OCL Interpreter in Java (Helios) [message #977915 is a reply to message #977693] Fri, 09 November 2012 17:32 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4115
Registered: July 2009
Senior Member
Hi

On 09/11/2012 13:50, Paschalis Tsolakidis wrote:
> This evaluates the Objects. It works fine on attribute level but I
> cannot manage to navigate to child elements.
>
Without seeing your failure, I cannot comment on why it fails.
> Also I print my query "self.upper.value > 0" (I want to navigate to
> the child "upper") and it is transformed into this: Query:
> self.upper.value.>(0)
> Why does this happen?Does this have an impact on the result?
You are using the ToStringVisitorImpl to view the toString() result.
This is documented as returning a debug-friendly print out, rather than
a pretty print. So value.>(0) denotes invocation of the operation named
">) with argument 0 on the value.

No. It has no effect on the result. It is just what is really there.

Regards

Ed Willink
Re: OCL Interpreter in Java (Helios) [message #979209 is a reply to message #977915] Sat, 10 November 2012 18:29 Go to previous messageGo to next message
Paschalis Tsolakidis is currently offline Paschalis Tsolakidis
Messages: 5
Registered: October 2012
Junior Member
Hi Ed,

Thanks for the clarification about the printing.

Now this is the context of the tree iterator:

TI DomainModel.EAST_ADL.Timing.impl.TimingImpl@55e552 (shortName: testTiming) (category: <unset>, uuid: <unset>) (name: tim)
TI DomainModel.EAST_ADL.Timing.impl.ExecutionTimeConstraintImpl@123ef32 (shortName: testExecTimeConstraint) (category: <unset>, uuid: <unset>) (name: exec)
TI DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@2cc04e (shortName: testLowerTimeDuration) (category: <unset>, uuid: <unset>) (name: testLowerTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 2.0)
TI DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@54c41 (shortName: testUpperTimeDuration) (category: <unset>, uuid: <unset>) (name: testUpperTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 1.0)
TI DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@1d16a7f (shortName: testVariationTimeDuration) (category: <unset>, uuid: <unset>) (name: testVariationTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 10.0)

What troubles me is that for a query like this "self.lower.value" I get :

TI DomainModel.EAST_ADL.Timing.impl.TimingImpl@1ac44c2 (shortName: testTiming) (category: <unset>, uuid: <unset>) (name: tim)
Evaluation: true
TI DomainModel.EAST_ADL.Timing.impl.ExecutionTimeConstraintImpl@f1468c (shortName: testExecTimeConstraint) (category: <unset>, uuid: <unset>) (name: exec)
Evaluation: false
TI DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@154bd83 (shortName: testLowerTimeDuration) (category: <unset>, uuid: <unset>) (name: testLowerTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 2.0)
Evaluation: true
TI DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@65ea4b(shortName: testUpperTimeDuration) (category: <unset>, uuid: <unset>) (name: testUpperTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 1.0)
Evaluation: true
TI DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@f8fdd3 (shortName: testVariationTimeDuration) (category: <unset>, uuid: <unset>) (name: testVariationTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 10.0)
Evaluation: true


which is correct cause the xmi has Timing as the root,ExecutionTimingConstraint as its child and the three TimingDurations(upper,lower and variation) are children of ExecutionTimingConstraint. So the query validates only for the ExecutionTimingConstraint.

Now if I change the query to "self.lower.value > 0" I get the same result even thought it is clear that in the lower Object the value is 2.0 so it should not validate. That is why I wrongly assumed that it was an issue of the printed formula.

Is there something wrong with my query?How can I get the correct validation?

Thanks in advance.

Paschalis.



[Updated on: Sat, 10 November 2012 18:30]

Report message to a moderator

Re: OCL Interpreter in Java (Helios) [message #979966 is a reply to message #979209] Sun, 11 November 2012 08:11 Go to previous message
Ed Willink is currently offline Ed Willink
Messages: 4115
Registered: July 2009
Senior Member
Hi

What you produce is no where near reproducible, so I can only make some
observations.

It's always good to check validation with some bad cases, otherwise you
might not be executing the validation code at all; it's easy for the
registrations to malfunction.

Your query "self.lower.value" seems to return 5 objects which suggests a
collection, so I would hope to see a syntax error from "self.lower.value
> 0". The actual value of the threshold should not affect validation at
all.

Regards

Ed Willink


On 10/11/2012 18:29, Paschalis Tsolakidis wrote:
> Hi Ed,
>
> Thanks for the clarification about the printing.
>
> Now this is the context of the tree iterator:
>
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimingImpl@55e552
> (shortName: testTiming) (category: <unset>, uuid: <unset>) (name: tim)
> TI
> mailto:DomainModel.EAST_ADL.Timing.impl.ExecutionTimeConstraintImpl@123ef32
> (shortName: testExecTimeConstraint) (category: <unset>, uuid: <unset>)
> (name: exec)
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@2cc04e
> (shortName: testLowerTimeDuration) (category: <unset>, uuid: <unset>)
> (name: testLowerTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 2.0)
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@54c41
> (shortName: testUpperTimeDuration) (category: <unset>, uuid: <unset>)
> (name: testUpperTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 1.0)
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@1d16a7f
> (shortName: testVariationTimeDuration) (category: <unset>, uuid:
> <unset>) (name: testVariationTimeDuration) (cseCode: 2, cseCodeFactor:
> 3, value: 10.0)
>
> What troubles me is that for a query like this "self.lower.value" I get :
>
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimingImpl@1ac44c2
> (shortName: testTiming) (category: <unset>, uuid: <unset>) (name: tim)
> Evaluation: true
> TI
> mailto:DomainModel.EAST_ADL.Timing.impl.ExecutionTimeConstraintImpl@f1468c
> (shortName: testExecTimeConstraint) (category: <unset>, uuid: <unset>)
> (name: exec)
> Evaluation: false
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@154bd83
> (shortName: testLowerTimeDuration) (category: <unset>, uuid: <unset>)
> (name: testLowerTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 2.0)
> Evaluation: true
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@65ea4b
> (shortName: testUpperTimeDuration) (category: <unset>, uuid: <unset>)
> (name: testUpperTimeDuration) (cseCode: 2, cseCodeFactor: 3, value: 1.0)
> Evaluation: true
> TI mailto:DomainModel.EAST_ADL.Timing.impl.TimeDurationImpl@f8fdd3
> (shortName: testVariationTimeDuration) (category: <unset>, uuid:
> <unset>) (name: testVariationTimeDuration) (cseCode: 2, cseCodeFactor:
> 3, value: 10.0)
> Evaluation: true
>
>
> which is correct cause the xmi has Timing as the
> root,ExecutionTimingConstraint as its child and the three
> TimingDurations(upper,lower and variation) are children of
> ExecutionTimingConstraint. So the query validates only for the
> ExecutionTimingConstraint.
> Now if I change the query to "self.lower.value > 0" I get the same
> result even thought it is clear that in the lower Object the value is
> 2.0 so it should not validate. That is why I wrongly assumed that it
> was an issue of the printed formula.
>
> Is there something wrong with my query?How can I get the correct
> validation?
>
> Thanks in advance.
>
> Paschalis.
>
>
>
>
Previous Topic:how to call OCL constraints( generated java source code of model ) from the main code of java
Next Topic:Operation Body Expression
Goto Forum:
  


Current Time: Fri Oct 31 21:29:11 GMT 2014

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

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