With the help of Jonas Hogstrom and Jan Nordén I have written 2 functions to evaluate OCL.
First is to evaluate OCL in EcoSpace:
public object EvaluateOclInEcoSpace(IElement rootValue, string ocl)
{
object result = null;
IElement e = ((IOclService)
this.GetEcoService(typeof(IOclService))).EvaluateAndSubscribe(rootValue, ocl,
null, null);
if (e != null)
result = e.AsObject;
return result;
} Second is to evaluate OCL in PS:
public object EvaluateOclInDB(IElement rootValue , string ocl)
{
object result = null;
IElement InternalElement = null;
IOclService oclService = (IOclService)
this.GetEcoService(typeof(IOclService));
IPersistenceService persistenceService = (IPersistenceService)
this.GetEcoService(typeof(IPersistenceService));
AbstractCondition cond = oclService.CreateRetrieveCondition(rootValue,
null, ocl);
InternalElement = persistenceService.GetAllWithCondition(cond, 1, 0);
if ((InternalElement != null) && ((InternalElement as IObjectList).Count
== 1))
result = (InternalElement as IObjectList)[0].AsObject;
return result;
}
Of course you can make these functions more complex.
You can return different results types, for example IObjectList, change parameters for GetAllWithCondition - in this version it returns only the first object.

Delicious
Digg
Google
Yahoo