import { Method } from '..'; import { AttributeType, DerivedComplexAttributeType, DerivedOCLAttributeType } from '../../attribute'; import { OwnershipType } from '../ownershipType'; import { EffectType } from '../effectType'; import { BusinessEventType } from '../../businessEventType'; import { IModifyingMethodJSON } from './iModifyingMethodJSON'; import { IModifyingMethodModelFormat } from './iModifyingMethodModelFormat'; /** * A `ModifyingMethod` is a [[Method]] that modifies the value of a business object's attribute. * * ModifyingMethods are **mutable** * * @specfield modifiedAttribute: attribute whose value can be modified when executing this */ export class ModifyingMethod extends Method { //================================ Representation ================================ private modifiedAttribute: AttributeType; //================================= Constructor ================================== /** * Creates a new ModifyingMethod m so that: * * this.name = name && * * this.triggeringEvent = triggeringEvent && * * this.ownershipType = ownershipType && * * this.effectType = EffectType.Modify && * * this.modifiedAttribute = modifiedAttribute && * @param name Name of the method * @param ownershipType Type of ownership of the method from the business object type * @param parameters Parameters to provide to execute the method * @param modifiedAttribute [[AttributeType]] whose instanced value can be modified when executing this */ public constructor(name: string, triggeringEvent: BusinessEventType, ownershipType: OwnershipType, parameters: Array = new Array(), modifiedAttribute?: AttributeType) { super(name, triggeringEvent, ownershipType, EffectType.Modify, parameters); this.modifiedAttribute = modifiedAttribute || null; } //=================================== Getters ==================================== /** * @willreturn this.modifiedAttribute */ public getModifiedAttribute(): AttributeType { return this.modifiedAttribute; } //================================== Overriding ================================== public toJSON(): IModifyingMethodJSON { const modifiedAttribute = this.modifiedAttribute ? this.modifiedAttribute.toJSON() : 'N/A'; return { id : this.getId(), name : this.getName(), triggeringEvent : this.getTriggeringEvent().toJSON(), ownershipType : this.getOwnershipType(), effectType : this.getEffectType(), parameters : Array.from(this.getParameters()).map(item => item.toJSON()), modifiedAttribute: modifiedAttribute }; } public toModelFormat(): IModifyingMethodModelFormat { const modifiedAttribute = this.modifiedAttribute ? this.modifiedAttribute.getId() : 'N/A'; return { id : this.getId(), name : this.getName(), triggeringEvent : this.getTriggeringEvent().getId(), ownershipType : this.getOwnershipType(), effectType : this.getEffectType(), parameters : this.getParameters().map(item => item.getId()), modifiedAttribute: modifiedAttribute }; } public isValid(): boolean { if(!super.isValid()) return false; for(const param of this.getParameters()) { if(param instanceof DerivedOCLAttributeType) return false; } return true; } }