[jboss-svn-commits] JBL Code SVN: r5989 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/common main/java/org/drools/reteoo/beta test/java/org/drools/reteoo/beta

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 28 11:09:22 EDT 2006


Author: tirelli
Date: 2006-08-28 11:09:11 -0400 (Mon, 28 Aug 2006)
New Revision: 5989

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemory.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemoryTest.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InstanceEqualsConstraint.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/BetaMemoryFactory.java
Log:
Adding Instance Equals indexing for BetaNode's right memory

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InstanceEqualsConstraint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InstanceEqualsConstraint.java	2006-08-28 14:04:35 UTC (rev 5988)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InstanceEqualsConstraint.java	2006-08-28 15:09:11 UTC (rev 5989)
@@ -47,6 +47,10 @@
     public Declaration[] getRequiredDeclarations() {
         return this.declarations;
     }
+    
+    public int getOtherColumn() {
+        return this.otherColumn;
+    }
 
     public boolean isAllowed(final Object object,
                              final Tuple tuple,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/BetaMemoryFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/BetaMemoryFactory.java	2006-08-28 14:04:35 UTC (rev 5988)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/BetaMemoryFactory.java	2006-08-28 15:09:11 UTC (rev 5989)
@@ -22,6 +22,7 @@
 import org.drools.base.ValueType;
 import org.drools.base.evaluators.Operator;
 import org.drools.common.BetaNodeBinder;
+import org.drools.common.InstanceEqualsConstraint;
 import org.drools.rule.VariableConstraint;
 import org.drools.spi.FieldConstraint;
 
@@ -114,9 +115,9 @@
         final FieldConstraint[] constraints = (binder != null) ? binder.getConstraints() : null;
         if ( (constraints != null) && (config.getBooleanProperty( RuleBaseConfiguration.PROPERTY_INDEX_RIGHT_BETA_MEMORY )) ) {
             for ( int i = 0; i < constraints.length; i++ ) {
+                BetaRightMemory innerMemory = null;
                 if ( constraints[i] instanceof VariableConstraint ) {
                     final VariableConstraint bvc = (VariableConstraint) constraints[i];
-                    BetaRightMemory innerMemory = null;
                     ValueType valueType = bvc.getEvaluator().getValueType();
                     if ( valueType == ValueType.BOOLEAN_TYPE ) {
                         if ( valueType == ValueType.BOOLEAN_TYPE ) {
@@ -136,20 +137,22 @@
                                                                                bvc.getEvaluator() );
                         }
                     }
-
-                    if ( innerMemory != null ) {
-                        if ( innerMostMemory != null ) {
-                            try {
-                                innerMostMemory.setInnerMemory( innerMemory );
-                                innerMostMemory = innerMemory;
-                            } catch ( final OperationNotSupportedException e ) {
-                                throw new RuntimeException( "BUG: Exception was not supposed to be raised",
-                                                            e );
-                            }
-                        } else {
-                            memory = innerMemory;
-                            innerMostMemory = memory;
+                } else if ( constraints[i] instanceof InstanceEqualsConstraint ) {
+                    final InstanceEqualsConstraint iec = (InstanceEqualsConstraint) constraints[i];
+                    innerMemory = new InstanceEqualConstrRightMemory( iec.getOtherColumn() );
+                }
+                if ( innerMemory != null ) {
+                    if ( innerMostMemory != null ) {
+                        try {
+                            innerMostMemory.setInnerMemory( innerMemory );
+                            innerMostMemory = innerMemory;
+                        } catch ( final OperationNotSupportedException e ) {
+                            throw new RuntimeException( "BUG: Exception was not supposed to be raised",
+                                                        e );
                         }
+                    } else {
+                        memory = innerMemory;
+                        innerMostMemory = memory;
                     }
                 }
             }

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemory.java	2006-08-28 14:04:35 UTC (rev 5988)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemory.java	2006-08-28 15:09:11 UTC (rev 5989)
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * 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.reteoo.beta;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeSet;
+
+import org.drools.WorkingMemory;
+import org.drools.common.DefaultFactHandle;
+import org.drools.reteoo.ObjectMatches;
+import org.drools.reteoo.ReteTuple;
+import org.drools.util.IdentityMap;
+import org.drools.util.MultiLinkedList;
+import org.drools.util.MultiLinkedListNodeWrapper;
+
+/**
+ * @author etirelli
+ *
+ */
+public class InstanceEqualConstrRightMemory
+    implements
+    BetaRightMemory {
+
+    private static final long serialVersionUID = -2834558788591711298L;
+
+    private BetaRightMemory innerMemory  = null;
+
+    private Map             memoryMap    = null;
+
+    private Object          selectedObject = null;
+
+    private int             column;
+
+    public InstanceEqualConstrRightMemory(final int column) {
+        this( column,
+              null );
+    }
+
+    public InstanceEqualConstrRightMemory(final int column,
+                                        final BetaRightMemory childMemory) {
+        this.column = column;
+        this.innerMemory = childMemory;
+        this.memoryMap = new IdentityMap();
+    }
+
+    /**
+     * 
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#add(org.drools.WorkingMemory, org.drools.reteoo.ObjectMatches)
+     */
+    public final void add(final WorkingMemory workingMemory,
+                          final ObjectMatches matches) {
+        this.memoryMap.put( matches.getFactHandle().getObject(), matches );
+
+        if ( this.innerMemory != null ) {
+            matches.setChild( new MultiLinkedListNodeWrapper( matches ) );
+            this.innerMemory.add( workingMemory,
+                                  (MultiLinkedListNodeWrapper) matches.getChild() );
+        }
+    }
+
+    /**
+     * 
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#remove(org.drools.WorkingMemory, org.drools.reteoo.ObjectMatches)
+     */
+    public final void remove(final WorkingMemory workingMemory,
+                             final ObjectMatches matches) {
+        if ( this.innerMemory != null ) {
+            this.innerMemory.remove( workingMemory,
+                                     (MultiLinkedListNodeWrapper) matches.getChild() );
+        }
+        this.memoryMap.remove( matches.getFactHandle().getObject() );
+    }
+
+    /**
+     * 
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#add(org.drools.WorkingMemory, org.drools.util.MultiLinkedListNodeWrapper)
+     */
+    public final void add(final WorkingMemory workingMemory,
+                          final MultiLinkedListNodeWrapper wrapper) {
+        final ObjectMatches matches = (ObjectMatches) wrapper.getNode();
+        this.memoryMap.put( matches.getFactHandle().getObject(), wrapper );
+
+        if ( this.innerMemory != null ) {
+            wrapper.setChild( new MultiLinkedListNodeWrapper( matches ) );
+            this.innerMemory.add( workingMemory,
+                                  (MultiLinkedListNodeWrapper) wrapper.getChild() );
+        }
+    }
+
+    /**
+     * 
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#remove(org.drools.WorkingMemory, org.drools.util.MultiLinkedListNodeWrapper)
+     */
+    public final void remove(final WorkingMemory workingMemory,
+                             final MultiLinkedListNodeWrapper matches) {
+        if ( this.innerMemory != null ) {
+            this.innerMemory.remove( workingMemory,
+                                     (MultiLinkedListNodeWrapper) matches.getChild() );
+        }
+        this.memoryMap.remove( ((ObjectMatches) matches.getNode()).getFactHandle().getObject() );
+    }
+
+    /**
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#iterator(org.drools.WorkingMemory, org.drools.reteoo.ReteTuple)
+     */
+    public final Iterator iterator(final WorkingMemory workingMemory,
+                                   final ReteTuple tuple) {
+        this.selectPossibleMatches( workingMemory,
+                                    tuple );
+        Iterator iterator = null;
+        if ( this.selectedObject != null ) {
+            iterator = Collections.singleton( this.selectedObject ).iterator();        
+        } else {
+            iterator = Collections.EMPTY_LIST.iterator();
+        }
+        return iterator;
+    }
+
+    /**
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#isEmpty()
+     */
+    public final boolean isEmpty() {
+        return this.memoryMap.isEmpty();
+    }
+
+    /**
+     * @inheritDoc 
+     *
+     * @see org.drools.reteoo.beta.BetaRightMemory#selectPossibleMatches(org.drools.WorkingMemory, org.drools.reteoo.ReteTuple)
+     */
+    public final void selectPossibleMatches(final WorkingMemory workingMemory,
+                                            final ReteTuple tuple) {
+        final Object select = tuple.get( this.column ).getObject();
+        this.selectedObject = this.memoryMap.get( select );
+
+        if ( this.innerMemory != null ) {
+            this.innerMemory.selectPossibleMatches( workingMemory,
+                                                    tuple );
+        }
+    }
+
+    public final boolean isPossibleMatch(final MultiLinkedListNodeWrapper wrapper) {
+        boolean ret = this.selectedObject == ((ObjectMatches)wrapper.getNode()).getFactHandle().getObject();
+        if ( ret && ( this.innerMemory != null ) ) {
+            ret = this.innerMemory.isPossibleMatch( (MultiLinkedListNodeWrapper) wrapper.getChild() );
+        }
+        return ret;
+    }
+
+    public final int size() {
+        return this.memoryMap.size();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public final Iterator iterator() {
+        final TreeSet set = new TreeSet( new Comparator() {
+            public int compare(Object arg0,
+                               Object arg1) {
+                DefaultFactHandle f0 = ((ObjectMatches) arg0).getFactHandle();
+                DefaultFactHandle f1 = ((ObjectMatches) arg1).getFactHandle();
+                return (f0.getRecency() == f1.getRecency()) ? 0 : (f0.getRecency() > f1.getRecency()) ? 1 : -1;
+            }
+
+        } );
+        for ( final Iterator i = this.memoryMap.values().iterator(); i.hasNext(); ) {
+            final MultiLinkedList list = (MultiLinkedList) i.next();
+            for ( final Iterator j = list.iterator(); j.hasNext(); ) {
+                set.add( j.next() );
+            }
+        }
+
+        return set.iterator();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public BetaRightMemory getInnerMemory() {
+        return this.innerMemory;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void setInnerMemory(final BetaRightMemory innerMemory) {
+        this.innerMemory = innerMemory;
+    }
+
+}


Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemory.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemoryTest.java	2006-08-28 14:04:35 UTC (rev 5988)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemoryTest.java	2006-08-28 15:09:11 UTC (rev 5989)
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * 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.reteoo.beta;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import junit.framework.Assert;
+
+import org.drools.base.ClassFieldExtractor;
+import org.drools.base.ClassObjectType;
+import org.drools.base.ValueType;
+import org.drools.base.evaluators.Operator;
+import org.drools.common.DefaultFactHandle;
+import org.drools.reteoo.ObjectMatches;
+import org.drools.reteoo.ReteTuple;
+import org.drools.rule.Column;
+import org.drools.rule.Declaration;
+import org.drools.spi.Evaluator;
+import org.drools.util.MultiLinkedListNodeWrapper;
+
+/**
+ * 
+ * ObjectEqualConstrRightMemoryTest
+ * A test case for ObjectEqualConstrRightMemory 
+ *
+ * @author <a href="mailto:tirelli at post.com">Edson Tirelli</a>
+ *
+ * Created: 28/02/2006
+ */
+public class InstanceEqualConstrRightMemoryTest extends BaseBetaRightMemoryTestClass {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        final ClassFieldExtractor extractor = new ClassFieldExtractor( DummyValueObject.class,
+                                                                       "objectAttr" );
+
+        Column column = new  Column(0, new ClassObjectType( DummyValueObject.class ) );
+        
+        final Declaration declaration = new Declaration( "myObject",
+                                                         extractor,
+                                                         column );
+        final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator( Operator.EQUAL );
+
+        this.memory = new ObjectEqualConstrRightMemory( extractor,
+                                                        declaration,
+                                                        evaluator,
+                                                        this.child );
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /*
+     * Test method for 'org.drools.reteoo.beta.ObjectEqualConstrRightMemory.iterator(WorkingMemory, ReteTuple)'
+     */
+    public void testIterator() {
+        try {
+            this.memory.add( this.workingMemory,
+                             this.matches0 );
+            this.memory.add( this.workingMemory,
+                             this.matches1 );
+            Assert.assertEquals( "Memory should have size 2",
+                                 2,
+                                 this.memory.size() );
+
+            Iterator iterator = this.memory.iterator( this.workingMemory,
+                                                      this.tuple0 );
+
+            Assert.assertTrue( "There should be a next element",
+                               iterator.hasNext() );
+            final ObjectMatches om0 = (ObjectMatches) iterator.next();
+            Assert.assertSame( "The first object to return should have been matches0",
+                               this.matches0,
+                               om0 );
+
+            try {
+                iterator.remove();
+                Assert.fail( "Right side memory Iterators are not supposed to support remove()" );
+            } catch ( final UnsupportedOperationException uoe ) {
+                // working fine
+            }
+            Assert.assertEquals( "Memory should have size 2",
+                                 2,
+                                 this.memory.size() );
+
+            Assert.assertFalse( "There should not be a next element",
+                                iterator.hasNext() );
+
+            try {
+                iterator.next();
+                Assert.fail( "Iterator is supposed to throw an Exception when there are no more elements" );
+            } catch ( final NoSuchElementException nse ) {
+                // working fine
+            }
+
+            final DummyValueObject obj2 = new DummyValueObject( false,
+                                                                "string3",
+                                                                30,
+                                                                "object3" );
+            final DefaultFactHandle f2 = (DefaultFactHandle) this.factory.newFactHandle( obj2 );
+
+            final ReteTuple tuple2 = new ReteTuple( f2 );
+            iterator = this.memory.iterator( this.workingMemory,
+                                             tuple2 );
+            Assert.assertFalse( "There should not be a next element",
+                                iterator.hasNext() );
+
+            try {
+                iterator.next();
+                Assert.fail( "Iterator is supposed to throw an Exception when there are no more elements" );
+            } catch ( final NoSuchElementException nse ) {
+                // working fine
+            }
+        } catch ( final Exception e ) {
+            e.printStackTrace();
+            Assert.fail( "Memory is not supposed to throw any exception during iteration" );
+        }
+    }
+
+    /*
+     * Test method for 'org.drools.reteoo.beta.ObjectEqualConstrRightMemory.selectPossibleMatches(WorkingMemory, ReteTuple)'
+     */
+    public void testSelectPossibleMatches() {
+        final MultiLinkedListNodeWrapper wrapper0 = new MultiLinkedListNodeWrapper( this.matches0 );
+        final MultiLinkedListNodeWrapper wrapper1 = new MultiLinkedListNodeWrapper( this.matches1 );
+        final MultiLinkedListNodeWrapper wrapper2 = new MultiLinkedListNodeWrapper( this.matches1 );
+
+        this.memory.add( this.workingMemory,
+                         wrapper0 );
+        this.memory.add( this.workingMemory,
+                         wrapper1 );
+
+        this.memory.selectPossibleMatches( this.workingMemory,
+                                           this.tuple0 );
+
+        Assert.assertTrue( "Wrapper0 was a possible match",
+                           this.memory.isPossibleMatch( wrapper0 ) );
+        Assert.assertFalse( "Wrapper1 was not a possible match",
+                            this.memory.isPossibleMatch( wrapper1 ) );
+        Assert.assertFalse( "Wrapper2 was not a possible match",
+                            this.memory.isPossibleMatch( wrapper2 ) );
+
+        final DummyValueObject obj2 = new DummyValueObject( false,
+                                                            "string3",
+                                                            30,
+                                                            "object3" );
+        final DefaultFactHandle f2 = (DefaultFactHandle) this.factory.newFactHandle( obj2 );
+        final ReteTuple tuple2 = new ReteTuple( f2 );
+
+        this.memory.selectPossibleMatches( this.workingMemory,
+                                           tuple2 );
+        Assert.assertFalse( "Wrapper0 was not a possible match",
+                            this.memory.isPossibleMatch( wrapper0 ) );
+        Assert.assertFalse( "Wrapper1 was not a possible match",
+                            this.memory.isPossibleMatch( wrapper1 ) );
+        Assert.assertFalse( "Wrapper2 was not a possible match",
+                            this.memory.isPossibleMatch( wrapper2 ) );
+    }
+
+    public void testModifyObjectAttribute() {
+        final DummyValueObject obj2 = new DummyValueObject( true,
+                                                            "string20",
+                                                            20,
+                                                            "object20" );
+        final DefaultFactHandle f2 = (DefaultFactHandle) this.factory.newFactHandle( obj2 );
+        final ObjectMatches matches2 = new ObjectMatches( f2 );
+
+        this.memory.add( this.workingMemory,
+                         matches2 );
+        Assert.assertEquals( "Memory should have size 1",
+                             1,
+                             this.memory.size() );
+
+        obj2.setObjectAttr( "object20-modified" );
+
+        this.memory.remove( this.workingMemory,
+                            matches2 );
+        Assert.assertEquals( "Memory should have size 0",
+                             0,
+                             this.memory.size() );
+        Assert.assertTrue( "Memory is leaking object references",
+                           ((ObjectEqualConstrRightMemory) this.memory).isClean() );
+    }
+}


Property changes on: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/beta/InstanceEqualConstrRightMemoryTest.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list