Action context

1. Introduction

    The action context is the environment in which the action is executed. To execute an action, the action context must be present.

    The execution environment of the action mainly includes two, one is the variable and the other is the thread state. Because dynamic contexts are mostly used to manage variables, action contexts are sometimes called variable contexts.

2. Management variables

2.1. Basic Concepts

     The action context is a stack, each layer of the stack is equivalent to a Map<String, Object>, and the action context as a whole can also be regarded as a Map<String, Object>. You can set and get variables through the variable context, or you can directly set and get the variables of a certain stack layer through the stack.

    It is important to note that threads do not share stacks. That is, in the same variable context, in different threads, threads use their own stacks, and push() or pop() in a thread does not affect other threads.

    Although different threads use different stacks, the bottom layer of the stack is shared by threads.

2.2. Acquisition order of variables

    When the variable is obtained through the variable context, it is obtained from the upper layer of the stack to the lower layer in turn. If a stack layer has this variable, it will be returned. The upper stack is the latest pushed stack layer.

    The schematic code for obtaining variables is as follows.

        Object value = null;
Stack<Bindings> bindingsStack = getBindingStack();
for (int i = bindingsStack.size() - 1; i >= 0; i--) {
Map<String, Object> map = bindingsStack.get(i);
value = map.get(key);

if (value != null) {
break;
} else if (map.containsKey(key)) {
break;
}
}

return value;

2.3.Variable scope

    The concept of relative global variables and local variables can be implemented according to the stack of the dynamic context.

    Local variables, can be understood as temporarily added variables. When used, they are pushed into a stack layer, and variables are set in this stack layer. Pop the stack layer, so the variable disappears. It should be noted that when using local variables, push and pop must be paired and placed in try/finally.

    Global variables, since the bottom stack layer of the action context is shared by all threads and will not be popped up in daily use, the variables of this stack layer can be as a global variable.

3. Thread status

    The thread state is used to indicate the behavior of the action. The thread state is related to the action of the grammar class. Thread states mainly include RUNNING, RETURN, BREAK, BREAK, CONTINUE and EXCEPTION.

    For example, an action is a while loop, and the code is as follows.

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

//while action model itself
Thing self = ...;

Object result = null;
List<Thing> children = self.getChilds();
while(self.doAction("condition", actionContext){
    for(Thing child : children){
        result = child.getAction().run(actionContext);

        int status = actionContext.getStatus();
        if(status == ActionContext.RETURN){
            //The return method executed by the child node
            return result;
        }else if(status == ActionContext.BREAK){
           //The child node requires break
            break;
        }else if(status == ActionContext.CONTINUE){
           //The child node requires continue;
           continue;
        }
    }
}

return result;

4. More action context documentation

    More documentation on action contexts can be found in Documentation and Examples in the XWorker Model Editor.

 

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