JBoss Community

getVariable and setVariable

created by minslay minslay in jBPM - View the full discussion

Hello,

 

I'm newbie with jBPM and I'm stuck with something I think it's easy but it doesn't work.

 

I'm trying define a process dynamically with Process API. I want to insert a variable into my actionNode so, I create a HashMap and I use kcontext.getVariable().

Now I get my variable into my actionNode and I can use it.

Then I want to modify (or create a new variable) my variable and its value. I don't know how to modify it and use kcontext.setVariable(); also I dont know how to recover that value in my java code.

 

Here my code

 

   

                    HashMap<String, Object> processParameters = new HashMap<String, Object>(); 

                    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

                    RuleFlowProcessFactory factoryP1 = RuleFlowProcessFactory.createProcess("org.jbpm.Process1");

 

                    // My variable and value

                    processParameters.put("result",5);

 

                    factoryP1.name("Process1").version("1.0").packageName("org.jbpm")

                              .imports("java.util.HashMap")


                              // creating a new var

                              .variable("resultReceived", new ObjectDataType("java.lang.Integer"))

 

                                        .startNode(1).name("Start").done()

                                        .actionNode(2).name("Action")

                                        // I receive my variable from java Code with kcontext.getVariable

                                        // and modify its value (now:6)

                                        .action("java",          "resultReceived = (Integer) kcontext.getVariable(\"result\")+1;" +

                                                            "System.out.println(resultReceived);" +

                                        // I put my new variable with kcontext.setVariable

                                                            "kcontext.setVariable(\"resultReceived\", resultReceived);").done()

                                        // another actionNode.

                                        .actionNode(3).name("Action")

                                        .action("java","System.out.println(\"Another Node \");").done()

                                        .endNode(4).name("End").done()

                                        // Connections

                                        .connection(1, 2)

                                        .connection(2, 3)

                                        .connection(3, 4);

 

                    RuleFlowProcess process1 = factoryP1.validate().getProcess();

 

                    kbuilder.add(ResourceFactory.newByteArrayResource(

                                        XmlBPMNProcessDumper.INSTANCE.dump(process1).getBytes()),

                                        ResourceType.BPMN2);

 

                    KnowledgeBase kbase = kbuilder.newKnowledgeBase();

                    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

                    ProcessInstance processInstance = ksession.startProcess("org.jbpm.Process1",processParameters);

 

                    // and now... How can I get resultReceived from actionNode???

Reply to this message by going to Community

Start a new discussion in jBPM at Community