blob: 0bbdef0ab71c866c3787a4c6fd0af1971795f800 [file] [log] [blame]
// Copied from org.eclipse.ocl.examples.test.xtext.PivotDocumentationExamples.java
OCL ocl = OCL.newInstance(OCL.CLASS_PATH);
ResourceSet resourceSet = ocl.getResourceSet();
ExpressionInOCL invariant = ocl.createInvariant(EXTLibraryPackage.Literals.LIBRARY,
"books->forAll(b1, b2 | b1 <> b2 implies b1.title <> b2.title)");
ExpressionInOCL query = ocl.createQuery(EXTLibraryPackage.Literals.LIBRARY,
"books->collect(b : Book | b.category)->asSet()");
// create a Query to evaluate our query expression
Query queryEval = ocl.createQuery(query);
// create another to check our constraint
Query constraintEval = ocl.createQuery(invariant);
List<Library> libraries = getLibraries(resourceSet); // hypothetical source of libraries
// only print the set of book categories for valid libraries
for (Library next : libraries) {
if (constraintEval.checkEcore(next)) {
// the OCL result type of our query expression is Set(BookCategory)
@SuppressWarnings("unchecked")
Set<BookCategory> categories = (Set<BookCategory>) queryEval.evaluateUnboxed(next);
debugPrintf("%s: %s%n\n", next.getName(), categories);
}
}