Wednesday, 31 August 2016

Installing KIE workbench in Apache Tomcat 7 in Windows

KIE Drools Workbench

KIE workbench is the workflow engine where complex business process can be develop and executed. It also provide interface to create guided and DRL rules. The workbench can be linked through a Java application to use the created rule in our application.

Installing KIE workbench in Apache Tomcat 7 in Windows

  • Download the Kie workbench from http://download.jboss.org/drools/release/6.4.0.Final/kie-drools-wb-distribution-wars-X.X.X.Final-tomcat7.war. The X.X.X is version of the workbench. It must correspond to the version of Drools engine used.
  • The war file downloaded need to be deployed in the web-app folder of Apache tomcat.
  • Download and copy following jar files into TOMCAT_HOME/lib
    • btm-2.1.4.jar
    • btm-tomcat55-lifecycle-2.1.4.jar
    • h2-1.3.161.jar
    • jta-1.1.jar
    • slf4j-api-1.7.2.jar
    • slf4j-jdk14-1.7.2.jar
    • kie-tomcat-integration
    • JACC (javax.security.jacc:artifactId=javax.security.jacc-api in JBoss Maven Repository)
  • Create configuration files inside TOMCAT_HOME/conf
    • btm-config.properties
      bitronix.tm.serverId=tomcat-btm-node0
      bitronix.tm.journal.disk.logPart1Filename=${btm.root}/work/btm1.tlog
      bitronix.tm.journal.disk.logPart2Filename=${btm.root}/work/btm2.tlog
      bitronix.tm.resource.configuration=${btm.root}/conf/resources.properties
    • resources.properties
      resource.ds1.className=bitronix.tm.resource.jdbc.lrc.LrcXADataSource
      resource.ds1.uniqueName=jdbc/jbpm
      resource.ds1.minPoolSize=10
      resource.ds1.maxPoolSize=20
      resource.ds1.driverProperties.driverClassName=org.h2.Driver
      resource.ds1.driverProperties.url=jdbc:h2:file:C:/apache-tomcat-7.0.53/temp2
      resource.ds1.driverProperties.user=sa
      resource.ds1.driverProperties.password=
      resource.ds1.allowLocalTransactions=true

      The Path in above file need to be change to the actual system path.

  • Create setenv.sh (or setenv.bat) file inside TOMCAT_HOME/bin and add following:
    set CATALINA_OPTS=-Xmx512M -XX:MaxPermSize=512m -Dbtm.root=C:\apache-tomcat-7.0.53\ -Dbitronix.tm.configuration=C:\apache-tomcat-7.0.53\conf\btm-config.properties -Djbpm.tsr.jndi.lookup=java:comp/env/TransactionSynchronizationRegistry -Djava.security.auth.login.config=C:\apache-tomcat-7.0.53\webapps\kie-drools-wb\WEB-INF\classes\login.config -Dorg.jboss.logging.provider=jdk -Dorg.uberfire.nio.git.dir=C:/apache-tomcat-7.0.53/temp
    view raw setenv.bat hosted with ❤ by GitHub
  • Add valve configuration into TOMCAT_HOME/conf/server.xml inside Host element as last valve definition:
    <Valve className="org.kie.integration.tomcat.JACCValve" />
    view raw server.xml hosted with ❤ by GitHub
  • Edit TOMCAT_HOME/conf/tomcat-users.xml to include roles and users, make sure there will be 'analyst' or 'admin' roles defined as it's required to be authorized to use kie-wb
    <user username="tomcat" password="tomcat" roles="manager-gui"/>
    <user username="naved" password="naved" roles="admin"/>
  • Start tomcat and open url http://127.0.0.1:8080/kie-drools-wb- distribution-wars-X.X.X.Final-tomcat7
  • Installing Drools in Eclipse and Creating Simple Hello World Application in Drools

    Drools

      • Drools is a Business rule management system
      • It is used to separate the data from logic.
      • It allow us to write logic (rules) in more layman readable form. This allow business people to create their own rules instead of depending on programmers and developers.

    Rules

    Rules are pieces of knowledge often expressed as, "When some conditions occur, then do some tasks."
    The most important part of a Rule is it’s when part. If the when part is satisfied, the then part is triggered
    rule <rule_name>
    <attribute> <value>
    when
    <conditions>
    then
    <actions>
    end
    view raw sample.drl hosted with ❤ by GitHub

    Installing Drools in Eclipse

    Drools comes with plugin for eclipse to create and manage the project from eclipse itself.

    Prerequisites to install Drools Plugin:

    • Java 1.5 (or higher)
    • Eclipse 4.2

    Installation Step

    Drools Runtime

    Drool runtime is needed to create and execute drool rules.
    • Click on Windows -> Preference -> Drools -> Installed Drools Runtime -> Add -> Browse.
    • Browse to the downloaded binaries folder and Add.

    Example Project

    1. File -> New -> Drool Project -> Create an empty project.
    2. Enter project name -> finish.
    3. The project follow maven folder structure. Two primary folder are
      1. src/main/java – Java code would go here.
      2. src/main/resources – Drool files would go here.
    4. Create a new java class file in src/main/java. Say ‘Hello.java’ in package ‘com.example’.
      package com.example;
      import org.kie.api.KieServices;
      import org.kie.api.runtime.KieContainer;
      import org.kie.api.runtime.KieSession;
      public class Hello {
      private String hellostr = "Hello World";
      public String getHellostr() {
      return hellostr;
      }
      public void setHellostr(String hellostr) {
      this.hellostr = hellostr;
      }
      public static void main(String[] args) {
      Hello hello = new Hello();
      System.out.println("String before firing rule: "+hello.getHellostr());
      // Create knowledge base from default drool file.
      // The default folder from where knowledge base will read
      // drool file is defined /src/main/resources/META-INF/kmodule.xml
      // The default value in this generated file is /src/main/resources/rules
      KieServices ks = KieServices.Factory.get();
      KieContainer kContainer = ks.getKieClasspathContainer();
      KieSession kSession = kContainer.newKieSession("ksession-rules");
      kSession.insert(hello);
      kSession.fireAllRules();
      System.out.println("String after firing rule: "+hello.getHellostr());
      }
      }
      view raw Hello.java hosted with ❤ by GitHub
    5. Create a new drool file in src/main/resources/rules. Say ‘ReplaceHello.drl’.
      import com.example.Hello;
      rule "SayGoodBye"
      when
      h : Hello(hellostr.contains("Hello"))
      then
      h.setHellostr(h.getHellostr().replace("Hello","Goodbye"));
      end
    6. Result.
      String before firing rule: Hello World
      String after firing rule: Goodbye World
      view raw Result hosted with ❤ by GitHub
    Apart from creating the rules file in resources folder, we can import the rules files from classpath as well as file system. Below example show an example of reading the rule file from classpath.
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kieFileSystem = ks.newKieFileSystem();
    kieFileSystem.write(ResourceFactory.newClassPathResource("myrule/HelloRule.drl"));
    KieBuilder builder = ks.newKieBuilder(kieFileSystem).buildAll();
    KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieSession kSession = kContainer.newKieSession();
    if(builder.getResults().hasMessages(Level.ERROR)) {
    throw new RuntimeException(builder.getResults().toString());
    }
    view raw Hello.java hosted with ❤ by GitHub