Skip to content

Installation

bash
npm install @stekoe/ocl.js --save

OCL.js is entirely written in TypeScript and is published to npm. It can be installed with npm or yarn and is designed to run in Node.js or in the browser.

Basic Usage

typescript
import { OclEngine } from '@stekoe/ocl.js';

class Person {
  private parents = [];
}

// Define OCL rule
const myOclExpression = `
    context Person
        inv: self.parents->forAll(p | p <> self)
`;

// Instantiate the OclEngine
const oclEngine = OclEngine.create();

// Add your first OCL expression
oclEngine.addOclExpression(myOclExpression);

// Evaluate an object against all known OCL expressions
const oclResult = oclEngine.evaluate(new Person());

// Prints 'true' to console!
console.log(oclResult.getResult());
javascript
const OclEngine = require("@stekoe/ocl.js").OclEngine;

// A simple class that represents a person
class Person {
  constructor() {
    this.parents = [];
  }
}

// Define OCL rule
const myOclExpression = `
    context Person
        inv: self.parents->forAll(p | p <> self)
`;

// Instantiate the OclEngine
const oclEngine = OclEngine.create();

// Add your first OCL expression
oclEngine.addOclExpression(myOclExpression);

// Evaluate an object against all known OCL expressions
const oclResult = oclEngine.evaluate(new Person());

// Prints 'true' to console!
console.log(oclResult.getResult());

When adding OCL.js via npm, you can start using it by importing the OclEngine provided by @stekoe/ocl.js.

The resulting oclResult object contains three fields:

  1. result — the actual boolean result of the evaluation run
  2. namesOfFailedInvs — names of failed invariants, or anonymous if none was provided
  3. evaluatedContexts — all ContextExpressions that were evaluated

Released under the MIT License.