Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Eclipse OCL
Eclipse OCL [message #1089863] Mon, 19 August 2013 10:54 Go to next message
maham khalid is currently offline maham khalidFriend
Messages: 3
Registered: August 2013
Junior Member
I Need help in evaluating ocl constraints on uml model using eclipse OCL ,I could not find OCLHelper methods for uml model...
Re: Eclipse OCL [message #1090137 is a reply to message #1089863] Mon, 19 August 2013 19:00 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Try reading the documentation and following the tutorials.

Regards

Ed Willink


On 19/08/2013 15:14, maham khalid wrote:
> I Need help in evaluating ocl constraints on uml model using eclipse
> OCL ,I could not find OCLHelper methods for uml model...
Re: Eclipse OCL [message #1090539 is a reply to message #1090137] Tue, 20 August 2013 09:33 Go to previous messageGo to next message
maham khalid is currently offline maham khalidFriend
Messages: 3
Registered: August 2013
Junior Member
i tried following tutorials and documentation but i am stuck because i am trying to validate uml model through eclipse ocl whereas the setContext method of OCLHelper requires a EClassifier. here is my code:
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.PACKAGE);

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.OCL ocl = OCL.newInstance(new UMLEnvironmentFactory(RESOURCE_SET));

org.eclipse.ocl.helper.OCLHelper helper1 = ocl.createOCLHelper();


helper1.setContext(org.eclipse.ocl.uml.util.OCLUMLUtil.getMetaclass(_model));
OCLExpression query=(OCLExpression) helper1.createQuery("Meeting.allInstances()->select(m|m.start>5 )->size() > 3 ");

//OCLExpression query = (OCLExpression) helper1.createQuery("self.books->collect(b : Book | b.category)->asSet()");
System.out.println(sourceElement.getName());
// create a Query to evaluate our query expression
// Query queryEval = (Query) ocl.createQuery(query);
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
It gives me the following exception:
java.lang.NullPointerException
Re: Eclipse OCL [message #1090592 is a reply to message #1090539] Tue, 20 August 2013 11:14 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

It clearly isn't your code since I see no import statements or main program.

At a quick glance I see

URI.createURI("jar:file:"+
transDir+"/org.eclipse.uml2.uml.resources-3.1.0.v201005031530.jar!/");

which makes me extremely ill. Qualified plugin names should never appear
in code.

I suggest you try following the basic UML2 Java tutorials.

Regards

Ed Willink



On 20/08/2013 10:33, maham khalid wrote:
> i tried following tutorials and documentation but i am stuck because i
> am trying to validate uml model through eclipse ocl whereas the
> setContext method of OCLHelper requires a EClassifier. here is my code:
> 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.PACKAGE);
>
> 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.OCL ocl = OCL.newInstance(new
> UMLEnvironmentFactory(RESOURCE_SET));
>
> org.eclipse.ocl.helper.OCLHelper helper1 =
> ocl.createOCLHelper();
>
>
>
> helper1.setContext(org.eclipse.ocl.uml.util.OCLUMLUtil.getMetaclass(_model));
> OCLExpression query=(OCLExpression)
> helper1.createQuery("Meeting.allInstances()->select(m|m.start>5
> )->size() > 3 ");
>
> //OCLExpression query = (OCLExpression)
> helper1.createQuery("self.books->collect(b : Book |
> b.category)->asSet()");
> System.out.println(sourceElement.getName());
> // create a Query to evaluate our query expression
> // Query queryEval = (Query) ocl.createQuery(query);
> } catch (ParserException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
> }
> }
> It gives me the following exception:
> java.lang.NullPointerException
Re: Eclipse OCL [message #1091152 is a reply to message #1090592] Wed, 21 August 2013 05:26 Go to previous messageGo to next message
maham khalid is currently offline maham khalidFriend
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..
Re: Eclipse OCL [message #1091181 is a reply to message #1091152] Wed, 21 August 2013 06:25 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
On 21/08/2013 06:26, maham khalid wrote:
> Hi,
>
> Hope u will get my problem now..
No.

You are asking for free help and just creating work for me.

I have to turn your email text into a working project, possibly failing
to reproduce your way of working.

It saves a lot of everyone's time if you you attach a zipped project.

Regards

Ed Willink
Re: Eclipse OCL [message #1092685 is a reply to message #1089863] Fri, 23 August 2013 05:01 Go to previous message
Umair Qudus is currently offline Umair QudusFriend
Messages: 1
Registered: August 2013
Junior Member
Hi,
You can download complete eclipse ocl from github org.eclipse.ocl.uml.tests.
that can help you in understanding ocl functions, like Helper class etc. Smile

Regards
Umair
Previous Topic:Custom Validation Message & Severity
Next Topic:Content Assist in Xtext OCL Console
Goto Forum:
  


Current Time: Tue Apr 16 07:44:42 GMT 2024

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

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

Back to the top