Setting and using variables

    Assuming that we have an action called Add, which adds variables x and y and returns them, the settings and usage of variables x and y are as follows.

1.Set variable

    ActionContext is used to manage variables in dynamic model programming, where action context can also be called variable context.

import org.xmeta.ActionContext;

//Declare an ActionContext
ActionContext actionContext = new ActionContext();

//Setting x and y variables
actionContext.peek().put('x", 10);
actionContext.peek().put("y", 20);

//Getting Add Model
Action action = World.getInstance().getAction("Add");

//Execute the Add model, using the variable context above
Object result = action.run(actionContext);

//Print results
System.out.println(result);

2.Pass Parameters

    Variables can be passed to the action through the action context by using the action context when the action is invoked.

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

//Declare an ActionContext
ActionContext actionContext = new ActionContext();

//Getting Add Model
Action action = World.getInstance().getAction("Add");

//Execute the Add model, using the variable context above, pass parameters x and y
Object result = action.run(actionContext, "x", 10, "y", 20);

//Print results
System.out.println(result);

3.Using variables

    Once the variables are passed to the action, the action can use the variables. Assuming that the Add action is implemented using Java Action, these variables can be used in Java code.

public class Add{
    public static Object add(ActionContext actionContext){
         Object x = actionContext.get("x");
         Object y = actionContext.get("y");
          return x + y;
    }
}

 

 

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