[jboss-svn-commits] JBL Code SVN: r25699 - in labs/jbossrules/trunk: drools-core/src/main/java/org/drools/event and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Mar 17 20:53:51 EDT 2009


Author: salaboy21
Date: 2009-03-17 20:53:51 -0400 (Tue, 17 Mar 2009)
New Revision: 25699

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessEventListenerTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventListenerExtension.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowVariableChangeEvent.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventSupport.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/context/variable/VariableScopeInstance.java
Log:
support for variable change event

Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessEventListenerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessEventListenerTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/ProcessEventListenerTest.java	2009-03-18 00:53:51 UTC (rev 25699)
@@ -0,0 +1,177 @@
+/*
+ *  Copyright 2009 salaboy.
+ * 
+ *  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.
+ *  under the License.
+ */
+
+package org.drools.integrationtests;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+import junit.framework.TestCase;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.StatefulSession;
+import org.drools.WorkingMemory;
+import org.drools.compiler.PackageBuilder;
+import org.drools.event.ProcessEvent;
+import org.drools.event.RuleFlowCompletedEvent;
+import org.drools.event.RuleFlowEventListenerExtension;
+import org.drools.event.RuleFlowGroupActivatedEvent;
+import org.drools.event.RuleFlowGroupDeactivatedEvent;
+import org.drools.event.RuleFlowNodeTriggeredEvent;
+import org.drools.event.RuleFlowStartedEvent;
+import org.drools.event.RuleFlowVariableChangeEvent;
+
+
+import org.drools.process.core.context.variable.VariableScope;
+import org.drools.process.instance.ProcessInstance;
+import org.drools.process.instance.context.variable.VariableScopeInstance;
+import org.drools.rule.Package;
+
+
+
+/**
+ *
+ * @author salaboy
+ */
+public class ProcessEventListenerTest extends TestCase{
+    public void testInternalNodeSignalEvent() {
+        PackageBuilder builder = new PackageBuilder();
+        Reader source = new StringReader(
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+            "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" +
+            "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
+            "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" +
+            "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.event\" package-name=\"org.drools\" version=\"1\" >\n" +
+            "\n" +
+            "  <header>\n" +
+    		"    <variables>\n" +
+    		"      <variable name=\"MyVar\" >\n" +
+    		"        <type name=\"org.drools.process.core.datatype.impl.type.StringDataType\" />\n" +
+    		"        <value>SomeText</value>\n" +
+    		"      </variable>\n" +
+    		"    </variables>\n" +
+            "  </header>\n" +
+            "\n" +
+            "  <nodes>\n" +
+            "    <start id=\"1\" name=\"Start\" />\n" +
+            "    <eventNode id=\"2\" name=\"Event\" variableName=\"MyVar\" >\n" +
+            "      <eventFilters>\n" +
+            "        <eventFilter type=\"eventType\" eventType=\"MyEvent\" />\n" +
+            "      </eventFilters>\n" +
+            "    </eventNode>\n" +
+            "    <actionNode id=\"3\" name=\"Signal Event\" >\n" +
+            "      <action type=\"expression\" dialect=\"java\" >context.getProcessInstance().signalEvent(\"MyEvent\", \"MyValue\");</action>\n" +
+            "    </actionNode>\n" +
+            "    <join id=\"4\" name=\"Join\" type=\"1\" />\n" +
+            "    <end id=\"5\" name=\"End\" />\n" +
+            "  </nodes>\n" +
+            "\n" +
+            "  <connections>\n" +
+            "    <connection from=\"1\" to=\"3\" />\n" +
+            "    <connection from=\"2\" to=\"4\" />\n" +
+            "    <connection from=\"3\" to=\"4\" />\n" +
+            "    <connection from=\"4\" to=\"5\" />\n" +
+            "  </connections>\n" +
+            "\n" +
+            "</process>");
+        builder.addRuleFlow(source);
+        Package pkg = builder.getPackage();
+        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        ruleBase.addPackage( pkg );
+        StatefulSession session = ruleBase.newStatefulSession();
+
+        final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
+
+        final RuleFlowEventListenerExtension listener = new RuleFlowEventListenerExtension() {
+
+            public void beforeVariableChange(RuleFlowVariableChangeEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void afterVariableChange(RuleFlowVariableChangeEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void beforeRuleFlowStarted(RuleFlowStartedEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void afterRuleFlowStarted(RuleFlowStartedEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void beforeRuleFlowCompleted(RuleFlowCompletedEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void afterRuleFlowCompleted(RuleFlowCompletedEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event, WorkingMemory workingMemory) {
+                //processEventList.add(event);
+            }
+
+            public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event, WorkingMemory workingMemory) {
+                //processEventList.add(event);
+            }
+
+            public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event, WorkingMemory workingMemory) {
+               // processEventList.add(event);
+            }
+
+            public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event, WorkingMemory workingMemory) {
+               // processEventList.add(event);
+            }
+
+            public void beforeRuleFlowNodeTriggered(RuleFlowNodeTriggeredEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void afterRuleFlowNodeTriggered(RuleFlowNodeTriggeredEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void beforeRuleFlowNodeLeft(RuleFlowNodeTriggeredEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+            public void afterRuleFlowNodeLeft(RuleFlowNodeTriggeredEvent event, WorkingMemory workingMemory) {
+                processEventList.add(event);
+            }
+
+           
+        };
+
+
+        ((WorkingMemory)session).addEventListener(listener);
+        ProcessInstance processInstance =
+            session.startProcess("org.drools.event");
+        assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
+        assertEquals("MyValue", ((VariableScopeInstance)
+                                    ((ProcessInstance) processInstance)
+                                        .getContextInstance(VariableScope.VARIABLE_SCOPE)).getVariable("MyVar"));
+        assertEquals( 28, processEventList.size() );
+        assertEquals( "org.drools.event", ((RuleFlowStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
+
+        assertEquals("MyVar",((RuleFlowVariableChangeEvent) processEventList.get(4)).getName());
+        assertEquals("SomeText",((RuleFlowVariableChangeEvent) processEventList.get(4)).getValue());
+        assertEquals("MyVar",((RuleFlowVariableChangeEvent) processEventList.get(5)).getName());
+        assertEquals("MyValue",((RuleFlowVariableChangeEvent) processEventList.get(5)).getValue());
+    }
+}

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventListenerExtension.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventListenerExtension.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventListenerExtension.java	2009-03-18 00:53:51 UTC (rev 25699)
@@ -0,0 +1,43 @@
+/*
+ *  Copyright 2009 salaboy.
+ * 
+ *  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.
+ *  under the License.
+ */
+
+package org.drools.event;
+
+import org.drools.WorkingMemory;
+import org.drools.event.process.ProcessEventListener;
+
+/**
+ *
+ * @author salaboy
+ */
+public interface RuleFlowEventListenerExtension extends RuleFlowEventListener{
+    /**
+     * this will be triggered before a variable change
+     * @param event
+     * @param workingMemory
+     */
+    void beforeVariableChange(RuleFlowVariableChangeEvent event,
+                                WorkingMemory workingMemory);
+
+    /**
+     * this will be triggered after a variable was changed
+     * @param event
+     * @param workingMemory
+     */
+    void afterVariableChange(RuleFlowVariableChangeEvent event,
+                                WorkingMemory workingMemory);
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventSupport.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventSupport.java	2009-03-18 00:31:23 UTC (rev 25698)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowEventSupport.java	2009-03-18 00:53:51 UTC (rev 25699)
@@ -24,6 +24,7 @@
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
+import org.drools.WorkingMemory;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.process.instance.ProcessInstance;
 import org.drools.runtime.process.NodeInstance;
@@ -237,8 +238,43 @@
         }
     }
 
+    public void fireBeforeVariableChange(final ProcessInstance instance,
+                                            final String name,
+                                            final Object value,
+                                            WorkingMemory workingMemory) {
+        if (this.listeners.isEmpty()) {
+            return;
+        }
+
+        final RuleFlowVariableChangeEvent event = new RuleFlowVariableChangeEvent(instance, name, value );
+
+        for ( RuleFlowEventListener listener: listeners ) {
+            if(listener instanceof RuleFlowEventListenerExtension) {
+                ((RuleFlowEventListenerExtension) listener).beforeVariableChange(event, workingMemory);
+            }
+        }
+    }
+
+     public void fireAfterVariableChange(final ProcessInstance instance,
+                                            final String name,
+                                            final Object value,
+                                            WorkingMemory workingMemory) {
+        if (this.listeners.isEmpty()) {
+            return;
+        }
+
+        final RuleFlowVariableChangeEvent event = new RuleFlowVariableChangeEvent(instance, name, value );
+
+        for ( RuleFlowEventListener listener: listeners ) {
+            if(listener instanceof RuleFlowEventListenerExtension) {
+                ((RuleFlowEventListenerExtension) listener).afterVariableChange(event, workingMemory);
+            }
+            
+        }
+    }
+
     public void reset() {
         this.listeners.clear();
     }
 
-}
\ No newline at end of file
+}

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowVariableChangeEvent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowVariableChangeEvent.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/event/RuleFlowVariableChangeEvent.java	2009-03-18 00:53:51 UTC (rev 25699)
@@ -0,0 +1,58 @@
+/*
+ *  Copyright 2009 salaboy.
+ * 
+ *  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.
+ *  under the License.
+ */
+
+package org.drools.event;
+
+import org.drools.runtime.process.ProcessInstance;
+
+/**
+ *
+ * @author salaboy
+ */
+public class RuleFlowVariableChangeEvent extends ProcessEvent {
+
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Object getValue() {
+        return value;
+    }
+
+    public void setValue(Object value) {
+        this.value = value;
+    }
+    private Object value;
+    public RuleFlowVariableChangeEvent(ProcessInstance instance,String name, Object value) {
+        super(instance);
+        this.name = name;
+        this.value = value;
+
+    }
+
+    public String toString() {
+        return "==>[VariableChangeEvent(name=" + getName() + "; value=" + getValue()
+            + "; processName=" + getProcessInstance().getProcessName() + "; processId=" + getProcessInstance().getProcessId() + ")]";
+    }
+
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/context/variable/VariableScopeInstance.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/context/variable/VariableScopeInstance.java	2009-03-18 00:31:23 UTC (rev 25698)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/context/variable/VariableScopeInstance.java	2009-03-18 00:53:51 UTC (rev 25699)
@@ -4,6 +4,7 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.drools.common.EventSupport;
 import org.drools.process.core.context.variable.Variable;
 import org.drools.process.core.context.variable.VariableScope;
 import org.drools.process.instance.ContextInstanceContainer;
@@ -36,7 +37,13 @@
             throw new IllegalArgumentException(
                 "The name of a variable may not be null!");
         }
+        if(getProcessInstance() != null){
+            ((EventSupport) getProcessInstance().getWorkingMemory()).getRuleFlowEventSupport().fireBeforeVariableChange(getProcessInstance(),name, variables.get(name), getProcessInstance().getWorkingMemory());
+        }
         variables.put(name, value);
+        if(getProcessInstance() != null){
+            ((EventSupport) getProcessInstance().getWorkingMemory()).getRuleFlowEventSupport().fireAfterVariableChange(getProcessInstance(),name, value, getProcessInstance().getWorkingMemory());
+        }
     }
     
     public VariableScope getVariableScope() {




More information about the jboss-svn-commits mailing list