Actions and behaviors

1. Convert the model to action execution

import org.xmeta.Thing;
import org.xmeta.Aciton;
import org.xmeta.ActionContext;

//Get the HelloWorldExample model
Thing helloWorldExample = world.getThing("test.HelloWorldExample");

//Convert into action, which is similar to static method in contrast to Java
Action helloWorldAction = helloWorldExample .getAction();

// execute the action
//Action context (ActionContext) is required when the behavior of actions and objects is executed, and the action context is used to manage variables and thread states, etc.
helloWorldAction.run(new ActionContext());

2. Use the model as an object and perform its behavior

import org.xmeta.Thing;
import org.xmeta.ActionContext;

//Get the HelloWorldExample model
Thing helloWorldExample = world.getThing("test.HelloWorldExample");
//Execute the run method of HelloWorldExample.
//When executing the behavior of the object, the object itself will be placed in the action context with the variable name self (equivalent to this variable), and the action can access the object model through the self variable.
helloWorldExample.doAction("run", new ActionContext());

3. The method of passing parameters is also applicable when the action is executed

//Pass parameters, method 1, parameter name + parameter value, you can set multiple, parameter name and parameter value appear in pairs
Object result = thing.doAction("xxx", new ActionContext(), "p1", p1Value, "p2", p2Value...);

//Pass parameters, method 2, pass parameters through Map<String,Object>
Map<String, Object> params = new HashMap<>();
params.put("p1", p1Value);
params.put("p2", p2Value);
Object result = thing.doAction("xxx", actionContext, params);

//Pass parameters, method 3, set parameters through ActionContext
ActionContext actionContext = new ActionContext();
actionContext.peek().put("p1", p1Value);
actionContext.peek().put("p2", p2Value);
......
Object result = thing.doAction("xxx", actionContext);

4. The difference between action and behavior

    When executing an action, the model does not pass itself into the action context as the self variable, but when executing it as an object. So the action executes without the self variable (or the self variable is not itself), whereas the self variable is the object model itself when the object's behavior is executed.

    For example, the self variable in the following code, when executing the run behavior of the HelloWorldExample model object, the self variable is the HelloWorldExample model itself.

    public static void run(ActionContext actionContext){
        //Model instance, equivalent to the role of Java's this variable
        Thing self = actionContext.getObject("self");
 
        System.out.println(self.getString("message"));
    }

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号