Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Saving OCL-constraint(question on how to save an ocl-constraint to an xml file)
Saving OCL-constraint [message #992832] Sat, 22 December 2012 19:29 Go to next message
Stiebar Maerck is currently offline Stiebar Maerck
Messages: 3
Registered: December 2012
Junior Member
Hi,

I've been trying to understand the tutorials (OCLinEcore), bu it's all new to me.
I've been trying to save an OCL-constraint to a new XMI/XML file, but don't seem to find the correct code to do so.

I've made an Ecore file containing the model, now I would like to create a file containing a serialized version of one or more OCL-constraints.
These OCL-constraints will be loaded later on to be validated using a self made version of OclValidationVisitor.

I've been able to validate OCL-constraints in code with the visitor. But i can't seem to serialize the constraint itself, let alone load it.

This is what i've got so far (large part from OCLinEcore tutorial):

-----
package test;

import java.io.IOException;
import java.util.Collections;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
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.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
import org.eclipse.ocl.ecore.OCL;
import org.eclipse.ocl.ecore.OCL.Helper;
import org.eclipse.ocl.expressions.OCLExpression;

import stevenmertens.ecoreOcl.ecoreXmlBuilder.EPackageBuilder;

public class TEST {

public void test(EPackageBuilder ep) throws ParserException, IOException {


ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( "ecore", new EcoreResourceFactoryImpl());
Resource resource = resourceSet.getResource(URI.createFileURI(ep.getFilePath()), true);
EPackage model = (EPackage) resource.getContents().get(0);

// create a resource in which to store our parsed OCL expressions and constraints
URI uri = URI.createPlatformResourceURI("myOcl.xmi", true);

Resource res = resourceSet.createResource(uri);

// initialize a new OCL environment, persisted in this resource
OCL ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE, res);

// for the new OCL environment, create a new helper
Helper helper = ocl.createOCLHelper();

helper.setContext(model.getEClassifier("Copy"));

// try a very simple expression
OCLExpression<EClassifier> query = helper.createQuery("self.Title = 'test'");

// store our query in this resource. All of its necessary environment has
// already been stored, so we insert the query as the first resource root
res.getContents().add(0, query);

res.save(Collections.emptyMap());
res.unload();
}

}
-------

The problem with this code is that variable 'res' is null. This causes a NullPointerException.
From what I understand a factory should be used somewhere, resolving the NullPointerException. But I don't find code examples working for me.

If someome could help me, it would be very much appreciated!

Thanks in advance,
Stiebar
Re: Saving OCL-constraint [message #992857 is a reply to message #992832] Sat, 22 December 2012 20:58 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4110
Registered: July 2009
Senior Member
Hi

OCLinEcore is for embedded OCL.

If you want a complementary OCL document uses Complete OCL.

Regards

Ed Willink

On 22/12/2012 20:37, Stiebar Maerck wrote:
> Hi,
>
> I've been trying to understand the tutorials (OCLinEcore), bu it's all
> new to me.
> I've been trying to save an OCL-constraint to a new XMI/XML file, but
> don't seem to find the correct code to do so.
>
> I've made an Ecore file containing the model, now I would like to
> create a file containing a serialized version of one or more
> OCL-constraints.
> These OCL-constraints will be loaded later on to be validated using a
> self made version of OclValidationVisitor.
>
> I've been able to validate OCL-constraints in code with the visitor.
> But i can't seem to serialize the constraint itself, let alone load it.
>
> This is what i've got so far (large part from OCLinEcore tutorial):
>
> -----
> package test;
>
> import java.io.IOException;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EClassifier;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EPackage;
> 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.xmi.impl.EcoreResourceFactoryImpl;
> import org.eclipse.ocl.ParserException;
> import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
> import org.eclipse.ocl.ecore.OCL;
> import org.eclipse.ocl.ecore.OCL.Helper;
> import org.eclipse.ocl.expressions.OCLExpression;
>
> import stevenmertens.ecoreOcl.ecoreXmlBuilder.EPackageBuilder;
>
> public class TEST {
>
> public void test(EPackageBuilder ep) throws ParserException,
> IOException {
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
> "ecore", new EcoreResourceFactoryImpl());
> Resource resource =
> resourceSet.getResource(URI.createFileURI(ep.getFilePath()), true);
> EPackage model = (EPackage) resource.getContents().get(0);
>
> // create a resource in which to store our parsed OCL
> expressions and constraints
> URI uri = URI.createPlatformResourceURI("myOcl.xmi", true);
>
> Resource res = resourceSet.createResource(uri);
>
> // initialize a new OCL environment, persisted in this resource
> OCL ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE, res);
>
> // for the new OCL environment, create a new helper
> Helper helper = ocl.createOCLHelper();
>
> helper.setContext(model.getEClassifier("Copy"));
>
> // try a very simple expression
> OCLExpression<EClassifier> query =
> helper.createQuery("self.Title = 'test'");
>
> // store our query in this resource. All of its necessary
> environment has
> // already been stored, so we insert the query as the first
> resource root
> res.getContents().add(0, query);
>
> res.save(Collections.emptyMap());
> res.unload();
> }
>
> }
> -------
>
> The problem with this code is that variable 'res' is null. This causes
> a NullPointerException.
> From what I understand a factory should be used somewhere, resolving
> the NullPointerException. But I don't find code examples working for me.
>
> If someome could help me, it would be very much appreciated!
>
> Thanks in advance,
> Stiebar
Re: Saving OCL-constraint [message #993149 is a reply to message #992857] Sun, 23 December 2012 15:03 Go to previous messageGo to next message
Stiebar Maerck is currently offline Stiebar Maerck
Messages: 3
Registered: December 2012
Junior Member
Hi, thanks for your quick response. I've checked out the CompleteOCL tutorial, but the OCL is stored as plain text and I didn't find how to load these files from within an Java application (in code).

Is there a way to store OCL-constraints in an XML-format using EclipseOCL?
Just to clarify, I don't mean like this (plain text in XML):
<constraint>
<context>Copy</context>
<inv>self.title = 'test'</inv>
</constraint>
What I whould like is something like this (this is selfmade, inspired by the structure of the visitors for validating OCL with EclipseOCL):
<operationCallExp name="=">
<source>
<propertyCallExp property="Title" propertyType="String">
<source>
<variableExp name="self" type="Copy"/>
</source>
</propertyCallExp>
</source>
<arguments>
<stringLiteralExp value="test"/>
</arguments>
</operationCallExp>


Thanks in advance for your help!
Re: Saving OCL-constraint [message #993174 is a reply to message #993149] Sun, 23 December 2012 16:48 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4110
Registered: July 2009
Senior Member
Hi

If you look at doRoundTripFromCompleteOCL in
GIT\org.eclipse.ocl\tests\org.eclipse.ocl.examples.xtext.tests\src\org\eclipse\ocl\examples\test\xtext\RoundTripTests.java
you'll find mind of what you want.

You can choose to save the Xtext friendly CST, which is proprietary and
subject to arbitrary change, or the AST which is a prototype for what
might become the OCL 2.5 specification; this too will change once I
complete alignment with UML 2.5.

org.eclipse.ocl.examples.pivot.OCL is evolving to provide many useful
facade methods.

Regards

Ed Willink

On 23/12/2012 15:03, Stiebar Maerck wrote:
> Hi, thanks for your quick response. I've checked out the CompleteOCL
> tutorial, but the OCL is stored as plain text and I didn't find how to
> load these files from within an Java application (in code).
>
> Is there a way to store OCL-constraints in an XML-format using
> EclipseOCL?
> Just to clarify, I don't mean like this (plain text in XML):
> <constraint>
> <context>Copy</context>
> <inv>self.title = 'test'</inv>
> </constraint>
> What I whould like is something like this (this is selfmade, inspired
> by the structure of the visitors for validating OCL with EclipseOCL):
> <operationCallExp name="=">
> <source>
> <propertyCallExp property="Title" propertyType="String">
> <source>
> <variableExp name="self" type="Copy"/>
> </source>
> </propertyCallExp>
> </source>
> <arguments>
> <stringLiteralExp value="test"/>
> </arguments>
> </operationCallExp>
>
>
> Thanks in advance for your help!
Re: Saving OCL-constraint [message #993181 is a reply to message #993174] Sun, 23 December 2012 17:15 Go to previous messageGo to next message
Stiebar Maerck is currently offline Stiebar Maerck
Messages: 3
Registered: December 2012
Junior Member
Ok, but where and how do I find/open the 'RoundTripTests.java'-class?
I've programmed with Eclipse before, but never on the level of using/opening Eclipse classes themself. So my appologies if this is something obvious.

Thanks,
Stiebar
Re: Saving OCL-constraint [message #993215 is a reply to message #993181] Sun, 23 December 2012 19:31 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4110
Registered: July 2009
Senior Member
Hi

See http://wiki.eclipse.org/MDT/OCL#Downloads_and_Updates

Regards

Ed Willink

On 23/12/2012 17:15, Stiebar Maerck wrote:
> Ok, but where and how do I find/open the 'RoundTripTests.java'-class?
> I've programmed with Eclipse before, but never on the level of
> using/opening Eclipse classes themself. So my appologies if this is
> something obvious.
>
> Thanks,
> Stiebar
Re: Saving OCL-constraint [message #1006118 is a reply to message #993215] Wed, 30 January 2013 00:55 Go to previous message
Fi Fi is currently offline Fi Fi
Messages: 133
Registered: March 2010
Senior Member
Dear Stiebar Maerck,
Can You please share with me how do you do to serialize a complete OCL document?

Best regards,
Fy Za
Previous Topic:PIVOT model and method overriding bug
Next Topic:[Announce] Eclipse OCL 4.0.2 (Juno) SR2 RC2 is now available
Goto Forum:
  


Current Time: Fri Oct 31 00:01:51 GMT 2014

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

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