[jboss-svn-commits] JBL Code SVN: r24396 - in labs/jbossrules/trunk: drools-compiler/src/test/java/org/drools/integrationtests and 9 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Dec 16 22:18:28 EST 2008
Author: tirelli
Date: 2008-12-16 22:18:28 -0500 (Tue, 16 Dec 2008)
New Revision: 24396
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldReader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldReader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/ArrayElementReader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/SelfReferenceClassFieldReader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateFieldExtractor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/InternalReadAccessor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/CompositeObjectSinkAdapterTest.java
Log:
JBRULES-1894: fixing the behavior of 'after' evaluator
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -749,8 +749,7 @@
}
}
- Target right = ( Date.class.isAssignableFrom( extractor.getExtractToClass() ) ||
- Number.class.isAssignableFrom( extractor.getExtractToClass() ) ) ? Target.FACT : Target.HANDLE;
+ Target right = getRightTarget( extractor );
Target left = ( declaration.isPatternDeclaration() && !
( Date.class.isAssignableFrom( declaration.getExtractor().getExtractToClass() ) ||
Number.class.isAssignableFrom( declaration.getExtractor().getExtractToClass() ) ) )? Target.HANDLE : Target.FACT;
@@ -796,8 +795,7 @@
return null;
}
- Target right = ( Date.class.isAssignableFrom( extractor.getExtractToClass() ) ||
- Number.class.isAssignableFrom( extractor.getExtractToClass() ) ) ? Target.FACT : Target.HANDLE;
+ Target right = getRightTarget( extractor );
Target left = Target.FACT;
final Evaluator evaluator = getEvaluator( context,
literalRestrictionDescr,
@@ -850,8 +848,7 @@
}
if ( implicit != null ) {
- Target right = ( Date.class.isAssignableFrom( extractor.getExtractToClass() ) ||
- Number.class.isAssignableFrom( extractor.getExtractToClass() ) ) ? Target.FACT : Target.HANDLE;
+ Target right = getRightTarget( extractor );
Target left = ( implicit.isPatternDeclaration() && !
( Date.class.isAssignableFrom( implicit.getExtractor().getExtractToClass() ) ||
Number.class.isAssignableFrom( implicit.getExtractor().getExtractToClass() ) ) )? Target.HANDLE : Target.FACT;
@@ -899,8 +896,7 @@
return null;
}
- Target right = ( Date.class.isAssignableFrom( extractor.getExtractToClass() ) ||
- Number.class.isAssignableFrom( extractor.getExtractToClass() ) ) ? Target.FACT : Target.HANDLE;
+ Target right = getRightTarget( extractor );
Target left = Target.FACT;
final Evaluator evaluator = getEvaluator( context,
qiRestrictionDescr,
@@ -919,6 +915,12 @@
extractor );
}
+ private Target getRightTarget(final InternalReadAccessor extractor) {
+ Target right = ( extractor.isSelfReference() && ! ( Date.class.isAssignableFrom( extractor.getExtractToClass() ) ||
+ Number.class.isAssignableFrom( extractor.getExtractToClass() ) ) ) ? Target.HANDLE : Target.FACT;
+ return right;
+ }
+
private ReturnValueRestriction buildRestriction(final RuleBuildContext context,
final Pattern pattern,
final InternalReadAccessor extractor,
@@ -946,8 +948,7 @@
analysis.getNotBoundedIdentifiers(),
factDeclarations );
- Target right = ( Date.class.isAssignableFrom( extractor.getExtractToClass() ) ||
- Number.class.isAssignableFrom( extractor.getExtractToClass() ) ) ? Target.FACT : Target.HANDLE;
+ Target right = getRightTarget( extractor );
Target left = Target.FACT;
final Evaluator evaluator = getEvaluator( context,
returnValueRestrictionDescr,
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -533,16 +533,22 @@
assertNotNull( handle1 );
assertNotNull( handle2 );
- assertFalse( handle1.isEvent() );
- assertFalse( handle2.isEvent() );
+ assertTrue( handle1.isEvent() );
+ assertTrue( handle2.isEvent() );
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
- assertEquals( 1,
+ assertEquals( 4,
results.size() );
+ assertEquals( tick1,
+ results.get( 0 ) );
assertEquals( tick2,
- results.get( 0 ) );
+ results.get( 1 ) );
+ assertEquals( tick1,
+ results.get( 2 ) );
+ assertEquals( tick2,
+ results.get( 3 ) );
}
// @FIXME: we need to decide on the semantics of expiration
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl 2008-12-17 03:18:28 UTC (rev 24396)
@@ -4,11 +4,45 @@
global java.util.List results;
+declare StockTick
+ @role( event )
+ @timestamp( time )
+end
+
rule "after operator on date"
+ salience 10
when
$a : StockTick( company == "DROO" )
- $b : StockTick( company == "ACME", dateTimestamp after[1s,10s] $a.dateTimestamp )
+ $b : StockTick( company == "ACME", dateTimestamp after[1s,11s] $a.dateTimestamp )
then
+ results.add( $a );
+end
+
+rule "after operator on long"
+ salience 5
+when
+ $a : StockTick( company == "DROO" )
+ $b : StockTick( company == "ACME", time after[1s,12s] $a.time )
+then
results.add( $b );
end
+
+rule "after operator on mixed event and date"
+ salience 3
+when
+ $a : StockTick( company == "DROO" )
+ $b : StockTick( company == "ACME", this after[1s,13s] $a.time )
+then
+ results.add( $a );
+end
+
+rule "after operator on mixed date and event"
+ salience 0
+when
+ $a : StockTick( company == "DROO" )
+ $b : StockTick( company == "ACME", time after[1s,14s] $a.time )
+then
+ results.add( $b );
+end
+
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldReader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldReader.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldReader.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -105,6 +105,10 @@
return false;
}
+ public boolean isSelfReference() {
+ return false;
+ }
+
public int hashCode() {
final int PRIME = 31;
int result = 1;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldReader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldReader.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassFieldReader.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -196,6 +196,10 @@
public boolean isGlobal() {
return false;
}
+
+ public boolean isSelfReference() {
+ return "this".equals( this.fieldName );
+ }
/**
* @param object
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -31,6 +31,7 @@
import org.drools.common.EventFactHandle;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
+import org.drools.rule.VariableRestriction.LongVariableContextEntry;
import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
import org.drools.rule.VariableRestriction.VariableContextEntry;
import org.drools.spi.Evaluator;
@@ -160,7 +161,7 @@
if ( this.cache == Collections.EMPTY_MAP ) {
this.cache = new HashMap<String, Evaluator>();
}
- String key = isNegated + ":" + parameterText;
+ String key = left+":"+right+":"+isNegated + ":" + parameterText;
Evaluator eval = this.cache.get( key );
if ( eval == null ) {
Long[] params = parser.parse( parameterText );
@@ -305,8 +306,20 @@
if ( context.rightNull ) {
return false;
}
- long rightTS = this.unwrapRight ? ((Date) ((ObjectVariableContextEntry) context).right).getTime() : ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getStartTimestamp();
-
+ long rightTS;
+ if( this.unwrapRight ) {
+ if( context instanceof ObjectVariableContextEntry ) {
+ if( ((ObjectVariableContextEntry) context).right instanceof Date ) {
+ rightTS = ((Date)((ObjectVariableContextEntry) context).right).getTime();
+ } else {
+ rightTS = ((Number)((ObjectVariableContextEntry) context).right).longValue();
+ }
+ } else {
+ rightTS = ((LongVariableContextEntry) context).right;
+ }
+ } else {
+ rightTS = ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getStartTimestamp();
+ }
long leftTS = this.unwrapLeft ? context.declaration.getExtractor().getLongValue( workingMemory,
left ) : ((EventFactHandle) left).getEndTimestamp();
@@ -324,8 +337,20 @@
long rightTS = this.unwrapRight ? context.extractor.getLongValue( workingMemory,
right ) : ((EventFactHandle) right).getStartTimestamp();
- long leftTS = this.unwrapLeft ? ((Date) ((ObjectVariableContextEntry) context).left).getTime() : ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp();
-
+ long leftTS;
+ if( this.unwrapLeft ) {
+ if( context instanceof ObjectVariableContextEntry ) {
+ if( ((ObjectVariableContextEntry) context).left instanceof Date ) {
+ leftTS = ((Date)((ObjectVariableContextEntry) context).left).getTime();
+ } else {
+ leftTS = ((Number)((ObjectVariableContextEntry) context).left).longValue();
+ }
+ } else {
+ leftTS = ((LongVariableContextEntry) context).left;
+ }
+ } else {
+ leftTS = ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp();
+ }
long dist = rightTS - leftTS;
return this.getOperator().isNegated() ^ (dist >= this.initRange && dist <= this.finalRange);
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/ArrayElementReader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/ArrayElementReader.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/ArrayElementReader.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -128,6 +128,10 @@
public boolean isGlobal() {
return false;
}
+
+ public boolean isSelfReference() {
+ return false;
+ }
public boolean getBooleanValue(Object object) {
return getBooleanValue( null,
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/SelfReferenceClassFieldReader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/SelfReferenceClassFieldReader.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/extractors/SelfReferenceClassFieldReader.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -43,4 +43,9 @@
public boolean isNullValue(InternalWorkingMemory workingMemory, final Object object) {
return getValue( workingMemory, object ) == null;
}
+
+ @Override
+ public boolean isSelfReference() {
+ return true;
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateFieldExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateFieldExtractor.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateFieldExtractor.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -122,6 +122,10 @@
return false;
}
+ public boolean isSelfReference() {
+ return false;
+ }
+
public boolean isNullValue(InternalWorkingMemory workingMemory,
Object object) {
return ((Fact) object).getFieldValue( this.fieldIndex ) == null;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/VariableRestriction.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -169,7 +169,7 @@
return new DoubleVariableContextEntry( fieldExtractor,
this.declaration,
this.evaluator );
- } else if ( coerced.isIntegerNumber() ) {
+ } else if ( coerced.isIntegerNumber() || coerced.isEvent() ) {
return new LongVariableContextEntry( fieldExtractor,
this.declaration,
this.evaluator );
@@ -328,9 +328,9 @@
this.object = evaluator.prepareLeftObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
this.right = this.extractor.getValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
}
public void resetTuple() {
@@ -394,11 +394,11 @@
this.object = evaluator.prepareLeftObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getLongValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
} else {
this.right = 0;
}
@@ -455,11 +455,11 @@
this.object = evaluator.prepareLeftObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getCharValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
} else {
this.right = 0;
}
@@ -516,11 +516,11 @@
this.object = evaluator.prepareLeftObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getDoubleValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
} else {
this.right = 0;
}
@@ -576,11 +576,11 @@
this.object = evaluator.prepareLeftObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getBooleanValue( workingMemory,
- evaluator.prepareLeftObject( handle ) );
+ evaluator.prepareRightObject( handle ) );
} else {
this.right = false;
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -197,6 +197,11 @@
public boolean isGlobal() {
return true;
}
+
+ public boolean isSelfReference() {
+ return false;
+ }
+
public boolean getBooleanValue(Object object) {
throw new RuntimeDroolsException("Can't extract a value from a global without a working memory reference");
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/InternalReadAccessor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/InternalReadAccessor.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/InternalReadAccessor.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -45,5 +45,7 @@
public int getHashCode(InternalWorkingMemory workingMemory, Object object);
public boolean isGlobal();
+
+ public boolean isSelfReference();
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -200,6 +200,10 @@
return false;
}
+ public boolean isSelfReference() {
+ return false;
+ }
+
public boolean getBooleanValue(Object object) {
return getBooleanValue( null,
object );
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -568,6 +568,10 @@
public void writeExternal(ObjectOutput out) throws IOException {
}
+ public boolean isSelfReference() {
+ return false;
+ }
+
public boolean getBooleanValue(InternalWorkingMemory workingMemory,
final Object object) {
return object != null ? ((Boolean) object).booleanValue() : false;
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -1077,6 +1077,10 @@
public void writeExternal(ObjectOutput out) throws IOException {
}
+ public boolean isSelfReference() {
+ return false;
+ }
+
public boolean getBooleanValue(InternalWorkingMemory workingMemory,
final Object object) {
return object != null ? ((Boolean) object).booleanValue() : false;
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/CompositeObjectSinkAdapterTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/CompositeObjectSinkAdapterTest.java 2008-12-16 21:58:17 UTC (rev 24395)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/CompositeObjectSinkAdapterTest.java 2008-12-17 03:18:28 UTC (rev 24396)
@@ -543,6 +543,10 @@
return false;
}
+ public boolean isSelfReference() {
+ return false;
+ }
+
}
static class MockBetaNode extends BetaNode {
More information about the jboss-svn-commits
mailing list