[jboss-svn-commits] JBL Code SVN: r17115 - in labs/jbossrules/branches/temporal_rete: drools-compiler/src/test/java/org/drools and 7 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Dec 8 12:09:59 EST 2007
Author: tirelli
Date: 2007-12-08 12:09:59 -0500 (Sat, 08 Dec 2007)
New Revision: 17115
Modified:
labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/StockTick.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TimeRelationalOperators.drl
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/BaseEvaluator.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/EvaluatorRegistry.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/SetEvaluatorsDefinition.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/VariableRestriction.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/spi/Evaluator.java
labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java
Log:
JBRULES-1374: adding support to 'after' operator
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/rule/builder/PatternBuilder.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -627,7 +627,8 @@
variableRestrictionDescr,
extractor.getValueType(),
variableRestrictionDescr.getEvaluator(),
- variableRestrictionDescr.isNegated() );
+ variableRestrictionDescr.isNegated(),
+ variableRestrictionDescr.getParameterText() );
if ( evaluator == null ) {
return null;
}
@@ -660,7 +661,8 @@
literalRestrictionDescr,
extractor.getValueType(),
literalRestrictionDescr.getEvaluator(),
- literalRestrictionDescr.isNegated() );
+ literalRestrictionDescr.isNegated(),
+ literalRestrictionDescr.getParameterText() );
if ( evaluator == null ) {
return null;
}
@@ -708,7 +710,8 @@
qiRestrictionDescr,
extractor.getValueType(),
qiRestrictionDescr.getEvaluator(),
- qiRestrictionDescr.isNegated() );
+ qiRestrictionDescr.isNegated(),
+ qiRestrictionDescr.getParameterText() );
if ( evaluator == null ) {
return null;
}
@@ -744,7 +747,8 @@
qiRestrictionDescr,
extractor.getValueType(),
qiRestrictionDescr.getEvaluator(),
- qiRestrictionDescr.isNegated() );
+ qiRestrictionDescr.isNegated(),
+ qiRestrictionDescr.getParameterText() );
if ( evaluator == null ) {
return null;
}
@@ -784,7 +788,8 @@
returnValueRestrictionDescr,
extractor.getValueType(),
returnValueRestrictionDescr.getEvaluator(),
- returnValueRestrictionDescr.isNegated() );
+ returnValueRestrictionDescr.isNegated(),
+ returnValueRestrictionDescr.getParameterText() );
if ( evaluator == null ) {
return null;
}
@@ -845,7 +850,8 @@
final BaseDescr descr,
final ValueType valueType,
final String evaluatorString,
- final boolean isNegated) {
+ final boolean isNegated,
+ final String parameterText ) {
final EvaluatorDefinition def = context.getConfiguration().getEvaluatorRegistry().getEvaluatorDefinition( evaluatorString );
if ( def == null ) {
@@ -858,7 +864,7 @@
final Evaluator evaluator = def.getEvaluator( valueType,
evaluatorString,
isNegated,
- null );
+ parameterText );
if ( evaluator == null ) {
context.getErrors().add( new RuleError( context.getRule(),
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/StockTick.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/StockTick.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/StockTick.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -45,6 +45,8 @@
this.time = time;
}
-
+ public String toString() {
+ return "StockTick( "+this.seq+" : " +this.company +" : "+ this.price +" )";
+ }
}
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -110,7 +110,7 @@
}
- public void FIXME_testTimeRelationalOperators() throws Exception {
+ public void testTimeRelationalOperators() throws Exception {
// read in the source
final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_TimeRelationalOperators.drl" ) );
final RuleBase ruleBase = loadRuleBase( reader );
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TimeRelationalOperators.drl
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TimeRelationalOperators.drl 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TimeRelationalOperators.drl 2007-12-08 17:09:59 UTC (rev 17115)
@@ -1,11 +1,13 @@
package org.drools;
+import event org.drools.StockTick;
+
global java.util.List results;
rule "after operator"
when
- $a : StockTick( stockSymbol == "DROO" )
- $b : StockTick( stockSymbol == "ACME", this ~after[5,10] $a )
+ $a : StockTick( company == "DROO" )
+ $b : StockTick( company == "ACME", this ~after[5,10] $a )
then
results.add( $b );
end
\ No newline at end of file
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/BaseEvaluator.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/BaseEvaluator.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/BaseEvaluator.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -17,6 +17,7 @@
*/
import org.drools.base.evaluators.Operator;
+import org.drools.common.InternalFactHandle;
import org.drools.spi.Evaluator;
/**
@@ -47,6 +48,14 @@
return this.type;
}
+ public ValueType getCoercedValueType() {
+ return this.type;
+ }
+
+ public Object prepareObject(InternalFactHandle handle) {
+ return handle.getObject();
+ }
+
public boolean equals(final Object object) {
if ( this == object ) {
return true;
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/AfterEvaluatorDefinition.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -24,8 +24,13 @@
import org.drools.RuntimeDroolsException;
import org.drools.base.BaseEvaluator;
import org.drools.base.ValueType;
+import org.drools.base.evaluators.MatchesEvaluatorsDefinition.StringMatchesEvaluator;
+import org.drools.base.evaluators.MatchesEvaluatorsDefinition.StringNotMatchesEvaluator;
+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;
import org.drools.spi.Extractor;
@@ -40,8 +45,13 @@
implements
EvaluatorDefinition {
- private static final String[] SUPPORTED_ID = {"after"};
-
+ public static final Operator AFTER = Operator.addOperatorToRegistry( "after",
+ false );
+ public static final Operator NOT_AFTER = Operator.addOperatorToRegistry( "after",
+ true );
+
+ private static final String[] SUPPORTED_IDS = { AFTER.getOperatorString() };
+
private Map<String, AfterEvaluator> cache = Collections.emptyMap();
/**
@@ -93,7 +103,7 @@
* @inheritDoc
*/
public String[] getEvaluatorIds() {
- return SUPPORTED_ID;
+ return SUPPORTED_IDS;
}
/**
@@ -125,11 +135,6 @@
public static class AfterEvaluator extends BaseEvaluator {
private static final long serialVersionUID = -4833205637340977934L;
- private static final Operator POSITIVE = Operator.addOperatorToRegistry( "after",
- false );
- private static final Operator NEGATIVE = Operator.addOperatorToRegistry( "after",
- true );
-
private long initRange;
private long finalRange;
@@ -137,43 +142,15 @@
final boolean isNegated,
final String parameters) {
super( type,
- isNegated ? NEGATIVE : POSITIVE );
+ isNegated ? NOT_AFTER : AFTER );
this.parseParameters( parameters );
}
-
- /**
- * This methods tries to parse the string of parameters to customize
- * the evaluator.
- *
- * @param parameters
- */
- private void parseParameters(String parameters) {
- if ( parameters == null || parameters.trim().length() == 0 ) {
- // open bounded range
- this.initRange = 1;
- this.finalRange = Long.MAX_VALUE;
- return;
- }
-
- try {
- String[] ranges = parameters.split( "," );
- if ( ranges.length == 1 ) {
- // deterministic point in time
- this.initRange = Long.parseLong( ranges[0] );
- this.finalRange = this.initRange;
- } else if ( ranges.length == 2 ) {
- // regular range
- this.initRange = Long.parseLong( ranges[0] );
- this.finalRange = Long.parseLong( ranges[1] );
- } else {
- throw new RuntimeDroolsException( "[After Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
- }
- } catch ( NumberFormatException e ) {
- throw new RuntimeDroolsException( "[After Evaluator]: Not possible to parse parameters: '" + parameters + "'",
- e );
- }
+
+ @Override
+ public Object prepareObject(InternalFactHandle handle) {
+ return handle;
}
-
+
public boolean evaluate(InternalWorkingMemory workingMemory,
final Extractor extractor,
final Object object1,
@@ -187,8 +164,8 @@
if ( context.rightNull ) {
return false;
}
- long dist = ((LongVariableContextEntry) context).right - context.declaration.getExtractor().getLongValue( workingMemory,
- left );
+ long dist = ((EventFactHandle)((ObjectVariableContextEntry) context).right).getStartTimestamp() -
+ ((EventFactHandle) left ).getEndTimestamp();
return dist >= this.initRange && dist <= this.finalRange;
}
@@ -199,8 +176,8 @@
right ) ) {
return false;
}
- long dist = context.extractor.getLongValue( workingMemory,
- right ) - ((LongVariableContextEntry) context).left;
+ long dist = ((EventFactHandle) right ).getStartTimestamp() -
+ ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp();
return dist >= this.initRange && dist <= this.finalRange;
}
@@ -214,9 +191,7 @@
object1 ) ) {
return false;
}
- long dist = extractor1.getLongValue( workingMemory,
- object1 ) - extractor2.getLongValue( workingMemory,
- object2 );
+ long dist = ((EventFactHandle) object1 ).getStartTimestamp() - ((EventFactHandle) object2 ).getEndTimestamp();
return dist >= this.initRange && dist <= this.finalRange;
}
@@ -247,6 +222,40 @@
final AfterEvaluator other = (AfterEvaluator) obj;
return finalRange == other.finalRange && initRange == other.initRange;
}
+
+ /**
+ * This methods tries to parse the string of parameters to customize
+ * the evaluator.
+ *
+ * @param parameters
+ */
+ private void parseParameters(String parameters) {
+ if ( parameters == null || parameters.trim().length() == 0 ) {
+ // open bounded range
+ this.initRange = 1;
+ this.finalRange = Long.MAX_VALUE;
+ return;
+ }
+
+ try {
+ String[] ranges = parameters.split( "," );
+ if ( ranges.length == 1 ) {
+ // deterministic point in time
+ this.initRange = Long.parseLong( ranges[0] );
+ this.finalRange = this.initRange;
+ } else if ( ranges.length == 2 ) {
+ // regular range
+ this.initRange = Long.parseLong( ranges[0] );
+ this.finalRange = Long.parseLong( ranges[1] );
+ } else {
+ throw new RuntimeDroolsException( "[After Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
+ }
+ } catch ( NumberFormatException e ) {
+ throw new RuntimeDroolsException( "[After Evaluator]: Not possible to parse parameters: '" + parameters + "'",
+ e );
+ }
+ }
+
}
}
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/EvaluatorRegistry.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/EvaluatorRegistry.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/EvaluatorRegistry.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -65,6 +65,13 @@
} else {
this.classloader = Thread.currentThread().getContextClassLoader() != null ? Thread.currentThread().getContextClassLoader() : this.getClass().getClassLoader();
}
+
+ // loading default built in evaluators
+ this.addEvaluatorDefinition( new EqualityEvaluatorsDefinition() );
+ this.addEvaluatorDefinition( new ComparableEvaluatorsDefinition() );
+ this.addEvaluatorDefinition( new SetEvaluatorsDefinition() );
+ this.addEvaluatorDefinition( new MatchesEvaluatorsDefinition() );
+ this.addEvaluatorDefinition( new SoundslikeEvaluatorsDefinition() );
}
/**
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/SetEvaluatorsDefinition.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/SetEvaluatorsDefinition.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/SetEvaluatorsDefinition.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -431,6 +431,11 @@
operator );
}
+ public ValueType getCoercedValueType() {
+ // during evaluation, always coerce to object
+ return ValueType.OBJECT_TYPE;
+ }
+
public boolean evaluate(InternalWorkingMemory workingMemory,
final Extractor extractor,
final Object object1,
@@ -512,6 +517,11 @@
operator );
}
+ public ValueType getCoercedValueType() {
+ // during evaluation, always coerce to object
+ return ValueType.OBJECT_TYPE;
+ }
+
public boolean evaluate(InternalWorkingMemory workingMemory,
final Extractor extractor,
final Object object1,
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/VariableRestriction.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/VariableRestriction.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/VariableRestriction.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -18,6 +18,7 @@
import java.util.Arrays;
+import org.drools.base.ValueType;
import org.drools.base.evaluators.Operator;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
@@ -73,23 +74,23 @@
final InternalWorkingMemory workingMemory) {
return this.evaluator.evaluate( workingMemory,
this.contextEntry.extractor,
- handle.getObject(),
+ this.evaluator.prepareObject( handle ),
this.contextEntry.declaration.getExtractor(),
- handle.getObject() );
+ this.evaluator.prepareObject( handle ) );
}
public boolean isAllowedCachedLeft(final ContextEntry context,
final InternalFactHandle handle) {
return this.evaluator.evaluateCachedLeft( ((VariableContextEntry) context).workingMemory,
(VariableContextEntry) context,
- handle.getObject() );
+ this.evaluator.prepareObject( handle ) );
}
public boolean isAllowedCachedRight(final ReteTuple tuple,
final ContextEntry context) {
return this.evaluator.evaluateCachedRight( ((VariableContextEntry) context).workingMemory,
(VariableContextEntry) context,
- tuple.get( this.declaration ).getObject() );
+ this.evaluator.prepareObject( tuple.get( this.declaration ) ) );
}
public String toString() {
@@ -125,27 +126,28 @@
private final VariableContextEntry createContextEntry(final Evaluator eval,
final FieldExtractor fieldExtractor) {
- // FIXME: remove this hardcoded reference to operator
- if ( "memberOf".equals( eval.getOperator().getOperatorString() ) ) {
+ ValueType coerced = eval.getCoercedValueType();
+
+ if ( coerced.isBoolean() ) {
+ return new BooleanVariableContextEntry( fieldExtractor,
+ this.declaration,
+ this.evaluator );
+ } else if ( coerced.isFloatNumber() ) {
+ return new DoubleVariableContextEntry( fieldExtractor,
+ this.declaration,
+ this.evaluator );
+ } else if ( coerced.isIntegerNumber() ) {
+ return new LongVariableContextEntry( fieldExtractor,
+ this.declaration,
+ this.evaluator );
+ } else if ( coerced.isChar() ) {
+ return new CharVariableContextEntry( fieldExtractor,
+ this.declaration,
+ this.evaluator );
+ } else {
return new ObjectVariableContextEntry( fieldExtractor,
- this.declaration );
- } else {
- if ( fieldExtractor.getValueType().isBoolean() ) {
- return new BooleanVariableContextEntry( fieldExtractor,
- this.declaration );
- } else if ( fieldExtractor.getValueType().isFloatNumber() ) {
- return new DoubleVariableContextEntry( fieldExtractor,
- this.declaration );
- } else if ( fieldExtractor.getValueType().isIntegerNumber() ) {
- return new LongVariableContextEntry( fieldExtractor,
- this.declaration );
- } else if ( fieldExtractor.getValueType().isChar() ) {
- return new CharVariableContextEntry( fieldExtractor,
- this.declaration );
- } else {
- return new ObjectVariableContextEntry( fieldExtractor,
- this.declaration );
- }
+ this.declaration,
+ this.evaluator );
}
}
@@ -157,6 +159,7 @@
implements
ContextEntry {
public FieldExtractor extractor;
+ public Evaluator evaluator;
public Object object;
public Declaration declaration;
public ReteTuple reteTuple;
@@ -166,9 +169,11 @@
public InternalWorkingMemory workingMemory;
public VariableContextEntry(final FieldExtractor extractor,
- final Declaration declaration) {
+ final Declaration declaration,
+ final Evaluator evaluator) {
this.extractor = extractor;
this.declaration = declaration;
+ this.evaluator = evaluator;
}
public ContextEntry getNext() {
@@ -211,9 +216,11 @@
public Object right;
public ObjectVariableContextEntry(final FieldExtractor extractor,
- final Declaration declaration) {
+ final Declaration declaration,
+ final Evaluator evaluator) {
super( extractor,
- declaration );
+ declaration,
+ evaluator);
}
public void updateFromTuple(final InternalWorkingMemory workingMemory,
@@ -221,19 +228,19 @@
this.reteTuple = tuple;
this.workingMemory = workingMemory;
this.leftNull = this.declaration.getExtractor().isNullValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
this.left = this.declaration.getExtractor().getValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
}
public void updateFromFactHandle(final InternalWorkingMemory workingMemory,
final InternalFactHandle handle) {
- this.object = handle.getObject();
+ this.object = evaluator.prepareObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
this.right = this.extractor.getValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
}
}
@@ -245,9 +252,11 @@
public long right;
public LongVariableContextEntry(final FieldExtractor extractor,
- final Declaration declaration) {
+ final Declaration declaration,
+ final Evaluator evaluator) {
super( extractor,
- declaration );
+ declaration,
+ evaluator );
}
public void updateFromTuple(final InternalWorkingMemory workingMemory,
@@ -255,11 +264,11 @@
this.reteTuple = tuple;
this.workingMemory = workingMemory;
this.leftNull = this.declaration.getExtractor().isNullValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
if ( !leftNull ) {
this.left = this.declaration.getExtractor().getLongValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
} else {
this.left = 0;
}
@@ -267,14 +276,14 @@
public void updateFromFactHandle(final InternalWorkingMemory workingMemory,
final InternalFactHandle handle) {
- this.object = handle.getObject();
+ this.object = evaluator.prepareObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getLongValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
} else {
this.right = 0;
}
@@ -289,9 +298,11 @@
public char right;
public CharVariableContextEntry(final FieldExtractor extractor,
- final Declaration declaration) {
+ final Declaration declaration,
+ final Evaluator evaluator) {
super( extractor,
- declaration );
+ declaration,
+ evaluator );
}
public void updateFromTuple(final InternalWorkingMemory workingMemory,
@@ -299,11 +310,11 @@
this.reteTuple = tuple;
this.workingMemory = workingMemory;
this.leftNull = this.declaration.getExtractor().isNullValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
if ( !leftNull ) {
this.left = this.declaration.getExtractor().getCharValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
} else {
this.left = 0;
}
@@ -311,14 +322,14 @@
public void updateFromFactHandle(final InternalWorkingMemory workingMemory,
final InternalFactHandle handle) {
- this.object = handle.getObject();
+ this.object = evaluator.prepareObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getCharValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
} else {
this.right = 0;
}
@@ -333,9 +344,11 @@
public double right;
public DoubleVariableContextEntry(final FieldExtractor extractor,
- final Declaration declaration) {
+ final Declaration declaration,
+ final Evaluator evaluator) {
super( extractor,
- declaration );
+ declaration,
+ evaluator );
}
public void updateFromTuple(final InternalWorkingMemory workingMemory,
@@ -343,11 +356,11 @@
this.reteTuple = tuple;
this.workingMemory = workingMemory;
this.leftNull = this.declaration.getExtractor().isNullValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
if ( !leftNull ) {
this.left = this.declaration.getExtractor().getDoubleValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
} else {
this.left = 0;
}
@@ -355,14 +368,14 @@
public void updateFromFactHandle(final InternalWorkingMemory workingMemory,
final InternalFactHandle handle) {
- this.object = handle.getObject();
+ this.object = evaluator.prepareObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getDoubleValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
} else {
this.right = 0;
}
@@ -376,9 +389,11 @@
public boolean right;
public BooleanVariableContextEntry(final FieldExtractor extractor,
- final Declaration declaration) {
+ final Declaration declaration,
+ final Evaluator evaluator) {
super( extractor,
- declaration );
+ declaration,
+ evaluator );
}
public void updateFromTuple(final InternalWorkingMemory workingMemory,
@@ -386,11 +401,11 @@
this.reteTuple = tuple;
this.workingMemory = workingMemory;
this.leftNull = this.declaration.getExtractor().isNullValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
if ( !leftNull ) {
this.left = this.declaration.getExtractor().getBooleanValue( workingMemory,
- tuple.get( this.declaration ).getObject() );
+ evaluator.prepareObject( tuple.get( this.declaration ) ) );
} else {
this.left = false;
}
@@ -398,14 +413,14 @@
public void updateFromFactHandle(final InternalWorkingMemory workingMemory,
final InternalFactHandle handle) {
- this.object = handle.getObject();
+ this.object = evaluator.prepareObject( handle );
this.workingMemory = workingMemory;
this.rightNull = this.extractor.isNullValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
if ( !rightNull ) { // avoid a NullPointerException
this.right = this.extractor.getBooleanValue( workingMemory,
- handle.getObject() );
+ evaluator.prepareObject( handle ) );
} else {
this.right = false;
}
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/spi/Evaluator.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/spi/Evaluator.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/spi/Evaluator.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -20,6 +20,7 @@
import org.drools.base.ValueType;
import org.drools.base.evaluators.Operator;
+import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
import org.drools.rule.VariableRestriction.VariableContextEntry;
@@ -43,8 +44,32 @@
* @return
*/
public Operator getOperator();
+
+ /**
+ * Returns the value type this evaluator will coerce
+ * operands to, during evaluation. This is useful for
+ * operators like "memberOf", that always convert to
+ * Object when evaluating, independently of the source
+ * operand value type.
+ *
+ * @return
+ */
+ public ValueType getCoercedValueType();
/**
+ * There are evaluators that operate on fact attributes and
+ * there are evaluators that operato on fact handle attributes
+ * (metadata).
+ *
+ * This method allows the evaluator to prepare the object
+ * to be evaluated. That includes, unwrapping the object if needed.
+ *
+ * @param handle
+ * @return
+ */
+ public Object prepareObject( InternalFactHandle handle );
+
+ /**
* This method will extract the value from the object1 using the
* extractor and compare it with the object2.
* @param workingMemory TODO
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java 2007-12-08 11:44:53 UTC (rev 17114)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/EvaluatorFactoryTest.java 2007-12-08 17:09:59 UTC (rev 17115)
@@ -29,12 +29,7 @@
import junit.framework.TestCase;
-import org.drools.base.evaluators.ComparableEvaluatorsDefinition;
-import org.drools.base.evaluators.EqualityEvaluatorsDefinition;
import org.drools.base.evaluators.EvaluatorRegistry;
-import org.drools.base.evaluators.MatchesEvaluatorsDefinition;
-import org.drools.base.evaluators.SetEvaluatorsDefinition;
-import org.drools.base.evaluators.SoundslikeEvaluatorsDefinition;
import org.drools.common.InternalWorkingMemory;
import org.drools.rule.Declaration;
import org.drools.rule.VariableRestriction.BooleanVariableContextEntry;
@@ -55,20 +50,8 @@
*/
public class EvaluatorFactoryTest extends TestCase {
- private EvaluatorRegistry registry;
+ private EvaluatorRegistry registry = new EvaluatorRegistry();
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- registry = new EvaluatorRegistry();
- registry.addEvaluatorDefinition( new EqualityEvaluatorsDefinition() );
- registry.addEvaluatorDefinition( new ComparableEvaluatorsDefinition() );
- registry.addEvaluatorDefinition( new SetEvaluatorsDefinition() );
- registry.addEvaluatorDefinition( new MatchesEvaluatorsDefinition() );
- registry.addEvaluatorDefinition( new SoundslikeEvaluatorsDefinition() );
- }
-
-
public void testObject() {
final List list = new ArrayList();
@@ -748,7 +731,8 @@
if( "memberOf".equals( evaluator.getOperator().getOperatorString() ) ) {
final ObjectVariableContextEntry context = new ObjectVariableContextEntry( extractor,
- declaration );
+ declaration,
+ evaluator );
if (row[2] == null) {
context.leftNull = true;
} else {
@@ -765,7 +749,8 @@
} else {
if ( valueType.isIntegerNumber() ) {
final LongVariableContextEntry context = new LongVariableContextEntry( extractor,
- declaration );
+ declaration,
+ evaluator );
if (row[2] == null) {
context.leftNull = true;
@@ -781,7 +766,8 @@
return context;
} else if ( valueType.isChar() ) {
final CharVariableContextEntry context = new CharVariableContextEntry( extractor,
- declaration );
+ declaration,
+ evaluator );
if (row[2] == null) {
context.leftNull = true;
@@ -797,7 +783,8 @@
return context;
} else if ( valueType.isBoolean() ) {
final BooleanVariableContextEntry context = new BooleanVariableContextEntry( extractor,
- declaration );
+ declaration,
+ evaluator );
if (row[2] == null) {
context.leftNull = true;
@@ -813,7 +800,8 @@
return context;
} else if ( valueType.isFloatNumber() ) {
final DoubleVariableContextEntry context = new DoubleVariableContextEntry( extractor,
- declaration );
+ declaration,
+ evaluator );
if (row[2] == null) {
context.leftNull = true;
} else {
@@ -828,7 +816,8 @@
return context;
} else {
final ObjectVariableContextEntry context = new ObjectVariableContextEntry( extractor,
- declaration );
+ declaration,
+ evaluator );
if (row[2] == null) {
context.leftNull = true;
} else {
More information about the jboss-svn-commits
mailing list