Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Validate an ecore model with ocl constraint from a standalone application
Validate an ecore model with ocl constraint from a standalone application [message #754946] Sat, 05 November 2011 17:34 Go to next message
chrisi_01 is currently offline chrisi_01
Messages: 4
Registered: August 2011
Junior Member
Hi,

I tried to validate an ecore model with OCL constraints from a standalone application
but somehow it doesn't work.
When I run the "application" the constraint is everytime broken.
I get the following output of the console:
(@ is replaced with [at])

Diagnosis of myModel.impl.MyModelImpl[at]1d04653{file:/f:/temp/My.mymodel#/}
An exception occurred while delegating evaluation of the 'lowSmallerEqualsUp' constraint on 'myModel.impl.MyLinkImpl[at]12f0999{file:/f:/temp/My.mymodel#//@myObjects.0/@mylinks.0}': org/eclipse/ocl/expressions/OCLExpression


I hope someone can help me.


This is the ecore model example with the constraints (using OCLinEcore)
(I must changed somw URIs because I can't post links here ... No enough posts)
module _'My.ecore'
import ecore : '.eclipse.org/emf/2002/Ecore#/';

package myModel : myModel = 'myModel.ecore'
{
	class MyModel
	{
		property myObjects : MyObject[*] { ordered composes };
	}
	class MyObject
	{
		property mylinks : MyLink[*] { ordered composes };
		attribute name : String[1] { ordered };
	}
	class MyLink
	{
		invariant lowSmallerEqualsUp: self.min <= self.max;
		property toMyObject : MyObject[1] { ordered };
		attribute min : ecore::EInt[1] { ordered };
		attribute max : ecore::EInt[1] { ordered };
	}
}


As you see the model has a simple invaiant (self.min <= self.max)

Now I tried to load an instance of this model and after that
to validate it.

import java.io.File;

import myModel.MyModelPackage;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EValidator;
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.Diagnostician;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.ocl.ecore.delegate.OCLDelegateDomain;
import org.eclipse.ocl.ecore.delegate.OCLInvocationDelegateFactory;
import org.eclipse.ocl.ecore.delegate.OCLSettingDelegateFactory;
import org.eclipse.ocl.ecore.delegate.OCLValidationDelegateFactory;


public class Test2 {

	public static void main(String[] args) { new Test2(); }
	
	private File file = new File("f:/temp/My.mymodel");
	
	public Test2() {
		init_OclStuff();
		init();
	}
	
	private void init_OclStuff() {
		String oclDelegateURI = OCLDelegateDomain.OCL_DELEGATE_URI;
		EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE.put(
				oclDelegateURI, new OCLInvocationDelegateFactory.Global());
		EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE
				.put(oclDelegateURI, new OCLSettingDelegateFactory.Global());
		EValidator.ValidationDelegate.Registry.INSTANCE.put(oclDelegateURI,
				new OCLValidationDelegateFactory.Global());
//		EValidator.Registry.INSTANCE.put(MyModelPackage.eINSTANCE, new MyModelValidator());
	}
	
	private Resource init() {
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
			("mymodel", new XMIResourceFactoryImpl());
		resourceSet.getPackageRegistry().put(MyModelPackage.eNS_URI, MyModelPackage.eINSTANCE);

		URI uri = URI.createFileURI(file.getAbsolutePath());

		Resource resource = null;
		try {
			resource = resourceSet.getResource(uri, true);
			for (EObject eObject : resource.getContents()) {
				Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
				if (diagnostic.getSeverity() != Diagnostic.OK) {
					printDiagnostic(diagnostic, "");
				}
			}
		} catch (RuntimeException exception) {
					exception.printStackTrace();
		}
		return resource;
	}

	protected static void printDiagnostic(Diagnostic diagnostic, String indent) {
		System.out.print(indent);
		System.out.println(diagnostic.getMessage());
		for (Diagnostic child : diagnostic.getChildren()) {
			printDiagnostic(child, indent + "  ");
		}
	}
}



This is the model instance I used.
<?xml version="1.0" encoding="UTF-8"?>
<myModel:MyModel xmi:version="2.0" xmlns:xmi="ww.omg.org/XMI" xmlns:myModel="myModel.ecore">
  <myObjects name="A">
    <mylinks toMyObject="//@myObjects.1" min="2" max="5"/>
  </myObjects>
  <myObjects name="B"/>
</myModel:MyModel>


In this example the ocl constraint it valide because min is bigger than max
but the application validate this as an error.
Why?


Do I miss something?
Maybe I forgot to register something?

[Updated on: Sat, 05 November 2011 18:29]

Report message to a moderator

Re: Validate an ecore model with ocl constraint from a standalone application [message #754955 is a reply to message #754946] Sat, 05 November 2011 20:30 Go to previous messageGo to next message
Axel Uhl is currently offline Axel Uhl
Messages: 40
Registered: July 2009
Member
Chrisi,

I've created a little sample project using the code and models you presented above. I created an additional genmodel for the ecore you showed and generated the model sources. With my initial attempt, this gave me:

Diagnosis of myModel.impl.MyModelImpl@d5e92d7{file:/c:/tmp/My.mymodel#/}
Unable to find delegate to evaluate the 'lowSmallerEqualsUp' constraint on 'myModel.impl.MyLinkImpl@23000bcf{file:/c:/tmp/My.mymodel#//@myObjects.0/@mylinks.0}': http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot

When I changed the http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot delegate URIs to http://www.eclipse.org/emf/2002/Ecore/OCL and re-generated the model sources, the test ran fine without errors. The problem is that the OCLInEcore editor uses the Pivot URIs. The Pivot infrastructure is fairly new and may still have some flaws here and there. If you need things to work for now, try going with the non-Pivot evaluator. However, you may have to manually patch the URIs that OCLInEcore puts in unless you chose to use any other editing infrastructure (is there any...?).

Trying to find out what's going wrong in Pivot, I then changed your line

String oclDelegateURI = OCLDelegateDomain.OCL_DELEGATE_URI;

to

String oclDelegateURI = OCLDelegateDomain.OCL_DELEGATE_URI+"/Pivot";

This got me one step further. This time, the diagnostics tell me

Diagnosis of myModel.impl.MyModelImpl@3bfa681c{file:/c:/tmp/My.mymodel#/}
An exception occurred while delegating evaluation of the 'lowSmallerEqualsUp' constraint on 'myModel.impl.MyLinkImpl@2c6691da{file:/c:/tmp/My.mymodel#//@myObjects.0/@mylinks.0}': null


Is that what you were getting (BTW: why didn't you present the error message you got during validation? Weren't there any?).

Replacing the packages from which you import the delegate factories from org.eclipse.ocl.ecore.delegate... to org.eclipse.ocl.examples.pivot.delegate took me yet a step further. This time, the error message I'm getting is

Diagnostic ERROR source=org.eclipse.emf.ecore code=0 Diagnosis of myModel.impl.MyModelImpl@2e8aeed0{file:/c:/tmp/My.mymodel#/} data=[myModel.impl.MyModelImpl@2e8aeed0] [Diagnostic ERROR source=myModel code=0 An exception occurred while delegating evaluation of the 'lowSmallerEqualsUp' constraint on 'myModel.impl.MyLinkImpl@66c8e4de{file:/c:/tmp/My.mymodel#//@myObjects.0/@mylinks.0}': No OCL Standard Library content available data=[myModel.impl.MyLinkImpl@66c8e4de (min: 2, max: 5)]]

This is where I'll stop and where Ed may take over if he happens to find the time. I'm not sure what needs to be done to make the OCL standard library available to the evaluator.

Best,
-- Axel
Re: Validate an ecore model with ocl constraint from a standalone application [message #754957 is a reply to message #754955] Sat, 05 November 2011 21:00 Go to previous messageGo to next message
chrisi_01 is currently offline chrisi_01
Messages: 4
Registered: August 2011
Junior Member
Sorry. I forgot to mention that I changed the delegate URIs to .../emf/2002/Ecore/OCL.
If I do this I don't get any exception but the constraint (self.min <= self.max) is validated wrong.
The diagnostics tells me everytime:

Diagnosis of myModel.impl.MyModelImpl[at]b988a6{file:/f:/temp/My.mymodel#/}
An exception occurred while delegating evaluation of the 'lowSmallerEqualsUp' constraint on 'myModel.impl.MyLinkImpl[at]19209ea{file:/f:/temp/My.mymodel#//@myObjects.0/@mylinks.0}': org/eclipse/ocl/expressions/OCLExpression

no matter if the constraint is true or false.
Re: Validate an ecore model with ocl constraint from a standalone application [message #754982 is a reply to message #754957] Sun, 06 November 2011 11:23 Go to previous message
chrisi_01 is currently offline chrisi_01
Messages: 4
Registered: August 2011
Junior Member
I don't know why but it is working now.
I build up the whole example in a new workspace again and it works ... very strange.

Maybe there was some other package registered that caused that problem.

How can I unregister all things befor running me application?

[Updated on: Sun, 06 November 2011 11:36]

Report message to a moderator

Previous Topic:EEnumLiteral and Integer
Next Topic:[Announce] Eclipse OCL 4.0.0 (Juno) M3 is now available
Goto Forum:
  


Current Time: Tue Sep 30 14:02:37 GMT 2014

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

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