Skip to content

@stekoe/ocl.js


@stekoe/ocl.js / OclEngine

Class: OclEngine

Defined in: components/OclEngine.ts:28

The OclEngine class is the main entry point to the OCL.js library.

This class allows to add new OCL expressions as well as evaluating given objects agains the saved OCL expressions.

Constructors

Constructor

ts
new OclEngine(): OclEngine;

Returns

OclEngine

Properties

PropertyModifierTypeDefault valueDefined in
Parserstatictypeof OclParserOclParsercomponents/OclEngine.ts:33
Utilsstatictypeof UtilsUtilscomponents/OclEngine.ts:32
versionstaticanyundefinedcomponents/OclEngine.ts:30

Methods

_inferType()

ts
_inferType(obj): string | undefined;

Defined in: components/OclEngine.ts:225

Parameters

ParameterType
objunknown

Returns

string | undefined


addOclExpression()

ts
addOclExpression(oclExpression, labels?): OclEngine;

Defined in: components/OclEngine.ts:166

Register a new OCL expression.

Each OCL expression is added to a map of OCL expressions where the context is the key: <pre> context Person inv: ... </pre>

Will be sorted into the map using "Person" as key. This allows faster lookup of OCL rules that should be applied when running OclEngine.evaluate.

Parameters

ParameterTypeDefault valueDescription
oclExpressionstringundefinedThe OCL expression as string. It will be parsed and added to the list of existing OCL expressions.
labelsstring[][]Add one or multiple lables to an OCL expression. Labels can be used to address particular expressions for evaluation.

Returns

OclEngine

the current OclEngine object for chaining

Throws

ParserError


addOclExpressions()

ts
addOclExpressions(oclExpressions, labels?): OclEngine;

Defined in: components/OclEngine.ts:143

Register a list of OCL expressions.

Parameters

ParameterTypeDefault valueDescription
oclExpressionsstring[]undefinedThe OCL expressions as string list. It will be parsed and added to the list of existing OCL expressions.
labelsstring[][]Add one or multiple lables to an OCL expression. Labels can be used to address particular expressions for evaluation.

Returns

OclEngine

the current OclEngine object for chaining

Throws

ParserError


clearAllOclExpressions()

ts
clearAllOclExpressions(): OclEngine;

Defined in: components/OclEngine.ts:194

Clears all registered OclExpressions from the current OclEngine instance.

Returns

OclEngine


createQuery()

ts
createQuery(oclExpression): Expression;

Defined in: components/OclEngine.ts:239

Specify a OCL query that can be used to extract information from an object.

Parameters

ParameterTypeDescription
oclExpressionstringAn OCL expression that is used to create a query of

Returns

Expression

Expression The AST of the parsed oclExpresion

Example

ts
oclEngine.createQuery('self.children.name') // Returns an array of the children's names

evaluate()

ts
evaluate(obj, labels?): OclResult;

Defined in: components/OclEngine.ts:207

This function actually evaluates the given object against the registered OCL expressions.

Parameters

ParameterTypeDefault valueDescription
objunknownundefinedThe object which is going to be evaluated against registered OCL expressions.
labelsstring[][]An array of labels that address expressions that should be evaluated.

Returns

OclResult

a result object, which contains the actual result and other info

See

OclResult


evaluateQuery()

ts
evaluateQuery(obj, oclExpression): unknown;

Defined in: components/OclEngine.ts:250

Execute a given OCL query on a given object.

Parameters

ParameterTypeDescription
objunknownThe object to query
oclExpressionExpressionThe query to run on the given object

Returns

unknown

the result of the provided query.


getMetamodelProvider()

ts
getMetamodelProvider(): IMetamodelProvider | undefined;

Defined in: components/OclEngine.ts:99

Get the currently configured metamodel provider, if any.

Returns

IMetamodelProvider | undefined

The current IMetamodelProvider or undefined if not set


registerEnum()

ts
registerEnum(name, values): OclEngine;

Defined in: components/OclEngine.ts:129

Register an enumeration that can be used in OCL expressions.

Parameters

ParameterTypeDescription
namestringThe name of the enumeration (e.g., 'Color')
valuesRecord<string, unknown>An object mapping enum member names to values (e.g., { RED: 0, GREEN: 1 })

Returns

OclEngine

the current OclEngine object for chaining


registerTypes()

ts
registerTypes(types): OclEngine;

Defined in: components/OclEngine.ts:117

Register additional object types in the engine which can be used for instanceof checking.

The following built-in JavaScript types are already registered:

  • Array
  • Boolean
  • Function
  • Number
  • Object
  • String

Parameters

ParameterTypeDescription
typesTypeRegistryA map of type names to constructor functions

Returns

OclEngine

the current OclEngine object for chaining


removeOclExpression()

ts
removeOclExpression(oclExpression): OclEngine;

Defined in: components/OclEngine.ts:183

Removes the given oclExpression if it has been registered to the OclEngine instance.

Parameters

ParameterTypeDescription
oclExpressionstring-

Returns

OclEngine


setMetamodelProvider()

ts
setMetamodelProvider(provider): OclEngine;

Defined in: components/OclEngine.ts:89

Set a metamodel provider for advanced type resolution and hierarchy support.

Use this method to integrate ocl.js with runtime metamodels where type hierarchies are not based on JavaScript prototype chains. The provider enables proper support for oclIsKindOf, oclIsTypeOf, and context type matching against external metamodel definitions.

Parameters

ParameterTypeDescription
providerIMetamodelProviderAn implementation of IMetamodelProvider that handles type resolution

Returns

OclEngine

the current OclEngine object for chaining

Example

typescript
const provider: IMetamodelProvider = {
  getTypeName(obj) { return obj._type; },
  isKindOf(obj, typeName) { return metamodel.isSubtypeOf(this.getTypeName(obj), typeName); },
  isTypeOf(obj, typeName) { return this.getTypeName(obj) === typeName; }
};

engine.setMetamodelProvider(provider)
  .addOclExpression('context Entity inv: self.oclIsKindOf(NamedElement)');

setTypeDeterminer()

ts
setTypeDeterminer(fn): OclEngine;

Defined in: components/OclEngine.ts:59

Set a TypeDeterminer function that receives an object and returns the type of the object.

This is a convenience method for simple type name resolution. For more advanced metamodel integration (including type hierarchy support), use setMetamodelProvider.

Parameters

ParameterTypeDescription
fn(obj) => stringA callback function that is used to determine the type of the object that is passed into the callback function

Returns

OclEngine

the current OclEngine object for chaining


create()

ts
static create(): OclEngine;

Defined in: components/OclEngine.ts:46

Static create method.

Returns

OclEngine

a fresh new instance of the OclEngine

Released under the MIT License.