Author: bagerman
Date: 2006-06-20 01:22:41 -0400 (Tue, 20 Jun 2006)
New Revision: 4782
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/AlphaMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedIteratorFromPositionToTableStart.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableEnd.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableStart.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/RecordComparator.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableIterator.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableReverseOrderIterator.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedFactTableIterator.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/FactTable.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/TokenEvaluator.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/Table.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/TableIterator.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/TableRecord.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/TokenStack.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/leaps/util/TableIteratorTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/leaps/util/TableTest.java
Log:
JBRULES-316
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/AlphaMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/AlphaMemory.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/AlphaMemory.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -0,0 +1,32 @@
+package org.drools.leaps;
+
+import org.drools.WorkingMemory;
+import org.drools.common.InternalFactHandle;
+import org.drools.spi.FieldConstraint;
+import org.drools.spi.Tuple;
+import org.drools.util.IdentityMap;
+
+class AlphaMemory {
+ private IdentityMap alphaChecks = new IdentityMap( );
+
+ AlphaMemory() {
+
+ }
+
+ boolean checkAlpha( final FieldConstraint alpha,
+ final InternalFactHandle factHandle,
+ final Tuple tuple,
+ final WorkingMemory workingMemory ) {
+ Boolean ret = (Boolean) this.alphaChecks.get( factHandle );
+ if (ret == null) {
+ ret = new Boolean( alpha.isAllowed( factHandle, tuple, workingMemory ) );
+ this.alphaChecks.put( factHandle, ret );
+ }
+
+ return ret.booleanValue( );
+ }
+
+ boolean isAlphaBeenChecked(final InternalFactHandle factHandle){
+ return this.alphaChecks != null && this.alphaChecks.containsKey( factHandle );
+ }
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/FactTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/FactTable.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/FactTable.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -125,7 +125,7 @@
// rest would be added to stack automatically
final DefaultFactHandle startFactHandle = new DefaultFactHandle( workingMemory.getIdLastFireAllAt( ),
new Object( ) );
- for (final Iterator it = this.tailIterator( startFactHandle, startFactHandle ); it.hasNext( );) {
+ for (final Iterator it = this.iteratorFromPositionToTableStart( startFactHandle, startFactHandle ); it.hasNext( );) {
final LeapsFactHandle handle = (LeapsFactHandle) it.next( );
workingMemory.pushTokenOnStack( handle, new Token( workingMemory,
handle,
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -218,8 +218,9 @@
}
tuple.removeBlockingNotFactHandle( assembly.getIndex( ) );
- TokenEvaluator.evaluateNotCondition( new LeapsFactHandle( factHandle.getRecency( ) + 1,
- new Object( ) ),
+ TokenEvaluator.evaluateNotCondition( (LeapsFactHandle) factHandle,
+// TokenEvaluator.evaluateNotCondition( new LeapsFactHandle( factHandle.getRecency( ) + 1,
+// new Object( ) ),
assembly.getIndex( ),
tuple,
this );
@@ -235,8 +236,9 @@
tuple );
}
tuple.removeExistsFactHandle( assembly.getIndex( ) );
- TokenEvaluator.evaluateExistsCondition( new LeapsFactHandle( factHandle.getRecency( ) + 1,
- null ),
+ TokenEvaluator.evaluateExistsCondition( (LeapsFactHandle)factHandle,
+// TokenEvaluator.evaluateExistsCondition( new LeapsFactHandle( factHandle.getRecency( ) + 1,
+// null ),
assembly.getIndex( ),
tuple,
this );
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/TokenEvaluator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/TokenEvaluator.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/TokenEvaluator.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -39,25 +39,26 @@
* @throws Exception
* @throws InvalidRuleException
*/
- final static protected void evaluate(final Token token) throws NoMatchesFoundException,
- InvalidRuleException {
- final LeapsWorkingMemory workingMemory = (LeapsWorkingMemory) token.getWorkingMemory();
- final LeapsRule leapsRule = token.getCurrentRuleHandle().getLeapsRule();
+ final static protected void evaluate( final Token token )
+ throws NoMatchesFoundException, InvalidRuleException {
+ final LeapsWorkingMemory workingMemory = (LeapsWorkingMemory) token.getWorkingMemory( );
+ final LeapsRule leapsRule = token.getCurrentRuleHandle( ).getLeapsRule( );
// sometimes there is no normal conditions, only not and exists
- final int numberOfColumns = leapsRule.getNumberOfColumns();
- // if (numberOfColumns > 0) {
- final int dominantFactPosition = token.getCurrentRuleHandle().getDominantPosition();
- final InternalFactHandle dominantFactHandle = token.getDominantFactHandle();
- if ( leapsRule.getColumnConstraintsAtPosition( dominantFactPosition ).isAllowedAlpha( dominantFactHandle,
- token,
- workingMemory ) ) {
+ final int numberOfColumns = leapsRule.getNumberOfColumns( );
+ // if (numberOfColumns > 0) {
+ final int dominantFactPosition = token.getCurrentRuleHandle( )
+ .getDominantPosition( );
+ final InternalFactHandle dominantFactHandle = token.getDominantFactHandle( );
+ if (leapsRule.getColumnConstraintsAtPosition( dominantFactPosition )
+ .isAllowedAlpha( dominantFactHandle, token, workingMemory )) {
final Class dominantClass = leapsRule.getColumnClassObjectTypeAtPosition( dominantFactPosition );
final TableIterator[] iterators = new TableIterator[numberOfColumns];
// getting iterators first
- for ( int i = 0; i < numberOfColumns; i++ ) {
- if ( i == dominantFactPosition ) {
+ for (int i = 0; i < numberOfColumns; i++) {
+ if (i == dominantFactPosition) {
iterators[i] = Table.singleItemIterator( dominantFactHandle );
- } else {
+ }
+ else {
final Class columnClass = leapsRule.getColumnClassObjectTypeAtPosition( i );
final ColumnConstraints constraints = leapsRule.getColumnConstraintsAtPosition( i );
final FactTable factTable = workingMemory.getFactTable( columnClass );
@@ -65,15 +66,18 @@
new Object( ) )
: (LeapsFactHandle) dominantFactHandle;
//
- if ( i > 0 && constraints.isAlphaPresent() ) {
- iterators[i] = factTable.tailConstrainedIterator( workingMemory,
- constraints,
- startFactHandle,
- (token.isResume() ? (LeapsFactHandle) token.get( i ) : startFactHandle) );
- } else {
- iterators[i] = factTable.tailIterator( startFactHandle,
- (token.isResume() ? (LeapsFactHandle) token.get( i ) : startFactHandle) );
+ if (i > 0 && constraints.isAlphaPresent( )) {
+ iterators[i] = factTable.constrainedIteratorFromPositionToTableStart( workingMemory,
+ constraints,
+ startFactHandle,
+ ( token.isResume( ) ? (LeapsFactHandle) token.get( i )
+ : startFactHandle ) );
}
+ else {
+ iterators[i] = factTable.iteratorFromPositionToTableStart( startFactHandle,
+ ( token.isResume( ) ? (LeapsFactHandle) token.get( i )
+ : startFactHandle ) );
+ }
}
}
@@ -81,25 +85,28 @@
// check if we resume and any starting facts disappeared than we
// do not do skip on resume
boolean doReset = false;
- boolean skip = token.isResume();
+ boolean skip = token.isResume( );
TableIterator currentIterator;
- for ( int i = 0; i < numberOfColumns; i++ ) {
+ for (int i = 0; i < numberOfColumns; i++) {
currentIterator = iterators[i];
// check if one of them is empty and immediate return
- if ( currentIterator.isEmpty() ) {
- throw new NoMatchesFoundException();
- } else {
- if ( !doReset ) {
- if ( skip && currentIterator.hasNext() && !currentIterator.peekNext().equals( token.get( i ) ) ) {
- // we tried to resume but our fact handle at marker disappear
- // no need to resume just reset all interators positioned
- // at the marker where we stoped last time
+ if (currentIterator.isEmpty( )) {
+ throw new NoMatchesFoundException( );
+ }
+ else {
+ if (!doReset) {
+ if (skip && currentIterator.hasNext( )
+ && !currentIterator.peekNext( ).equals( token.get( i ) )) {
+ // we tried to resume but our fact handle at marker
+ // disappear no need to resume just reset all interators
+ // positioned at the marker where we stoped last time
skip = false;
doReset = true;
}
- } else {
- currentIterator.reset();
}
+ else {
+ currentIterator.reset( );
+ }
}
}
@@ -108,53 +115,59 @@
int jj = 0;
boolean done = false;
final int stopIteratingCount = numberOfColumns - 1;
- while ( !done ) {
+ while (!done) {
currentIterator = iterators[jj];
- if ( !currentIterator.hasNext() ) {
- if ( jj == 0 ) {
+ if (!currentIterator.hasNext( )) {
+ if (jj == 0) {
done = true;
- } else {
- // nothing for this column, go back and check next
+ }
+ else {
+ // nothing for this column, go back and check next
// on the one level up in nested loop
- currentIterator.reset();
+ currentIterator.reset( );
jj = jj - 1;
- if ( skip ) {
+ if (skip) {
skip = false;
}
}
- } else {
- final LeapsFactHandle currentFactHandle = (LeapsFactHandle) currentIterator.next();
+ }
+ else {
+ final LeapsFactHandle currentFactHandle = (LeapsFactHandle) currentIterator.next( );
// check if match found we need to check only beta for
// dominant fact
// alpha was already checked
boolean localMatch = false;
- if ( !skip ) {
- if ( jj != 0 || jj == dominantFactPosition ) {
- localMatch = leapsRule.getColumnConstraintsAtPosition( jj ).isAllowedBeta( currentFactHandle,
- token,
- workingMemory );
- } else {
- localMatch = leapsRule.getColumnConstraintsAtPosition( jj ).isAllowed( currentFactHandle,
- token,
- workingMemory );
+ if (!skip) {
+ if (jj != 0 || jj == dominantFactPosition) {
+ localMatch = leapsRule.getColumnConstraintsAtPosition( jj )
+ .isAllowedBeta( currentFactHandle,
+ token,
+ workingMemory );
}
+ else {
+ localMatch = leapsRule.getColumnConstraintsAtPosition( jj )
+ .isAllowed( currentFactHandle,
+ token,
+ workingMemory );
+ }
}
- if ( localMatch || skip ) {
- token.set( jj,
- currentFactHandle );
+ if (localMatch || skip) {
+ token.set( jj, currentFactHandle );
// start iteratating next iterator or for the last
// one check negative conditions and fire consequence
- if ( jj == stopIteratingCount ) {
- if ( !skip ) {
- if ( processAfterAllPositiveConstraintOk( token.getTuple(),
- leapsRule,
- workingMemory ) ) {
+ if (jj == stopIteratingCount) {
+ if (!skip) {
+ if (processAfterAllPositiveConstraintOk( token.getTuple( ),
+ leapsRule,
+ workingMemory )) {
return;
}
- } else {
+ }
+ else {
skip = false;
}
- } else {
+ }
+ else {
jj = jj + 1;
}
}
@@ -162,7 +175,7 @@
}
}
// nothing was found. inform caller about it
- throw new NoMatchesFoundException();
+ throw new NoMatchesFoundException( );
}
/**
@@ -215,13 +228,12 @@
* @return
* @throws Exception
*/
- private final static boolean evaluateEvalConditions(final LeapsRule leapsRule,
- final LeapsTuple tuple,
- final LeapsWorkingMemory workingMemory) {
- final EvalCondition[] evals = leapsRule.getEvalConditions();
- for ( int i = 0; i < evals.length; i++ ) {
- if ( !evals[i].isAllowed( tuple,
- workingMemory ) ) {
+ private final static boolean evaluateEvalConditions( final LeapsRule leapsRule,
+ final LeapsTuple tuple,
+ final LeapsWorkingMemory workingMemory ) {
+ final EvalCondition[] evals = leapsRule.getEvalConditions( );
+ for (int i = 0; i < evals.length; i++) {
+ if (!evals[i].isAllowed( tuple, workingMemory )) {
return false;
}
}
@@ -237,26 +249,23 @@
* @return success
* @throws Exception
*/
- final static void evaluateNotConditions(final LeapsTuple tuple,
- final LeapsRule rule,
- final LeapsWorkingMemory workingMemory) {
- final ColumnConstraints[] not = rule.getNotColumnConstraints();
- for ( int i = 0, length = not.length; i < length; i++ ) {
+ final static void evaluateNotConditions( final LeapsTuple tuple,
+ final LeapsRule rule,
+ final LeapsWorkingMemory workingMemory ) {
+ final ColumnConstraints[] not = rule.getNotColumnConstraints( );
+ for (int i = 0, length = not.length; i < length; i++) {
final ColumnConstraints constraint = not[i];
// scan table starting at start fact handle
- final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType() ).reverseOrderIterator();
+ final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType( ) )
+ .reverseOrderIterator( );
// stops if exists
boolean done = false;
- while ( !done && tableIterator.hasNext() ) {
- final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next();
+ while (!done && tableIterator.hasNext( )) {
+ final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next( );
// check constraint conditions
- if ( constraint.isAllowed( factHandle,
- tuple,
- workingMemory ) ) {
- tuple.setBlockingNotFactHandle( factHandle,
- i );
- factHandle.addNotTuple( tuple,
- i );
+ if (constraint.isAllowed( factHandle, tuple, workingMemory )) {
+ tuple.setBlockingNotFactHandle( factHandle, i );
+ factHandle.addNotTuple( tuple, i );
done = true;
}
}
@@ -273,26 +282,23 @@
* @param rule
* @param workingMemory
*/
- final static void evaluateNotCondition(final LeapsFactHandle startFactHandle,
- final int index,
- final LeapsTuple tuple,
- final LeapsWorkingMemory workingMemory) {
- final LeapsRule rule = tuple.getLeapsRule();
+ final static void evaluateNotCondition( final LeapsFactHandle startFactHandle,
+ final int index,
+ final LeapsTuple tuple,
+ final LeapsWorkingMemory workingMemory ) {
+ final LeapsRule rule = tuple.getLeapsRule( );
// scan table starting at start fact handle
- final ColumnConstraints constraint = rule.getNotColumnConstraints()[index];
- final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType() ).headReverseOrderIterator( startFactHandle );
+ final ColumnConstraints constraint = rule.getNotColumnConstraints( )[index];
+ final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType( ) )
+ .iteratorFromPositionToTableEnd( startFactHandle );
// stops if exists
boolean done = false;
- while ( !done && tableIterator.hasNext() ) {
- final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next();
+ while (!done && tableIterator.hasNext( )) {
+ final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next( );
// check constraint conditions
- if ( constraint.isAllowed( factHandle,
- tuple,
- workingMemory ) ) {
- tuple.setBlockingNotFactHandle( factHandle,
- index );
- factHandle.addNotTuple( tuple,
- index );
+ if (constraint.isAllowed( factHandle, tuple, workingMemory )) {
+ tuple.setBlockingNotFactHandle( factHandle, index );
+ factHandle.addNotTuple( tuple, index );
done = true;
}
}
@@ -305,20 +311,21 @@
* @param memory
* @throws Exception
*/
- private final static void evaluateExistsConditions(final LeapsTuple tuple,
- final LeapsRule rule,
- final LeapsWorkingMemory workingMemory) {
- final ColumnConstraints[] exists = rule.getExistsColumnConstraints();
- for ( int i = 0, length = exists.length; i < length; i++ ) {
+ private final static void evaluateExistsConditions( final LeapsTuple tuple,
+ final LeapsRule rule,
+ final LeapsWorkingMemory workingMemory ) {
+ final ColumnConstraints[] exists = rule.getExistsColumnConstraints( );
+ for (int i = 0, length = exists.length; i < length; i++) {
final ColumnConstraints constraint = exists[i];
// scan table starting at start fact handle
- final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType() ).reverseOrderIterator();
+ final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType( ) )
+ .reverseOrderIterator( );
// stop if exists
boolean done = false;
- while ( !done && tableIterator.hasNext() ) {
- final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next();
+ while (!done && tableIterator.hasNext( )) {
+ final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next( );
// check constraint conditions
- if ( constraint.isAllowed( factHandle,
+ if (constraint.isAllowed( factHandle,
tuple,
workingMemory ) ) {
tuple.setExistsFactHandle( factHandle,
@@ -341,28 +348,25 @@
* @param rule
* @param workingMemory
*/
- final static void evaluateExistsCondition(final LeapsFactHandle startFactHandle,
- final int index,
- final LeapsTuple tuple,
- final LeapsWorkingMemory workingMemory) {
- final LeapsRule rule = tuple.getLeapsRule();
+ final static void evaluateExistsCondition( final LeapsFactHandle startFactHandle,
+ final int index,
+ final LeapsTuple tuple,
+ final LeapsWorkingMemory workingMemory ) {
+ final LeapsRule rule = tuple.getLeapsRule( );
// scan table starting at start fact handle
- final ColumnConstraints constraint = rule.getExistsColumnConstraints()[index];
- final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType() ).headReverseOrderIterator( startFactHandle );
+ final ColumnConstraints constraint = rule.getExistsColumnConstraints( )[index];
+ final TableIterator tableIterator = workingMemory.getFactTable( constraint.getClassType( ) )
+ .iteratorFromPositionToTableEnd( startFactHandle );
// stop if exists
boolean done = false;
- while ( !done && tableIterator.hasNext() ) {
- final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next();
+ while (!done && tableIterator.hasNext( )) {
+ final LeapsFactHandle factHandle = (LeapsFactHandle) tableIterator.next( );
// check constraint conditions
- if ( constraint.isAllowed( factHandle,
- tuple,
- workingMemory ) ) {
- tuple.setExistsFactHandle( factHandle,
- index );
- factHandle.addExistsTuple( tuple,
- index );
+ if (constraint.isAllowed( factHandle, tuple, workingMemory )) {
+ tuple.setExistsFactHandle( factHandle, index );
+ factHandle.addExistsTuple( tuple, index );
done = true;
}
}
}
-}
\ No newline at end of file
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableIterator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableIterator.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableIterator.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -1,109 +0,0 @@
-package org.drools.leaps.util;
-
-/*
- * 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.
- */
-
-import java.util.NoSuchElementException;
-
-/**
- * Leaps specific iterator for leaps tables. relies on leaps table double link
- * list structure for navigation
- *
- * @author Alexander Bagerman
- *
- */
-public class BaseTableIterator
- implements
- TableIterator {
- /**
- * interator that was not initialized as "empty" iterator (one or another
- * record was submitted to constractor) will set it to false
- */
- private TableRecord firstRecord;
-
- private TableRecord lastRecord;
-
- private TableRecord currentRecord;
-
- private TableRecord nextRecord;
-
- /**
- * constracts an leaps iterator to iterate over a single record. Used for
- * Dominant fact dimention iteration
- *
- * @param record
- * to iterate over
- */
-
- /**
- * constracts an leaps iterator to iterate over a single record. Used for
- * Dominant fact dimention iteration
- *
- * @param record
- * to iterate over
- */
- protected BaseTableIterator(final TableRecord record) {
- this.firstRecord = record;
- this.lastRecord = record;
- this.currentRecord = null;
- this.nextRecord = this.firstRecord;
- }
-
- protected BaseTableIterator(final TableRecord startRecord,
- final TableRecord currentRecord,
- final TableRecord lastRecord) {
- this.firstRecord = startRecord;
- this.nextRecord = currentRecord;
- this.lastRecord = lastRecord;
- this.currentRecord = null;
- }
-
- public boolean isEmpty() {
- return this.firstRecord == null;
- }
-
- public void reset() {
- this.currentRecord = null;
- this.nextRecord = this.firstRecord;
- }
-
- public boolean hasNext() {
- return this.nextRecord != null;
- }
-
- public Object next() {
- this.currentRecord = this.nextRecord;
- if ( this.currentRecord != null ) {
- // need to check on last record because we iterate of subset of
- // all data limited by last record
- if ( this.currentRecord == this.lastRecord ) {
- this.nextRecord = null;
- } else {
- this.nextRecord = this.currentRecord.right;
- }
- } else {
- throw new NoSuchElementException( "No more elements to return" );
- }
- return this.currentRecord.object;
- }
-
- public Object peekNext() {
- return this.nextRecord.object;
- }
-
- public void remove() {
- }
-}
\ No newline at end of file
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableReverseOrderIterator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableReverseOrderIterator.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/BaseTableReverseOrderIterator.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -1,113 +0,0 @@
-package org.drools.leaps.util;
-
-/*
- * 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.
- */
-
-import java.util.NoSuchElementException;
-
-/**
- * Leaps specific iterator for leaps tables. relies on leaps table double link
- * list structure for navigation
- *
- * @author Alexander Bagerman
- *
- */
-public class BaseTableReverseOrderIterator
- implements
- TableIterator {
- /**
- * interator that was not initialized as "empty" iterator (one or another
- * record was submitted to constractor) will set it to false
- */
- private TableRecord firstRecord;
-
- private TableRecord lastRecord;
-
- private TableRecord currentRecord;
-
- private TableRecord nextRecord;
-
- /**
- * constracts an leaps iterator to iterate over a single record. Used for
- * Dominant fact dimention iteration
- *
- * @param record
- * to iterate over
- */
-
- /**
- * constracts an leaps iterator to iterate over a single record. Used for
- * Dominant fact dimention iteration
- *
- * @param record
- * to iterate over
- */
- protected BaseTableReverseOrderIterator(final TableRecord record) {
- this.firstRecord = record;
- this.lastRecord = record;
- this.currentRecord = null;
- this.nextRecord = this.firstRecord;
- }
-
- protected BaseTableReverseOrderIterator(final TableRecord startRecord,
- final TableRecord currentRecord,
- final TableRecord lastRecord) {
- this.firstRecord = startRecord;
- this.nextRecord = currentRecord;
- this.lastRecord = lastRecord;
- this.currentRecord = null;
- }
-
- public boolean isEmpty() {
- return this.firstRecord == null;
- }
-
- public void reset() {
- this.currentRecord = null;
- this.nextRecord = this.firstRecord;
- }
-
- public boolean hasNext() {
- return this.nextRecord != null;
- }
-
- public Object next() {
- this.currentRecord = this.nextRecord;
- if ( this.currentRecord != null ) {
- // need to check on last record because we iterate of subset of
- // all data limited by last record
- if ( this.currentRecord == this.lastRecord ) {
- this.nextRecord = null;
- } else {
- this.nextRecord = this.currentRecord.left;
- }
- } else {
- throw new NoSuchElementException( "No more elements to return" );
- }
- return this.currentRecord.object;
- }
-
- public Object current() {
- return this.currentRecord.object;
- }
-
- public Object peekNext() {
- return this.nextRecord.object;
- }
-
- public void remove() {
- }
-}
\ No newline at end of file
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedFactTableIterator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedFactTableIterator.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedFactTableIterator.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -1,161 +0,0 @@
-package org.drools.leaps.util;
-
-/*
- * 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.
- */
-
-import java.util.NoSuchElementException;
-
-import org.drools.WorkingMemory;
-import org.drools.common.InternalFactHandle;
-import org.drools.leaps.ColumnConstraints;
-
-/**
- * this class is for multi pass iterations to sort out facts that do not satisfy
- * alpha nodes
- *
- * previous to the left
- * next to the right
- *
- * @author Alexander Bagerman
- *
- */
-public class ConstrainedFactTableIterator
- implements
- TableIterator {
- private boolean finishInitialPass = false;
-
- final WorkingMemory workingMemory;
-
- final ColumnConstraints constraints;
-
- private int size = 0;
-
- private TableRecord firstRecord;
-
- private TableRecord lastRecord;
-
- private TableRecord currentRecord;
-
- private TableRecord nextRecord;
-
- private TableRecord currentTableRecord;
-
- private TableRecord lastTableRecord;
-
- protected ConstrainedFactTableIterator(final WorkingMemory workingMemory,
- final ColumnConstraints constraints,
- final TableRecord startRecord,
- final TableRecord currentRecord,
- final TableRecord lastRecord) {
- this.workingMemory = workingMemory;
- this.constraints = constraints;
- this.lastTableRecord = lastRecord;
- this.currentTableRecord = startRecord;
- boolean done = false;
- boolean reachCurrentRecord = false;
- while ( !done && this.currentTableRecord != null && !this.finishInitialPass ) {
- if ( !reachCurrentRecord && this.currentTableRecord == currentRecord ) {
- reachCurrentRecord = true;
- } else {
- if ( this.constraints.isAllowedAlpha( (InternalFactHandle) this.currentTableRecord.object,
- null,
- this.workingMemory ) ) {
- this.add( this.currentTableRecord.object );
- }
- if ( reachCurrentRecord && !this.isEmpty() ) {
- done = true;
- }
- if ( this.currentTableRecord == this.lastTableRecord ) {
- this.finishInitialPass = true;
- }
- this.currentTableRecord = this.currentTableRecord.right;
- }
- }
- //
- this.nextRecord = this.lastRecord;
- }
-
- private void add(final Object object) {
- final TableRecord record = new TableRecord( object );
- if ( this.firstRecord == null ) {
- this.firstRecord = record;
- this.lastRecord = record;
- } else {
- this.lastRecord.right = record;
- record.left = this.lastRecord;
- this.lastRecord = record;
- }
- this.size++;
- }
-
- public boolean isEmpty() {
- return this.firstRecord == null;
- }
-
- public void reset() {
- this.currentRecord = null;
- this.nextRecord = this.firstRecord;
- }
-
- public Object next() {
- this.currentRecord = this.nextRecord;
- if ( this.currentRecord != null ) {
- this.nextRecord = this.currentRecord.right;
- } else {
- throw new NoSuchElementException( "No more elements to return" );
- }
- return this.currentRecord.object;
- }
-
- public Object current() {
- return this.currentRecord.object;
- }
-
- public Object peekNext() {
- return this.nextRecord.object;
- }
-
- public void remove() {
- }
-
- public boolean hasNext() {
- if ( !this.finishInitialPass ) {
- if ( this.nextRecord == null ) {
- boolean found = false;
- while ( !found && this.currentTableRecord != null ) {
- if ( this.constraints.isAllowedAlpha( (InternalFactHandle) this.currentTableRecord.object,
- null,
- this.workingMemory ) ) {
- this.add( this.currentTableRecord.object );
- found = true;
- }
- if ( this.currentTableRecord == this.lastTableRecord ) {
- this.finishInitialPass = true;
- }
- this.currentTableRecord = this.currentTableRecord.right;
- }
- //
- if ( found ) {
- this.nextRecord = this.lastRecord;
- }
- return found;
- }
- return true;
- } else {
- return this.nextRecord != null;
- }
- }
-}
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedIteratorFromPositionToTableStart.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedIteratorFromPositionToTableStart.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/ConstrainedIteratorFromPositionToTableStart.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -0,0 +1,115 @@
+package org.drools.leaps.util;
+
+/*
+ * 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.
+ */
+
+import org.drools.WorkingMemory;
+import org.drools.common.InternalFactHandle;
+import org.drools.leaps.ColumnConstraints;
+
+/**
+ * this class is for multi pass iterations to sort out facts that do not satisfy
+ * alpha nodes
+ *
+ * previous to the left
+ * next to the right
+ *
+ * @author Alexander Bagerman
+ *
+ */
+public class ConstrainedIteratorFromPositionToTableStart extends IteratorFromPositionToTableStart {
+ private boolean finishInitialPass = false;
+
+ final WorkingMemory workingMemory;
+
+ final ColumnConstraints constraints;
+
+ private TableRecord currentTableRecord;
+
+ protected ConstrainedIteratorFromPositionToTableStart(final WorkingMemory workingMemory,
+ final ColumnConstraints constraints,
+ final TableRecord startRecord,
+ final TableRecord currentRecord) {
+ super( null );
+ this.workingMemory = workingMemory;
+ this.constraints = constraints;
+ this.currentTableRecord = startRecord;
+ boolean done = false;
+ boolean reachCurrentRecord = false;
+ while (!done && this.currentTableRecord != null && !this.finishInitialPass) {
+ if (!reachCurrentRecord && this.currentTableRecord == currentRecord) {
+ reachCurrentRecord = true;
+ }
+ else {
+ if (this.constraints.isAllowedAlpha( (InternalFactHandle) this.currentTableRecord.object,
+ null,
+ this.workingMemory )) {
+ this.add( this.currentTableRecord.object );
+ }
+ if (reachCurrentRecord && !this.isEmpty( )) {
+ done = true;
+ }
+ if (this.currentTableRecord.right == null) {
+ this.finishInitialPass = true;
+ }
+ this.currentTableRecord = this.currentTableRecord.right;
+ }
+ }
+ }
+
+ private void add( final Object object ) {
+ final TableRecord record = new TableRecord( object );
+ if (this.firstRecord == null) {
+ this.firstRecord = record;
+ this.currentRecord = record;
+ }
+ else {
+ this.currentRecord.right = record;
+ record.left = this.currentRecord;
+ this.currentRecord = record;
+ }
+
+ this.nextRecord = this.currentRecord;
+ }
+
+ public boolean hasNext() {
+ if (!this.finishInitialPass) {
+ if (this.nextRecord == null) {
+ boolean found = false;
+ while (!found && this.currentTableRecord != null) {
+ if (this.constraints.isAllowedAlpha( (InternalFactHandle) this.currentTableRecord.object,
+ null,
+ this.workingMemory )) {
+ this.add( this.currentTableRecord.object );
+ found = true;
+ }
+ if (this.currentTableRecord == null) {
+ this.finishInitialPass = true;
+ }
+ this.currentTableRecord = this.currentTableRecord.right;
+ }
+
+ return found;
+ }
+ else {
+ return true;
+ }
+ }
+ else {
+ return super.hasNext( );
+ }
+ }
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableEnd.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableEnd.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableEnd.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -0,0 +1,63 @@
+package org.drools.leaps.util;
+
+/*
+ * 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.
+ */
+
+import java.util.NoSuchElementException;
+
+/**
+ * Leaps specific iterator for leaps tables. relies on leaps table double link
+ * list structure for navigation
+ *
+ * @author Alexander Bagerman
+ *
+ */
+public class IteratorFromPositionToTableEnd extends IteratorFromPositionToTableStart {
+ /**
+ * @see IteratorFromPositionToTableStart
+ */
+ protected IteratorFromPositionToTableEnd(final TableRecord record) {
+ super( record );
+ }
+
+ /**
+ * @see IteratorFromPositionToTableStart
+ */
+ protected IteratorFromPositionToTableEnd(final TableRecord startRecord,
+ final TableRecord currentRecord) {
+ super( startRecord, currentRecord );
+ }
+
+ /**
+ * the difference here is that we are going to the different direction that
+ * base class next() method
+ *
+ * @see IteratorFromPositionToTableStart
+ *
+ */
+
+ public Object next() {
+ this.currentRecord = this.nextRecord;
+ if (this.currentRecord != null) {
+ this.nextRecord = this.currentRecord.left;
+ }
+ else {
+ throw new NoSuchElementException( "No more elements to return" );
+ }
+ return this.currentRecord.object;
+ }
+
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableStart.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableStart.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/IteratorFromPositionToTableStart.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -0,0 +1,90 @@
+package org.drools.leaps.util;
+
+/*
+ * 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.
+ */
+
+import java.util.NoSuchElementException;
+
+/**
+ * Leaps specific iterator for leaps tables. relies on leaps table double link
+ * list structure for navigation
+ *
+ * @author Alexander Bagerman
+ *
+ */
+public class IteratorFromPositionToTableStart implements TableIterator {
+ /**
+ * interator that was not initialized as "empty" iterator (one or another
+ * record was submitted to constractor) will set it to false
+ */
+ TableRecord firstRecord;
+
+ TableRecord currentRecord;
+
+ TableRecord nextRecord;
+
+ /**
+ * constracts an leaps iterator to iterate over a single record. Used for
+ * Dominant fact dimention iteration
+ *
+ * @param record
+ * to iterate over
+ */
+
+ protected IteratorFromPositionToTableStart(final TableRecord record) {
+ this.firstRecord = record;
+ this.currentRecord = null;
+ this.nextRecord = this.firstRecord;
+ }
+
+ protected IteratorFromPositionToTableStart(final TableRecord startRecord,
+ final TableRecord currentRecord) {
+ this.firstRecord = startRecord;
+ this.nextRecord = currentRecord;
+ this.currentRecord = null;
+ }
+
+ public boolean isEmpty() {
+ return this.firstRecord == null;
+ }
+
+ public void reset() {
+ this.currentRecord = null;
+ this.nextRecord = this.firstRecord;
+ }
+
+ public boolean hasNext() {
+ return this.nextRecord != null;
+ }
+
+ public Object next() {
+ this.currentRecord = this.nextRecord;
+ if (this.currentRecord != null) {
+ this.nextRecord = this.currentRecord.right;
+ }
+ else {
+ throw new NoSuchElementException( "No more elements to return" );
+ }
+ return this.currentRecord.object;
+ }
+
+ public Object peekNext() {
+ return this.nextRecord.object;
+ }
+
+ public void remove() {
+ }
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/RecordComparator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/RecordComparator.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/RecordComparator.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -0,0 +1,43 @@
+package org.drools.leaps.util;
+
+/*
+ * 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.
+ */
+
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * this class wraps object comparator to sort records holding given objects in
+ * a list
+ *
+ * @author Alexander Bagerman
+ *
+ */
+
+class RecordComparator implements Comparator, Serializable {
+ private Comparator objectComparator;
+
+ RecordComparator(Comparator objectComparator) {
+ this.objectComparator = objectComparator;
+ }
+
+ public int compare( Object record1, Object record2 ) {
+ return this.objectComparator.compare( ( (TableRecord) record1 ).object,
+ ( (TableRecord) record2 ).object );
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/Table.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/Table.java 2006-06-20 02:13:42 UTC (rev 4781)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/util/Table.java 2006-06-20 05:22:41 UTC (rev 4782)
@@ -20,27 +20,26 @@
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.TreeSet;
import org.drools.WorkingMemory;
import org.drools.leaps.ColumnConstraints;
/**
+ * double linked list structure to store objects in the ordered list
+ * and iterate over the list for leaps
*
* @author Alexander Bagerman
*
*/
-public class Table
- implements
- Serializable {
+public class Table implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2614082619270512055L;
- private final TreeMap map;
+ private final TreeSet set;
protected TableRecord headRecord;
@@ -51,63 +50,57 @@
private int count = 0;
public Table(final Comparator comparator) {
- this.map = new TreeMap( comparator );
+ this.set = new TreeSet( new RecordComparator(comparator) );
}
protected void clear() {
this.headRecord = new TableRecord( null );
this.empty = true;
this.count = 0;
- this.map.clear();
+ this.set.clear( );
}
/**
* @param object
* to add
*/
- public void add(final Object object) {
- boolean foundEqualObject = false;
+ public void add( final Object object ) {
final TableRecord newRecord = new TableRecord( object );
- if ( this.empty ) {
+ if (this.empty) {
this.headRecord = newRecord;
this.empty = false;
- } else {
- final SortedMap bufMap = this.map.headMap( object );
+ }
+ else {
try {
// check on first key should work faster than check on empty
// but logically we check on empty
// if map empty it will throw exception
- bufMap.firstKey();
-
- // if ( !bufMap.isEmpty() ) {
- final TableRecord bufRec = (TableRecord) this.map.get( bufMap.lastKey() );
- if ( bufRec.right != null ) {
+ final TableRecord bufRec = (TableRecord) this.set.headSet( newRecord )
+ .last( );
+ if (bufRec.right != null) {
bufRec.right.left = newRecord;
}
newRecord.right = bufRec.right;
bufRec.right = newRecord;
newRecord.left = bufRec;
- } //else {
- catch ( final NoSuchElementException nsee ) {
+ }
+ catch (final NoSuchElementException nsee) {
// means map is empty
this.headRecord.left = newRecord;
newRecord.right = this.headRecord;
this.headRecord = newRecord;
}
}
- if ( !foundEqualObject ) {
- // check if the new record was added at the end of the list
- // and assign new value to the tail record
- if ( newRecord.right == null ) {
- this.tailRecord = newRecord;
- }
- //
- this.count++;
- //
- this.map.put( object,
- newRecord );
+ // check if the new record was added at the end of the list
+ // and assign new value to the tail record
+ if (newRecord.right == null) {
+ this.tailRecord = newRecord;
}
+ //
+ this.count++;
+ //
+ this.set.add( newRecord );
}
/**
@@ -116,39 +109,46 @@
* @param object
* to remove from the table
*/
- public void remove(final Object object) {
- if ( !this.empty ) {
- final TableRecord record = (TableRecord) this.map.get( object );
+ public void remove( final Object object ) {
+ if (!this.empty) {
+ try {
+ final TableRecord record = (TableRecord) this.set.tailSet( new TableRecord( object ) )
+ .first( );
- if ( record != null ) {
- if ( record == this.headRecord ) {
- if ( record.right != null ) {
- this.headRecord = record.right;
- this.headRecord.left = null;
- } else {
- // single element in table being valid
- // table is empty now
- this.headRecord = new TableRecord( null );
- this.tailRecord = this.headRecord;
- this.empty = true;
+ if (record != null) {
+ if (record == this.headRecord) {
+ if (record.right != null) {
+ this.headRecord = record.right;
+ this.headRecord.left = null;
+ }
+ else {
+ // ...
[truncated message content] |