瀏覽代碼

initial import

Grega Bremec 2 年之前
當前提交
6702021a05

+ 8 - 0
.gitignore

@@ -0,0 +1,8 @@
+.*.sw?
+.DS_Store
+.classpath
+.project
+.settings/
+.vscode/
+target/
+tmp/

+ 122 - 0
pom.xml

@@ -0,0 +1,122 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>com.redhat.training</groupId>
+  <artifactId>greeting-rules</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>greeting-rules</name>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>11</maven.compiler.source>
+    <maven.compiler.target>11</maven.compiler.target>
+    <kie.version>7.59.0.Final-redhat-00010</kie.version>
+  </properties>
+
+  <dependencyManagement>
+    <dependencies>
+      <dependency>
+        <groupId>org.kie</groupId>
+        <artifactId>kie-bom</artifactId>
+        <version>${kie.version}</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.drools</groupId>
+        <artifactId>drools-bom</artifactId>
+        <version>${kie.version}</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.jbpm</groupId>
+        <artifactId>jbpm-bom</artifactId>
+        <version>${kie.version}</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+    </dependencies>
+  </dependencyManagement>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.kie</groupId>
+      <artifactId>kie-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-compiler</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-mvel</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-decisiontables</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.11</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
+      <plugins>
+        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
+        <plugin>
+          <artifactId>maven-clean-plugin</artifactId>
+          <version>3.1.0</version>
+        </plugin>
+        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
+        <plugin>
+          <artifactId>maven-resources-plugin</artifactId>
+          <version>3.0.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>3.8.0</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.22.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-jar-plugin</artifactId>
+          <version>3.0.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-install-plugin</artifactId>
+          <version>2.5.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-deploy-plugin</artifactId>
+          <version>2.8.2</version>
+        </plugin>
+        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
+        <plugin>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>3.7.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-project-info-reports-plugin</artifactId>
+          <version>3.0.0</version>
+        </plugin>
+        <!-- <plugin>
+          <groupId>org.kie</groupId>
+          <artifactId>kie-maven-plugin</artifactId>
+          <version>${kie.version}</version>
+        </plugin> -->
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>

+ 37 - 0
src/main/java/com/redhat/training/App.java

@@ -0,0 +1,37 @@
+package com.redhat.training;
+
+import org.kie.api.KieServices;
+import org.kie.api.runtime.KieSession;
+
+/**
+ * Hello world!
+ *
+ */
+public class App 
+{
+    public static void main( String[] args )
+    {
+        Customer foo = new Customer();
+        foo.setMaritalStatus(MaritalStatus.MARRIED);
+        foo.setGender(Gender.MALE);
+
+        GreetingRequest grq = new GreetingRequest();
+        grq.setCurrentHour(12);
+
+        GreetingResponse grs = new GreetingResponse();
+
+        KieServices ks = KieServices.Factory.get();
+        KieSession s = ks.getKieClasspathContainer().newKieSession();
+
+        s.insert(foo);
+        s.insert(grq);
+        s.insert(grs);
+        s.fireAllRules();
+
+        // at this point, grs should at least contain a greeting
+        System.out.println(grs.getGreeting());
+
+        // now that we also added a decision table, let's see
+        System.out.println(grs.getSalutation());
+    }
+}

+ 18 - 0
src/main/java/com/redhat/training/Customer.java

@@ -0,0 +1,18 @@
+package com.redhat.training;
+
+public class Customer {
+    private Gender gender;
+    private MaritalStatus maritalStatus;
+    public Gender getGender() {
+        return gender;
+    }
+    public void setGender(Gender gender) {
+        this.gender = gender;
+    }
+    public MaritalStatus getMaritalStatus() {
+        return maritalStatus;
+    }
+    public void setMaritalStatus(MaritalStatus maritalStatus) {
+        this.maritalStatus = maritalStatus;
+    }
+}

+ 6 - 0
src/main/java/com/redhat/training/Gender.java

@@ -0,0 +1,6 @@
+package com.redhat.training;
+
+public enum Gender {
+    MALE,
+    FEMALE;
+}

+ 14 - 0
src/main/java/com/redhat/training/GreetingRequest.java

@@ -0,0 +1,14 @@
+package com.redhat.training;
+
+public class GreetingRequest {
+    private int currentHour;
+
+    public int getCurrentHour() {
+        return currentHour;
+    }
+
+    public void setCurrentHour(int currentHour) {
+        this.currentHour = currentHour;
+    }
+    
+}

+ 18 - 0
src/main/java/com/redhat/training/GreetingResponse.java

@@ -0,0 +1,18 @@
+package com.redhat.training;
+
+public class GreetingResponse {
+    private String greeting;
+    private String salutation;
+    public String getGreeting() {
+        return greeting;
+    }
+    public void setGreeting(String greeting) {
+        this.greeting = greeting;
+    }
+    public String getSalutation() {
+        return salutation;
+    }
+    public void setSalutation(String salutation) {
+        this.salutation = salutation;
+    }
+}

+ 6 - 0
src/main/java/com/redhat/training/MaritalStatus.java

@@ -0,0 +1,6 @@
+package com.redhat.training;
+
+public enum MaritalStatus {
+    SINGLE,
+    MARRIED;
+}

+ 8 - 0
src/main/resources/META-INF/kmodule.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xmlns="http://jboss.org/kie/6.0.0/kmodule">
+
+    <kbase name="foo" packages="com.redhat.training" default="true">
+        <ksession name="bar" default="true"/>
+    </kbase>
+</kmodule>

+ 49 - 0
src/main/resources/com/redhat/training/greeting_rules.drl

@@ -0,0 +1,49 @@
+package com.redhat.training
+
+rule "Good Morning"
+when
+    GreetingRequest(currentHour < 11, currentHour >= 7)
+    rsp: GreetingResponse(this != null)
+then
+    rsp.setGreeting("Good Morning");
+end
+
+rule "Too Early"
+when
+    GreetingRequest(currentHour < 7, currentHour >= 0)
+    rsp: GreetingResponse(this != null)
+then
+    rsp.setGreeting("Hello, Sleepless");
+end
+
+rule "Good Afternoon"
+when
+    GreetingRequest(currentHour < 17, currentHour >= 11)
+    rsp: GreetingResponse(this != null)
+then
+    rsp.setGreeting("Good Afternoon");
+end
+
+rule "Good Evening"
+when
+    GreetingRequest(currentHour < 22, currentHour >= 17)
+    rsp: GreetingResponse(this != null)
+then
+    rsp.setGreeting("Good Evening");
+end
+
+rule "Good Night"
+when
+    GreetingRequest(currentHour < 24, currentHour >= 22)
+    rsp: GreetingResponse(this != null)
+then
+    rsp.setGreeting("Good Night");
+end
+
+rule "Invalid Time"
+when
+    GreetingRequest(currentHour >= 24)
+    rsp: GreetingResponse(this != null)
+then
+    rsp.setGreeting("ERROR: Invalid Time");
+end

二進制
src/main/resources/com/redhat/training/salutation.xls


+ 20 - 0
src/test/java/com/redhat/training/AppTest.java

@@ -0,0 +1,20 @@
+package com.redhat.training;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+{
+    /**
+     * Rigorous Test :-)
+     */
+    @Test
+    public void shouldAnswerWithTrue()
+    {
+        assertTrue( true );
+    }
+}