Jbpm - Java:使用进程访问对象并在这些对象上调用方法

时间:2013-08-29 11:07:39

标签: java jbpm

我是Jbpm的新手,我目前正在建立一个贷款申请,我有一个有贷款金额的人员类。

我使用Map in Process传递Person对象作为参数,如下所示:

KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kBuilder.add(ResourceFactory.newClassPathResource("bpmn/sampleProcess.bpmn"),ResourceType.BPMN2 );

        KieBase kBase = kBuilder.newKnowledgeBase();

        KieSession kSession = kBase.newKieSession();

        // PROCESS PARAMETERS ::: Process Level Variables with Process Scope
        Map<String,Object> params = new HashMap<>();

        //Creating a Person who applies to loan.
        Person p = new Person();
        p.setName("Ayushya K Devmurari");
        p.setUniqueId("A007");
        p.setLoanAmmount(5_00_000.00);
        p.setIncome(2_40_000.00);

        // setting parameter in Map
        params.put("Applicant1", p);

        //Starting the process.
        kSession.startProcess("com.discusit.loanapplication", params);       

        kSession.dispose();

以下是我的流程图的链接:

http://tinypic.com/view.php?pic=2pza22o&s=5

我可以在启动脚本中访问person对象,代码如下:

System.out.println("Person is :::"+kcontext.getVariable("Applicant1"));

问题是每当我尝试调用此对象上的任何方法/尝试将其强制转换为Person时,它会抛出错误,如下所示:

Exception in thread "main" java.lang.IllegalArgumentException: Could not parse knowledge.
    at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.newKnowledgeBase(KnowledgeBuilderImpl.java:79)
    at com.discusit.loanapplication.main.MainClass.main(MainClass.java:23)

这是我的.bpmn文件:

<?xml version="1.0" encoding="UTF-8"?> 
<definitions id="Definition"
             targetNamespace="http://www.jboss.org/drools"
             typeLanguage="http://www.java.com/javaTypes"
             expressionLanguage="http://www.mvel.org/2.0"
             xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"
             xmlns:g="http://www.jboss.org/drools/flow/gpd"
             xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
             xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
             xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
             xmlns:tns="http://www.jboss.org/drools">

  <itemDefinition id="_personItem" structureRef="Person" />
  <itemDefinition id="_amountItem" structureRef="Float" />

  <process processType="Private" isExecutable="true" id="com.discusit.loanapplication" name="Sample Process" tns:packageName="defaultPackage" >

    <!-- process variables -->
    <property id="person" itemSubjectRef="_personItem"/>
    <property id="amount" itemSubjectRef="_amountItem"/>

    <!-- nodes -->
    <startEvent id="_1"  isInterrupting="true"/>
    <endEvent id="_16" name="End" >
        <terminateEventDefinition />
    </endEvent>
    <exclusiveGateway id="_3" name="Gateway" gatewayDirection="Diverging" />
    <scriptTask id="_18" name="Starting" scriptFormat="http://www.java.com/java" >
      <script>System.out.println("Process initialization...");
System.out.println("Person Object "+kcontext.getVariable("Applicant1"));
</script>
    </scriptTask>
    <scriptTask id="_4" name="Low Amount" scriptFormat="http://www.java.com/java" >
      <script>

System.out.println("Amount is Low :::::::");
</script>
    </scriptTask>
    <scriptTask id="_5" name="Medium Amount" scriptFormat="http://www.java.com/java" >
      <script>System.out.println("Amount is medium :::::");
</script>
    </scriptTask>
    <scriptTask id="_6" name="High Amount" scriptFormat="http://www.java.com/java" >
      <script>System.out.println("Amount is HIGH :::::::::");</script>
    </scriptTask>
    <parallelGateway id="_22" name="Gateway" gatewayDirection="Converging" />

    <!-- connections -->
    <sequenceFlow id="_22-_16" sourceRef="_22" targetRef="_16" />
    <sequenceFlow id="_18-_3" sourceRef="_18" targetRef="_3" />
    <sequenceFlow id="_1-_18" sourceRef="_1" targetRef="_18" />
    <sequenceFlow id="_3-_4" sourceRef="_3" targetRef="_4" name="low" tns:priority="1" >
      <conditionExpression xsi:type="tFormalExpression" language="http://www.java.com/java" >return false;</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_3-_5" sourceRef="_3" targetRef="_5" name="medium" tns:priority="1" >
      <conditionExpression xsi:type="tFormalExpression" language="http://www.java.com/java" >return false;</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_3-_6" sourceRef="_3" targetRef="_6" name="high" tns:priority="1" >
      <conditionExpression xsi:type="tFormalExpression" language="http://www.java.com/java" >return true;</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_5-_22" sourceRef="_5" targetRef="_22" />
    <sequenceFlow id="_6-_22" sourceRef="_6" targetRef="_22" />
    <sequenceFlow id="_4-_22" sourceRef="_4" targetRef="_22" />

  </process>

  <bpmndi:BPMNDiagram>
    <bpmndi:BPMNPlane bpmnElement="com.discusit.loanapplication" >
      <bpmndi:BPMNShape bpmnElement="_1" >
        <dc:Bounds x="265" y="57" width="48" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_16" >
        <dc:Bounds x="265" y="558" width="48" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_3" >
        <dc:Bounds x="263" y="223" width="48" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_18" >
        <dc:Bounds x="248" y="134" width="80" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_4" >
        <dc:Bounds x="21" y="356" width="132" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_5" >
        <dc:Bounds x="217" y="355" width="142" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_6" >
        <dc:Bounds x="475" y="346" width="132" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_22" >
        <dc:Bounds x="264" y="462" width="48" height="48" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_22-_16" >
        <di:waypoint x="288" y="486" />
        <di:waypoint x="289" y="582" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_18-_3" >
        <di:waypoint x="288" y="158" />
        <di:waypoint x="287" y="247" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_1-_18" >
        <di:waypoint x="289" y="81" />
        <di:waypoint x="288" y="158" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_3-_4" >
        <di:waypoint x="287" y="247" />
        <di:waypoint x="87" y="380" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_3-_5" >
        <di:waypoint x="287" y="247" />
        <di:waypoint x="288" y="379" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_3-_6" >
        <di:waypoint x="287" y="247" />
        <di:waypoint x="541" y="370" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_5-_22" >
        <di:waypoint x="288" y="379" />
        <di:waypoint x="288" y="486" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_6-_22" >
        <di:waypoint x="541" y="370" />
        <di:waypoint x="288" y="486" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_4-_22" >
        <di:waypoint x="87" y="380" />
        <di:waypoint x="288" y="486" />
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>

</definitions>

我想要做的是访问“分叉网关”上Person对象内的loanAmount,以对贷款类型进行分类。谁能帮我?在我出错的地方,我找不到它。谢谢。

2 个答案:

答案 0 :(得分:1)

理论上,您应该能够看到有关编译过程中出现问题的更多信息(在控制台或早期的堆栈跟踪中)。如果没有,请使用kbuilder.getErrors()获取更多详细信息。

快速浏览一下这个过程让我想知道你是不是应该为你的Person类等使用完全限定的名字(包括包)?

克里斯

答案 1 :(得分:0)

这:线程“main”中的异常java.lang.IllegalArgumentException:无法解析知识。     at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.newKnowledgeBase(KnowledgeBuilderImpl.java:79)

意味着你的过程中的某些东西是错误的,但你没有分享完整的堆栈跟踪,所以我们帮助你更复杂..如果仔细查看堆栈跟踪,你会发现那里发生了什么。

干杯