import { MetaElement } from '../metaElement'; import { State, StateType } from './state'; import { Transition } from './transition'; import { BusinessObjectType } from '../businessObjectType'; import { EffectType } from '../method/effectType'; import { IFSMJSON } from './iFSMJSON'; import { IFSMModelFormat } from './iFSMModelFormat'; import { Model } from '../../model'; /** * A [[FSM]] is a [[MetaElement]] representing a finite state machine. * * FSMs are **mutable** * * @specfield states : set of states within the FSM * @specfield transitions: set of transitions within the FSM */ export class FSM extends MetaElement { //================================ Representation ================================ private states : Map; private transitions: Map //================================= Constructor ================================== /** * Creates a new FSM f so that: * * f.name = FSM_ + attachedBOT.name && * * f.states = states || {} * * f.transitions = transitions || {} * @param attachedBOT Business Object Type to which the FSM will be attached * @param states Set of states of the FSM * @param transitions Set of transitions of the FSM */ public constructor(attachedBOT: BusinessObjectType, states: Array = new Array(), transitions: Array = new Array()) { super(`FSM_${attachedBOT.getName()}`); this.states = MetaElement.createMapFromArray(states); this.transitions = MetaElement.createMapFromArray(transitions); } //=================================== Getters ==================================== /** * @willreturn this.states */ public getStates(): Array { return Array.from(this.states.values()); } /** * @throws an error in case there is no state with id `stateId` in this * @willreturn otherwise, returns a state s so that s.id = state.id && s is in this.states * @param stateId Id of the state to be retrieved */ public getState(stateId: string): State { const elementToReturn = this.states.get(stateId); if(!elementToReturn) throw new Error(`[FSM.getState(string)]: State with id ${stateId} does not belong to FSM with id: ${this.getId()}`); return elementToReturn; } /** * @willreturn this.transitions */ public getTransitions(): Array { return Array.from(this.transitions.values()); } /** * @throws an error in case there is no transition with id `transitionId` in this * @willreturn otherwise, returns a transition so that t.id = transitionId && t is in this.transitions * @param transitionId */ public getTransition(transitionId: string): Transition { const elementToReturn = this.transitions.get(transitionId); if(!elementToReturn) throw new Error(`[FSM.getTransition(string)]: Transition with id ${transitionId} does not belong to FSM with id: ${this.getId()}`); return elementToReturn; } //=================================== Setters ==================================== /** * @modifies this * @effects this_post.states = this.states U { state } * @param state New state to add to this */ public addState(state: State): void { this.states.set(state.getId(), state); } /** * @modifies this * @effects this_post.transitions = this.transitions U { transition } * @param transition */ public addTransition(transition: Transition): void { this.transitions.set(transition.getId(), transition); } //================================ Static Methods ================================= /** * @willreturn A default FSM generated for a given [[BusinessObjectType]] * @param bot BusinessObjectType for which the FSM needs to be generated */ public static generateDefaultFSM(bot: BusinessObjectType): FSM { const initialState = new State('initial', StateType.INITIAL); const existingState = new State('existing', StateType.EXISTING); const finalState = new State('final', StateType.FINAL); const fsm = new FSM(bot); fsm.addState(initialState); fsm.addState(existingState); fsm.addState(finalState); //Creating Transitions const creatingMethods = bot.getMethods().filter(method => method.getEffectType() == EffectType.Create); const modifyingMethods = bot.getMethods().filter(method => method.getEffectType() == EffectType.Modify); const endingMethods = bot.getMethods().filter(method => method.getEffectType() == EffectType.End); for(const method of creatingMethods) { const tr = new Transition(initialState, existingState, method); fsm.addTransition(tr); } for(const method of modifyingMethods) { const tr = new Transition(existingState, existingState, method); fsm.addTransition(tr); } for(const method of endingMethods) { const tr = new Transition(existingState, finalState, method); fsm.addTransition(tr); } return fsm; } /** * @willreturn a new FSM that has the same name and same id as specified in modelElement (without loading states and transitions) * @param model model containing the business object type to which the fsm to be created is attached * @param modelElement a representation of the fsm in the exported model format */ public static loadFromModelFormat(model: Model, modelElement: IFSMModelFormat): FSM { const botName: string = modelElement.name.substring(4, modelElement.name.length); const linkedBot: BusinessObjectType = model.getBusinessObjectTypeFromName(botName); const fsm: FSM = new FSM(linkedBot); fsm.setId(modelElement.id); linkedBot.setFSM(fsm); return fsm; } //================================== Overriding ================================== public toJSON(): IFSMJSON { return { id : this.getId(), name : this.getName(), states : Array.from(this.states.values()).map(item => item.toJSON()), transitions: Array.from(this.transitions.values()).map(item => item.toJSON()) }; } public toModelFormat(): IFSMModelFormat { return { id : this.getId(), name : this.getName(), states : Array.from(this.states.values()).map(item => item.getId()), transitions: Array.from(this.transitions.values()).map(item => item.getId()) }; } };