{"id":123,"date":"2004-06-01T15:06:07","date_gmt":"2004-06-01T05:06:07","guid":{"rendered":"http:\/\/www.malcolmgroves.com\/blog\/?p=123"},"modified":"2008-09-16T15:06:00","modified_gmt":"2008-09-16T05:06:00","slug":"evaluate-ocl-in-code","status":"publish","type":"post","link":"http:\/\/www.malcolmgroves.com\/blog\/?p=123","title":{"rendered":"Evaluate OCL In Code"},"content":{"rendered":"<p>A quick one for today. <\/p>\n<p>Just had a question from someone on how to evaluate OCL from code. Assuming you have our Person class from the previous examples, place the following code in a button&#8217;s click event:<\/p>\n<p>procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);    <br \/>var     <br \/>&#160; OCLResult : IElement;     <br \/>&#160; ObjList : IObjectList;     <br \/>&#160; i : Integer;     <br \/>begin     <br \/>&#160; OCLResult := ECOSpace.OclService.EvaluateAndSubscribe(nil, &#8216;<strong>Person.AllInstances<\/strong>&#8216;,&#160; <br \/>nil, nil);     <br \/>&#160; if OCLResult is IObjectList then     <br \/>&#160; begin     <br \/>&#160;&#160;&#160; ObjList := IObjectList(OCLResult);<\/p>\n<p>&#160;&#160;&#160; for i := 0 to ObjList.Count &#8211; 1 do    <br \/>&#160;&#160;&#160; begin     <br \/>&#160;&#160;&#160;&#160;&#160; MessageBox.Show(Person(ObjList.Item[i].AsObject).Firstname);     <br \/>&#160;&#160;&#160; end;     <br \/>&#160; end;     <br \/>end;<\/p>\n<p>Here, I&#8217;m executing some simple OCL (Person.AllInstances) and then iterating through the resulting collection. The Item property of IObjectList returns an IObject. You can then reference its AsObject method to get back a TObject, which you can cast as your Person and away you go. <\/p>\n<p>Once you have your Person, dealing with any attributes or associations that it has will be much easier, as they will be correctly typed. It&#8217;s just &quot;getting inside&quot; our object model where we need to cast all over the shop.<\/p>\n<p>Obviously, if your OCL doesn&#8217;t return a collection, then you&#8217;d deal differently, but the above code should get you started. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick one for today. Just had a question from someone on how to evaluate OCL from code. Assuming you have our Person class from the previous examples, place the following code in a button&#8217;s click event: procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs); var &#160; OCLResult : IElement; &#160; ObjList : IObjectList; &#160; i : Integer; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123"}],"version-history":[{"count":1,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":125,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/125"}],"wp:attachment":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}