简体   繁体   中英

Jbpm - Java : Accessing objects withing process and calling methods on these objects

I am new to Jbpm, I am currently building a Loan Application, I have a Person class which has the loan amount.

I am passing Person object as parameter using Map in Process as follow:

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();

Here is the link to my process diagram:

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

I can access the person object in Starting script the code is as follow:

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

The problem is whenever I try to call any method on this object/try to cast it to Person, it throws error as follows:

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)

Here is my .bpmn file:

<?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>

What I want to do is access the loanAmount which is inside the Person object on the "diverge gateway" to classify the type of loan. Can anyone help me? Where I am going wrong I can't find it.Thank you.

In theory you should be able to see more information about what's going wrong during compilation (in the console or an earlier stack trace). If not, use kbuilder.getErrors() to get more detail.

A quick look at the process made me wonder if you shouldn't use the fully qualified name (including the package) for your Person class etc.?

Kris

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

Means that something in your process is wrong, but you are not sharing the complete stack trace so it's more complicated for us to help you out.. if you look carefully the stack trace you will find out what is going on there.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM