@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
new OclEngine(): OclEngine;Returns
OclEngine
Properties
| Property | Modifier | Type | Default value | Defined in |
|---|---|---|---|---|
Parser | static | typeof OclParser | OclParser | components/OclEngine.ts:33 |
Utils | static | typeof Utils | Utils | components/OclEngine.ts:32 |
version | static | any | undefined | components/OclEngine.ts:30 |
Methods
_inferType()
_inferType(obj): string | undefined;Defined in: components/OclEngine.ts:225
Parameters
| Parameter | Type |
|---|---|
obj | unknown |
Returns
string | undefined
addOclExpression()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
oclExpression | string | undefined | The OCL expression as string. It will be parsed and added to the list of existing OCL expressions. |
labels | string[] | [] | 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()
addOclExpressions(oclExpressions, labels?): OclEngine;Defined in: components/OclEngine.ts:143
Register a list of OCL expressions.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
oclExpressions | string[] | undefined | The OCL expressions as string list. It will be parsed and added to the list of existing OCL expressions. |
labels | string[] | [] | 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()
clearAllOclExpressions(): OclEngine;Defined in: components/OclEngine.ts:194
Clears all registered OclExpressions from the current OclEngine instance.
Returns
OclEngine
createQuery()
createQuery(oclExpression): Expression;Defined in: components/OclEngine.ts:239
Specify a OCL query that can be used to extract information from an object.
Parameters
| Parameter | Type | Description |
|---|---|---|
oclExpression | string | An OCL expression that is used to create a query of |
Returns
Expression
Expression The AST of the parsed oclExpresion
Example
oclEngine.createQuery('self.children.name') // Returns an array of the children's namesevaluate()
evaluate(obj, labels?): OclResult;Defined in: components/OclEngine.ts:207
This function actually evaluates the given object against the registered OCL expressions.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
obj | unknown | undefined | The object which is going to be evaluated against registered OCL expressions. |
labels | string[] | [] | 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()
evaluateQuery(obj, oclExpression): unknown;Defined in: components/OclEngine.ts:250
Execute a given OCL query on a given object.
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | unknown | The object to query |
oclExpression | Expression | The query to run on the given object |
Returns
unknown
the result of the provided query.
getMetamodelProvider()
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()
registerEnum(name, values): OclEngine;Defined in: components/OclEngine.ts:129
Register an enumeration that can be used in OCL expressions.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the enumeration (e.g., 'Color') |
values | Record<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()
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
| Parameter | Type | Description |
|---|---|---|
types | TypeRegistry | A map of type names to constructor functions |
Returns
OclEngine
the current OclEngine object for chaining
removeOclExpression()
removeOclExpression(oclExpression): OclEngine;Defined in: components/OclEngine.ts:183
Removes the given oclExpression if it has been registered to the OclEngine instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
oclExpression | string | - |
Returns
OclEngine
setMetamodelProvider()
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
| Parameter | Type | Description |
|---|---|---|
provider | IMetamodelProvider | An implementation of IMetamodelProvider that handles type resolution |
Returns
OclEngine
the current OclEngine object for chaining
Example
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()
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
| Parameter | Type | Description |
|---|---|---|
fn | (obj) => string | A 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()
static create(): OclEngine;Defined in: components/OclEngine.ts:46
Static create method.
Returns
OclEngine
a fresh new instance of the OclEngine