Home » Modeling » OCL » OCL Interpreter in Java (Helios)
OCL Interpreter in Java (Helios) [message #973434] |
Tue, 06 November 2012 10:26 |
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 |
Ed Willink Messages: 7655 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 #977693 is a reply to message #973842] |
Fri, 09 November 2012 13:50 |
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 #979209 is a reply to message #977915] |
Sat, 10 November 2012 18:29 |
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 |
Ed Willink Messages: 7655 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.
>
>
>
>
|
|
|
Goto Forum:
Current Time: Thu Mar 28 19:36:57 GMT 2024
Powered by FUDForum. Page generated in 0.06677 seconds
|