Tuesday, September 29, 2009


New from Eco5 is the ability to handle tuples. Tuple is fancy word for something “taped together from bits and pieces of other stuff”. This is very useful when it comes to build result sets that is not necessary something that exists in your model. Like sums or collecting name and address together even if they are stored in different classes.

Tuples are what an sql server return when you omit the “*” and write explicit stuff in the select part. In fact the columns in the select part is the tuple-definition as far as the sql server is concerned.

In Eco 5 OCL operators create tuples: GroupBy and Collect.

Consider this model:


I want to GroupBy the contents of Class3.Attribute1;

IElement elem = ocl.Evaluate("Class1.allinstances.class2.class3->groupby(x|x.Attribute1)");

So what do I get back? A Tuple (don’t you just hate rhetoric questions? (I don’t))!

The tuple consists of a type with a generated name and specific properties. The properties are: Attribute1 and List.

The code below shows how to access the tuple result in code:

   1: private void RenderPrintbands()
   2: {
   3:   IOclService ocl=EcoServiceHelper.GetOclService(_ecoSpace);
   4:   IElement elem = ocl.Evaluate("Class1.allinstances.class2.class3->groupby(x|x.Attribute1)");
   5:   foreach (IElement listelem in elem.GetAsCollection())
   6:   {
   7:     string NameOfTupleResultForGroupedValue = ((listelem as IFeatured).Properties[0]).StructuralFeature.Name;
   8:     string NameOfTupleResultForTheGroupByList = ((listelem as IFeatured).Properties[1]).StructuralFeature.Name;
  10:     foreach (IElement groupedlistelem in (listelem as IFeatured).Properties[NameOfTupleResultForTheGroupByList].GetAsCollection())
  11:     {
  12:       Class3 c3=groupedlistelem.GetValue<Class3>();
  13:     }
  15:   }
  16: }

If I had grouped in more stuff, my tuple would have more properties.

If my tuple property is not an existing “thing” from my model but a tuple in itself it will be named part1 (if I another one, part2 etc).


Blogger Dmytrii said...

This is a very good feature. But usually grouping is an expensive operation and should be executed on the DB?

Can you do it using Linq *in PS*? Like this:

var productCategories = ecoService.PSQuery<Product>().GroupBy(x => x.Category).Select(x => new { Category = x.Category, ProductName = x.Name));

September 30, 2009 at 1:23 AM  
Blogger Hans Karlsen said...

Thanks Nagir, Currently tuples and OCL-to-SQL does not work but we all want it so it will happen eventually. I have suggested to Jonas that we should expose a plug-point to access the complete ocl parse tree. A plug-point like that can be used to create new OCL-to-something services.

October 10, 2009 at 1:50 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Powered by Blogger

Subscribe to
Posts [Atom]

Contact Us | Terms of Use | Privacy Statement © 2009 CapableObjects