Invoking Native java code

The VIATRA2 framework support two ways to invoke native Java code, namely: native ASM rule and function invocation.

Native ASM function

For creating a native ASM function you should follow these steps:

import org.eclipse.viatra2.natives.NativeFunctionParameter.ParameterType;
@VIATRANativeFunction(
   	name="clean",
   	remark="Deletes model elements faster than the \"delete()\" rule (recursive).",
       params={
      @NativeFunctionParameter(
          name = "entities", 
          type = { ParameterType.MODEL_ELEMENT }, 
          isVarArg = true,
               description="model element references to top-level entities to be deleted."
                )	
       }, returns = { ParameterType.BOOLEAN }
       )
public class CleanFunction implements ASMNativeFunction 
{
  /* ... */
}
let U = ref("UML.metamodel.Class"), A = undef in update A = clean(U);

 Active native ASM rules in the gigantoman model space

Native ASM Rule

VIATRA2 supports native ASM rules that can be invoked within a VTCL program. They can be created by following:

public class TestNativeASMRule implements INativeASMRule {
/**
 * The textual description of the ASM rule.
 */
 public String getDescription() {
   return "A test native ASM rule";
 }
 /**
 * The unique ID of the native ASM rule. Has to be the FQN of the ASM rule that it will replace
 */
 public String getID() {
   return "testmachine.testnativeasm";
 }
/**
 * The name of the native ASM rule.
 */
 @Override
 public String getName() {
   return "testnativeasm";
 }
 /** This function will be called by the GTASM interpreter instead of the ASM rule invocation.
 * @param msp The model space of the invocation context
 * @param params The variable to term mapping table. All return parameters will have to added to the map
 * @return true if the execution succeeded false in all other case which will indicate the same behaviour as in case of the ASM rule failure
 * @throws ViatraTransformationException
 */
 public Boolean invoke(IModelSpace msp, Map<Variable, Object> params)
     throws ViatraTransformationException {
   System.out.println("Hello Native ASM World! The input parameters and their values are:");
   if(params.size() == 0)
     System.out.println("None");
   else
     for(Entry<Variable,Object> entry: params.entrySet())
       {
       System.out.println("Variable "+entry.getKey().getName()+" = "+entry.getValue().toString());
       }
   return true;
 }
}

machine testmachine ...

rule main() = ... call testnativeams("Test Data"); ...
@native
rule testnativeasm(in Input) = print("Hello Viatra World ");

...