Question

I am writing a Java program for a course that takes a UML class diagram, which is a metamodel, as input and allows the user to create diagrams of the type specified in the metamodel. The user should then be able to model instances of what this diagram modeled.

As such, I am parsing the generated XML file that represents the UML and extract all classes and associations. So far, so good.

But then there are constraints which I'll need to know about and warn when the user violates them. However, I have no idea how to do the OCL parsing. I have looked into dresden OCL but I'm not sure if this is what I want, as I need to parse the OCL during runtime, as opposed to importing a model and generate java code from OCL using eclipse.

As such, I'd really appreciate it if someone could point me to a way of parsing OCL and extracting its basic syntax.

Best Regards, João Fernandes

Was it helpful?

Solution

Eclipse OCL project provides standalone usage (just a java program out of Eclipse) and there is some documentation and examples about how to do it.

Specifically, see the following links about:

Some Jave API usage example, taken from the help, to expose how invariants and queries can be created and evaluated:

OCL ocl = OCL.newInstance(new PivotEnvironmentFactory());
OCLHelper helper = ocl.createOCLHelper(EXTLibraryPackage.Literals.LIBRARY);
ExpressionInOCL invariant = helper.createInvariant(
    "books->forAll(b1, b2 | b1 <> b2 implies b1.title <> b2.title)");
ExpressionInOCL query = helper.createQuery(
    "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();  // hypothetical source of libraries
// only print the set of book categories for valid libraries
for (Library next : libraries) {
   if (constraintEval.check(next)) {
      // the OCL result type of our query expression is Set(BookCategory)
      @SuppressWarnings("unchecked")
      Set<BookCategory> categories = (Set<BookCategory>) queryEval.evaluate(next);

      System.out.printf("%s: %s%n", next.getName(), categories);
   }
}

OTHER TIPS

Take a look at the Eclipse OCL component (it offers a validator feature that evaluates whether a model/instance satisfies the well-formedness rules of the metamodel/model) and the USE tool (which was created specifically with the purpose to let people play and learn with OCL by letting them evaluate OCL expressions on sets of instances to see the effect of the expression). Both are open source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top