The VIATRA2 framework support two ways to invoke native Java code, namely: native ASM rule and function invocation.
For creating a native ASM function you should follow these steps:
org.eclipse.viatra2.core2.nativefunction
(extension point, provided by the
viatra2 core) and register a class that implements the ASMNativeFunction
interface.
@VIATRANativefunction
annotation. An example annotation is shown below.
@NativeFunctionParameter
annotations. Each annotation has four parameters: (i)
name the name of the parameter, (ii)
remarks description of the parameter,
type type of the parameter and finally
isVarArgs.
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 { /* ... */ }
enum ParameterType {MODEL_ELEMENT,STRING,INTEGER,DOUBLE,BOOLEAN,NATIVE};
NATIVE
type means that it will not be processed by the interpreter and can contain any kind of java.lang.Object. It can be used to pass parameters between native ASM functions and rules within VTCL code.let U = ref("UML.metamodel.Class"), A = undef in update A = clean(U);
VIATRA2 supports native ASM rules that can be invoked within a VTCL program. They can be created by following:
org.eclipse.viatra2.gtasm.interpreter.nativeasmrule
extension point provided by the interpreter (INativeAMSRule
interface)@native
annotation in order to execute its native representation ( don't foget that the FQN of the ASM rule must be equal with the ID of its native representation)
testnativeasm
rule in the testmachine
machine. It can be invoked with any number of parameters (this is important as it is not necessary to follow the signature of the defining ASM rule!) and writes out the variables' name-value pairs. 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 ");
...