import { MetaElement, Method } from '..'; import { AttributeType } from '..'; import { IBusinessObjectTypeJSON } from './iBusinessObjectTypeJSON'; import { FSM } from '../fsm'; import { IBusinessObjectTypeModelFormat } from './iBusinessObjectTypeModelFormat'; /** * A `BusinessObjectType` represents a type of business object. * * BusinessObjectTypes are **mutable** * * @specfield participant (boolean): determines whether or not the business object type represents a participant type * @specfield attributes : set of [[AttributeType]] held in the business object type * @specfield masters : set of master [[BusinessObjectType]]s * @specfield fsm ([[FSM]]) : finite state machine attached to the business object type */ export class BusinessObjectType extends MetaElement { //================================ Representation ================================ private participant : boolean; private attributes : Map; private masters : Set; private methods : Map; private fsm : FSM; private hasDefaultFSM: boolean; //================================= Constructor ================================== /** * Creates a new BusinessObjectType bot such that: * * this.name = name && * * this.participant = participant && * * this.attributes = attributes && * * this.masters = masters && * * this.methods = methods && * * this.fsm = a default FSM generated for this * @param name Name of the business object type * @param participant Defines whether or not the bot represents a participant type * @param attributes Defines the data attributes held in the business object type * @param masters Set of master business object types * @param methods Set of methods added to this */ public constructor(name: string, participant: boolean, attributes: Set = new Set(), masters: Set = new Set(), methods: Set = new Set()) { super(name); this.participant = participant; this.attributes = MetaElement.createMapFromArray(Array.from(attributes)); this.masters = masters; this.methods = MetaElement.createMapFromArray(Array.from(methods)); this.fsm = FSM.generateDefaultFSM(this); this.hasDefaultFSM = true; } //=================================== Getters ==================================== /** * @willreturn this.participant */ public isParticipant(): boolean { return this.participant; } /** * @willreturn this.attributes */ public getAttributes(): Array { return Array.from(this.attributes.values()); } /** * @willreturn this.masters */ public getMasters(): Set { return this.masters; } /** * @throws an Error if attributeType with id attributeId is not part of this * @willreturn otherwise, retrurns an attribute type a so that a is in this.attributes and a.id = attributeId * @param attributeId ID of the attribute to be retrieved */ public getAttribute(attributeId: string): AttributeType { const elementToReturn = this.attributes.get(attributeId); if(!elementToReturn) throw new Error(`[BusinessObjectType.getAttribute(string)]: Attribute with id ${attributeId} is not available for BusinessObjectType ${this.getName()}`); return elementToReturn; } /** * @willreturn this.methods */ public getMethods(): Array { return Array.from(this.methods.values()); } /** * @throws an error if there is method with methodId as id that belongs to this * @willreturn otherwise, returns a method m so that m is in this.methods and m.id = methodId; * @param methodId Id of the method to be retrieved */ public getMethod(methodId: string): Method { if(!this.methods.has(methodId)) throw new Error(`[BusinessObjectType.getMethod(string)]: Method with id ${methodId} does not exist in business object type ${this.getName()} (${this.getId()})`); return this.methods.get(methodId); } /** * @willreturn this.fsm */ public getFSM(): FSM { return this.fsm; } //=================================== Setters ==================================== /** * @modifies this * @effects this_post.attributes = this.attributes U { attributeType } * @param attributeType attribute type to add to this */ public addAttribute(attributeType: AttributeType): void { this.attributes.set(attributeType.getId(), attributeType); } /** * @modifies this * @effects this_post.masters = this.masters U { master } * @param master Master Business object type to add for this */ public addMaster(master: BusinessObjectType): void { this.masters.add(master); } /** * @modifies this * @effects this_post.methods = this.methods U { method } * @param method method to be added to this.methods */ public addMethod(method: Method): void { this.methods.set(method.getId(), method); if(this.hasDefaultFSM) this.fsm = FSM.generateDefaultFSM(this); } /** * @modifies this * @effects this_post.fsm = fsm * @param fsm new FSM to be assigned to this */ public setFSM(fsm: FSM): void { this.fsm = fsm; this.hasDefaultFSM = false; } //================================== Overriding ================================== public isValid(): boolean { return true; } public toJSON(): IBusinessObjectTypeJSON { return { id : this.getId(), name : this.getName(), participant: this.participant, attributes : Array.from(this.attributes.values()).map(item => item.toJSON()), masters : Array.from(this.masters).map(item => item.toJSON()), methods : Array.from(this.methods.values()).map(item => item.toJSON()), fsm : this.fsm.toJSON() }; } public toModelFormat(): IBusinessObjectTypeModelFormat { return { id : this.getId(), name : this.getName(), participant: this.participant, attributes : Array.from(this.attributes.values()).map(item => item.getId()), masters : Array.from(this.masters).map(item => item.getId()), methods : Array.from(this.methods.values()).map(item => item.getId()), fsm : this.fsm.getId() }; } };