Hey I encountered a really wired problem when trying to parse an ocl invariant or postcondition with an ocl Helper. With the Peace of code at the end of the post the parsing works well as long as the line
//p.getType(); // THIS CAUSES THE ERROR
stays commented out. Whenever there has been a call to getType() before the ocl parser can not figure out the type of the Property correctly and will give an error message saying
Cannot find operation (>=(Integer)) for the type (null)
I found two workarounds for that:
1: in this minimal code example you could just replace the line causing the error by
((PropertyImpl) p).basicGetType();
this works for that example but fails when you want to evaluate several Constraints on the same model since OCL itself calls the function getType().
2: another workaround that seemed to work for me was not using the UMLPrimitiveType library. Instead you need to model Basic Datatypes with the names Integer, Boolean ... In this case it is crucial to use exactly the same names as in the UMLPrimitiveTypeLibrary.
Now my Questions: is this a bug in OCL? or did i just do something that was not intendet? Are there better ways to solve this problem?
I attached a simple model and a peace of code reproducing the error.
public static void main(String[] args) throws ParserException, IOException {
// Loading the Resource
ResourceSet RESOURCE_SET = new ResourceSetImpl();
UMLResourcesUtil.init(RESOURCE_SET); // MDT/UML2 4.0.0 (Juno)
Resource resource = RESOURCE_SET.createResource(URI
.createFileURI("src/oclBugMinimal/Model.uml"));
resource.load(null);
// navigating through the model finding context elements
Model model = (Model) resource.getContents().get(0);
Class class1 = (Class) model.getPackagedElement("Class1");
for (Property p : class1.getOwnedAttributes()) {
//p.getType(); // THIS CAUSES THE ERROR
}
// Try parsing a simple ocl constraint
OCL ocl = OCL.newInstance();
Helper helper = ocl.createOCLHelper();
helper.setContext(class1);
helper.createInvariant("x>=5");
}
On 17/07/2013 21:48, felix Kurth wrote:
> Hey I encountered a really wired problem when trying to parse an ocl
> invariant or postcondition with an ocl Helper. With the Peace of code
> at the end of the post the parsing works well as long as the
> line //p.getType(); // THIS CAUSES THE ERROR stays commented
> out. Whenever there has been a call to getType() before the ocl parser
> can not figure out the type of the Property correctly and will give an
> error message saying Cannot find operation (>=(Integer)) for the type
> (null) I found two workarounds for that:
> 1: in this minimal code example you could just replace the line
> causing the error by ((PropertyImpl) p).basicGetType(); this works for
> that example but fails when you want to evaluate several Constraints
> on the same model since OCL itself calls the function getType().
> 2: another workaround that seemed to work for me was not using the
> UMLPrimitiveType library. Instead you need to model Basic Datatypes
> with the names Integer, Boolean ... In this case it is crucial to use
> exactly the same names as in the UMLPrimitiveTypeLibrary.
>
> Now my Questions: is this a bug in OCL? or did i just do something
> that was not intendet? Are there better ways to solve this problem?
>
> I attached a simple model and a peace of code reproducing the error.
> public static void main(String[] args) throws ParserException,
> IOException {
> // Loading the Resource
> ResourceSet RESOURCE_SET = new ResourceSetImpl();
> UMLResourcesUtil.init(RESOURCE_SET); // MDT/UML2 4.0.0 (Juno)
> Resource resource = RESOURCE_SET.createResource(URI
> .createFileURI("src/oclBugMinimal/Model.uml"));
> resource.load(null);
>
> // navigating through the model finding context elements
> Model model = (Model) resource.getContents().get(0);
> Class class1 = (Class) model.getPackagedElement("Class1");
>
> for (Property p : class1.getOwnedAttributes()) {
> //p.getType(); // THIS CAUSES THE ERROR
> }
>
> // Try parsing a simple ocl constraint
> OCL ocl = OCL.newInstance();
> Helper helper = ocl.createOCLHelper();
> helper.setContext(class1);
> helper.createInvariant("x>=5");
>
> }
> The content of the model file is the following:
> <?xml version="1.0" encoding="UTF-8"?>
> <uml:Model xmi:version="20110701"
> xmlns:xmi="HTTPECLIPSEORG/spec/XMI/20110701"
> xmlns:uml="HTTPECLIPSEORG/uml2/4.0.0/UML"
> xmi:id="_824b49cf-8739-46e1-946f-01fe7db160ef"
> name="SomeActivityDiagrams">
> <packagedElement xmi:type="uml:Class"
> xmi:id="_b46209c4-a730-4f9c-8663-740dd5f97be7" name="Class1">
> <ownedAttribute xmi:id="_02d2fb85-4294-44cb-a4ff-aaff3731fe83"
> name="x" visibility="private" aggregation="composite">
> <type xmi:type="uml:PrimitiveType"
> href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#Integer"/>
> </ownedAttribute>
> </packagedElement>
> </uml:Model>
>