I recently had to write a Groovy script to iteratively call an opaque behavior inside a loop. (Unless I am missing something, this appears to be the only way to create truly iterative operations in Cameo queries?)
I based this on a very simple example from @SD in this YouTube video: https://www.youtube.com/watch?v=kO-dJRIDgyk
I used the following bit of code to call the opaque behavior from the script.
import com.nomagic.uml2.ext.magicdraw.commonbehaviors.mdbasicbehaviors.Behavior
import com.nomagic.uml2.ext.magicdraw.commonbehaviors.mdbasicbehaviors.OpaqueBehavior
import com.nomagic.magicdraw.core.Application
import com.nomagic.magicdraw.uml.Finder
import com.nomagic.magicdraw.expressions.ExpressionHelper
def project   = Application.getInstance().getProject()
// locate the existing opaque behavior (adjust qualified name if it lives in a package)
def target = Finder.byQualifiedName().find(project, "AllMethodsUsedInBehaviors")
if (target == null || !(target instanceof OpaqueBehavior))
    throw new IllegalStateException("Opaque behavior 'AllMethodsUsedInBehaviors' not found.")
def expr = ExpressionHelper.getBehaviorExpression(target)
def args   = context ?: []   // context is the input parameter of this opaque behavior
def result = ExpressionHelper.call(expr, args)
return result
This works, but calling the opaque behavior in this way seems to be very depended on where the opaque behavior is located in the model. Now when I move the opaque behavior to our library I will have to modify the script to hard code the new qualified name.
Is there a better way to do this that is less dependent on where we save our opaque behaviors?
