import { BusinessObjectType, AttributeType, BusinessEventType, ExistenceDependency, FSM, State, Transition, Method, EventParticipantTable, IEventParticipantTableModelFormat, AttributeReadingTable, IAttributeReadingTableModelFormat, InstanceAccessRule, IInstanceAccessRuleModelFormat } from "../meta-model"; import { IModelPackage } from './iModelPackage'; import { readFileSync } from 'fs'; import { IAttributeTypeModelFormat, IDerivedOCLAttributeTypeModelFormat, DerivedOCLAttributeType, IDerivedComplexAttributeTypeModelFormat, DerivedComplexAttributeType } from "../meta-model/attribute"; import { EffectType } from "../meta-model/method/effectType"; import { IMethodModelFormat, IModifyingMethodModelFormat, ModifyingMethod } from "../meta-model/method"; import { IBusinessEventTypeModelFormat } from "../meta-model/businessEventType"; import { IStateModelFormat } from "../meta-model/fsm/state"; import { ITransitionModelFormat } from "../meta-model/fsm/transition"; import { IFSMModelFormat } from "../meta-model/fsm"; import { IBusinessObjectTypeModelFormat } from "../meta-model/businessObjectType"; import { IExistenceDependencyModelFormat } from "../meta-model/existenceDependency"; /** * A `Model` is an artifact that includes all the meta-elements described in B-MERODE (business object types, fsms, methods, etc.) * It also allows to export and import models. Furthermore, it can be used to evaluate the model's consistency. * * Models are **mutable** * * @specfield id (string) : unique id of the model * @specfield businessObjectTypes : set of [[BusinessObjectType]]s included in the model * @specfield attributeTypes : set of [[AttributeType]]s included in the model * @specfield businessEventTypes : set of [[BusinessEventType]]s included in the model * @specfield existenceDependencies: set of [[ExistenceDependency]] objects included in the model * @specfield finiteStateMachines : set of [[FSM]]s included in the model * @specfield states : set of [[State]]s included in the model's [[FSM]]s * @specfield transitions: set of [[Transition]]s included in the model's [[FSM]]s * @specfield methods: set of [[Method]]s inlcuded in the model * @specfield ept: [[EventParticipantTable]] applicable to the model * @specfield art: [[AttributeReadingTable]] applicable to the model * @specfield iars: set of [[InstanceAccessRule]]s applicable to the model */ export class Model { //================================ Representation ================================ private id : string; private businessObjectTypes : Map; private attributeTypes : Map; private businessEventTypes : Map; private existenceDependencies: Map; private finiteStateMachines : Map; private states : Map; private transitions : Map; private methods : Map; private eventParticipantTable: EventParticipantTable; private attributeReadingTable: AttributeReadingTable; private instanceAccessRules : Set; //================================= Constructor ================================== /** * Creates a new model m so that: * * m.id = id && * * m.businessObjectTypes = [] && * * m.attributeTypes = [] && * * m.businessEventTypes = [] && * * m.existenceDependencies = [] && * * m.finiteStateMachines = [] && * * m.states = [] && * * m.transitions = [] && * * m.methods = [] && * * m.ept = null && * * m.apt = null && * * m.instanceAccessRules = [] * @param id Unqiue ID of the model */ public constructor(id: string) { this.id = id; this.businessObjectTypes = new Map(); this.attributeTypes = new Map(); this.businessEventTypes = new Map(); this.existenceDependencies = new Map(); this.finiteStateMachines = new Map(); this.states = new Map(); this.transitions = new Map(); this.methods = new Map(); this.eventParticipantTable = null; this.attributeReadingTable = null; this.instanceAccessRules = new Set(); } /** * @throws An error in case there is no business object type in this, with botName as a name * @willreturn Otherwise, returns a business object type "bot" so that bot.name = botName && bot is in this * @param botName Name of the business object type to retrieve */ public getBusinessObjectTypeFromName(botName: string): BusinessObjectType { const bot = Array.from(this.businessObjectTypes.values()).filter(item => item.getName() == botName)[0]; if(!bot) throw new Error(`[Model.getBusinessObjectTypeFromName(string)]: No BOT with name ${botName} found in the model`); return bot; } /** * @modifies this * @effects this_post.ept = ept * @param ept EventParticipantTable to use for the current model */ public setEventParticipantTable(ept: EventParticipantTable): void { this.eventParticipantTable = ept; } /** * @modifies this * @effectts this_post.art = art * @param art AttributetReadingTable to use for the current model */ public setAttributeReadingTable(art: AttributeReadingTable): void { this.attributeReadingTable = art; } /** * @modifies this * @effects this_post.instanceAccessRules = this.instanceAccessRules U { iar } * @param iar InstanceAccessRule to add to the model */ public addInstanceAccessRule(iar: InstanceAccessRule): void { this.instanceAccessRules.add(iar); } /** * @modifies this * @effects this_post.attributeTypes = this.attributeTypes U { at } * @param at AttributeType to add to this */ public addAttributeType(at: AttributeType): void { this.attributeTypes.set(at.getId(), at); } /** * @modifies this * @effects this_post.businessEventTypes = this.businessEventTypes U { businessEventType } * @param businessEventType Business event type to add to this */ public addBusinessEventType(businessEventType: BusinessEventType): void { this.businessEventTypes.set(businessEventType.getId(), businessEventType); } /** * @modifies this * @effects this_post.methods = this.methods U { method } * @param method Method to add to this */ public addMethod(method: Method): void { this.methods.set(method.getId(), method); } /** * @modifies this * @effects this_post.businessObjectTypes = this.businessObjectTypes U { bot } * @param bot Business object type to add to this */ public addBusinessObjectType(bot: BusinessObjectType): void { this.businessObjectTypes.set(bot.getId(), bot); this.addFSM(bot.getFSM()); } /** * @modifies this * @effects this_post.states = this.states U { state } * @param state State to add to this */ private addState(state: State): void { this.states.set(state.getId(), state); } /** * @modifies this * @effects this_post.transitions = this.transitions U { transition } * @param transition Transition to add to this */ private addTransition(transition: Transition): void { this.transitions.set(transition.getId(), transition); } /** * @modifies this * @effects this_post.finiteStateMachines = this.finiteStateMachines U { fsm } * @param fsm FSM to add to this */ private addFSM(fsm: FSM): void { this.finiteStateMachines.set(fsm.getId(), fsm); for(const state of fsm.getStates()) this.addState(state); for(const transition of fsm.getTransitions()) this.addTransition(transition); } /** * @modifies this * @effects this_post.existenceDependencies = this.existenceDependencies U { ed } * @param ed Existence dependcy to add to this */ public addExistenceDependency(ed: ExistenceDependency): void { this.existenceDependencies.set(ed.getId(), ed); } /** * @modifies this * @effects this_post.ept = a default EPT generated for this in case this.ept is not defined * @willreturn a JSON that represents the serialized version of this */ public package(): IModelPackage { if(!this.eventParticipantTable) { this.eventParticipantTable = new EventParticipantTable(this); } if(!this.attributeReadingTable) { this.attributeReadingTable = new AttributeReadingTable(this); } return { id : this.id, attributeTypes : Array.from(this.attributeTypes.values()).map(item => item.toModelFormat()), businessEventTypes : Array.from(this.businessEventTypes.values()).map(item => item.toModelFormat()), methods : Array.from(this.methods.values()).map(item => item.toModelFormat()), states : Array.from(this.states.values()).map(item => item.toModelFormat()), transitions : Array.from(this.transitions.values()).map(item => item.toModelFormat()), finiteStateMachines : Array.from(this.finiteStateMachines.values()).map(item => item.toModelFormat()), businessObjectTypes : Array.from(this.businessObjectTypes.values()).map(item => item.toModelFormat()), existenceDependencies: Array.from(this.existenceDependencies.values()).map(item => item.toModelFormat()), eventParticipantTable: this.eventParticipantTable.toModelFormat(), attributeReadingTable: this.attributeReadingTable.toModelFormat(), instanceAccessRules : Array.from(this.instanceAccessRules).map(item => item.toModelFormat()) }; } /** * @willreturn a new model m that is imported from the serialized model format file located at filePath * @param filePath Path where the file containing the serialized model can be found */ public static loadFromFile(filePath: string): Model { const pkg: IModelPackage = JSON.parse(readFileSync(filePath, 'utf-8')); const model: Model = new Model(pkg.id); for(const attribute of pkg.attributeTypes) { model.addAttributeType(Model.loadAttributeTypeFromModel(attribute)); } for(const event of pkg.businessEventTypes) { model.addBusinessEventType(Model.loadBusinessEventTypesFromModel(event)); } for(const method of pkg.methods) { model.addMethod(Model.loadMethodFromModel(model, method)); } for(const state of pkg.states) { model.addState(Model.loadStateFromModel(state)); } for(const transition of pkg.transitions) { model.addTransition(Model.loadTransitionFromModel(model, transition)); } for(const bot of pkg.businessObjectTypes) { model.addBusinessObjectType(Model.loadBusinessObjectTypeFromModel(model, bot)); } for(const fsm of pkg.finiteStateMachines) { model.addFSM(Model.loadFsmFromModel(model, fsm)); } for(const ed of pkg.existenceDependencies) { model.addExistenceDependency(Model.loadExistenceDependencyFromModel(model, ed)); } model.setEventParticipantTable(Model.loadEventParticipantTableFromModel(model, pkg.eventParticipantTable)); model.attributeReadingTable = Model.loadAttributeReadingTableFromModel(model, pkg.attributeReadingTable); for(const iar of pkg.instanceAccessRules) { model.addInstanceAccessRule(Model.loadInstanceAccessRuleFromModel(model, iar)); } return model; } /** * @willreturn a new AttributeType imported from the serialized model format * @param modelElement attribute type in the serialized model format */ private static loadAttributeTypeFromModel(modelElement: IAttributeTypeModelFormat): AttributeType { if(modelElement.type == 'BASE') { const at = new AttributeType(modelElement.name, modelElement.valueType, modelElement.optional); at.setId(modelElement.id); return at; } else if (modelElement.type == 'DERIVED_OCL') { const element: IDerivedOCLAttributeTypeModelFormat = (modelElement); const at = new DerivedOCLAttributeType(element.name, element.valueType, element.oclRule, element.optional); at.setId(element.id); return at; } else if (modelElement.type == 'DERIVED_COMPLEX') { const element = (modelElement); const at = new DerivedComplexAttributeType(element.name, element.valueType, element.implementation, element.optional); at.setId(element.id); return at; } else { throw new Error('[AttributeType.fromModelFormat(IAttributeTypeModelFormat)]: Invalid Type of Attribute: ' + modelElement.type); } } /** * @willreturn a new BusinessEventType imported from the serialized model format. * @param modelElement business object type in the serialized model format. */ private static loadBusinessEventTypesFromModel(modelElement: IBusinessEventTypeModelFormat): BusinessEventType { const event: BusinessEventType = new BusinessEventType(modelElement.name); event.setId(modelElement.id); return event; } /** * @willreturn a new method imported from the serialized model format * @param model the model being loaded from a file * @param modelElement the method to import in the serialized model format */ private static loadMethodFromModel(model: Model, modelElement: IMethodModelFormat): Method { if(modelElement.effectType == EffectType.Modify) { const element: IModifyingMethodModelFormat = (modelElement); const modifiedAttribute = element.modifiedAttribute == 'N/A' ? null : model.attributeTypes.get(element.modifiedAttribute); const event: BusinessEventType = model.businessEventTypes.get(element.triggeringEvent); const parameters: Array = []; for(const param of element.parameters) parameters.push(model.attributeTypes.get(param)); const method: ModifyingMethod = new ModifyingMethod(element.name, event, element.ownershipType, parameters, modifiedAttribute); method.setId(element.id); return method; } else { const event: BusinessEventType = model.businessEventTypes.get(modelElement.triggeringEvent); const parameters: Array = []; for(const param of modelElement.parameters) parameters.push(model.attributeTypes.get(param)); const method: Method = new Method(modelElement.name, event, modelElement.ownershipType, modelElement.effectType, parameters); method.setId(modelElement.id); return method; } } /** * @willreturn a state loaded from the serialized model format. * @param modelElement state to be loaded, in the serialized model format. */ private static loadStateFromModel(modelElement: IStateModelFormat): State { const st: State = new State(modelElement.name, modelElement.type); st.setId(modelElement.id); return st; } /** * @willreturn a Transition loaded from the serialized model format * @param model Model being loaded from a file * @param modelElement Transition to be loaded, in the serialized model format. */ private static loadTransitionFromModel(model: Model, modelElement: ITransitionModelFormat): Transition { const origin: State = model.states.get(modelElement.origin); const destination: State = model.states.get(modelElement.destination); const triggeringMethod: Method = model.methods.get(modelElement.method); const tr: Transition = new Transition(origin, destination, triggeringMethod, modelElement.guard); tr.setId(modelElement.id); return tr; } /** * @willreturn a Business Object Type loaded from the serialized model format * @param model Model being loaded from a file * @param modelElement Business object type to be loaded, in serialzed model format */ private static loadBusinessObjectTypeFromModel(model: Model, modelElement: IBusinessObjectTypeModelFormat): BusinessObjectType { const bot: BusinessObjectType = new BusinessObjectType(modelElement.name, modelElement.participant); bot.setId(modelElement.id); for(const attribute of modelElement.attributes) bot.addAttribute(model.attributeTypes.get(attribute)); for(const method of modelElement.methods) { bot.addMethod(model.methods.get(method)); } return bot; } /** * @willreturn a FSM loaded from the serialized model format * @param model Model being loaded from a file * @param modelElement FSM to be loaded, in the serialized model format */ private static loadFsmFromModel(model: Model, modelElement: IFSMModelFormat): FSM { const emptyFSM: FSM = FSM.loadFromModelFormat(model, modelElement); for(const state of modelElement.states) emptyFSM.addState(model.states.get(state)); for(const transition of modelElement.transitions) emptyFSM.addTransition(model.transitions.get(transition)); return emptyFSM; } /** * @willreturn an ExistenceDependency loaded from the serialized model format * @param model Model being loaded from file * @param modelElement ExistenceDependency to load, in the serialized model format. */ private static loadExistenceDependencyFromModel(model: Model, modelElement: IExistenceDependencyModelFormat): ExistenceDependency { const master: BusinessObjectType = model.businessObjectTypes.get(modelElement.master); const dependent: BusinessObjectType = model.businessObjectTypes.get(modelElement.dependent); const ed: ExistenceDependency = new ExistenceDependency(modelElement.name, master, dependent, modelElement.type); ed.setId(modelElement.id); return ed; } private static loadEventParticipantTableFromModel(model: Model, modelElement: IEventParticipantTableModelFormat): EventParticipantTable { const ept = new EventParticipantTable(model); for(const singleAuth of modelElement.authorizations) { const evt = model.getBusinessEventType(singleAuth.businessEventType); const ptcp = model.getBusinessObjectType(singleAuth.participantType); if(singleAuth.allowed) ept.allow(evt, ptcp); else ept.deny(evt, ptcp); } return ept; } private static loadAttributeReadingTableFromModel(model: Model, modelElement: IAttributeReadingTableModelFormat): AttributeReadingTable { const art = new AttributeReadingTable(model); for(const singleauh of modelElement.authorizations) { const attr = model.getAttributeType(singleauh.attributeType); const ptcp = model.getBusinessObjectType(singleauh.participantType); if(singleauh.allowed) { art.allow(attr, ptcp); } else { art.deny(attr, ptcp); } } return art; } private static loadInstanceAccessRuleFromModel(model: Model, modelElement: IInstanceAccessRuleModelFormat): InstanceAccessRule { const bot = model.getBusinessObjectType(modelElement.businessObjectType); const ptcp = model.getBusinessObjectType(modelElement.participantType); const iar = new InstanceAccessRule(modelElement.operationTypes, bot, ptcp, modelElement.condition, modelElement.action); return iar; } public getBusinessObjectTypes(): Array { return Array.from(this.businessObjectTypes.values()); } public getAttributeType(id: string): AttributeType { const attr = this.attributeTypes.get(id); if(!attr) throw new Error(`[Model.getAttributeType(...)]: No AttributeType with id ${id} found in the model`); return attr; } public getBusinessObjectType(id: string): BusinessObjectType { const bot = this.businessObjectTypes.get(id); if(!bot) throw new Error(`[Model.getBusinessObjectType(...)]: There is no BOT with id ${id} in the model`); return bot; } public getBusinessEventTypes(): Array { return Array.from(this.businessEventTypes.values()); } public getBusinessEventType(id: string): BusinessEventType { const evt = this.businessEventTypes.get(id); if(!evt) throw new Error(`[Model.getBusinessEventType(string)]: There is no business event type with id ${id} in the model`); return evt; } }