Skip to main content

JBoss BRMS - 呼叫Rule 與 BPMN 的API

學 BRMS 呼叫 Rules 與 BPMN 的 API 真的很划算,因為學一套 API 在 Rules 與 BPMN 都在用。而且概念還滿簡單的。

最主要就是這四個Class, 會使用他們基本上大概就可以操作JBoss Rules (Drools) 與 jBPM了!


  • KnowledgeBuilder
  • KnowledgeBase
  • StatefulKnowledgeSession
  • StatelessKnowledgeSession

KnowledgeBuilder

望文生義,這個Class 就是專門把各地的的資源,如 Drl 的檔案,jBPM 的 BPMN, 整包的binary package讀取進來,透過KnowledgeBuilderFactory 取得Builder。

如何建立一個Builder 並載入需要的Rule與資源?

A. 載入Drools ,
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();kbuilder.add( ResourceFactory.newUrlResource( "file://myrules.drl" ),ResourceType.DRL);
B.載入 Excel 的 Decision Table,

DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtconf.setInputType( DecisionTableInputType.XLS );
dtconf.setWorksheetName( "Tables_2" );
kbuilder.add( ResourceFactory.newUrlResource( "file://IntegrationExampleTest.xls" ),ResourceType.DTABLE,dtconf );


C.載入 jBPM 的BPMN2.0檔案

kbuilder.add( ResourceFactory.newUrlResource( "file://myProcess.bpmn2" ),
ResourceType.BPMN2 );


D.從遠端的BRMS管理網站讀取打包好的package檔案(包含了以上三種)

String url = "http://IP:PORT/jboss-brms/rest/packages/xx.yy.aa/binary"; UrlResource resource = (UrlResource) ResourceFactory.newUrlResource(url); resource.setBasicAuthentication("enabled"); resource.setUsername("ID"); resource.setPassword("PWD"); kbuilder.add(resource, ResourceType.PKG);
E. 除了以上寫在程式內的方式外,還可以獨立用XML定義,可以新增一個sampleRule.xml <change-set xmlns='http://drools.org/drools-5.0/change-set'xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'xs:schemaLocation='http://drools.org/drools-5.0/change-set.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >   <add>   <resource source='http:org/domain/myrules.drl' type='DRL' />   <resource source='classpath:data/IntegrationExampleTest.xls' type="DTABLE"> <decisiontable-conf input-type="XLS" worksheet-name="Tables_2" />   </resource>   </add> </change-set> kbuilder.add( ResourceFactory.newUrlResource( "sampleRule.xml" ), ResourceType.CHANGE_SET );
讀取後,最好保持好習慣確認一下有沒有錯誤,
if ( kbuilder.hasErrors() ) {
log.exception( kbuilder.getErrors().toString());
}

當Builder把資源讀取完之後,就會把編譯後可以執行的 binary放置的地方拉。所以如果你想要更新引擎上面執行的Rules還是流程,就要重新載入這個部分!因為要將所有的Rule 編譯,所以會滿耗費資源的,因此我建議大家啦~ 如果可以就把這個KnowledgeBase 暫存起來在系統裡,反覆地使用會比較好!

KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

這個Session 會保持一連串呼叫的狀態。因為他會保留context中的資訊,可以再上一個執行結果之下再做更多的計算跟使用。以jBPM來說,大部份都是使用這種session.
呼叫之後就可以直接insert Facts ,insert的 Fact都會被留在working memory裡面,狀態也會保存。直到session 結束為止。

StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

for( Object fact : facts ) {
     ksession.insert( fact );
}

ksession.fireAllRules();
ksession.dispose();


StatelessKnowledgeSession

這個Session 並不會保持狀態,也就是說,上一次的變動與下一次的變動是獨立的。因為是這種特性,它可以重複的被一直使用。所以適合在需要一直重複執行然後取得一個結果的規則。
執行的方式,就是呼叫execute, 直接把物件帶入,

StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
ksession.execute( collection );

如果想要同時輸入不同的物件,就需要借助 CommandFactory 把物件存放在一個 collection裡面執行啦。

List cmds = new ArrayList();
cmds.add( CommandFactory.newSetGlobal( "list1", new ArrayList() ) );
cmds.add( CommandFactory.newInsert( new Person( "jon", 102 ) ) );

StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); 
ksession.execute( CommandFactory.newBatchExecution( cmds ) );

以上。





Comments

Popular posts from this blog

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

Red Hat JBoss Fuse - Getting Started with Fuse Integration Service 2.0 Tech preview

I just realized that I did not do a getting started for Fuse Integration Service 2.0 Tech preview before I did the pipeline demo, thanks for those of you who reminded me! :)

To get started with FIS 2.0, for people who has just getting to know the technology, here is how I interpret it. Basically, it's divide into two aspect,

1. Integration development, FIS uses Apache Camel as the core technology that creates, orchestrate, compose microservices into a super lightweight thin integration layer, and become the API provider and service orchestrator through exposing RESTful or messaging service endpoints. And you can choose to either package and run it with Spring-Boot or Karaf.


2. Application Deployment and Management, FIS takes advantages of OpenShift platform, and allows you to separately deploy the micro-integration service among distributed environment, at the same time takes care of the failover, high availability, load balancing and service lookup problem for you.


So, now we know …