|
|
|
|
Re: Eclipse OCL [message #1091152 is a reply to message #1090592] |
Wed, 21 August 2013 05:26 |
maham khalid Messages: 3 Registered: August 2013 |
Junior Member |
|
|
Hi,
I m trying to run a simple OCL example for uml model with hard coded paths for now and there is no problem with UML2 java implementation as the model is loaded correctly.I get exception with setcontext() method of helper class. here is my detailed code :
import java.io.File;
import java.util.Map;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLPackage;
import org.eclipse.emf.mapping.ecore2xml.util.Ecore2XMLResource;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.helper.OCLHelper;
import org.eclipse.ocl.uml.UMLEnvironmentFactory;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UML22UMLResource;
import org.eclipse.uml2.uml.resource.UMLResource;
public class test {
protected final ResourceSet RESOURCE_SET = new ResourceSetImpl();
public static void registerPackages(ResourceSet resourceSet) {
Map packageRegistry = resourceSet.getPackageRegistry();
packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
packageRegistry.put(Ecore2XMLPackage.eNS_URI, Ecore2XMLPackage.eINSTANCE);
packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
packageRegistry .put("http://www.eclipse.org/uml2/2.1.0/UML",UMLPackage.eINSTANCE);
packageRegistry .put("http://www.eclipse.org/uml2/3.0.0/UML",UMLPackage.eINSTANCE);
}
public static void registerPathmaps(URI uri, ResourceSet resourceSet) {
Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.UML_PRIMITIVE_TYPES_LIBRARY_URI), uri.appendSegment("libraries").appendSegment(""));
uriMap.put(URI.createURI("pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml"),
uri.appendSegment("libraries").appendSegment("UMLPrimitiveTypes.library.uml"));
}
public static void registerResources(URI uri, ResourceSet resourceSet)
{
registerPathmaps(uri, resourceSet);
registerPackages(resourceSet);
registerResourceFactories();
}
public static void registerResourceFactories() {
Map<String, Object> extensionFactoryMap = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap();
extensionFactoryMap.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(Ecore2XMLResource.FILE_EXTENSION, Ecore2XMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UML22UMLResource.FILE_EXTENSION, UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UMLResource.FILE_EXTENSION, UML22UMLResource.Factory.INSTANCE);
}
void process(String ur)
{
URI model = URI.createURI(ur);
String transDir = "EMF_lib\\";
URIConverter.URI_MAP.put(URI.createURI("platform:/plugin/org.eclipse.uml2.uml/"),
URI.createURI("jar:file:" + transDir + "org.eclipse.uml2.uml_3.1.2.v201010261927.jar!/"));
registerPackages(RESOURCE_SET);
registerResourceFactories();
URI uri = URI.createURI("jar:file:"+ transDir+"/org.eclipse.uml2.uml.resources-3.1.0.v201005031530.jar!/");
registerResources(uri, RESOURCE_SET);
Resource resource = null;
try {
resource = RESOURCE_SET.getResource(model, true);
}
catch (Exception e) {
e.printStackTrace();
}
Model _model = (Model)EcoreUtil.getObjectByType(resource.getContents(),
UMLPackage.Literals.MODEL);
EList<PackageableElement> sourcePackagedElements = _model.getPackagedElements();
for (PackageableElement sourceElement : sourcePackagedElements)
{
// create an OCL instance for Ecore
if (sourceElement.eClass() == UMLPackage.Literals.CLASS)
{
if(sourceElement.getName().contentEquals("Meeting"))
{
try
{
org.eclipse.ocl.uml.OCL ocl = org.eclipse.ocl.uml.OCL.newInstance(new UMLEnvironmentFactory(RESOURCE_SET));
OCLHelper helper1 = ocl.createOCLHelper();
helper1.setContext(org.eclipse.ocl.uml.util.OCLUMLUtil.getMetaclass(_model));
org.eclipse.ocl.uml.OCLExpression query = (org.eclipse.ocl.uml.OCLExpression) helper1.createQuery("Meeting.allInstances()->select(m|m.start>5 )->size() > 3");
System.out.println(ocl.evaluate(_model, query));
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws Exception
{
File f=new File("C:/Users/Maham/workspace/OCLSolver/examples/example1/uml file/testclass.uml");
String uri = f.toURI().toString();
test obj=new test();
obj.process(uri);
}
}
I get the following Exception trace starting from line ' helper1.setContext(org.eclipse.ocl.uml.util.OCLUMLUtil.getMetaclass(_model))':
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.eclipse.ocl.uml.UMLPackage$Literals.<clinit>(UMLPackage.java:14517)
at org.eclipse.ocl.uml.impl.VariableImpl.eStaticClass(VariableImpl.java:184)
at org.eclipse.emf.ecore.impl.MinimalEObjectImpl.eClass(MinimalEObjectImpl.java:688)
at org.eclipse.emf.ecore.impl.ENotificationImpl.getFeature(ENotificationImpl.java:394)
at org.eclipse.emf.ecore.util.ECrossReferenceAdapter.selfAdapt(ECrossReferenceAdapter.java:436)
at org.eclipse.uml2.common.util.CacheAdapter.selfAdapt(CacheAdapter.java:534)
at org.eclipse.emf.ecore.util.ECrossReferenceAdapter.notifyChanged(ECrossReferenceAdapter.java:424)
at org.eclipse.uml2.common.util.CacheAdapter.notifyChanged(CacheAdapter.java:342)
at org.eclipse.uml2.uml.internal.impl.ElementImpl.eNotify(ElementImpl.java:970)
at org.eclipse.uml2.uml.internal.impl.NamedElementImpl.setName(NamedElementImpl.java:268)
at org.eclipse.ocl.uml.internal.UMLReflectionImpl.setName(UMLReflectionImpl.java:754)
at org.eclipse.ocl.AbstractEnvironmentFactory.createClassifierContext(AbstractEnvironmentFactory.java:138)
at org.eclipse.ocl.internal.helper.OCLHelperImpl.setContext(OCLHelperImpl.java:119)
at org.eclipse.ocl.uml.OCLHelperImpl.setContext(OCLHelperImpl.java:125)
at org.eclipse.ocl.uml.OCLHelperImpl.setContext(OCLHelperImpl.java:1)
at test.process(test.java:102)
at test.main(test.java:125)
Caused by: java.lang.NullPointerException
at org.eclipse.ocl.uml.impl.UMLPackageImpl.initializePackageContentsGen(UMLPackageImpl.java:1237)
at org.eclipse.ocl.uml.impl.UMLPackageImpl.initializePackageContents(UMLPackageImpl.java:1199)
at org.eclipse.ocl.uml.impl.UMLPackageImpl.init(UMLPackageImpl.java:506)
at org.eclipse.ocl.uml.UMLPackage.<clinit>(UMLPackage.java:81)
... 17 more
Hope u will get my problem now..
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04019 seconds