首页 > 其他分享 >Dynamic Process Creation Using API [JBPM 5.1]

Dynamic Process Creation Using API [JBPM 5.1]

时间:2023-05-16 11:33:07浏览次数:39  
标签:5.1 JBPM Process XmlBPMNProcessDumper factory process import org jbpm


[url]http://atulkotwale.blogspot.com/2012/01/dynamic-process-creation-using-api-jbpm.html[/url]
Hi Friends,

As you guys all know jbpm has came up with new version. It has some beautiful features. In this post I will show you how can we make dyanamic process using JBPM 5 API. While it is recommended to define processes using the graphical editor or the underlying XML (to shield yourself from internal APIs) but sometime we come across such scenario where this feature would really helpful.

Using this API you can
Create process and set the attributes.
Create task and add to that process. Also you can set the task related attributes.
Establish connections between task.
Get the xml representation of process.
Last but not least we can validation the process and can get the list of errors which were found during validation.
Isn't it great!!!! Let see how can we do it.

Here I created simple process with human task .

package com.sample;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.bpmn2.xml.XmlBPMNProcessDumper;
import org.jbpm.process.workitem.wsht.WSHumanTaskHandler;
import org.jbpm.ruleflow.core.RuleFlowProcessFactory;
import org.jbpm.ruleflow.core.factory.HumanTaskNodeFactory;

/**
 * This is a sample file to launch a process.
 */
public class ProcessTest {

    public static final void main(String[] args) {
        try {

            RuleFlowProcessFactory factory =

            RuleFlowProcessFactory.createProcess("org.jbpm.createLetter");

            //creating process dynamically
            factory = factory.name("LetterCreation").version("1.0")
                    .packageName("org.atul");


            // adding start node
            factory.startNode(1).name("start").done();

            // adding human task
            HumanTaskNodeFactory nodefactory = factory.humanTaskNode(2);
            nodefactory.name("atul").actorId("department").done();

            // adding end node
            factory.endNode(3).name("end").done();

            // making connections
            factory.connection(1, 2);
            factory.connection(2, 3);

            System.out.println("id is:-"
                    + factory.validate().getProcess().getId());

            // We can get xml for the process using following code.
            String asXml = XmlBPMNProcessDumper.INSTANCE.dump(
                    factory.getProcess(), XmlBPMNProcessDumper.NO_META_DATA);
            System.out.println(asXml);

            //adding to the knowledge base
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
                    .newKnowledgeBuilder();
            kbuilder.add(ResourceFactory
                    .newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(
                            factory.getProcess()).getBytes()),
                    ResourceType.BPMN2);

            StatefulKnowledgeSession ksession = kbuilder.newKnowledgeBase()
                    .newStatefulKnowledgeSession();
            ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
                    new WSHumanTaskHandler());

            // start a new process instance
            ksession.startProcess("org.jbpm.createLetter");
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}



As you can see in the example that we can create process using "createProcess" method if "RuleFlowProcessFactory" class. This class also provides method to set the attributes of the process. It provides method to create start, end node.you can see that these methods return a specific NodeFactory, that allows you to set the properties of that node. Once you have finished configuring that specific node, the done() method returns you to the current RuleFlowProcessFactory so you can add more nodes, if necessary. Using "connection" method we can establish connections between the task.



Using following code we can get xml presentation of the process.


String asXml = XmlBPMNProcessDumper.INSTANCE.dump(
                    factory.getProcess(), XmlBPMNProcessDumper.NO_META_DATA);



Finally once you done with creation of the process, you can validate the process by validate() method.



We can add the process in to the knowledge base as BPMN2 process.


kbuilder.add(ResourceFactory
                    .newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(
                            factory.getProcess()).getBytes()),
                    ResourceType.BPMN2);





Please let me know your view and suggestion to make this post better !!!


标签:5.1,JBPM,Process,XmlBPMNProcessDumper,factory,process,import,org,jbpm
From: https://blog.51cto.com/u_3871599/6282984

相关文章

  • Spring 3 & jBPM 5 & LocalTaskService
    帖子地址:[url]https://community.jboss.org/thread/195386[/url]HiGuys,IamalsostrugglinginconfigSpring+LocalHumanTask.Iamusing[color=red][b]Spring3.0,JBPM5.4.0.Final,Drools5.5.0.Final[/b][/color]IcanconfigtouseJTA......
  • 5.15
       编写一个计算个人所得税的程序,要求输入收入金额后,能够输出应缴的个人所得税个人所得税征收办法如下:起征点为3500元。不超过1500元的部分,征收3%;超过1500~4500元的部分,征收10%;超过4500~9000元的部分,征收20%;超过9000~35000元的部分,征收25%;超过35000~55000元的部分,征收30%;......
  • 5.15
    #include<iostream>usingnamespacestd;#include<cmath>#include"time_user.h"classpoint{private:   intx,y,z;public:   voidset()   {       cin>>x>>y>>z;   }   voidout()   {       cout......
  • 5.15每日总结
    今天上课老师讲了人机交互设计,讲了软件的设计如何变得更加人性化,满足用户的真实需求。还在团队中讨论了后续的项目冲刺验收要完成的任务和分工,未来要实现和改进哪些功能。之后的时间对记账本App页面的交互设计进行了一些改进。  ......
  • 每日总结 5.15
    今天上课进行了进一步的代码2书写。主要是进行广告方面的设计对于数据库的管理。try{pre=c.prepareStatement(sql);pre.setString(1,"111");//pre.setString(2,tel);res=pre.executeQuery();......
  • 建民打卡日记5.15
    一、问题描述一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一......
  • 5.8-5.14
    C1.PokémonArmy(easyversion)Problem-1420C1-Codeforces线性dp呃啊啊啊啊啊啊啊太久没写dp了,下周开始要把重点放到算法上意识到是个dp后就很简单了,状态转移方程也很好写出\[\begin{cases}f[i][0]\=\max(f[i-1][0],\f[i-1][1]+num[i]\\f[i][1]\=\max(f[i-1][......
  • 5.15打卡
    一、问题描述:2000以内的不小于4的正偶数都能够分解为两个素数之和(即验证歌德巴赫猜想对2000以内的正偶数成立)。二、设计思路:该问题我们可以采用函数来解决。定义一个函数,函数名设为fun,在其中判断传进来的实际参数(设为n(n=2)),是否为素数,如果是素数则返回1,否则返回0。在判断是......
  • 5.15每日总结
    今天上课老师讲了人机交互设计,讲了软件的设计如何变得更加人性化,满足用户的真实需求。还在团队中讨论了后续的项目冲刺验收要完成的任务和分工,未来要实现和改进哪些功能。之后的时间对记账本App页面的交互设计进行了一些改进。......
  • 5.15 2.8
    一、问题魔术师利用一副牌中的13张黑桃,预先将它们排好后迭在一起,并使牌面朝下。然后他对观众说:我不看牌,只要数数就可以猜到每张牌是什么,我大声数数,你们听,不信?你们就看,魔术师将最上面的那张牌数为1,把它翻过来正好是黑桃A,他将黑桃A放在桌子上,然后按顺序从上到下数手中的余牌......