[jboss-svn-commits] JBL Code SVN: r24619 - in labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main: rules and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jan 8 21:18:19 EST 2009


Author: tirelli
Date: 2009-01-08 21:18:19 -0500 (Thu, 08 Jan 2009)
New Revision: 24619

Added:
   labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/BrokerServices.java
Modified:
   labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/Broker.java
   labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/rules/broker.drl
Log:
Working on the example

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/Broker.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/Broker.java	2009-01-09 01:37:31 UTC (rev 24618)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/Broker.java	2009-01-09 02:18:19 UTC (rev 24619)
@@ -16,18 +16,11 @@
  */
 package org.drools.examples.broker;
 
-import java.io.IOException;
-
 import org.drools.KnowledgeBase;
 import org.drools.KnowledgeBaseFactory;
-import org.drools.RuleBase;
-import org.drools.RuleBaseFactory;
-import org.drools.StatefulSession;
 import org.drools.builder.KnowledgeBuilder;
 import org.drools.builder.KnowledgeBuilderFactory;
 import org.drools.builder.ResourceType;
-import org.drools.compiler.DroolsParserException;
-import org.drools.compiler.PackageBuilder;
 import org.drools.examples.broker.events.Event;
 import org.drools.examples.broker.events.EventReceiver;
 import org.drools.examples.broker.model.Company;
@@ -35,7 +28,6 @@
 import org.drools.examples.broker.model.StockTick;
 import org.drools.examples.broker.ui.BrokerWindow;
 import org.drools.io.ResourceFactory;
-import org.drools.rule.EntryPoint;
 import org.drools.runtime.StatefulKnowledgeSession;
 import org.drools.runtime.rule.WorkingMemoryEntryPoint;
 
@@ -44,7 +36,7 @@
  *  
  * @author etirelli
  */
-public class Broker implements EventReceiver {
+public class Broker implements EventReceiver, BrokerServices {
     private static final String RULES_FILE = "/broker.drl";
     
     private BrokerWindow window;
@@ -74,6 +66,7 @@
     private StatefulKnowledgeSession createSession() {
         KnowledgeBase kbase = loadRuleBase();
         StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
+        session.setGlobal( "services", this );
         for( Company company : this.companies.getCompanies() ) {
             session.insert( company );
         }
@@ -92,6 +85,10 @@
         kbase.addKnowledgePackages( builder.getKnowledgePackages() );
         return kbase;
     }
+
+    public void log(String message) {
+        window.log( message );
+    }
     
     
 }

Added: labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/BrokerServices.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/BrokerServices.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/BrokerServices.java	2009-01-09 02:18:19 UTC (rev 24619)
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2008 Red Hat
+ * 
+ * Licensed 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.drools.examples.broker;
+
+/**
+ * An interface for exposing services to the rules
+ * 
+ * @author etirelli
+ */
+public interface BrokerServices {
+    
+    public void log( String message );
+
+}


Property changes on: labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/java/org/drools/examples/broker/BrokerServices.java
___________________________________________________________________
Name: svn:executable
   + *

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/rules/broker.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/rules/broker.drl	2009-01-09 01:37:31 UTC (rev 24618)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-fusion/src/main/rules/broker.drl	2009-01-09 02:18:19 UTC (rev 24619)
@@ -1,21 +1,85 @@
 package org.drools.examples.broker;
 
+# importing classes
 import org.drools.examples.broker.model.Company;
 import org.drools.examples.broker.model.StockTick;
 
+# a common pattern is to expose services to rules through
+# a helper service class
+global org.drools.examples.broker.BrokerServices services
+
+# default dialect for the semantic code will be MVEL
 dialect "mvel"
 
-/*declare StockTick
+# tells the engine that a StockTick instance will assume the
+# role (semantics) of events and that the default retention 
+# policy will be 2 minutes 
+declare StockTick
     @role( event )
-end*/
+    @expires( 1m )
+end 
+
+# One can even declare helper facts to make rules easier and
+# simpler to write and maintain
+declare Statistics
+    symbol : String @key()
+    average : double
+end
  
-rule "Update price"
+rule "Setup statistics"
+   agenda-group "setup"
 when
+   $c : Company( $s : symbol )
+   not( Statistics( symbol == $s ) )
+then
+   Statistics s = new Statistics();
+   s.symbol = $s;
+   insert( s );
+end
+
+# a simple rule to show that it is possible to join
+# events from an entry-point (stream) with facts 
+# present in the working memory
+rule "Update stock price"
+    agenda-group "evaluation"
+    auto-focus true
+when
     $st : StockTick( $sb : symbol, $pr : price ) from entry-point "StockTick stream"
     $cp : Company( symbol == $sb )
 then
+    // This shows an update on working memory facts with data from joined events
     $cp.currentPrice = $pr;
+    
+    // Although events are considered immutable, a common pattern is to use a class
+    // to represent an event and enrich that event instance with data derived from other facts/events.
+    // Bellow we "enrich" the event instance with the percentual change in the price, 
+    // based on the previous price
     $st.delta = $cp.delta;
 end
 
- 
\ No newline at end of file
+rule "average over last minute"
+    agenda-group "evaluation"
+    auto-focus true
+    lock-on-active
+when
+    $stat : Statistics( $symbol : symbol )
+    Number( $av : doubleValue ) from accumulate(
+         StockTick( symbol == $symbol, $p : price ) over window:time( 1m )
+                                                    from entry-point "StockTick stream",
+         average( $p ) )
+then
+    System.out.println($symbol +": $"+$av );
+    modify( $stat ) {
+         average = $av
+    } 
+end
+
+rule "sudden drop"
+when
+    $st : StockTick( $sb : symbol, $ts : timestamp, $pr : price ) from entry-point "StockTick stream"
+    not( StockTick( symbol == $sb, timestamp > $ts ) from entry-point "StockTick stream" )
+    Statistics( symbol == $sb, $av : average > ( $pr * 1.02 ) )
+then
+    services.log( "Drop +5%: "+$sb+" avg: $"+$av+" price: $"+$pr );
+end
+




More information about the jboss-svn-commits mailing list