Repository: servicemix Updated Branches: refs/heads/master dc3801887 -> 532cfb48c
SM-2536: Provide simple sample for Drools 6 (closes #22) Thanks to Grzegorz Halajko for the pull request!! Project: http://git-wip-us.apache.org/repos/asf/servicemix/repo Commit: http://git-wip-us.apache.org/repos/asf/servicemix/commit/532cfb48 Tree: http://git-wip-us.apache.org/repos/asf/servicemix/tree/532cfb48 Diff: http://git-wip-us.apache.org/repos/asf/servicemix/diff/532cfb48 Branch: refs/heads/master Commit: 532cfb48c73e687825e50a9ae4dd84c8cc8a0703 Parents: dc38018 Author: Grzegorz Halajko <[email protected]> Authored: Wed Apr 22 11:49:25 2015 +0000 Committer: Krzysztof Sobkowiak <[email protected]> Committed: Wed Apr 29 20:20:42 2015 +0200 ---------------------------------------------------------------------- .../src/main/filtered-resources/examples.xml | 5 + examples/drools/drools-simple/README.txt | 112 +++++++++++++++++++ examples/drools/drools-simple/pom.xml | 95 ++++++++++++++++ .../examples/drools/simple/model/Customer.java | 87 ++++++++++++++ .../drools/simple/model/CustomerType.java | 26 +++++ .../drools/simple/osgi/SimpleRuleActivator.java | 104 +++++++++++++++++ .../examples/drools/simple/osgi/Utils.java | 51 +++++++++ .../src/main/resources/META-INF/kmodule.xml | 23 ++++ .../src/main/resources/rule/simple.drl | 48 ++++++++ .../examples/drools/test/ActivatorTest.java | 41 +++++++ .../examples/drools/test/SimpleTest.java | 104 +++++++++++++++++ examples/drools/pom.xml | 80 +++++++++++++ examples/pom.xml | 1 + .../itests/Drools6IntegrationTest.scala | 71 ------------ .../itests/Drools6IntegrationTests.scala | 96 ++++++++++++++++ 15 files changed, 873 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/assembly/src/main/filtered-resources/examples.xml ---------------------------------------------------------------------- diff --git a/assembly/src/main/filtered-resources/examples.xml b/assembly/src/main/filtered-resources/examples.xml index 9ba6061..5ec1730 100644 --- a/assembly/src/main/filtered-resources/examples.xml +++ b/assembly/src/main/filtered-resources/examples.xml @@ -161,4 +161,9 @@ <bundle>mvn:org.apache.servicemix.examples/akka-camel/${version}</bundle> </feature> + <feature name="examples-drools-simple" version="${version}" resolver="(obr)"> + <feature version="${drools6.version}">drools6-module</feature> + <bundle>mvn:org.apache.servicemix.examples/drools-simple/${version}</bundle> + </feature> + </features> http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/README.txt ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/README.txt b/examples/drools/drools-simple/README.txt new file mode 100644 index 0000000..b22fce4 --- /dev/null +++ b/examples/drools/drools-simple/README.txt @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +Drools 6 Simple Example +========================== + +Purpose +------- +This example will show you how to use Drools 6 inside Apache ServiceMix and how to +use rule engine in low level. + +We use OSGi activator to create theKIE Session, insert facts to rule engine and print +the result to server log. + + + +Prerequisites for Running the Example +------------------------------------- +1. You must have the following installed on your machine: + + - JDK 1.6 or higher + + - Maven 3.0.2 or higher (for building) + + For more information, see the README in the top-level examples + directory. + +2. Start ServiceMix by running the following command: + + <servicemix_home>/bin/servicemix (on UNIX) + <servicemix_home>\bin\servicemix (on Windows) + + +Running the Example +------------------- +To install and run the example where you build the example bundle +yourself, complete the following steps: + +1. Before being able to run this example, you have to install some additional + features into the container first to add support for the Drools 6. + + feature:install drools6-module + +2. Build the example by opening a command prompt, changing directory to + examples/drools/drools-simple (this example) and entering the following Maven + command: + + mvn clean install + + If all of the required OSGi bundles are available in your local Maven + repository, the example will build very quickly. Otherwise it may + take some time for Maven to download everything it needs. + + The mvn install command builds the example deployment bundle and + copies it to your local Maven repository and to the target directory + of this example. + +3. Install the example by entering the following command in + the ServiceMix console: + + bundle:install -s mvn:org.apache.servicemix.examples/drools-simple/${project.version} + +4. Once the bundle has been started, you will see on console logs from rule engine. + + ==>[AfterActivationFiredEvent: getActivation()=[[ Customer VIP active=false ] + [ [fact 0:3:115401058:115401058:6:DEFAULT:NON_TRAIT:Customer [salary=9001, type=VIP]] ] ], + getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@7631375e] + +and in server logs + + KieSession fireAllRules. Customer [salary=9001, type=null] + After rule Customer [salary=9001, type=VIP] + + +Stopping and Uninstalling the Example +------------------------------------- +First, find the bundle id for the deployed example bundle by doing + + osgi:list + +and looking for a line that looks like this one + +229 | Active | 80 | | Apache ServiceMix :: Examples :: Drools :: Simple + +In the above case, the bundle id would be 229 + + +To stop the example, enter the following command in the ServiceMix +console: + + bundle:stop <bundle_id> + + +To uninstall the example, enter one of the following commands in +the ServiceMix console: + + bundle:uninstall <bundle_id> + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/pom.xml b/examples/drools/drools-simple/pom.xml new file mode 100644 index 0000000..8d73c87 --- /dev/null +++ b/examples/drools/drools-simple/pom.xml @@ -0,0 +1,95 @@ +<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"> + + <!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.servicemix.examples</groupId> + <artifactId>drools</artifactId> + <version>6.0.0-SNAPSHOT</version> + </parent> + <artifactId>drools-simple</artifactId> + <name>Apache ServiceMix :: Examples :: Drools :: Simple</name> + <packaging>bundle</packaging> + + <properties> + <osgi.private.package>org.apache.servicemix.examples.drools.simple.osgi</osgi.private.package> + <osgi.bundle.activator>org.apache.servicemix.examples.drools.simple.osgi.SimpleRuleActivator + </osgi.bundle.activator> + <osgi.export.package>org.apache.servicemix.examples.drools.simple.model</osgi.export.package> + <osgi.import.package> + org.osgi.framework;version="[1.5,2)", + org.slf4j;version="[1.6,2)", + org.kie.api.*;version="[6.0,7)", + </osgi.import.package> + </properties> + + <dependencies> + <dependency> + <groupId>org.kie</groupId> + <artifactId>kie-api</artifactId> + </dependency> + <dependency> + <groupId>org.drools</groupId> + <artifactId>drools-core</artifactId> + </dependency> + <dependency> + <groupId>org.drools</groupId> + <artifactId>drools-compiler</artifactId> + </dependency> + <dependency> + <groupId>org.drools</groupId> + <artifactId>drools-osgi-integration</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.framework</artifactId> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/Customer.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/Customer.java b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/Customer.java new file mode 100644 index 0000000..cbe521c --- /dev/null +++ b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/Customer.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.simple.model; + +/** + * @author ghalajko + * + */ +public class Customer { + + /** + * Salary + */ + private int salary; + + /** + * IS VIP. + */ + private CustomerType type; + + /** + * Create customer with salary. + * + * @param salary + */ + public Customer(int salary) { + this.salary = salary; + } + + /** + * @return the salary + */ + public int getSalary() { + return salary; + } + + /** + * @return the type + */ + public CustomerType getType() { + return type; + } + + /** + * @param salary + * the salary to set + */ + public void setSalary(int salary) { + this.salary = salary; + } + + /** + * @param type + * the type to set + */ + public void setType(CustomerType type) { + this.type = type; + } + + /** + * + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Customer [salary=").append(salary).append(", type=") + .append(type).append("]"); + return builder.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/CustomerType.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/CustomerType.java b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/CustomerType.java new file mode 100644 index 0000000..273800a --- /dev/null +++ b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/model/CustomerType.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.simple.model; + +/** + * + * @author ghalajko + * + */ +public enum CustomerType { + POOR, NORMAL, VIP; +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/SimpleRuleActivator.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/SimpleRuleActivator.java b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/SimpleRuleActivator.java new file mode 100644 index 0000000..dce296d --- /dev/null +++ b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/SimpleRuleActivator.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.simple.osgi; + +import static org.apache.servicemix.examples.drools.simple.osgi.Utils.customerPoor; +import static org.apache.servicemix.examples.drools.simple.osgi.Utils.customerNormal; +import static org.apache.servicemix.examples.drools.simple.osgi.Utils.customerVip; + +import org.apache.servicemix.examples.drools.simple.model.Customer; +import org.kie.api.KieBase; +import org.kie.api.KieServices; +import org.kie.api.event.rule.DebugAgendaEventListener; +import org.kie.api.event.rule.DebugRuleRuntimeEventListener; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; +import org.kie.api.runtime.rule.FactHandle; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author ghalajko + * + */ +public class SimpleRuleActivator implements BundleActivator { + /** + * LOGGER. + */ + private static final Logger logger = LoggerFactory + .getLogger(SimpleRuleActivator.class); + + /** + * KieSession. + */ + private KieSession ksession; + + /** + * + */ + @Override + public void start(BundleContext context) throws Exception { + KieServices ks = KieServices.Factory.get(); + KieContainer kcont = ks.newKieClasspathContainer(getClass() + .getClassLoader()); + KieBase kbase = kcont.getKieBase("SimpleRuleKBase"); + + logger.info("KieSession newKieSession."); + ksession = kbase.newKieSession(); + + ksession.addEventListener(new DebugAgendaEventListener()); + ksession.addEventListener(new DebugRuleRuntimeEventListener()); + + Customer customer = customerPoor(); + + logger.info("KieSession fireAllRules. {}", customer); + FactHandle fh = ksession.insert(customer); + ksession.fireAllRules(); + ksession.delete(fh); + logger.info("After rule {}", customer); + + customer = customerNormal(); + logger.info("KieSession fireAllRules. {}", customer); + fh = ksession.insert(customer); + ksession.fireAllRules(); + ksession.delete(fh); + logger.info("After rule {}", customer); + + customer = customerVip(); + logger.info("KieSession fireAllRules. {}", customer); + fh = ksession.insert(customer); + ksession.fireAllRules(); + ksession.delete(fh); + + logger.info("After rule {}", customer); + } + + /** + * Dispose ksession + */ + @Override + public void stop(BundleContext context) throws Exception { + if (ksession != null) { + ksession.dispose(); + logger.debug("KieSession disposed."); + } + + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/Utils.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/Utils.java b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/Utils.java new file mode 100644 index 0000000..7e9861a --- /dev/null +++ b/examples/drools/drools-simple/src/main/java/org/apache/servicemix/examples/drools/simple/osgi/Utils.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.simple.osgi; + +import org.apache.servicemix.examples.drools.simple.model.Customer; + +/** + * @author ghalajko + * + */ +public final class Utils { + + /** + * + * @return + */ + public static Customer customerPoor() { + return new Customer(1000); + } + + /** + * + * @return + */ + public static Customer customerNormal() { + return new Customer(5000); + } + + /** + * + * @return + */ + public static Customer customerVip() { + return new Customer(9001); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/main/resources/META-INF/kmodule.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/main/resources/META-INF/kmodule.xml b/examples/drools/drools-simple/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 0000000..c07213d --- /dev/null +++ b/examples/drools/drools-simple/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> + <kbase name="SimpleRuleKBase" packages="rule" + default="true"> + <ksession name="simpleRuleKSession" default="true"/> + </kbase> +</kmodule> http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/main/resources/rule/simple.drl ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/main/resources/rule/simple.drl b/examples/drools/drools-simple/src/main/resources/rule/simple.drl new file mode 100644 index 0000000..ac7b964 --- /dev/null +++ b/examples/drools/drools-simple/src/main/resources/rule/simple.drl @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package rule; +import org.apache.servicemix.examples.drools.simple.model.Customer; +import org.apache.servicemix.examples.drools.simple.model.CustomerType; + +rule "Customer poor" +when + $c : Customer( salary <= 1000, type == null) +then + modify( $c ) { + setType(CustomerType.POOR) + }; +end + + +rule "Customer NORMAL" +when + $c : Customer( salary > 1000, salary <= 9000, type == null ) +then + modify( $c ) { + setType(CustomerType.NORMAL) + }; +end + + +rule "Customer VIP" +when + $c : Customer( salary > 9000, type == null) +then + modify( $c ) { + setType(CustomerType.VIP) + }; +end \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/ActivatorTest.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/ActivatorTest.java b/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/ActivatorTest.java new file mode 100644 index 0000000..02b2bb7 --- /dev/null +++ b/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/ActivatorTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.test; + +import org.apache.servicemix.examples.drools.simple.osgi.SimpleRuleActivator; +import org.junit.Test; + +/** + * + * @author ghalajko + * + */ +public class ActivatorTest { + /** + * Simple test. Checks method + * @throws Exception + */ + @Test + public void test() throws Exception{ + SimpleRuleActivator act = new SimpleRuleActivator(); + try{ + act.start(null); + }finally{ + act.stop(null); + } + } +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/SimpleTest.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/SimpleTest.java b/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/SimpleTest.java new file mode 100644 index 0000000..e04bd41 --- /dev/null +++ b/examples/drools/drools-simple/src/test/java/org/apache/servicemix/examples/drools/test/SimpleTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.examples.drools.test; + +import org.apache.servicemix.examples.drools.simple.model.Customer; +import org.apache.servicemix.examples.drools.simple.model.CustomerType; +import org.apache.servicemix.examples.drools.simple.osgi.Utils; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.kie.api.KieBase; +import org.kie.api.KieServices; +import org.kie.api.event.rule.DebugAgendaEventListener; +import org.kie.api.event.rule.DebugRuleRuntimeEventListener; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; +import org.kie.api.runtime.rule.FactHandle; + +/** + * + * @author ghalajko + * + */ +public class SimpleTest { + /** + * KieSession + */ + private static KieSession ksession; + + /** + * beforeClass. + */ + @BeforeClass + public static void beforeClass() { + KieServices ks = KieServices.Factory.get(); + KieContainer kcont = ks.newKieClasspathContainer(); + KieBase kbase = kcont.getKieBase("SimpleRuleKBase"); + ksession = kbase.newKieSession(); + ksession.addEventListener(new DebugAgendaEventListener()); + ksession.addEventListener(new DebugRuleRuntimeEventListener()); + } + + /** + * afterClass. + */ + @BeforeClass + public static void afterClass() { + if (null != ksession) { + ksession.dispose(); + } + } + + /** + * Rule Poor + */ + @Test + public void testPool() { + test(Utils.customerPoor(), CustomerType.POOR); + } + + /** + * Rule Normal + */ + @Test + public void testNormal() { + test(Utils.customerNormal(), CustomerType.NORMAL); + } + + /** + * Rule Poor + */ + @Test + public void testVip() { + test(Utils.customerVip(), CustomerType.VIP); + } + + /** + * + */ + private void test(Customer customer, CustomerType expected) { + FactHandle fh = ksession.insert(customer); + + ksession.fireAllRules(); + + ksession.delete(fh); + + Assert.assertEquals(expected, customer.getType()); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/drools/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/pom.xml b/examples/drools/pom.xml new file mode 100644 index 0000000..7054a87 --- /dev/null +++ b/examples/drools/pom.xml @@ -0,0 +1,80 @@ +<?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"> + + <!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.servicemix.examples</groupId> + <artifactId>examples</artifactId> + <version>6.0.0-SNAPSHOT</version> + </parent> + + <artifactId>drools</artifactId> + <packaging>pom</packaging> + <name>Apache ServiceMix :: Examples :: Drools</name> + + <properties> + <osgi.bundle.activator></osgi.bundle.activator> + <osgi.import.package>!*</osgi.import.package> + <osgi.private.package></osgi.private.package> + <osgi.export.package>!*</osgi.export.package> + </properties> + + + <dependencyManagement> + <dependencies> + <!-- define drools BOM --> + <dependency> + <groupId>org.drools</groupId> + <artifactId>drools-bom</artifactId> + <type>pom</type> + <version>${drools6.version}</version> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> + <Bundle-Name>${project.name}</Bundle-Name> + <Bundle-Activator>${osgi.bundle.activator}</Bundle-Activator> + <Import-Package>${osgi.import.package}</Import-Package> + <Private-Package>${osgi.private.package}</Private-Package> + <Export-Package>${osgi.export.package}</Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + + </pluginManagement> + </build> + + <modules> + <module>drools-simple</module> + </modules> +</project> http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index e52b60d..57b1c26 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -39,6 +39,7 @@ <module>akka</module> <module>camel</module> <module>cxf</module> + <module>drools</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTest.scala ---------------------------------------------------------------------- diff --git a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTest.scala b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTest.scala deleted file mode 100644 index f63bbac..0000000 --- a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTest.scala +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.servicemix.itests - -import org.junit.runner.RunWith -import org.junit.{ Ignore, Test } -import org.ops4j.pax.exam.spi.reactors.{ PerMethod, PerClass, ExamReactorStrategy } -import org.ops4j.pax.exam.Configuration -import org.ops4j.pax.exam.junit.PaxExam -import org.ops4j.pax.exam.CoreOptions._ -import org.ops4j.pax.exam.karaf.options.KarafDistributionOption._ -import org.apache.karaf.features.Feature - -/** - * Base configuration for all Drools 6 integration tests - */ -@RunWith(classOf[PaxExam]) -@ExamReactorStrategy(Array(classOf[PerClass])) -class Drools6IntegrationTest extends ExamplesIntegrationTests { - /** - * Check feature installation - */ - def installed(feature: String): Option[String] = { - var f: Feature = featuresService.getFeature(feature) - if (featuresService.isInstalled(f)) Some("Ok") else None - } - - /** - * Test for The Drools 6 feature kie-aries-blueprint - */ - @Test - def testKieBlueprintFeature = testWithFeature("kie-aries-blueprint") { - expect { - installed("kie-aries-blueprint") - } - } - - /** - * Test for The Drools 6 feature kie-spring - */ - @Test - def testKieSpringFeature = testWithFeature("kie-spring") { - expect { - installed("kie-spring") - } - } - - /** - * Test for The Drools 6 feature kie-camel - */ - @Test - def testKieCamelFeature = testWithFeature("kie-camel") { - expect { - installed("kie-camel") - } - } -} http://git-wip-us.apache.org/repos/asf/servicemix/blob/532cfb48/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala ---------------------------------------------------------------------- diff --git a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala new file mode 100644 index 0000000..d903122 --- /dev/null +++ b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.servicemix.itests + +import org.junit.runner.RunWith +import org.junit.{ Ignore, Test } +import org.ops4j.pax.exam.spi.reactors.{ PerMethod, PerClass, ExamReactorStrategy } +import org.ops4j.pax.exam.Configuration +import org.ops4j.pax.exam.junit.PaxExam +import org.ops4j.pax.exam.CoreOptions._ +import org.ops4j.pax.exam.karaf.options.KarafDistributionOption._ +import org.apache.karaf.features.Feature + +/** + * Base configuration for all Drools 6 integration tests + */ +@RunWith(classOf[PaxExam]) +@ExamReactorStrategy(Array(classOf[PerClass])) +abstract class Drools6IntegrationTests extends ExamplesIntegrationTests { + +} +/** + * Tests for the Drools feature installation + */ +class Drools6FeatureTest extends Drools6IntegrationTests { + /** + * Check feature installation + */ + def installed(feature: String): Option[String] = { + var f: Feature = featuresService.getFeature(feature) + if (featuresService.isInstalled(f)) Some("Ok") else None + } + + /** + * Test for The Drools 6 feature kie-aries-blueprint + */ + @Test + def testKieBlueprintFeature = testWithFeature("kie-aries-blueprint") { + expect { + installed("kie-aries-blueprint") + } + } + + /** + * Test for The Drools 6 feature kie-spring + */ + @Test + def testKieSpringFeature = testWithFeature("kie-spring") { + expect { + installed("kie-spring") + } + } + + /** + * Test for The Drools 6 feature kie-camel + */ + @Test + def testKieCamelFeature = testWithFeature("kie-camel") { + expect { + installed("kie-camel") + } + } +} + +/** + * Tests for the Drools examples + */ +class Drools6ExamplesTest extends Drools6IntegrationTests { + + @Test + def testDroolsSimpleExample = testWithFeature("examples-drools-simple") { + expect { + logging.containsMessage(line => line.contains("Customer [salary=1000, type=POOR]")) + } + expect { + logging.containsMessage(line => line.contains("Customer [salary=5000, type=NORMAL]")) + } + expect { + logging.containsMessage(line => line.contains("Customer [salary=9001, type=VIP]")) + } + } +}
