首頁 »
2006/03/13

客製化ECO-OCL

Delphi and Custom OCL Operations I always liked TStringList.CommaText, so I decided to try and implement a “commaText“ custom OCL Operation for Collections... I couldn't believe how easy it was going to be until I tried: here it is:

unit Borland.Eco.Ocl.Support.CommaText; interface uses Borland.Eco.Ocl.Support, Borland.Eco.ObjectRepresentation, Borland.Eco.UmlRt, Borland.Eco.Handles, Borland.Eco.Services, Borland.Eco.Subscription; type CommaText = class (OclOperationBase) strict protected procedure Init; override; public constructor Create (ES : EcoSpace); procedure Evaluate(oclParameters: IOclOperationParameters); override; class procedure Install (ES: EcoSpace); strict private MyEcoSpace: EcoSpace; end; implementation { CommaText } constructor CommaText.Create(ES: EcoSpace); begin inherited Create; //will be necessary for evaluation of the default string representation of collection //elements MyEcoSpace := ES; end; procedure CommaText.Evaluate(oclParameters: IOclOperationParameters); var ElemCol: IElementCollection; Elem: IElement; Res : String; OclService: IOclService; SB: StringBuilder; begin inherited; //gimme the collection ElemCol := oclParameters.Values[0].Element.GetAsCollection; //gimme the OclService as welll OclService := MyEcoSpace.GetEcoService(typeof(IOclService)) as IOclService; //for each element in the collection, get me the default string representation SB := StringBuilder.Create; for Elem in ElemCol do SB.Append(OclService.Evaluate(Elem,'self.AsString').AsObject.ToString+','); Res := SB.ToString; if Length(Res)>0 then Delete (Res,Length(Res),1); //return Res as a string oclParameters.Result.SetOwnedElement(Support.CreateNewConstant(Support.StringType,Res)); end; procedure CommaText.Init; var OclParams : array of IOclType; begin //tell the framework I have only one parameter SetLength (OclParams,1); //the parameter is an ObjectList (collection) OclParams[0] := Support.ObjectListType; //inform the operation name, its parameters, and result type InternalInit ('commaText',OclParams,Support.StringType); end; class procedure CommaText.Install(ES: EcoSpace); var OclService: IOclService; begin //gimme the OclService OclService := ES.GetEcoService(typeof(IOclService)) as IOclService; //install the new operation OclService.InstallOperation(CommaText.Create(ES)); end; end. Here's how you install it: CommaText.Install(EcoSpace); Here's how to use it: Customer.Create(EcoSpace).name := 'Daniel'; Customer.Create(EcoSpace).name := 'John'; Customer.Create(EcoSpace).name := 'Jesper'; Customer.Create(EcoSpace).name := 'Jan'; Customer.Create(EcoSpace).name := 'Jonas'; ExpressionHandle1.Expression := 'Customer.allInstances->commaText'; MessageBox.Show(ExpressionHandle1.Element.AsObject.ToString);



首頁│ 下一篇→DELPHI2006.ECOIII的狀態機案例
本文引用網址: