How to create an invariant for an UML class in OCL 6.0? [message #1699203] |
Mon, 22 June 2015 10:26  |
Eclipse User |
|
|
|
Hi
There is a very simple program which defines the OCL constraint for the Person UML class:
https://github.com/AresEkb/ocl6_test
package ocl6_test;
import java.io.File;
import java.util.Iterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.ocl.pivot.Element;
import org.eclipse.ocl.pivot.ExpressionInOCL;
import org.eclipse.ocl.pivot.resource.ASResource;
import org.eclipse.ocl.pivot.utilities.OCL;
import org.eclipse.ocl.pivot.utilities.OCLHelper;
import org.eclipse.ocl.pivot.utilities.ParserException;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.UMLPackage;
public class Main {
public static void main(String[] args) {
final String input = "model/My.uml";
System.out.println("Initialization");
ResourceSet rs = new ResourceSetImpl();
rs.setURIConverter(new CustomURIConverter());
org.eclipse.ocl.pivot.model.OCLstdlib.install();
org.eclipse.ocl.pivot.uml.UMLStandaloneSetup.init();
OCL ocl = OCL.newInstance(rs);
System.out.println("Loading UML model " + input);
Resource resource = ocl.getResourceSet().getResource(createFileURI(input), true);
Model uml = (Model)EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.eINSTANCE.getModel());
System.out.println("Root: " + uml);
Class person = (Class)uml.getPackagedElement("Person");
System.out.println("Class: " + person);
try {
OCLHelper helper = ocl.createOCLHelper(person);
System.out.println("Helper: " + helper);
System.out.println("Context: " + helper.getContextClass());
ExpressionInOCL expr = ocl.createInvariant(person, "age2 > 0");
System.out.println("Expression: " + expr);
}
catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ocl.dispose();
}
private static URI createFileURI(String relativePath)
{
return URI.createFileURI(new File(relativePath).getAbsolutePath());
}
}
The problem is that it throws the exception:
Exception in thread "main" java.lang.IllegalStateException: Undefined contextClass
at org.eclipse.ocl.pivot.internal.helper.OCLHelperImpl.createInvariant(OCLHelperImpl.java:110)
at org.eclipse.ocl.pivot.utilities.OCL.createInvariant(OCL.java:261)
at ocl6_test.Main.main(Main.java:48)
As far I understand I shoud pass an org.eclipse.ocl.pivot.Class (instead of org.eclipse.uml2.uml.Class) into the ocl.createInvariant() method. Am I right? But how can I get a pivot Class for the UML Class?
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07081 seconds