Repository: servicemix Updated Branches: refs/heads/master cae255f75 -> 4aabf4685
SM-2550: Provide Drools Camel example (closes #24) 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/4aabf468 Tree: http://git-wip-us.apache.org/repos/asf/servicemix/tree/4aabf468 Diff: http://git-wip-us.apache.org/repos/asf/servicemix/diff/4aabf468 Branch: refs/heads/master Commit: 4aabf4685c3d41e9b91d84aab04e7de515ee953c Parents: cae255f Author: Grzegorz Halajko <[email protected]> Authored: Thu Apr 30 09:25:06 2015 +0000 Committer: Krzysztof Sobkowiak <[email protected]> Committed: Sun May 10 23:46:13 2015 +0200 ---------------------------------------------------------------------- .../src/main/filtered-resources/examples.xml | 6 + .../drools/drools-camel-blueprint/README.txt | 116 ++++++++++++++++ examples/drools/drools-camel-blueprint/pom.xml | 91 +++++++++++++ .../drools/camel/blueprint/model/Customer.java | 79 +++++++++++ .../camel/blueprint/model/CustomerType.java | 22 ++++ .../osgi/DebugAgendaEventListener.java | 132 +++++++++++++++++++ .../osgi/DebugRuleRuntimeEventListener.java | 61 +++++++++ .../drools/camel/blueprint/osgi/Utils.java | 52 ++++++++ .../OSGI-INF/blueprint/camel-context.xml | 45 +++++++ .../OSGI-INF/blueprint/kmodule-blueprint.xml | 44 +++++++ .../src/main/resources/rule/customer-score.drl | 48 +++++++ .../examples/drools/simple/model/Customer.java | 4 - .../drools/simple/model/CustomerType.java | 6 +- .../drools/simple/osgi/SimpleRuleActivator.java | 8 +- .../examples/drools/simple/osgi/Utils.java | 20 +-- examples/drools/pom.xml | 1 + .../itests/Drools6IntegrationTests.scala | 7 + 17 files changed, 710 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/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 b3dfdba..fe9ad22 100644 --- a/assembly/src/main/filtered-resources/examples.xml +++ b/assembly/src/main/filtered-resources/examples.xml @@ -165,5 +165,11 @@ <feature version="${drools6.version}">drools6-module</feature> <bundle>mvn:org.apache.servicemix.examples/drools-simple/${version}</bundle> </feature> + + <feature name="examples-drools-camel-blueprint" version="${version}" resolver="(obr)"> + <feature version="${drools6.version}">kie-camel</feature> + <feature version="${drools6.version}">kie-aries-blueprint</feature> + <bundle>mvn:org.apache.servicemix.examples/drools-camel-blueprint/${version}</bundle> + </feature> </features> http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/examples/drools/drools-camel-blueprint/README.txt ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/README.txt b/examples/drools/drools-camel-blueprint/README.txt new file mode 100644 index 0000000..fe0fe01 --- /dev/null +++ b/examples/drools/drools-camel-blueprint/README.txt @@ -0,0 +1,116 @@ +/* + * 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 Camel Example +========================== + +Purpose +------- +This example will show you how to use Drools 6 inside Apache ServiceMix and how to +use blueprint with camel routes. + + +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 kie-camel + feature:install kie-aries-blueprint + +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-camel/${project.version} + +4. Once the bundle has been started, you will see on server logs events from rule engine. + Timer on camel route periodically create random facts and fire rule engine. Rules engine + operation can be seen in the server log file. + + ==>[ActivationCreatedEvent: getActivation()=[[ Customer NORMAL active=false ] [ [fact 0 +:1:1761378644:1761378644:1:DEFAULT:NON_TRAIT:Customer [salary=5796, type=null]] ] ], + getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@675f2d51] +==>[BeforeActivationFiredEvent: getActivation()=[[ Customer NORMAL active=false ] [ [f +act 0:1:1761378644:1761378644:1:DEFAULT:NON_TRAIT:Customer [salary=5796, type=null]] ] ], +getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@675f2d51] +==>[ObjectUpdatedEventImpl: getFactHandle()=[fact 0:1:1761378644:17613786 +44:2:DEFAULT:NON_TRAIT:Customer [salary=5796, type=NORMAL]], getObject()=Customer [salary=5796, type=NORMAL], +getOldObject()=Customer [salary=5796, type=NORMAL], getKnowledgeRuntime()=org.drools.core.impl.Stateful +KnowledgeSessionImpl@675f2d51, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, +factHandle=[fact 0:1:1761378644:1761378644:2:DEFAULT:NON_TRAIT:Customer [salary=5796, type=NORMAL]], +leftTuple=[[ Customer NORMAL active=false ] [ [fact 0:1:1761378644:1761378644:2:DEFAULT:NON_TRAIT:Customer [salary=5796, type=NORMAL]] ] ], + originOffset=-1, propagationNumber=3, rule=[Rule name=Customer NORMAL, agendaGroup=MAIN, salience=0, no-loop=false], type=2]] + + +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 :: Camel + +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/4aabf468/examples/drools/drools-camel-blueprint/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/pom.xml b/examples/drools/drools-camel-blueprint/pom.xml new file mode 100644 index 0000000..fbf8622 --- /dev/null +++ b/examples/drools/drools-camel-blueprint/pom.xml @@ -0,0 +1,91 @@ +<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-camel-blueprint</artifactId> + <name>Apache ServiceMix :: Examples :: Drools :: Camel Blueprint</name> + <packaging>bundle</packaging> + + <properties> + <osgi.private.package> + org.apache.servicemix.examples.drools.camel.blueprint.osgi, + org.apache.servicemix.examples.drools.camel.blueprint.model + </osgi.private.package> + <osgi.import.package> + org.slf4j;version="[1.6,2)", + org.apache.camel;version="[2.15,2.16)", + org.kie.api.*;version="[6.0,7)", + org.kie.aries.blueprint.factorybeans;version="[6.0,7)", + org.kie.internal.command;version="[6.0,7)", + org.drools.core.command.runtime.rule;version="[6.0,7)", + org.drools.core.impl;version="[6.0,7)", + org.osgi.service.blueprint, + </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.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + </dependencies> + + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/Customer.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/Customer.java b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/Customer.java new file mode 100644 index 0000000..b9e3302 --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/Customer.java @@ -0,0 +1,79 @@ +/* + * 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.camel.blueprint.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/4aabf468/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/CustomerType.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/CustomerType.java b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/model/CustomerType.java new file mode 100644 index 0000000..26739ea --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/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.camel.blueprint.model; + + +public enum CustomerType { + POOR, NORMAL, VIP; +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/DebugAgendaEventListener.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/DebugAgendaEventListener.java b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/DebugAgendaEventListener.java new file mode 100644 index 0000000..bd8ed1a --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/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.camel.blueprint.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/4aabf468/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/DebugRuleRuntimeEventListener.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/DebugRuleRuntimeEventListener.java b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/DebugRuleRuntimeEventListener.java new file mode 100644 index 0000000..3b32f12 --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/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.camel.blueprint.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/4aabf468/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/Utils.java ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/Utils.java b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/Utils.java new file mode 100644 index 0000000..990949a --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/java/org/apache/servicemix/examples/drools/camel/blueprint/osgi/Utils.java @@ -0,0 +1,52 @@ +/* + * 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.camel.blueprint.osgi; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.servicemix.examples.drools.camel.blueprint.model.Customer; +import org.kie.api.command.Command; +import org.kie.internal.command.CommandFactory; + + + +public class Utils { + + private static final Random rand = new Random(12345); + + public Customer customer() { + return new Customer(rand.nextInt(9999)); + } + + + public void insertAndFireAll(Exchange exchange) { + final Message in = exchange.getIn(); + final Object body = in.getBody(); + + final List<Command<?>> commands = new ArrayList<Command<?>>(2); + commands.add(CommandFactory.newInsert(body)); + commands.add(CommandFactory.newFireAllRules()); + + Command<?> batch = CommandFactory.newBatchExecution(commands); + in.setBody(batch); + } + +} http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/camel-context.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/camel-context.xml b/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/camel-context.xml new file mode 100644 index 0000000..1f50310 --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/camel-context.xml @@ -0,0 +1,45 @@ +<?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. +--> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:kie="http://drools.org/schema/kie-aries-blueprint/1.0.0" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0 + http://drools.org/schema/kie-aries-blueprint/1.0.0 http://drools.org/schema/kie-aries-blueprint/1.0.0"> + + <bean id="CustomerUtil" class="org.apache.servicemix.examples.drools.camel.blueprint.osgi.Utils" /> + + + <!-- Camel Route --> + <camelContext xmlns="http://camel.apache.org/schema/blueprint"> + + <route trace="false" id="testRoute"> + <from uri="timer:test?period=10s&delay=10s" /> + <bean method="customer" ref="CustomerUtil" /> + <to uri="kie:simpleRuleKSession?action=insertBody" /> + </route> + + <route trace="false" id="testRunCommand"> + <from uri="timer:RunCommandTimer?period=15s&delay=10s" /> + <bean method="customer" ref="CustomerUtil" /> + <bean method="insertAndFireAll" ref="CustomerUtil" /> + <to uri="kie:simpleRuleKSession?action=execute" /> + </route> + + </camelContext> + +</blueprint> http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml b/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml new file mode 100644 index 0000000..46068cd --- /dev/null +++ b/examples/drools/drools-camel-blueprint/src/main/resources/OSGI-INF/blueprint/kmodule-blueprint.xml @@ -0,0 +1,44 @@ +<?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. +--> +<blueprint default-timeout="1000" + xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:kie="http://drools.org/schema/kie-aries-blueprint/1.0.0" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0 + http://drools.org/schema/kie-aries-blueprint/1.0.0 http://drools.org/schema/kie-aries-blueprint/1.0.0"> + + <bean id="DebugAgendaEventListener" + class="org.apache.servicemix.examples.drools.camel.blueprint.osgi.DebugAgendaEventListener" /> + <bean id="DebugRuleRuntimeEventListener" + class="org.apache.servicemix.examples.drools.camel.blueprint.osgi.DebugRuleRuntimeEventListener" /> + + <kie:eventListeners id="debugListeners"> + <kie:agendaEventListener ref="DebugAgendaEventListener" /> + <kie:ruleRuntimeEventListener ref="DebugRuleRuntimeEventListener" /> + </kie:eventListeners> + + <kie:kmodule id="id-kmodule"> + <kie:kbase name="SimpleRuleKBase" default="true" + equalsBehavior="equality" packages="rule"> + <kie:ksession name="simpleRuleKSession" type="stateless" + listeners-ref="debugListeners" default="true"> + </kie:ksession> + </kie:kbase> + </kie:kmodule> + +</blueprint> http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/examples/drools/drools-camel-blueprint/src/main/resources/rule/customer-score.drl ---------------------------------------------------------------------- diff --git a/examples/drools/drools-camel-blueprint/src/main/resources/rule/customer-score.drl b/examples/drools/drools-camel-blueprint/src/main/resources/rule/customer-score.drl new file mode 100644 index 0000000..d5cdb45 --- /dev/null +++ b/examples/drools/drools-camel-blueprint/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.camel.blueprint.model.Customer; +import org.apache.servicemix.examples.drools.camel.blueprint.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/4aabf468/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 index cbe521c..4f76d39 100644 --- 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 @@ -16,10 +16,6 @@ */ package org.apache.servicemix.examples.drools.simple.model; -/** - * @author ghalajko - * - */ public class Customer { /** http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/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 index 273800a..b407421 100644 --- 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 @@ -16,11 +16,7 @@ */ 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/4aabf468/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 index dce296d..d6f7525 100644 --- 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 @@ -33,10 +33,6 @@ import org.osgi.framework.BundleContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * @author ghalajko - * - */ public class SimpleRuleActivator implements BundleActivator { /** * LOGGER. @@ -49,9 +45,7 @@ public class SimpleRuleActivator implements BundleActivator { */ private KieSession ksession; - /** - * - */ + @Override public void start(BundleContext context) throws Exception { KieServices ks = KieServices.Factory.get(); http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/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 index 7e9861a..5b7503a 100644 --- 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 @@ -18,32 +18,20 @@ 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/4aabf468/examples/drools/pom.xml ---------------------------------------------------------------------- diff --git a/examples/drools/pom.xml b/examples/drools/pom.xml index 7054a87..1929682 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> </modules> </project> http://git-wip-us.apache.org/repos/asf/servicemix/blob/4aabf468/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 d903122..ddaf439 100644 --- a/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala +++ b/itests/src/test/scala/org/apache/servicemix/itests/Drools6IntegrationTests.scala @@ -93,4 +93,11 @@ class Drools6ExamplesTest extends Drools6IntegrationTests { logging.containsMessage(line => line.contains("Customer [salary=9001, type=VIP]")) } } + + @Test + def testDroolsCamelExample = testWithFeature("examples-drools-camel-blueprint") { + expect { + logging.containsMessage(line => line.contains("Total 2 routes, of which 2 is started")) + } + } }
