动态模型编程本身没有定义任何语法类的动作模型,比如XWorker中的Begin、Do、If等等模型都是后期创建的,同样你可以创建和定义自己的语法模型。
IfExample是一个if else的语法模型,模型代码如下。
<?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>
IfExample的run方法使用Groovy脚本实现的,它首先执行Condition子节点下的动作,然后根据返回值执行Then或Else子节点下的动作。
import xworker.util.UtilAction; import xworker.util.UtilData; //条件 def condition = UtilAction.runChildActions(getChilds(self, "Condition"), actionContext); //根据条件执行Then或Else if(UtilData.isTrue(condition)){ return UtilAction.runChildActions(getChilds(self, "Then"), actionContext); }else{ return UtilAction.runChildActions(getChilds(self, "Else"), actionContext); } //获取指定名称的子节点的第一个子节点下的子节点列表 def getChilds(self, name){ def child = self.getThing(name + "@0"); if(child != null){ return child.getChilds(); }else{ return []; } }
下面的模型代码是IfExample动作模型的测试模型。
<?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>