It's not clear to me how to use the API of this project. Specifically I'm having problems of building up the "instance" models to be checked.
I'm very interested in integrating OCL into a metamodeling framework, but after playing around with the ocl.js I can't get it to work. I've included a snippet, any pointers are welcome.
(I'm using the structure below since I'm intending to generate the models to be evaluated on the fly based on the models in our framework.)
global.window = undefined; // Needed to avoid exception at import..
const OCLEngine = require("@stekoe/ocl.js");
function OCLObject(type, attributes, pointers, sets) {
this.type = type;
this.attributes = attributes;
this.pointers = pointers;
this.sets = sets;
}
// Super-classes
const Person = new OCLObject('Person', {name: 'Person', isMale: true}, {dad: null, mom: null}, {});
const Family = new OCLObject('Family', {name: 'Family', address: ''}, {}, {familyMembers: []});
// "Instances"
const aFamily = Object.assign(Object.create(Family), {
attributes: {name: 'Smiths', address: 'Main Street'},
});
const dad = Object.assign(Object.create(Person), {
attributes: {name: 'Joe', isMale: true},
});
const mom = Object.assign(Object.create(Person), {
attributes: {name: 'Jen', isMale: false},
});
const son = Object.assign(Object.create(Person), {
attributes: {name: 'JoeJr', isMale: true},
pointers: {dad: dad, mom: mom}
});
const daughter = Object.assign(Object.create(Person), {
attributes: {name: 'JenJr', isMale: false},
pointers: {dad: dad, mom: mom}
});
const derp = Object.assign(Object.create(Person), {
attributes: {name: 'derp', isMale: false},
pointers: {mom: dad, dad: mom}
});
Object.assign(aFamily, {sets: {familyMembers: [dad, mom, son, daughter, derp]}});
// Check the instance model
const oclEngine = new OCLEngine();
const dadIsMale = `
-- Check that dad is indeed a male
context Person
inv: self.pointers.dad->notEmpty() implies self.pointers.dad.isMale = true
`;
oclEngine.addOclExpression(dadIsMale);
var res = oclEngine.evaluate(aFamily);
console.log(res); // t { namesOfFailedInvs: [], evaluatedContexts: [], result: true }
res = oclEngine.evaluate(son);
console.log(res); // t { namesOfFailedInvs: [], evaluatedContexts: [], result: true }
res = oclEngine.evaluate(derp);
console.log(res); // t { namesOfFailedInvs: [], evaluatedContexts: [], result: true }