Executing OCL expressions on plain Java – How to do it?

Tweet about this on TwitterShare on FacebookBuffer this pageShare on RedditShare on LinkedInShare on Google+Email this to someone

In twitter, Sébastien Mosser raised the question of how to execute OCL expressions on plain Java classes. As you may know, we can use EMFQuery to query EObjects using OCL expressions as filters as shown in this example (taken from this tutorial) where we select all objects of the Writer class having written books in two or more different categories.



Resource myResource = ... // get the resource

OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();
Condition condition = new BooleanOCLCondition(
    ocl.getEnvironment(),
    "self.books->collect(b : Book | b.category)->asSet()->size() > 2",
    EXTLibraryPackage.Literals.WRITER);

SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
	new FROM(myResource.getContents()), new WHERE(condition),
	new NullProgressMonitor());

IQueryResult results = statement.execute();
	
// do something with the results
selectInEditor(results);

This is good as long as we work within the EMF framework but what about using OCL as an internal DSL embedded in Java? Do you know any tool that would help us with that?

(I do know tools that translate OCL to native Java methods but they do that as part of a code-generation process, they do not allow mixing OCL as part of the Java program itself).

Suggestions very much appreciated!

Tweet about this on TwitterShare on FacebookBuffer this pageShare on RedditShare on LinkedInShare on Google+Email this to someone
Comments
  1. Ed Willink

Reply

Your email address will not be published. Required fields are marked *