Create a grammar model

    Dynamic model programming itself does not define any action models of grammar classes, such as Begin, Do, If, etc. in XWorker, which are created later. Similarly, you can create and define your own grammar model.

Example

IfExample

     IfExample is a grammatical model of if else. The model code is as follows.

<?xml version="1.0" encoding="utf-8"?>

<thing name="IfExample" descriptors="xworker.lang.MetaDescriptor3" extends="xworker.lang.actions.SelfAction">
    <actions>
        <GroovyAction name="run">
            <code><![CDATA[...]]></code>
        </GroovyAction>
    </actions>
    <attribute name="name"></attribute>
    <attribute name="label"></attribute>
    <attribute name="description" inputtype="html"></attribute>
    <thing name="Condition" extends="xworker.lang.actions.Actions"></thing>
    <thing name="Then" extends="xworker.lang.actions.Actions"></thing>
    <thing name="Else" extends="xworker.lang.actions.Actions"></thing>
</thing>

Implementation of IfExample Action

    The run method of IfExample is implemented using Groovy script. It first performs the actions under the Condition sub-node, and then executes the actions under the Then or Else sub-node according to the return value.

import xworker.util.UtilAction;
import xworker.util.UtilData;

//Condition
def condition = UtilAction.runChildActions(getChilds(self, "Condition"), actionContext);

//Conditionally execute Then or Else
if(UtilData.isTrue(condition)){
    return UtilAction.runChildActions(getChilds(self, "Then"), actionContext);
}else{
    return UtilAction.runChildActions(getChilds(self, "Else"), actionContext);
}

//Gets the list of child nodes under the first child node of the child node with the specified name
def getChilds(self, name){
    def child = self.getThing(name + "@0");
    if(child != null){
        return child.getChilds();
    }else{
        return [];
    }
}

Test Model of IfExample

    The following model code is the test model of the IfExample action model.

<?xml version="1.0" encoding="utf-8"?>

<IfExample name="TestIfExample" descriptors="_local.test.core.actions.test.IfExample">
    <Condition>
        <GroovyAction name="GroovyAction" code="return true;"></GroovyAction>
    </Condition>
    <Then>
        <Println name="Println" message="True"></Println>
    </Then>
    <Else>
        <Println name="Println" message="False"></Println>
    </Else>
</IfExample>

 

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