Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [EMF-OCL-Java] OCL Query on Ecore model from Java.
[EMF-OCL-Java] OCL Query on Ecore model from Java. [message #633629] Mon, 18 October 2010 15:17
Dave  is currently offline Dave Friend
Messages: 37
Registered: September 2010
Member
Hi all,

I'm trying to execute an OCL query on an Ecore model, from Java code, but I've got some difficulties and I hope someone could help me.

I've written the following method:

public static IQueryResult execute(Resource ecoreResource, String query) {
	System.out.println("Received " + ecoreResource);
	OCL ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
	Condition condition;
	IQueryResult results = null;
	try {
		condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(ocl.getEnvironment(),query,null);
		SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
					new FROM(ecoreResource.getContents()), new WHERE((EObjectCondition) condition), new NullProgressMonitor() );
		results = statement.execute();
	} catch (ParserException e) {
		e.printStackTrace();
	}
	return results;
}



This method, is invoked by the following main of the Java class:


public static void main(String[] args) throws IOException {
	// Create a resource set to hold the resources.
	ResourceSet resourceSet = new ResourceSetImpl();
	URI modelUri = URI.createFileURI(new File("model/systemModel.ecore").getAbsolutePath());
	
	EcoreResourceFactoryImpl ecoreFactory = new EcoreResourceFactoryImpl();		
	resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", ecoreFactory);
	
	// Register the package to ensure it is available during loading.
	resourceSet.getPackageRegistry().put
		(MancoosiPackage.eNS_URI, 
		 MancoosiPackage.eINSTANCE);
	
	// Demand load resource for this file.
	Resource resource = resourceSet.getResource(modelUri, true);
	System.out.println("Loaded " + modelUri);
	
	IQueryResult res = QueryExecutor.execute(resource, "mancoosi::InstalledPackage.allInstances()->exists(c | c.installedSize > 259)");	
	java.util.Iterator iterator = res.iterator();
	while(iterator.hasNext()) {
		System.out.println(iterator.next().toString());
	}
	System.out.println("SIZE: " + res.size());
	System.out.println("OCL Query execution terminated.");
}



As you can see, the main method passes the ecore model (resource) and the String representing the OCL query. Moreover, the while statement provides just a feedback for realizing what the execution has actually produced. Such feedback, reported in the output console when the main is executed, seems to be the list of all the (15631...?) elements in the ecore model.
I report the last part of the produced output, for givin' you an idea of what I'm talkin' about:

mancoosi.impl.SingleDepImpl@9bfee2 (version: ge, value: 1:0.3-1)
mancoosi.impl.SingleDepImpl@98cbf7 (version: ge, value: 2.3.18)
mancoosi.impl.SingleDepImpl@1deb5f7 (version: ge, value: 0.5)
mancoosi.impl.SingleDepImpl@eb9f58 (version: ge, value: 2.15.4)
mancoosi.impl.FileImpl@17f61bb (name: 70-modules-unload.sh) (extension: null, description: null, size: 0, checkSum: null, isDirectory: false, suid: false, guid: false, permission: -rwxr-xr-x, location: /etc/acpi/suspend.d/70-modules-unload.sh, isMissing: false)
mancoosi.impl.SingleDepImpl@157bf4c (version: ge, value: 2.2.11~dfsg1-3ubuntu2~)
mancoosi.impl.NotInstalledPackageImpl@486d1d (name: xtel) (version: null)
mancoosi.impl.SingleConflictImpl@9b777a (version: llt, value: 22.1-0ubuntu10.1)
mancoosi.impl.FileImpl@101f23d (name: tosh-mute) (extension: null, description: null, size: 0, checkSum: null, isDirectory: false, suid: false, guid: false, permission: -rw-r--r--, location: /etc/acpi/events/tosh-mute, isMissing: false)
mancoosi.impl.SingleConflictImpl@16396f5 (version: le, value: 1.3.0)
mancoosi.impl.AndConflictImpl@9b774e
mancoosi.impl.SingleDepImpl@1eb8f71 (version: ge, value: 0.5.8.1)
mancoosi.impl.SingleDepImpl@11c9fcc (version: ge, value: 1:1.0.0)
mancoosi.impl.AndDepImpl@126cdd1
mancoosi.impl.SingleDepImpl@1eb8f6d (version: eq, value: null)
mancoosi.impl.FileImpl@d4927f (name: text.fr-2007.tbl) (extension: null, description: null, size: 0, checkSum: null, isDirectory: false, suid: false, guid: false, permission: -rw-r--r--, location: /etc/brltty/text.fr-2007.tbl, isMissing: false)
mancoosi.impl.SingleConflictImpl@1f4c4a3 (version: eq, value: null)
mancoosi.impl.SingleDepImpl@8c2d23 (version: eq, value: null)
mancoosi.impl.SingleDepImpl@9c87c1 (version: ge, value: 1:2.17.90)
SIZE: 15631
OCL Query execution terminated.



However, it doesn't seem to be correct, because, as you can see, the query result contains instances of mancoosi.impl.NotInstalledPackageImpl, but the query is "mancoosi::InstalledPackage.allInstances()->exists(c | c.installedSize > 259)", so NotInstalledPackages cannot be returned. In conclusion, the query seems to be executed, but there something wrong in the result.

I hope someone could help me.

Many thanks in advance,
Dave.
Previous Topic:Substitution groups without importing from XSD
Next Topic:MissingResourceException when using EMF in standalone mode
Goto Forum:
  


Current Time: Thu Oct 24 01:47:28 GMT 2019

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

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

Back to the top