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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Nov 27 18:55:49 EST 2007


Author: mark.proctor at jboss.com
Date: 2007-11-27 18:55:49 -0500 (Tue, 27 Nov 2007)
New Revision: 16870

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/ConstraintEvaluator.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/RuleFlowConstraintEvaluator.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/Constraint.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConnectionImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConstraintImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/RuleFlowNodeInstance.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowNodeInstanceImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowSplitInstanceImpl.java
Log:
JBRULES-1352 Allow Constraints to also use dialectable expression evaluations
-minor refactoring to separate ruleflow parts to a delegate on ConstraintImpl

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/Constraint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/Constraint.java	2007-11-27 23:50:41 UTC (rev 16869)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/Constraint.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -1,5 +1,7 @@
 package org.drools.ruleflow.core;
 
+import org.drools.ruleflow.nodes.split.ConstraintEvaluator;
+
 /*
  * Copyright 2005 JBoss Inc
  * 
@@ -23,9 +25,15 @@
  * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
  */
 public interface Constraint {
+    
+    /**
+     * Returns the evaluation delegate for this constraint
+     * @return
+     */
+    ConstraintEvaluator getConstraintDelegate();
 
     /**
-     * Typically this method returns the constraint
+     * Typically this method returns the constraint as a String
      * @return the constraint
      */
     String getConstraint();

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConnectionImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConnectionImpl.java	2007-11-27 23:50:41 UTC (rev 16869)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConnectionImpl.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -33,8 +33,8 @@
     private static final long serialVersionUID = 400L;
 
     private int               type;
-    private NodeImpl              from;
-    private NodeImpl              to;
+    private NodeImpl          from;
+    private NodeImpl          to;
 
     private ConnectionImpl() {
     }
@@ -48,8 +48,8 @@
      * @param type		The connection type
      */
     public ConnectionImpl(final Node from,
-                      final Node to,
-                      final int type) {
+                          final Node to,
+                          final int type) {
         if ( from == null ) {
             throw new IllegalArgumentException( "From node is null!" );
         }
@@ -86,17 +86,17 @@
         return this.type;
     }
 
-//    public boolean equals(final Object object) {
-//        if ( object instanceof Connection ) {
-//            final Connection connection = (Connection) object;
-//            return this.type == connection.getType() && getFrom().equals( connection.getFrom() ) && getTo().equals( connection.getTo() );
-//        }
-//        return false;
-//    }
+    //    public boolean equals(final Object object) {
+    //        if ( object instanceof Connection ) {
+    //            final Connection connection = (Connection) object;
+    //            return this.type == connection.getType() && getFrom().equals( connection.getFrom() ) && getTo().equals( connection.getTo() );
+    //        }
+    //        return false;
+    //    }
 
-//    public int hashCode() {
-//        return (getFrom() == null ? 0 : getFrom().hashCode()) + 7 * (getTo() == null ? 0 : getTo().hashCode()) + 19 * getType();
-//    }
+    //    public int hashCode() {
+    //        return (getFrom() == null ? 0 : getFrom().hashCode()) + 7 * (getTo() == null ? 0 : getTo().hashCode()) + 19 * getType();
+    //    }
 
     public String toString() {
         final StringBuffer sb = new StringBuffer( "Connection " );

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConstraintImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConstraintImpl.java	2007-11-27 23:50:41 UTC (rev 16869)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/core/impl/ConstraintImpl.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -18,6 +18,9 @@
 
 import java.io.Serializable;
 
+import org.drools.ruleflow.nodes.split.ConstraintEvaluator;
+import org.drools.ruleflow.nodes.split.RuleFlowConstraintEvaluator;
+
 /**
  * Default implementation of a constraint.
  * 
@@ -28,13 +31,14 @@
     org.drools.ruleflow.core.Constraint,
     Serializable {
 
-    private static final long serialVersionUID = 400L;
+    private static final long  serialVersionUID = 400L;
 
-    private String            name;
-    private String            constraint;
-    private int               priority;
-    private String            dialect;
-    private String            type;
+    private String             name;
+    private String             constraint;
+    private ConstraintEvaluator delegate  = new RuleFlowConstraintEvaluator();
+    private int                priority;
+    private String             dialect;
+    private String             type;
 
     public String getConstraint() {
         return this.constraint;
@@ -44,6 +48,14 @@
         this.constraint = constraint;
     }
 
+    public void setDelegate(ConstraintEvaluator delegate) {
+        this.delegate = delegate;
+    }
+
+    public ConstraintEvaluator getConstraintDelegate() {
+        return this.delegate;
+    }
+
     public String getName() {
         return this.name;
     }
@@ -79,6 +91,5 @@
     public void setType(String type) {
         this.type = type;
     }
-    
-    
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/RuleFlowNodeInstance.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/RuleFlowNodeInstance.java	2007-11-27 23:50:41 UTC (rev 16869)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/RuleFlowNodeInstance.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -1,5 +1,7 @@
 package org.drools.ruleflow.instance;
 
+import org.drools.ruleflow.core.Node;
+
 /*
  * Copyright 2005 JBoss Inc
  * 
@@ -33,6 +35,8 @@
     void setNodeId(long nodeId);
 
     long getNodeId();
+    
+    Node getNode();
 
     void setProcessInstance(RuleFlowProcessInstance processInstance);
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowNodeInstanceImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowNodeInstanceImpl.java	2007-11-27 23:50:41 UTC (rev 16869)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowNodeInstanceImpl.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -59,7 +59,7 @@
         return this.processInstance;
     }
 
-    protected Node getNode() {
+    public Node getNode() {
         return this.processInstance.getRuleFlowProcess().getNode( this.nodeId );
     }
     

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowSplitInstanceImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowSplitInstanceImpl.java	2007-11-27 23:50:41 UTC (rev 16869)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/instance/impl/RuleFlowSplitInstanceImpl.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -19,13 +19,10 @@
 import java.util.Iterator;
 import java.util.List;
 
-import org.drools.common.RuleFlowGroupNode;
 import org.drools.ruleflow.core.Connection;
 import org.drools.ruleflow.core.Constraint;
 import org.drools.ruleflow.core.Split;
 import org.drools.ruleflow.instance.RuleFlowNodeInstance;
-import org.drools.spi.Activation;
-import org.drools.spi.RuleFlowGroup;
 
 /**
  * Runtime counterpart of a split node.
@@ -52,49 +49,39 @@
                 outgoing = split.getOutgoingConnections();
                 int priority = Integer.MAX_VALUE;
                 Connection selected = null;
-            	RuleFlowGroup systemRuleFlowGroup = getProcessInstance().getAgenda().getRuleFlowGroup("DROOLS_SYSTEM");
                 for ( final Iterator iterator = outgoing.iterator(); iterator.hasNext(); ) {
                     final Connection connection = (Connection) iterator.next();
-                    Constraint constraint = split.getConstraint(connection);
-                    if (constraint != null && constraint.getPriority() < priority) {
-                    	String rule = "RuleFlow-Split-" + getProcessInstance().getProcess().getId() + "-" +
-                		getNode().getId() + "-" + connection.getTo().getId();
-                    	for (Iterator activations = systemRuleFlowGroup.iterator(); activations.hasNext(); ) {
-                    		Activation activation = ((RuleFlowGroupNode) activations.next()).getActivation();
-                    		if (rule.equals(activation.getRule().getName())) {
-                        		selected = connection;
-                        		priority = constraint.getPriority();
-                        		break;
-                    		}
-                    	}
+                    Constraint constraint = split.getConstraint( connection );
+                    if ( constraint != null && constraint.getPriority() < priority ) {
+                        if ( constraint.getConstraintDelegate().evaluate( this,
+                                                                          connection,
+                                                                          constraint ) ) {
+                            selected = connection;
+                            priority = constraint.getPriority();
+                            break;
+                        }
                     }
                 }
-                if (selected == null) {
-                	throw new IllegalArgumentException("XOR split could not find at least one valid outgoing connection for split " + getSplitNode().getName());
+                if ( selected == null ) {
+                    throw new IllegalArgumentException( "XOR split could not find at least one valid outgoing connection for split " + getSplitNode().getName() );
                 }
                 getProcessInstance().getNodeInstance( selected.getTo() ).trigger( this );
                 break;
             case Split.TYPE_OR :
                 outgoing = split.getOutgoingConnections();
                 boolean found = false;
-            	systemRuleFlowGroup = getProcessInstance().getAgenda().getRuleFlowGroup("DROOLS_SYSTEM");
                 for ( final Iterator iterator = outgoing.iterator(); iterator.hasNext(); ) {
                     final Connection connection = (Connection) iterator.next();
-                    Constraint constraint = split.getConstraint(connection);
-                    if (constraint != null) {
-                    	String rule = "RuleFlow-Split-" + getProcessInstance().getProcess().getId() + "-" +
-                    		getNode().getId() + "-" + connection.getTo().getId();
-                    	for (Iterator activations = systemRuleFlowGroup.iterator(); activations.hasNext(); ) {
-                    		Activation activation = ((RuleFlowGroupNode) activations.next()).getActivation();
-                    		if (rule.equals(activation.getRule().getName())) {
-                                getProcessInstance().getNodeInstance( connection.getTo() ).trigger( this );
-                                found = true;
-                        		break;
-                    		}
-                    	}
+                    Constraint constraint = split.getConstraint( connection );
+
+                    if ( constraint != null && constraint.getConstraintDelegate().evaluate( this,
+                                                                                            connection,
+                                                                                            constraint ) ) {
+                        getProcessInstance().getNodeInstance( connection.getTo() ).trigger( this );
+                        found = true;
                     }
-                    if (!found) {
-                    	throw new IllegalArgumentException("OR split could not find at least one valid outgoing connection for split " + getSplitNode().getName());
+                    if ( !found ) {
+                        throw new IllegalArgumentException( "OR split could not find at least one valid outgoing connection for split " + getSplitNode().getName() );
                     }
                 }
                 break;
@@ -102,5 +89,4 @@
                 throw new IllegalArgumentException( "Illegal split type " + split.getType() );
         }
     }
-
 }

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/ConstraintEvaluator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/ConstraintEvaluator.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/ConstraintEvaluator.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -0,0 +1,12 @@
+package org.drools.ruleflow.nodes.split;
+
+import org.drools.ruleflow.core.Connection;
+import org.drools.ruleflow.core.Constraint;
+import org.drools.ruleflow.core.Split;
+import org.drools.ruleflow.instance.impl.RuleFlowSplitInstanceImpl;
+
+public interface ConstraintEvaluator {
+    public boolean evaluate(RuleFlowSplitInstanceImpl instance,
+                         Connection connection,
+                         Constraint constraint);
+}
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/RuleFlowConstraintEvaluator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/RuleFlowConstraintEvaluator.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ruleflow/nodes/split/RuleFlowConstraintEvaluator.java	2007-11-27 23:55:49 UTC (rev 16870)
@@ -0,0 +1,33 @@
+package org.drools.ruleflow.nodes.split;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.drools.common.RuleFlowGroupNode;
+import org.drools.ruleflow.core.Connection;
+import org.drools.ruleflow.core.Constraint;
+import org.drools.ruleflow.core.Split;
+import org.drools.ruleflow.instance.RuleFlowProcessInstance;
+import org.drools.ruleflow.instance.impl.RuleFlowSplitInstanceImpl;
+import org.drools.spi.Activation;
+import org.drools.spi.RuleFlowGroup;
+
+public class RuleFlowConstraintEvaluator
+    implements
+    ConstraintEvaluator {
+    public boolean evaluate(RuleFlowSplitInstanceImpl instance,
+                            Connection connection,
+                            Constraint constraint) {
+        RuleFlowProcessInstance processInstance = instance.getProcessInstance();
+        RuleFlowGroup systemRuleFlowGroup = processInstance.getAgenda().getRuleFlowGroup( "DROOLS_SYSTEM" );
+
+        String rule = "RuleFlow-Split-" + processInstance.getProcess().getId() + "-" + instance.getNode().getId() + "-" + connection.getTo().getId();
+        for ( Iterator activations = systemRuleFlowGroup.iterator(); activations.hasNext(); ) {
+            Activation activation = ((RuleFlowGroupNode) activations.next()).getActivation();
+            if ( rule.equals( activation.getRule().getName() ) ) {
+                return true;
+            }
+        }
+        return false;
+    }
+}




More information about the jboss-svn-commits mailing list