Skip to content

@stekoe/ocl.js


@stekoe/ocl.js / IMetamodelProvider

Interface: IMetamodelProvider

Defined in: components/IMetamodelProvider.ts:32

Interface for pluggable metamodel providers.

Implement this interface to integrate ocl.js with runtime metamodels where type hierarchies are not based on JavaScript prototype chains.

This is particularly useful for:

  • Modeling platforms with dynamic metamodels (e.g., EMF/Ecore-style)
  • Systems where objects are plain data and type information is external
  • Runtime environments where types and relationships evolve dynamically

Example

typescript
const provider: IMetamodelProvider = {
  getTypeName(obj) {
    return obj._type; // or lookup in external metamodel registry
  },
  isKindOf(obj, typeName) {
    const objType = this.getTypeName(obj);
    return metamodel.isSubtypeOf(objType, typeName);
  },
  isTypeOf(obj, typeName) {
    return this.getTypeName(obj) === typeName;
  }
};

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

Methods

getTypeName()

ts
getTypeName(obj): string | undefined;

Defined in: components/IMetamodelProvider.ts:39

Returns the type name for a given object.

Parameters

ParameterTypeDescription
objunknownThe object to determine the type of

Returns

string | undefined

The type name as a string, or undefined if the type cannot be determined


isKindOf()

ts
isKindOf(obj, typeName): boolean;

Defined in: components/IMetamodelProvider.ts:52

Checks if an object is an instance of the specified type or any of its supertypes.

This method should implement the metamodel's inheritance/subtype relationship. For example, if Person extends Entity, then isKindOf(personObj, 'Entity') should return true.

Parameters

ParameterTypeDescription
objunknownThe object to check
typeNamestringThe type name to check against (including supertypes)

Returns

boolean

true if the object is an instance of typeName or any supertype


isTypeOf()

ts
isTypeOf(obj, typeName): boolean;

Defined in: components/IMetamodelProvider.ts:64

Checks if an object is exactly of the specified type (no inheritance).

Unlike isKindOf, this method should only return true if the object's direct type matches the specified type name exactly.

Parameters

ParameterTypeDescription
objunknownThe object to check
typeNamestringThe exact type name to match

Returns

boolean

true if the object is exactly of the specified type

Released under the MIT License.