I often complain about the lack of good tools for the Object Constraint Language. Instead of having more and better OCL tools, we see the opposite, there are fewer ones as some promising tools just died (most of them were research prototypes which have a high mortality rate). That’s why I’m so happy to talk today about a new OCL tool that enables you to write and evaluate OCL expressions in JavaScript.

OCL.js is an open-source tool that implements the OCL standard in JavaScript. The tool has been developed by Stephan Köninger and its also available as a npm package. It is designed to run either in a node environment or in the browser. As alternative, you can also download the ocl.min.js file from GitHub and include that to your code. In the tool website, you can see plenty of examples of how to use the tool.

For instance, this code excerpt shows you how to create a new OCL expression, add it to the OCL engine and evaluate over a given object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { OCLEngine } from "@stekoe/ocl.js"
 
// Define OCL rule
const myOclExpression = `
    context Person
        inv: self.parents->forAll(p | p <> self)
`;
 
// Instantiate the OclEngine here
const oclEngine = OclEngine.create();
 
// Add your first OCL expression here
oclEngine.addOclExpression(myOclExpression);
 
// Evaluate an object obj against all know OCL expressions
const oclResult = oclEngine.evaluate(obj);

Note that the objects are standard JavaScript objects. With OCL.js you can effectively create and instantiate your domain model and then evaluate OCL expressions on it. It’s as simple as writing something like this:

1
2
3
4
let company = new Company();
company.employee.push(new Person());
 
const oclResult = oclEngine.evaluate(company);

The implementation does not cover the full OCL language but it’s enough to be able to play with OCL in JavaScript and get an idea of all the interesting things you could do with it. And the tool is open source, so you can always take it as a starting point to build a more powerful engine. The more modeling tools are ported to other languages beyond Java (which until now has always been the default option with very few exceptions, e.g. PyEcore or Ecore.js) the better.

So, what are you waiting for? Start enriching your JavaScript programs with all the power of OCL!

Want to build better software faster?

Want to build better software faster?

Read about the latest trends on software modeling and low-code development

You have Successfully Subscribed!

Pin It on Pinterest

Share This