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!