import { InvariantExpression } from '../InvariantExpression' ; import { DefExpression } from '../DefExpression' ; import { OclExecutionContext } from '../../OclExecutionContext' ; import { ContextExpression } from './ContextExpression' ; oclExpression export class ClassifierContextExpression extends ContextExpression { private readonly invs : Array < InvariantExpression > ; private readonly defs : Array < DefExpression > ; constructor ( targetType , rules ) { super ( ) ; if ( ! ( rules instanceof Array ) ) { rules = [ rules ] ; } this . targetType = targetType ; this . invs = rules . filter ( i => i instanceof InvariantExpression ) ; this . defs = rules . filter ( i => i instanceof DefExpression ) ; } getInvs ( ) { return this . invs ; } getDefs ( ) { return this . defs ; } accept ( visitor ) { const accept = super . accept ( visitor ) ; if ( accept === false ) { return false ; } else { const visitorTargetType = visitor . getRegisteredType ( visitor . getTargetTypeName ( ) ) || visitor . getTargetTypeName ( ) ; const expressionTargetType = visitor . getRegisteredType ( this . targetType ) || this . targetType ; if ( typeof visitorTargetType === 'string' || typeof expressionTargetType === 'string' ) { return this . targetType === visitor . getTargetTypeName ( ) ; } else { return visitorTargetType instanceof expressionTargetType || visitorTargetType === expressionTargetType ; } } } evaluate ( visitor ) { super . evaluate ( visitor ) ; if ( this . accept ( visitor ) ) { this . getDefs ( ) . forEach ( def => def . evaluate ( visitor ) ) ; const invs = this . getInvs ( ) ; return ! invs . map ( inv => { const evaluationResult = inv . evaluate ( visitor ) ; if ( evaluationResult === false ) { visitor . addFailedInvariant ( inv ) ; } return evaluationResult ; } ) . some ( inv => inv === false ) ; } } }