`
babydrool
  • 浏览: 7555 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

[翻译]Drools6.2帮助文档-9.Drools命令

阅读更多
9.1. API
Drools提供的XML序列化/反序列化命令需要使用特殊的类,这将会在下面的章节详细介绍。
下面示例脚本的网址,包括JAXB, Xstream, JSON的序列化:
• http://fisheye.jboss.org/browse/JBossRules/trunk/drools-camel/src/test/resources/org/drools/camel/component/jaxb.mvt?r=HEAD
• http://fisheye.jboss.org/browse/JBossRules/trunk/drools-camel/src/test/resources/org/drools/camel/component/jaxb.mvt?r=HEAD
• http://fisheye.jboss.org/browse/JBossRules/trunk/drools-camel/src/test/resources/org/drools/camel/component/xstream.mvt?r=HEAD

9.1.1. XStream
在使用Xstream序列化命令之前,需要从DroolsHelperProvider中获取一个XStream实例。这个方法可以将转换器注册到实例中。
序列化
BatchExecutionHelperProviderImpl.newXStreamMarshaller().toXML(command);
反序列化
BatchExecutionHelperProviderImpl.newXStreamMarshaller().fromXML(xml)

9.1.2. JSON
使用JSON API序列化/反序列化的方法与XStream类似
• 序列化
BatchExecutionHelper.newJSonMarshaller().toXML(command);
• 反序列化
BatchExecutionHelper.newJSonMarshaller().fromXML(xml)

9.1.3. JAXB
有两个方法使用JAXB,你可以定论模型的XSD文件,也可以使用POJO模型。这两种情况下,你必须先在JAXBContext中声明你的类型。为了做到这一点,你需要 使用Drools Helper类。一旦你有了JAXBContext,你就可以根据需要创建序列化器/反序列化器。
9.1.3.1 使用XSD文件定义模型
为了使用在XSD文件中定义的模型,你需要有一个KnowledgeBase(知识库),这样你可以把XSD模型以资源的形式加载到KnowledgeBase中。
首先,XSD文件必须以XSD资源类型加载到KnowledgeBuilder中,再使用KnowledgeBuilder创建KnowledgeBase,最后通过KnowledgeBase创建JAXBContext。
代码段如下:
Options xjcOpts = new Options();
xjcOpts.setSchemaLanguage(Language.XMLSCHEMA);
JaxbConfiguration jaxbConfiguration = KnowledgeBuilderFactory.newJaxbConfiguration( xjcOpts, "xsd" );
kbuilder.add(ResourceFactory.newClassPathResource("person.xsd", getClass()), ResourceType.XSD, jaxbConfiguration);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
List<String> classesName = new ArrayList<String>();
classesName.add("org.drools.compiler.test.Person");
   
JAXBContext jaxbContext = KnowledgeBuilderHelper.newJAXBContext(classesName.toArray(new String[classesName.size()]), kbase);

9.1.3.2.使用POJO模型
使用DroolsJaxbHelperProviderImpl创建JAXBContext,有两个参数:
1.classNames: 一个包含规范类名的数组,将在序列化/反序列化过程中使用的类。
2.properties: JAXB自定义的属性
代码段
List<String> classNames = new ArrayList<String>(); classNames.add("org.drools.compiler.test.Person"); JAXBContext jaxbContext = DroolsJaxbHelperProviderImpl.createDroolsJaxbContext(classNames, null); Marshaller marshaller = jaxbContext.createMarshaller();


9.2.支持的命令
目前,支持以下命令:
• BatchExecutionCommand
• InsertObjectCommand
• RetractCommand
• ModifyCommand
• GetObjectCommand
• InsertElementsCommand
• FireAllRulesCommand
• StartProcessCommand
• SignalEventCommand
• CompleteWorkItemCommand
• AbortWorkItemCommand
• QueryCommand
• SetGlobalCommand
• GetGlobalCommand
• GetObjectsCommand
注意:对于下面这样的代码片段,我们将使用一个POJO对象
org.drools.compiler.test.Person that has two fields
• name: String
• age: Integer
注意:后面的例子中,为了序列化我们需要先使用下面的代码片段:
• XStream
String xml = BatchExecutionHelper.newXStreamMarshaller().toXML(command);
• JSON
String xml = BatchExecutionHelper.newJSonMarshaller().toXML(command);
• JAXB
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter xml = new StringWriter();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(command, xml);

9.2.1. BatchExecutionCommand
• 描述:包含一组将要被发送并执行的命令的Command
• 属性:表9.1 BatchExecutionCommand属性
名称 描述 是否必需
lookup 设置将要被执行的命令的knowledge session id 是
commands 将要被执行的命令的数组 否
• 创建命令代码段:
BatchExecutionCommand command = new BatchExecutionCommand(); command.setLookup("ksession1"); InsertObjectCommand insertObjectCommand = new InsertObjectCommand(new Person("john", 25)); FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand(); command.getCommands().add(insertObjectCommand); command.getCommands().add(fireAllRulesCommand);

• XML 输出
○ XStream
<batch-execution lookup="ksession1">
  <insert>
    <org.drools.compiler.test.Person>
      <name>john</name>
      <age>25</age>
    </org.drools.compiler.test.Person>
  </insert>
  <fire-all-rules/>
</batch-execution>
○ JSON
{"batch-execution":{"lookup":"ksession1","commands":[{"insert":{"object":{"org.drools.compiler.test.Person":{"name":"john","age":25}}}},{"fire-all-rules":""}]}}
○ JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="ksession1">
    <insert>
        <object xsi:type="person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <age>25</age>
            <name>john</name>
        </object>
    </insert>
    <fire-all-rules max="-1"/>
</batch-execution>

9.2.2. InsertObjectCommand
描述:向Knowledge session中插入一个Object
属性:表9.2.InsertObjectCommand属性
名称 描述 是否必需
object 需要插入的对象 true
outIdentifier FactHandle的ID,在对象被插入并添加到执行队列中时创建 false
returnObject Boolean值,当对象必须在执行结果中返回时创建。默认值:true false
entryPoint 插入的入口 false
创建代码
List<Command> cmds = ArrayList<Command>(); Command insertObjectCommand = CommandFactory.newInsert(new Person("john", 25), "john", false, null); cmds.add( insertObjectCommand ); BatchExecutionCommand command = CommandFactory.createBatchExecution(cmds, "ksession1" );
XML输出
• XStream
<batch-execution lookup="ksession1">
  <insert out-identifier="john" entry-point="my stream" return-object="false">
    <org.drools.compiler.test.Person>
      <name>john</name>
      <age>25</age>
    </org.drools.compiler.test.Person>
  </insert>
</batch-execution>
• JSON
{"batch-execution":{"lookup":"ksession1","commands":{"insert":{"entry-point":"my stream", "out-identifier":"john","return-object":false,"object":{"org.drools.compiler.test.Person":{"name":"john","age":25}}}}}}
• JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="ksession1">
    <insert out-identifier="john" entry-point="my stream" >
        <object xsi:type="person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <age>25</age>
            <name>john</name>
        </object>
    </insert>
</batch-execution>

9.2.3. RetractCommand
描述:从knowledge session中回收一个对象
属性:表9.3 RetractCommand属性
名称 描述 是否必需
handle 与将被回收的对象关联的FactHandle true
创建代码:我们有两个方案,并得到同样的输出
1. 从一个String对象创建FactHandle
BatchExecutionCommand command = new BatchExecutionCommand();
command.setLookup("ksession1");
RetractCommand retractCommand = new RetractCommand();
retractCommand.setFactHandleFromString("123:234:345:456:567");
command.getCommands().add(retractCommand);
2. 当对象插入时获取FactHandle,并设置给RetractCommand
BatchExecutionCommand command = new BatchExecutionCommand();
command.setLookup("ksession1");
RetractCommand retractCommand = new RetractCommand(factHandle);
command.getCommands().add(retractCommand);
XML输出
• XStream
<batch-execution lookup="ksession1">
  <retract fact-handle="0:234:345:456:567"/>
</batch-execution>
• JSON
{"batch-execution":{"lookup":"ksession1","commands":{"retract":{"fact-handle":"0:234:345:456:567"}}}}
• JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="ksession1">
    <retract fact-handle="0:234:345:456:567"/>
</batch-execution>

9.2.4. ModifyCommand
描述:允许你修改之前插入到knowledge session的对象的值
属性:表9.4.ModifyCommand属性
名称 描述 是否必需
handle 与待修改对象关联的FactHandle true
setters 修改对象的Setter数组 true
• 创建代码
BatchExecutionCommand command = new BatchExecutionCommand();
command.setLookup("ksession1");
ModifyCommand modifyCommand = new ModifyCommand();
modifyCommand.setFactHandleFromString("123:234:345:456:567");
List<Setter> setters = new ArrayList<Setter>();
setters.add(new SetterImpl("age", "30"));
modifyCommand.setSetters(setters);
command.getCommands().add(modifyCommand);
• XML输出
○ XStream
<batch-execution lookup="ksession1">
  <modify fact-handle="0:234:345:456:567">
    <set accessor="age" value="30"/>
  </modify>
</batch-execution>
○ JSON
{"batch-execution":{"lookup":"ksession1","commands":{"modify":{"fact-handle":"0:234:345:456:567","setters":{"accessor":"age","value":30}}}}}
○ JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="ksession1">
    <modify fact-handle="0:234:345:456:567">
        <set value="30" accessor="age"/>
    </modify>
</batch-execution>

9.2.5. GetObjectCommand
描述:从knowledge session中获取一个对象
属性:表9.5.GetObjectCommand属性
Name Description required
factHandle 与将获取的对象关联的FactHandle true
outIdentifier FactHandle的ID,在对象被插入并添加到执行队列中时创建 false
创建代码:
• BatchExecutionCommand command = new BatchExecutionCommand();
command.setLookup("ksession1");
GetObjectCommand getObjectCommand = new GetObjectCommand();
getObjectCommand.setFactHandleFromString("123:234:345:456:567");
getObjectCommand.setOutIdentifier("john");
command.getCommands().add(getObjectCommand);
• XML 输出
○ XStream
<batch-execution lookup="ksession1">
  <get-object fact-handle="0:234:345:456:567" out-identifier="john"/>
</batch-execution>
○ JSON
{"batch-execution":{"lookup":"ksession1","commands":{"get-object":{"fact-handle":"0:234:345:456:567","out-identifier":"john"}}}}
○ JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="ksession1">
    <get-object out-identifier="john" fact-handle="0:234:345:456:567"/>
</batch-execution>

9.2.6. InsertElementsCommand
描述:插入一个对象数组
属性:表9.6.InsertElementsCommand
Name Description required
objects 将要插入的对象数组 true
outIdentifier FactHandle的ID,在对象被插入并添加到执行队列中时创建 false
returnObject Boolean值,当对象必须在执行结果中返回时创建。默认值:true false
entryPoint 插入的入口 False
创建代码:
• List<Command> cmds = ArrayList<Command>();

List<Object> objects = new ArrayList<Object>();
objects.add(new Person("john", 25));
objects.add(new Person("sarah", 35));

Command insertElementsCommand = CommandFactory.newInsertElements( objects );
cmds.add( insertElementsCommand );

BatchExecutionCommand command = CommandFactory.createBatchExecution(cmds, "ksession1" );
• XML output
○ XStream
<batch-execution lookup="ksession1">
  <insert-elements>
    <org.drools.compiler.test.Person>
      <name>john</name>
      <age>25</age>
    </org.drools.compiler.test.Person>
    <org.drools.compiler.test.Person>
      <name>sarah</name>
      <age>35</age>
    </org.drools.compiler.test.Person>
  </insert-elements>
</batch-execution>
○ JSON
{"batch-execution":{"lookup":"ksession1","commands":{"insert-elements":{"objects":[{"containedObject":{"@class":"org.drools.compiler.test.Person","name":"john","age":25}},{"containedObject":{"@class":"Person","name":"sarah","age":35}}]}}}}
○ JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="ksession1">
    <insert-elements return-objects="true">
        <list>
            <element xsi:type="person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <age>25</age>
                <name>john</name>
            </element>
            <element xsi:type="person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <age>35</age>
                <name>sarah</name>
            </element>
        <list>
    </insert-elements>
</batch-execution>
• 以下略:参见原文第9章
• http://docs.jboss.org/drools/release/6.2.0.Final/drools-docs/html/index.html
9.2.7. FireAllRulesCommand
9.2.8. StartProcessCommand
9.2.9. SignalEventCommand
9.2.10. CompleteWorkItemCommand
9.2.11. AbortWorkItemCommand
9.2.12. QueryCommand
9.2.13. SetGlobalCommand
9.2.14. GetGlobalCommand
9.2.15. GetObjectsCommand
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics