import * as pkg from '../../package.json' ; import { OclParser } from './parser/OclParser' ; import { Utils } from './Utils' ; import { OclExecutionContext } from './OclExecutionContext' ; import { Expression , PackageDeclaration } from './expressions' ; import { OclResult } from './OclResult' ; /**
 * 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.
 */ export class OclEngine { static version = ( pkg as any ) . version ; static Utils = Utils ; static Parser = OclParser ; private packageDeclarations : Array < PackageDeclaration > = [ ] ; private typeDeterminerFn : ( obj : any ) => string ; private registeredTypes : any = OclParser . registeredTypes ; private registeredEnums : any = OclParser . registeredEnums ; returns static create ( ) { return new OclEngine ( ) ; } param fn setTypeDeterminer ( fn ) { if ( typeof fn === 'function' ) { OclEngine . Utils . typeDeterminerFn = fn ; } } param types registerTypes ( types ) { this . registeredTypes = { ... this . registeredTypes , ... types } ; OclParser . registeredTypes = this . registeredTypes ; } registerEnum ( name , values ) { this . registeredEnums = { ... this . registeredEnums , [ name ] : values } ; OclParser . registeredEnums = this . registeredEnums ; } param oclExpressions returns param labels throws addOclExpressions ( oclExpressions , labels = [ ] ) { oclExpressions . forEach ( oclExpression => this . addOclExpression ( oclExpression , labels ) ) ; return this ; } param oclExpression param labels returns throws addOclExpression ( oclExpression , labels = [ ] ) { try { const parsedExpression = OclEngine . Parser . parse ( oclExpression , labels , this . registeredTypes ) ; this . packageDeclarations . push ( parsedExpression ) ; } catch ( e ) { e . oclExpression = oclExpression ; throw e ; } return this ; } param oclExpression removeOclExpression ( oclExpression ) { this . packageDeclarations = this . packageDeclarations . filter ( packageDeclaration => { return Utils . hashCode ( packageDeclaration . getRawOclExpression ( ) ) !== Utils . hashCode ( oclExpression ) ; } ) ; return this ; } /**
     * Clears all registered OclExpressions from the current OclEngine instance.
     */ clearAllOclExpressions ( ) { this . packageDeclarations . length = 0 ; return this ; } param obj param labels returns see OclResult evaluate ( obj , labels = [ ] ) { const visitor = new OclExecutionContext ( obj , Array . isArray ( labels ) ? labels : [ labels ] ) ; visitor . registerTypes ( this . registeredTypes ) ; visitor . setRegisteredEnumerations ( this . registeredEnums ) ; this . packageDeclarations . forEach ( e => e . evaluate ( visitor ) ) ; return new OclResult ( visitor . getFailedInvariants ( ) . map ( inv => inv . getName ( ) ) , visitor . getEvaluatedContexts ( ) ) ; } _inferType ( obj ) { return Utils . getClassName ( obj ) ; } example param oclExpression returns createQuery ( oclExpression ) { return OclEngine . Parser . parseQuery ( oclExpression , this . registeredTypes ) ; } param obj param oclExpression returns evaluateQuery ( obj , oclExpression ) { const visitor = new OclExecutionContext ( obj ) ; visitor . registerTypes ( this . registeredTypes ) ; return oclExpression . evaluate ( visitor ) ; } }