You can write custom OCL operations. Here an example:
[EcoOclOperation(typeof(OclEcoId), true)]
public class OclEcoId : OclOperationBase
{
/// <summary>
/// Initialisation of the OCL funktion
/// </summary>
protected override void Init()
{
var parameter = new IOclType[1]
{
this.Support.ObjectType,
};
this.InternalInit("EcoId", parameter, this.Support.IntegerType);
}
/// <summary>
/// Implementation of the OCL function
/// </summary>
/// <param name="parameters">OCL parameters</param>
public override void Evaluate(IOclOperationParameters parameters)
{
var iObject = parameters.Values[0].Element as IObject;
if (iObject != null && iObject.AsObject != null)
{
var ecoid = iObject.EcoID();
this.Support.MakeNewInteger(parameters.Result, int.Parse(ecoid));
}
}
}
Such operations have to be installed into an EcoSpace like this:
public static class OclInstaller
{
/// <summary>
/// InstallOclOperations
/// </summary>
/// <param name="ecoSpace">The EcoSpaceparam>
public static void InstallOclOperations(EcoSpace ecoSpace)
{
if (ecoSpace != null)
{
var ocls = ecoSpace.GetEcoService<IOclService>();
var eals = ecoSpace.GetEcoService<IActionLanguageService>();
// Install all available operations in current assembly
foreach (Type t in System.Reflection.Assembly.GetAssembly(typeof(OclInstaller)).GetTypes())
{
if (t.BaseType == typeof(OclOperationBase))
{
var op = (OclOperationBase)Activator.CreateInstance(t);
eals.InstallOperation(op);
object[] attributes = t.GetCustomAttributes(typeof(EcoOclOperationAttribute), false);
if (attributes.Length == 1)
{
if ((attributes[0] as EcoOclOperationAttribute).IsQuery)
{
ocls.InstallOperation(op);
}
}
else
{
ocls.InstallOperation(op);
}
}
}
}
else
{
throw new NullReferenceException("Error in [InstallOclOperations]: EcoSpace is null.");
}
}
}
Call this installer in the constructor of the ecospace:
OclInstaller.InstallOclOperations(this);
It is amazing how easy it is with MDriven to add new custom OCL operations. As Hans tells, it is not possible to evaluate OCLs out of the box, but you could write you own evaluator. But please take a step back and take a proper archtitectural decision about the way you implement your function.
You can also add custom EcoServices but this is a competely different story.