Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » How to create an invariant for an UML class in OCL 6.0?
How to create an invariant for an UML class in OCL 6.0? [message #1699203] Mon, 22 June 2015 14:26 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 170
Registered: August 2013
Senior Member
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?
Re: How to create an invariant for an UML class in OCL 6.0? [message #1699226 is a reply to message #1699203] Mon, 22 June 2015 17:49 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5577
Registered: July 2009
Senior Member
Hi

You just need.

org.eclipse.ocl.pivot.Class asPerson =
ocl.getMetamodelManager().getASOf(org.eclipse.ocl.pivot.Class.class,
person);

When you run you will get a nice error message telling you that you need

EssentialOCLStandaloneSetup.doSetup();

The following now happens pretty automatically (lazily)

org.eclipse.ocl.pivot.model.OCLstdlib.install();

getASOf is a new more flexible API than getASOfEcore (formerly
getPivotOfEcore). It supports any plugged-in External Syntax technology;
currently just Ecore and optionally UML2.
(org.eclipse.ocl.pivot.as_resource_factory extension point.)

Unfortunately getASOf is not used as widely as it could be. Bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=470738 raised. Once this
is fixed, your program would work as is, up until the error message
diagnosing the missing EssentialOCLStandaloneSetup.doSetup();

Regards

Ed Willink

On 22/06/2015 15:26, Denis Nikiforov wrote:
> 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?
Re: How to create an invariant for an UML class in OCL 6.0? [message #1699254 is a reply to message #1699226] Tue, 23 June 2015 05:36 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 170
Registered: August 2013
Senior Member
Thanks a lot! It works!

It seems that the "Pivot Standalone Configuration" section in the documentation is a little bit outdated. The initialization is simpler now.
Re: How to create an invariant for an UML class in OCL 6.0? [message #1699256 is a reply to message #1699254] Tue, 23 June 2015 05:41 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5577
Registered: July 2009
Senior Member
Hi

Are you looking at the latest 6.0.0 documentation. I updated quite a lot
but time always runs out ...

Regards

Ed Willink


On 23/06/2015 06:36, Denis Nikiforov wrote:
> Thanks a lot! It works!
>
> It seems that the "Pivot Standalone Configuration" section in the
> documentation is a little bit outdated. The initialization is simpler
> now.
Re: How to create an invariant for an UML class in OCL 6.0? [message #1699259 is a reply to message #1699256] Tue, 23 June 2015 05:54 Go to previous message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 170
Registered: August 2013
Senior Member
Hi

I'm lookig at http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.ocl.doc%2Fhelp%2FPivotStandalone.html&cp=49_6_2

Here is a list of possible initialization methods:
org.eclipse.ocl.pivot.OCL.initialize(resourceSet);
org.eclipse.ocl.pivot.uml.UML2AS.initialize(resourceSet);
org.eclipse.ocl.pivot.model.OCLstdlib.install();
org.eclipse.ocl.pivot.delegate.OCLDelegateDomain.initialize(resourceSet);
org.eclipse.ocl.xtext.completeocl.CompleteOCLStandaloneSetup.doSetup();
org.eclipse.ocl.xtext.oclinecore.OCLinEcoreStandaloneSetup.doSetup();
org.eclipse.ocl.xtext.oclstdlib.OCLstdlibStandaloneSetup.doSetup();
org.eclipse.ocl.domain.utilities.StandaloneProjectMap.getAdapter(resourceSet);
Previous Topic:How to import an UML element into a pivot model?
Next Topic:Announcing the Mars 6.0.0 Eclipse OCL release
Goto Forum:
  


Current Time: Thu Jan 18 02:24:51 GMT 2018

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

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