Saving OCL-constraint [message #992832] |
Sat, 22 December 2012 19:29 |
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 |
Ed Willink Messages: 7670 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
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04195 seconds