Repository: servicemix Updated Branches: refs/heads/master ae8a50de5 -> 8ae570000
SM-2543: Provide Spring example for Drools 6 (closes #26) 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/c928d62f Tree: http://git-wip-us.apache.org/repos/asf/servicemix/tree/c928d62f Diff: http://git-wip-us.apache.org/repos/asf/servicemix/diff/c928d62f Branch: refs/heads/master Commit: c928d62f03a36441f7be364f4ff81adb15ee156b Parents: ae8a50d Author: Grzegorz Halajko <[email protected]> Authored: Tue May 26 21:06:32 2015 +0200 Committer: Krzysztof Sobkowiak <[email protected]> Committed: Tue May 26 23:19:04 2015 +0200 ---------------------------------------------------------------------- .../src/main/filtered-resources/examples.xml | 5 + examples/drools/drools-spring/README.txt | 106 +++++++++++++++ examples/drools/drools-spring/pom.xml | 73 ++++++++++ .../examples/drools/spring/model/Customer.java | 80 +++++++++++ .../drools/spring/model/CustomerType.java | 22 ++++ .../spring/osgi/DebugAgendaEventListener.java | 132 +++++++++++++++++++ .../osgi/DebugRuleRuntimeEventListener.java | 61 +++++++++ .../drools/spring/osgi/SimpleRuleBean.java | 83 ++++++++++++ .../examples/drools/spring/osgi/Utils.java | 37 ++++++ .../resources/META-INF/spring/simple-spring.xml | 55 ++++++++ .../src/main/resources/rule/customer-score.drl | 48 +++++++ examples/drools/pom.xml | 1 + .../itests/Drools6IntegrationTests.scala | 16 +++ 13 files changed, 719 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/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 fe9ad22..8bfe111 100644 --- a/assembly/src/main/filtered-resources/examples.xml +++ b/assembly/src/main/filtered-resources/examples.xml @@ -172,4 +172,9 @@ <bundle>mvn:org.apache.servicemix.examples/drools-camel-blueprint/${version}</bundle> </feature> + <feature name="examples-drools-spring" version="${version}" resolver="(obr)"> + <feature version="${drools6.version}">kie-spring</feature> + <bundle>mvn:org.apache.servicemix.examples/drools-spring/${version}</bundle> + </feature> + </features> http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/README.txt ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/README.txt b/examples/drools/drools-spring/README.txt new file mode 100644 index 0000000..f425fde --- /dev/null +++ b/examples/drools/drools-spring/README.txt @@ -0,0 +1,106 @@ +/* + * 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 with spring. + + +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 spring support for the Drools 6. + + feature:install kie-spring + +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-spring/${project.version} + +4. Once the bundle has been started, you will see on console logs from rule engine. + + | ------------------- START ------------------ + KieSession fireAllRules. Customer [salary=978, type=null] + DebugRuleRuntimeEventListener | 286 - drools-spring - 6.0.0.SNAPSHOT | objectInserted ==>[ObjectInsertedEventImpl: + getFactHandle()=[fact 0:1:1887740067:18877 40067:1:DEFAULT:NON_TRAIT:Customer [salary=978, type=null]], + getObject()=Customer [salary=978, type=null], ...... + + + +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 :: Spring + +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/c928d62f/examples/drools/drools-spring/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/pom.xml b/examples/drools/drools-spring/pom.xml new file mode 100644 index 0000000..e922827 --- /dev/null +++ b/examples/drools/drools-spring/pom.xml @@ -0,0 +1,73 @@ +<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-spring</artifactId> + <name>Apache ServiceMix :: Examples :: Drools :: Spring</name> + <packaging>bundle</packaging> + <properties> + <osgi.private.package> + org.apache.servicemix.examples.drools.spring.osgi, + org.apache.servicemix.examples.drools.spring.model + </osgi.private.package> + <osgi.import.package> + org.slf4j;version="[1.6,2)", + org.kie.api.*;version="[6.0,7)", + org.kie.spring;version="[6.0,7)", + org.kie.internal.command;version="[6.0,7)", + org.drools.osgi.spring;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-osgi-integration</artifactId> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </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/c928d62f/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/Customer.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/Customer.java b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/Customer.java new file mode 100644 index 0000000..8fbc587 --- /dev/null +++ b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/Customer.java @@ -0,0 +1,80 @@ +/* + * 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.spring.model; + + +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 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/c928d62f/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/CustomerType.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/CustomerType.java b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/CustomerType.java new file mode 100644 index 0000000..c989fbf --- /dev/null +++ b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/model/CustomerType.java @@ -0,0 +1,22 @@ +/* + * 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.spring.model; + + +public enum CustomerType { + POOR, NORMAL, VIP; +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugAgendaEventListener.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugAgendaEventListener.java b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugAgendaEventListener.java new file mode 100644 index 0000000..159b683 --- /dev/null +++ b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugAgendaEventListener.java @@ -0,0 +1,132 @@ +/* + * 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.spring.osgi; + +import org.kie.api.event.rule.AfterMatchFiredEvent; +import org.kie.api.event.rule.AgendaEventListener; +import org.kie.api.event.rule.AgendaGroupPoppedEvent; +import org.kie.api.event.rule.AgendaGroupPushedEvent; +import org.kie.api.event.rule.BeforeMatchFiredEvent; +import org.kie.api.event.rule.MatchCancelledEvent; +import org.kie.api.event.rule.MatchCreatedEvent; +import org.kie.api.event.rule.RuleFlowGroupActivatedEvent; +import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Drools 6 Rule Engine events logger. + */ +public class DebugAgendaEventListener implements AgendaEventListener { + /** + * Logger. + */ + private static final Logger log = LoggerFactory + .getLogger(DebugAgendaEventListener.class); + + /** + * @see AgendaEventListener#matchCreated(org.kie.api.event.rule.MatchCreatedEvent) + */ + @Override + public void matchCreated(MatchCreatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#matchCancelled(org.kie.api.event.rule.MatchCancelledEvent) + */ + @Override + public void matchCancelled(MatchCancelledEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent) + */ + @Override + public void beforeMatchFired(BeforeMatchFiredEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent) + */ + @Override + public void afterMatchFired(AfterMatchFiredEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent) + */ + @Override + public void agendaGroupPopped(AgendaGroupPoppedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent) + */ + @Override + public void agendaGroupPushed(AgendaGroupPushedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) + */ + @Override + public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) + */ + @Override + public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) + */ + @Override + public void beforeRuleFlowGroupDeactivated( + RuleFlowGroupDeactivatedEvent event) { + log.info("{}", event); + + } + + /** + * @see AgendaEventListener#beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) + */ + @Override + public void afterRuleFlowGroupDeactivated( + RuleFlowGroupDeactivatedEvent event) { + log.info("{}", event); + + } +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugRuleRuntimeEventListener.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugRuleRuntimeEventListener.java b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugRuleRuntimeEventListener.java new file mode 100644 index 0000000..ce79a47 --- /dev/null +++ b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/DebugRuleRuntimeEventListener.java @@ -0,0 +1,61 @@ +/* + * 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.spring.osgi; + +import org.kie.api.event.rule.ObjectDeletedEvent; +import org.kie.api.event.rule.ObjectInsertedEvent; +import org.kie.api.event.rule.ObjectUpdatedEvent; +import org.kie.api.event.rule.RuleRuntimeEventListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Drools 6 Working memory events. + */ +public class DebugRuleRuntimeEventListener implements RuleRuntimeEventListener { + /** + * Logger. + */ + private static final Logger log = LoggerFactory + .getLogger(DebugRuleRuntimeEventListener.class); + + /** + * @see RuleRuntimeEventListener#objectInserted(ObjectInsertedEvent) + */ + @Override + public void objectInserted(ObjectInsertedEvent event) { + log.info("objectInserted {}", event); + } + + /** + * @see RuleRuntimeEventListener#objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent) + */ + @Override + public void objectUpdated(ObjectUpdatedEvent event) { + log.info("objectUpdated {}", event); + + } + + /** + * @see RuleRuntimeEventListener#objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent) + */ + @Override + public void objectDeleted(ObjectDeletedEvent event) { + log.info("objectDeleted {}", event); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/SimpleRuleBean.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/SimpleRuleBean.java b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/SimpleRuleBean.java new file mode 100644 index 0000000..41f76ea --- /dev/null +++ b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/SimpleRuleBean.java @@ -0,0 +1,83 @@ +/* + * 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.spring.osgi; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.servicemix.examples.drools.spring.model.Customer; +import org.kie.api.command.Command; +import org.kie.api.runtime.ExecutionResults; +import org.kie.api.runtime.StatelessKieSession; +import org.kie.internal.command.CommandFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.servicemix.examples.drools.spring.osgi.Utils.customer; + +/** + * Simple spring bean. + */ +public class SimpleRuleBean { + /** + * LOGGER. + */ + protected static final transient Logger logger = LoggerFactory + .getLogger(SimpleRuleBean.class); + + /** + * StatelessKieSession + */ + private StatelessKieSession ksession; + + /** + * + */ + public void start() throws Exception { + + for (int i = 0; i < 10; i++) { + Customer customer = customer(); + logger.info("------------------- START ------------------\n" + + " KieSession fireAllRules. {}", customer); + + List<Command<?>> commands = new ArrayList<Command<?>>(); + + commands.add(CommandFactory.newInsert(customer, "customer")); + commands.add(CommandFactory.newFireAllRules("num-rules-fired")); + + ExecutionResults results = ksession.execute(CommandFactory + .newBatchExecution(commands)); + + int fired = Integer.parseInt(results.getValue("num-rules-fired") + .toString()); + + customer = (Customer)results.getValue("customer"); + + logger.info("After rule rules-fired={} {} \n" + + "------------------- STOP ---------------------", fired, + customer); + } + } + + /** + * @param ksession the ksession to set + */ + public void setKsession(StatelessKieSession ksession) { + this.ksession = ksession; + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/Utils.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/Utils.java b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/Utils.java new file mode 100644 index 0000000..82ebe1d --- /dev/null +++ b/examples/drools/drools-spring/src/main/java/org/apache/servicemix/examples/drools/spring/osgi/Utils.java @@ -0,0 +1,37 @@ +/* + * 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.spring.osgi; + +import java.util.Random; + +import org.apache.servicemix.examples.drools.spring.model.Customer; + + +public class Utils { + + /** + * + */ + private static final Random rand = new Random(12345); + + /** + * @return + */ + public static Customer customer() { + return new Customer(rand.nextInt(9999)); + } +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/src/main/resources/META-INF/spring/simple-spring.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/resources/META-INF/spring/simple-spring.xml b/examples/drools/drools-spring/src/main/resources/META-INF/spring/simple-spring.xml new file mode 100644 index 0000000..43877f4 --- /dev/null +++ b/examples/drools/drools-spring/src/main/resources/META-INF/spring/simple-spring.xml @@ -0,0 +1,55 @@ +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:kie="http://drools.org/schema/kie-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd"> + + <bean id="kiePostProcessor" + class="org.drools.osgi.spring.OsgiKModuleBeanFactoryPostProcessor" /> + + <kie:kmodule id="id-kmodule"> + <kie:kbase name="SimpleRuleKBase" default="true" + packages="rule"> + <kie:ksession name="simpleRuleKSession" type="stateless" + default="true" listeners-ref="debugListeners"> + </kie:ksession> + </kie:kbase> + </kie:kmodule> + + + <bean id="DebugAgendaEventListener" + class="org.apache.servicemix.examples.drools.spring.osgi.DebugAgendaEventListener" /> + <bean id="DebugRuleRuntimeEventListener" + class="org.apache.servicemix.examples.drools.spring.osgi.DebugRuleRuntimeEventListener" /> + + <kie:eventListeners id="debugListeners"> + <kie:agendaEventListener ref="DebugAgendaEventListener" /> + <kie:ruleRuntimeEventListener ref="DebugRuleRuntimeEventListener" /> + <kie:processEventListener /> + </kie:eventListeners> + + <!-- Bean create facts and rune drools engine --> + <bean + class="org.apache.servicemix.examples.drools.spring.osgi.SimpleRuleBean" + init-method="start"> + <property name="ksession" ref="simpleRuleKSession" /> + </bean> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/examples/drools/drools-spring/src/main/resources/rule/customer-score.drl ---------------------------------------------------------------------- diff --git a/examples/drools/drools-spring/src/main/resources/rule/customer-score.drl b/examples/drools/drools-spring/src/main/resources/rule/customer-score.drl new file mode 100644 index 0000000..b2f276c --- /dev/null +++ b/examples/drools/drools-spring/src/main/resources/rule/customer-score.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.spring.model.Customer; +import org.apache.servicemix.examples.drools.spring.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/c928d62f/examples/drools/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/pom.xml b/examples/drools/pom.xml index 1cd4778..97291cd 100644 --- a/examples/drools/pom.xml +++ b/examples/drools/pom.xml @@ -76,5 +76,6 @@ <modules> <module>drools-simple</module> <module>drools-camel-blueprint</module> + <module>drools-spring</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/servicemix/blob/c928d62f/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 index ddaf439..371e138 100644 --- a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala +++ b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala @@ -101,3 +101,19 @@ class Drools6ExamplesTest extends Drools6IntegrationTests { } } } + +/** + * Tests for the Drools with spring + */ +class Drools6SpringExamplesTest extends Drools6IntegrationTests { + + /** + * Test installation Spring with drools example. + */ + @Test + def testDroolsSpringExample = testWithFeature("examples-drools-spring") { + expect { + logging.containsMessage(line => line.contains("KieModule was added: org.drools.osgi.compiler.OsgiKieModule")) + } + } +}
