Jboss Rule Engine KIE Drools 6.3.0 Final tutorial (2)

Source: Internet
Author: User

Jboss Rule Engine KIE Drools 6.3.0 Final tutorial (2)
Calling rules with a JAVA program-running rules on a KIE-DROOLS
Step 1: Create a MAVEN Java project POM. XML to provide the pom. xml file

 
  
   4.0.0
  
  
   os.nut.drools
  drools-training
  
   0.0.1-SNAPSHOT
  
  
   
    6.3.0.Final
   
  
  
   
    
     guvnor-m2-repo
    
    
     Guvnor M2 Repo
    
    
     http://192.168.0.101:8080/kie-drools/maven2/
    
   
  
  
   
   
    
     org.kie
    kie-api
    
     ${drools.version}
    
   
   
    
     org.drools
    drools-core
    
     ${drools.version}
    
   
   
    
     org.drools
    drools-compiler
    
     ${drools.version}
    
   
   
    
     org.drools
    drools-decisiontables
    
     ${drools.version}
    
   
   
    
     org.drools
    drools-templates
    
     ${drools.version}
    
   
   
    
     org.kie
    kie-ci
    
     ${drools.version}
    
   
   
   
    
     junit
    junit
    
     4.7
    
    
     test
    
   
  
 
The key to POM. XML explanation lies in the following two sections: the Java program reads the rules in the kie-drools Repository from a remote address and uses this package. Step 2: Keep the project structure in the same name as the package on the remote KIE-DROOLS PaymentInfo. for java files, you can directly store the Facts code in the data object in kie-drools to your local machine to Write Remote Call clients.
Package org. sky. drools. approvaldecision; import java. io. inputStream; import org. drools. compiler. kproject. releaseIdImpl; import org. drools. core. io. impl. urlResource; import org. kie. api. kieServices; import org. kie. api. builder. kieModule; import org. kie. api. builder. kieRepository; import org. kie. api. runtime. kieContainer; import org. kie. api. runtime. statelessKieSession; public class RemotePaymentRulz {public static Void main (String [] args) throws Exception {String url = "http: // 192.168.0.101: 8080/kie-drools/maven2/org/sky/drools/approvalde.pdf/1.0.0/ApprovalDecision-1.0.0.jar "; ReleaseIdImpl releaseId = new ReleaseIdImpl (" org. sky. Drools "," approvaldeves "," LATEST "); KieServices ks = KieServices. factory. get (); KieRepository kr = ks. getRepository (); UrlResource urlResource = (UrlResource) ks. getResources (). newUrlResource (url); urlResource. setUsername ("tomcat"); urlResource. setPassword ("tomcat"); urlResource. setBasicAuthentication ("enabled"); InputStream is = urlResource. getInputStream (); KieModule kModule = kr. addKieModule (ks. getResources (). newInputStreamResource (is); KieContainer kContainer = ks. newKieContainer (kModule. getReleaseId (); StatelessKieSession kSession = kContainer. newStatelessKieSession ("defaultStatelessKieSession"); PaymentInfo m = new PaymentInfo (); m. setMoneyAmount (5001); kSession.exe cute (m); System. out. println (m. getDecisionPath (); if (m. getDecisionPath (). equalsIgnoreCase ("m") {System. out. println ("amount <= 5000 requires manager approval");} else {System. out. println ("amount> 5000 requires approval from the General Manager ");}}}

Remote client core code explanation
String url = "http://192.168.0.101:8080/kie-drools/maven2/org/sky/drools/ApprovalDecision/1.0.0/ApprovalDecision-1.0.0.jar";KieServices ks = KieServices.Factory.get();KieRepository kr = ks.getRepository();UrlResource urlResource = (UrlResource) ks.getResources().newUrlResource(url);
The above code is very simple. It tells JAVA where to call our remote deployment rules
urlResource.setUsername("tomcat");urlResource.setPassword("tomcat");urlResource.setBasicAuthentication("enabled");InputStream is = urlResource.getInputStream();KieModule kModule = kr.addKieModule(ks.getResources().newInputStreamResource(is));KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());

The above code is equivalent to opening the KIE-DROOLS Web address in IE, then enter the user name, password and click Login
StatelessKieSession kSession = kContainer. newStatelessKieSession ("defaultStatelessKieSession"); PaymentInfo m = new paymentinfo(~~m.setmoneyamount(5001~~ksession.exe cute (m); System. out. println (m. getDecisionPath (); if (m. getDecisionPath (). equalsIgnoreCase ("m") {System. out. println ("amount <= 5000 requires manager approval");} else {System. out. println ("amount> 5000 requires approval from the General Manager ");}

This Code uses the statelessSession interface to call rules.
Because there is a saying" Modify (m) {setDecisionPath ("GM ")};"
Therefore, the rule is based on PaymentInfo. paymentAmountTo modify the value PaymentInfo. decisionPath. The client code has been written. Do not rush to run it !!!


Because we haven't generated the project we're located on the KIE-DROOLS:


Org/sky/drools/approvalde.pdf/1.0.0/ApprovalDecision-1.0.0.jar


Such as the above available "Resources !!!


To generate a "resource" that can be remotely called for a rule project, follow these steps:

Let's take a look at the list of resources generated after BUILD that can be remotely called. Let's take a look at the effect of the operation. We can consider using BRMS in combination with various business scenarios. Now we can consider using BRMS in combination with rules. next, how can we quickly respond to changes in our business. Step 1: Open the rule engine and directly change the rule online Step 2: run the test case after the rule is changed online Step 3: Submit the test result to the business department for review Step 4: step 5 of the online re-build rule project: Check the JAVA code execution results without modifying the code, No need to restartThe subsequent tutorials will show the differences between stateful (stateful) and stateless (stateless). The comparison and explanation will also be given in this article. So: Today we are using Stateless.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.