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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Jul 1 22:32:35 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-07-01 22:32:35 -0400 (Sun, 01 Jul 2007)
New Revision: 12987

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java
Log:
JBRULES-947 sequential rete
-you can now specify sequential or dynamic agenda for sequential mode

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java	2007-07-02 01:38:41 UTC (rev 12986)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java	2007-07-02 02:32:35 UTC (rev 12987)
@@ -51,6 +51,8 @@
  */
 
 /**
+ * drools.sequential = <true|false>
+ * drools.sequential.agenda = <sequential|dynamic>
  * drools.removeIdentities = <true|false>
  * drools.shareAlphaNodes  = <true|false>
  * drools.shareBetaNodes = <true|false>
@@ -59,8 +61,8 @@
  * drools.compositeKeyDepth  =<1..3>
  * drools.indexLeftBetaMemory = <true/false>
  * drools.indexRightBetaMemory = <true/false>
- * drools.assertBehaviour = <IDENTITY|EQUALITY>
- * drools.logicalOverride = <DISCARD|PRESERVE>
+ * drools.assertBehaviour = <identity|equality>
+ * drools.logicalOverride = <discard|preserve>
  * drools.executorService = <qualified class name>
  * drools.conflictResolver = <qualified class name>
  * 
@@ -74,8 +76,9 @@
     private ChainedProperties   chainedProperties;
 
     private boolean             immutable;
-    
+
     private boolean             sequential;
+    private SequentialAgenda    sequentialAgenda;
 
     private boolean             maintainTms;
     private boolean             removeIdentities;
@@ -111,9 +114,12 @@
         if ( properties != null ) {
             this.chainedProperties.addProperties( properties );
         }
-        
+
+        setSequentialAgenda( SequentialAgenda.determineSequentialAgenda( this.chainedProperties.getProperty( "drools.sequential.agenda",
+                                                                                                             "sequential" ) ) );
+
         setSequential( Boolean.valueOf( this.chainedProperties.getProperty( "drools.sequential",
-                        "false" ) ).booleanValue() );        
+                                                                            "false" ) ).booleanValue() );
 
         setMaintainTms( Boolean.valueOf( this.chainedProperties.getProperty( "drools.maintainTms",
                                                                              "true" ) ).booleanValue() );
@@ -142,9 +148,9 @@
                                                                                       "true" ) ).booleanValue() );
 
         setAssertBehaviour( AssertBehaviour.determineAssertBehaviour( this.chainedProperties.getProperty( "drools.assertBehaviour",
-                                                                                                          "IDENTITY" ) ) );
+                                                                                                          "identity" ) ) );
         setLogicalOverride( LogicalOverride.determineLogicalOverride( this.chainedProperties.getProperty( "drools.logicalOverride",
-                                                                                                          "DISCARD" ) ) );
+                                                                                                          "discard" ) ) );
 
         setExecutorService( RuleBaseConfiguration.determineExecutorService( this.chainedProperties.getProperty( "drools.executorService",
                                                                                                                 "org.drools.concurrent.DefaultExecutorService" ) ) );
@@ -178,11 +184,11 @@
             throw new UnsupportedOperationException( "Can't set a property after configuration becomes immutable" );
         }
     }
-    
+
     public void setSequential(boolean sequential) {
         this.sequential = sequential;
     }
-    
+
     public boolean isSequential() {
         return this.sequential;
     }
@@ -341,9 +347,9 @@
         }
 
         public static AssertBehaviour determineAssertBehaviour(final String value) {
-            if ( value.equals( "IDENTITY" ) ) {
+            if ( "IDENTITY".equalsIgnoreCase( value ) ) {
                 return IDENTITY;
-            } else if ( value.equals( "EQUALITY" ) ) {
+            } else if ( "EQUALITY".equalsIgnoreCase( value ) ) {
                 return EQUALITY;
             } else {
                 throw new IllegalArgumentException( "Illegal enum value '" + value + "' for AssertBehaviour" );
@@ -381,9 +387,9 @@
         }
 
         public static LogicalOverride determineLogicalOverride(final String value) {
-            if ( value.equals( "PRESERVE" ) ) {
+            if ( "PRESERVE".equalsIgnoreCase( value ) ) {
                 return PRESERVE;
-            } else if ( value.equals( "DISCARD" ) ) {
+            } else if ( "DISCARD".equalsIgnoreCase( value ) ) {
                 return DISCARD;
             } else {
                 throw new IllegalArgumentException( "Illegal enum value '" + value + "' for LogicalOverride" );
@@ -406,15 +412,67 @@
         }
     }
 
+    public static class SequentialAgenda
+        implements
+        Serializable {
+        private static final long            serialVersionUID  = 320L;
+
+        public static final SequentialAgenda SEQUENTIAL = new SequentialAgenda( 0 );
+        public static final SequentialAgenda DYNAMIC    = new SequentialAgenda( 1 );
+
+        private int                          value;
+
+        private SequentialAgenda(final int value) {
+            this.value = value;
+        }
+
+        public static SequentialAgenda determineSequentialAgenda(final String value) {
+            if ( "sequential".equalsIgnoreCase( value ) ) {
+                return SEQUENTIAL;
+            } else if ( "dynamic".equalsIgnoreCase( value ) ) {
+                return DYNAMIC;
+            } else {
+                throw new IllegalArgumentException( "Illegal enum value '" + value + "' for SequentialAgenda" );
+            }
+        }
+
+        private Object readResolve() throws java.io.ObjectStreamException {
+            switch ( this.value ) {
+                case 0 :
+                    return SEQUENTIAL;
+                case 1 :
+                    return DYNAMIC;
+                default :
+                    throw new IllegalArgumentException( "Illegal enum value '" + this.value + "' for SequentialAgenda" );
+            }
+        }
+
+        public String toString() {
+            return "SequentialAgenda : " + ((this.value == 0) ? "sequential" : "dynamic");
+        }
+    }
+
     public AgendaGroupFactory getAgendaGroupFactory() {
         if ( isSequential() ) {
-            return ArrayAgendaGroupFactory.getInstance();
-            //return PriorityQueueAgendaGroupFactory.getInstance();
+            if ( this.sequentialAgenda == SequentialAgenda.SEQUENTIAL ) {
+                return ArrayAgendaGroupFactory.getInstance();
+            } else {
+                return PriorityQueueAgendaGroupFactory.getInstance();
+            }
         } else {
             return PriorityQueueAgendaGroupFactory.getInstance();
         }
     }
-    
+
+    public SequentialAgenda getSequentialAgenda() {
+        return this.sequentialAgenda;
+    }
+
+    public void setSequentialAgenda(final SequentialAgenda sequentialAgenda) {
+        checkCanChange(); // throws an exception if a change isn't possible;
+        this.sequentialAgenda = sequentialAgenda;
+    }
+
     private static ConflictResolver determineConflictResolver(String className) {
         Class clazz = null;
         try {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2007-07-02 01:38:41 UTC (rev 12986)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2007-07-02 02:32:35 UTC (rev 12987)
@@ -88,8 +88,10 @@
 
     private final AgendaGroup           main;
 
-    private KnowledgeHelper      knowledgeHelper;
+    private AgendaGroupFactory          agendaGroupFactory;
 
+    private KnowledgeHelper             knowledgeHelper;
+
     public int                          activeActivations;
 
     public int                          dormantActivations;
@@ -111,16 +113,18 @@
         if ( ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().isSequential() ) {
             this.knowledgeHelper = new SequentialKnowledgeHelper( this.workingMemory );
         } else {
-            this.knowledgeHelper = new DefaultKnowledgeHelper( this.workingMemory ); 
+            this.knowledgeHelper = new DefaultKnowledgeHelper( this.workingMemory );
         }
         this.agendaGroups = new HashMap();
         this.activationGroups = new HashMap();
         this.ruleFlowGroups = new HashMap();
         this.focusStack = new LinkedList();
 
+        this.agendaGroupFactory = ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getAgendaGroupFactory();
+
         // MAIN should always be the first AgendaGroup and can never be removed
-        AgendaGroupFactory factory = ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getAgendaGroupFactory();
-        this.main = factory.createAgendaGroup( AgendaGroup.MAIN, ((InternalRuleBase) this.workingMemory.getRuleBase()) );        
+        this.main = agendaGroupFactory.createAgendaGroup( AgendaGroup.MAIN,
+                                                          ((InternalRuleBase) this.workingMemory.getRuleBase()) );
 
         this.agendaGroups.put( AgendaGroup.MAIN,
                                this.main );
@@ -252,8 +256,8 @@
         if ( agendaGroup == null ) {
             // The AgendaGroup is defined but not yet added to the
             // Agenda, so create the AgendaGroup and add to the Agenda.
-            AgendaGroupFactory factory = ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getAgendaGroupFactory();
-            agendaGroup = factory.createAgendaGroup( name, ((InternalRuleBase) this.workingMemory.getRuleBase()) );
+            agendaGroup = agendaGroupFactory.createAgendaGroup( name,
+                                                                ((InternalRuleBase) this.workingMemory.getRuleBase()) );
             addAgendaGroup( agendaGroup );
         }
         return agendaGroup;

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java	2007-07-02 01:38:41 UTC (rev 12986)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java	2007-07-02 02:32:35 UTC (rev 12987)
@@ -3,6 +3,10 @@
 import java.util.Properties;
 
 import org.drools.RuleBaseConfiguration.AssertBehaviour;
+import org.drools.RuleBaseConfiguration.LogicalOverride;
+import org.drools.RuleBaseConfiguration.SequentialAgenda;
+import org.drools.common.ArrayAgendaGroupFactory;
+import org.drools.common.PriorityQueueAgendaGroupFactory;
 
 import junit.framework.TestCase;
 
@@ -54,5 +58,60 @@
         
         assertTrue( cfg.isShadowed( "java.lang.String" ) );
     }
+    
+    public void testAssertBehaviour() {
+        Properties properties = new Properties();
+        properties.setProperty( "drools.assertBehaviour", "identity" );
+        RuleBaseConfiguration cfg = new RuleBaseConfiguration(properties);
+        
+        assertEquals( AssertBehaviour.IDENTITY, cfg.getAssertBehaviour() );
+        
+        properties = new Properties();
+        properties.setProperty( "drools.assertBehaviour", "equality" );
+        cfg = new RuleBaseConfiguration(properties);
+        
+        assertEquals( AssertBehaviour.EQUALITY, cfg.getAssertBehaviour() );        
+    }
+    
+    public void testLogicalOverride() {
+        Properties properties = new Properties();
+        properties.setProperty( "drools.logicalOverride", "preserve" );
+        RuleBaseConfiguration cfg = new RuleBaseConfiguration(properties);
+        
+        assertEquals( LogicalOverride.PRESERVE, cfg.getLogicalOverride() );
+        
+        properties = new Properties();
+        properties.setProperty( "drools.logicalOverride", "discard" );
+        cfg = new RuleBaseConfiguration(properties);
+        
+        assertEquals( LogicalOverride.DISCARD, cfg.getLogicalOverride() );        
+    }    
+    
+    public void testSequential() {
+        Properties properties = new Properties();
+        properties.setProperty( "drools.sequential", "false" );
+        RuleBaseConfiguration cfg = new RuleBaseConfiguration(properties);
+        
+        assertFalse( cfg.isSequential() );
+        assertTrue( cfg.getAgendaGroupFactory() instanceof PriorityQueueAgendaGroupFactory );
+        
+        properties = new Properties();
+        properties.setProperty( "drools.sequential.agenda", "sequential" );
+        properties.setProperty( "drools.sequential", "true" );
+        cfg = new RuleBaseConfiguration(properties);
+        
+        assertTrue( cfg.isSequential() );
+        assertEquals( SequentialAgenda.SEQUENTIAL, cfg.getSequentialAgenda() );
+        assertTrue( cfg.getAgendaGroupFactory() instanceof ArrayAgendaGroupFactory );
+        
+        properties = new Properties();
+        properties.setProperty( "drools.sequential.agenda", "dynamic" );
+        properties.setProperty( "drools.sequential", "true" );
+        cfg = new RuleBaseConfiguration(properties);
+        
+        assertTrue( cfg.isSequential() );
+        assertEquals( SequentialAgenda.DYNAMIC, cfg.getSequentialAgenda() );
+        assertTrue( cfg.getAgendaGroupFactory() instanceof PriorityQueueAgendaGroupFactory );
+    }    
 
 }




More information about the jboss-svn-commits mailing list