Installation
bash
npm install @stekoe/ocl.js --saveOCL.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:
result— the actual boolean result of the evaluation runnamesOfFailedInvs— names of failed invariants, oranonymousif none was providedevaluatedContexts— allContextExpressionsthat were evaluated