[jboss-svn-commits] JBL Code SVN: r5296 - in labs/jbossrules/trunk/drools-compiler/src: main/java/org/drools/semantics/java test/java/org/drools test/java/org/drools/integrationtests test/resources/org/drools/integrationtests
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jul 26 14:59:25 EDT 2006
Author: mark.proctor at jboss.com
Date: 2006-07-26 14:59:20 -0400 (Wed, 26 Jul 2006)
New Revision: 5296
Added:
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultiRestrictionFieldConstraint.drl
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Person.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java
Log:
JBRULES-333 Update constraints to handle connectives
JBRULES-334 language support for field constraints
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java 2006-07-26 16:52:42 UTC (rev 5295)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java 2006-07-26 18:59:20 UTC (rev 5296)
@@ -49,27 +49,37 @@
import org.drools.lang.descr.PatternDescr;
import org.drools.lang.descr.PredicateDescr;
import org.drools.lang.descr.QueryDescr;
+import org.drools.lang.descr.RestrictionConnectiveDescr;
+import org.drools.lang.descr.RestrictionDescr;
import org.drools.lang.descr.ReturnValueRestrictionDescr;
import org.drools.lang.descr.RuleDescr;
import org.drools.lang.descr.VariableRestrictionDescr;
import org.drools.rule.And;
+import org.drools.rule.AndCompositeRestriction;
import org.drools.rule.Column;
import org.drools.rule.Declaration;
import org.drools.rule.EvalCondition;
import org.drools.rule.Exists;
import org.drools.rule.GroupElement;
import org.drools.rule.LiteralConstraint;
+import org.drools.rule.LiteralRestriction;
+import org.drools.rule.MultiRestrictionFieldConstraint;
import org.drools.rule.Not;
import org.drools.rule.Or;
+import org.drools.rule.OrCompositeRestriction;
import org.drools.rule.Package;
import org.drools.rule.PredicateConstraint;
import org.drools.rule.Query;
import org.drools.rule.ReturnValueConstraint;
+import org.drools.rule.ReturnValueRestriction;
import org.drools.rule.Rule;
import org.drools.rule.VariableConstraint;
+import org.drools.rule.VariableRestriction;
import org.drools.spi.Evaluator;
+import org.drools.spi.Extractor;
import org.drools.spi.FieldExtractor;
import org.drools.spi.FieldValue;
+import org.drools.spi.Restriction;
import org.drools.spi.TypeResolver;
/**
@@ -100,7 +110,7 @@
private List errors;
- private final TypeResolver typeResolver;
+ private final TypeResolver typeResolver;
private Map notDeclarations;
@@ -115,10 +125,10 @@
// @todo move to an interface so it can work as a decorator
private final JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
- private ClassFieldExtractorCache classFieldExtractorCache;
+ private ClassFieldExtractorCache classFieldExtractorCache;
-
- public RuleBuilder(TypeResolver resolver, ClassFieldExtractorCache cache) {
+ public RuleBuilder(TypeResolver resolver,
+ ClassFieldExtractorCache cache) {
this.classFieldExtractorCache = cache;
this.typeResolver = resolver;
this.errors = new ArrayList();
@@ -165,7 +175,6 @@
this.descrLookups = new HashMap();
this.columnCounter = new ColumnCounter();
-
this.ruleDescr = ruleDescr;
if ( ruleDescr instanceof QueryDescr ) {
@@ -228,7 +237,7 @@
build( this.rule,
(ConditionalElementDescr) object,
and,
- false, // do not decrement offset
+ false, // do not decrement offset
false ); // do not decrement first offset
this.rule.addPattern( and );
} else if ( object instanceof OrDescr ) {
@@ -237,7 +246,7 @@
build( this.rule,
(ConditionalElementDescr) object,
or,
- true, // when OR is used, offset MUST be decremented
+ true, // when OR is used, offset MUST be decremented
false ); // do not decrement first offset
this.rule.addPattern( or );
} else if ( object instanceof NotDescr ) {
@@ -248,7 +257,7 @@
build( this.rule,
(ConditionalElementDescr) object,
not,
- true, // when NOT is used, offset MUST be decremented
+ true, // when NOT is used, offset MUST be decremented
true ); // when NOT is used, offset MUST be decremented for first column
this.rule.addPattern( not );
@@ -266,7 +275,7 @@
build( this.rule,
(ConditionalElementDescr) object,
exists,
- true, // when EXIST is used, offset MUST be decremented
+ true, // when EXIST is used, offset MUST be decremented
true ); // when EXIST is used, offset MUST be decremented for first column
// remove declarations bound inside not node
for ( final Iterator notIt = this.notDeclarations.keySet().iterator(); notIt.hasNext(); ) {
@@ -312,7 +321,7 @@
build( rule,
(ConditionalElementDescr) object,
and,
- false, // do not decrement offset
+ false, // do not decrement offset
false ); // do not decrement first offset
ce.addChild( and );
} else if ( object instanceof OrDescr ) {
@@ -321,7 +330,7 @@
build( rule,
(ConditionalElementDescr) object,
or,
- true, // when OR is used, offset MUST be decremented
+ true, // when OR is used, offset MUST be decremented
false ); // do not decrement first offset
ce.addChild( or );
} else if ( object instanceof NotDescr ) {
@@ -330,7 +339,7 @@
build( rule,
(ConditionalElementDescr) object,
not,
- true, // when NOT is used, offset MUST be decremented
+ true, // when NOT is used, offset MUST be decremented
true ); // when NOT is used, offset MUST be decremented for first column
ce.addChild( not );
} else if ( object instanceof ExistsDescr ) {
@@ -339,7 +348,7 @@
build( rule,
(ConditionalElementDescr) object,
exists,
- true, // when EXIST is used, offset MUST be decremented
+ true, // when EXIST is used, offset MUST be decremented
true ); // when EXIST is used, offset MUST be decremented for first column
ce.addChild( exists );
} else if ( object instanceof EvalDescr ) {
@@ -412,39 +421,165 @@
} else if ( object instanceof FieldConstraintDescr ) {
build( column,
(FieldConstraintDescr) object );
- }
+ } else if ( object instanceof PredicateDescr ) {
+ build( column,
+ (PredicateDescr) object );
+ }
}
return column;
}
+
private void build(final Column column,
final FieldConstraintDescr fieldConstraintDescr) {
-
- for ( final Iterator it = fieldConstraintDescr.getRestrictions().iterator(); it.hasNext(); ) {
- final Object object = it.next();
- if ( object instanceof FieldBindingDescr ) {
- build( column,
- (FieldBindingDescr) object );
- } else if ( object instanceof LiteralRestrictionDescr ) {
- build( column,
- fieldConstraintDescr,
- (LiteralRestrictionDescr) object );
+
+ final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
+
+ final FieldExtractor extractor = getFieldExtractor( fieldConstraintDescr,
+ clazz,
+ fieldConstraintDescr.getFieldName() );
+ if ( extractor == null ) {
+ // @todo log error
+ return;
+ }
+
+ if ( fieldConstraintDescr.getRestrictions().size() == 1 ) {
+ final Object object = fieldConstraintDescr.getRestrictions().get( 0 );
+
+ Restriction restriction = buildRestriction( extractor, fieldConstraintDescr, ( RestrictionDescr ) object );
+ if ( restriction == null ) {
+ // @todo log errors
+ return;
+ }
+
+ if ( object instanceof LiteralRestrictionDescr ) {
+ column.addConstraint( new LiteralConstraint( extractor,
+ ( LiteralRestriction ) restriction ) );
} else if ( object instanceof VariableRestrictionDescr ) {
- build( column,
- fieldConstraintDescr,
- (VariableRestrictionDescr) object );
+ column.addConstraint( new VariableConstraint( extractor,
+ ( VariableRestriction ) restriction ) );
} else if ( object instanceof ReturnValueRestrictionDescr ) {
- build( column,
- fieldConstraintDescr,
- (ReturnValueRestrictionDescr) object );
- } //else if ( object instanceof PredicateDescr ) {
-// build( column,
-// (PredicateDescr) object );
-// }
- }
+ column.addConstraint( new ReturnValueConstraint( extractor,
+ ( ReturnValueRestriction ) restriction ) );
+ }
+
+ return;
+ }
+
+ List orList = new ArrayList();
+ List andList = null;
+ RestrictionDescr currentRestriction = null;
+ RestrictionDescr previousRestriction = null;
+
+ List currentList = null;
+ List previousList = null;
+
+
+ for ( final Iterator it = fieldConstraintDescr.getRestrictions().iterator(); it.hasNext(); ) {
+ Object object = it.next();
+
+ // Process an and/or connective
+ if ( object instanceof RestrictionConnectiveDescr ) {
+
+ // is the connective an 'and'?
+ if ( ((RestrictionConnectiveDescr) object).getConnective() == RestrictionConnectiveDescr.AND ) {
+ // if andList is null, then we know its the first
+ if ( andList == null ) {
+ andList = new ArrayList();
+ }
+ previousList = currentList;
+ currentList= andList;
+ } else {
+ previousList = currentList;
+ currentList = orList;
+ }
+ } else {
+ Restriction restriction = null;
+ if ( currentList != null ) {
+ // Are we are at the first operator? if so treat differently
+ if ( previousList == null ) {
+ restriction = buildRestriction( extractor, fieldConstraintDescr, previousRestriction );
+ if ( currentList == andList ) {
+ andList.add( restriction );
+ } else {
+ orList.add( restriction );
+ }
+ } else {
+ restriction = buildRestriction( extractor, fieldConstraintDescr, previousRestriction );
+
+ if ( previousList == andList && currentList == orList ) {
+ andList.add( restriction );
+ if ( andList.size() == 1 ) {
+ // Can't have an 'and' connective with one child, so add directly to the or list
+ orList.add( andList.get( 0 ) );
+ } else {
+ Restriction restrictions = new AndCompositeRestriction( (Restriction[]) andList.toArray( new Restriction[andList.size()] ) );
+ orList.add( restrictions );
+ }
+ andList = null;
+ } else if ( previousList == andList && currentList == andList ) {
+ andList.add( restriction );
+ } else if ( previousList == orList && currentList == andList ) {
+ andList.add( restriction );
+ } else if ( previousList == orList && currentList == orList ) {
+ orList.add( restriction );
+ }
+ }
+ }
+ }
+ previousRestriction = currentRestriction;
+ currentRestriction = ( RestrictionDescr ) object;
+ }
+
+ Restriction restriction = buildRestriction( extractor, fieldConstraintDescr, currentRestriction );
+ currentList.add( restriction );
+
+ Restriction restrictions = null;
+ if ( currentList == andList && !orList.isEmpty() ) {
+ // Check if it finished with an and, and process it
+ if ( andList != null ) {
+ if ( andList.size() == 1 ) {
+ // Can't have an 'and' connective with one child, so add directly to the or list
+ orList.add( andList.get( 0 ) );
+ } else {
+ orList.add( new AndCompositeRestriction( (Restriction[]) andList.toArray( new Restriction[andList.size()] ) ) );
+ }
+ andList = null;
+ }
+ }
+
+ if ( !orList.isEmpty() ) {
+ restrictions = new OrCompositeRestriction(( Restriction[] ) orList.toArray( new Restriction[ orList.size() ] ));
+ } else if ( andList != null && !andList.isEmpty() ) {
+ restrictions = new AndCompositeRestriction( (Restriction[]) andList.toArray( new Restriction[andList.size()] ) );
+ } else {
+ // @todo throw error
+ }
+
+ column.addConstraint( new MultiRestrictionFieldConstraint( extractor, restrictions ) );
}
-
+
+ private Restriction buildRestriction(FieldExtractor extractor, FieldConstraintDescr fieldConstraintDescr, RestrictionDescr restrictionDescr) {
+ Restriction restriction = null;
+ if ( restrictionDescr instanceof LiteralRestrictionDescr ) {
+ restriction = build( extractor,
+ fieldConstraintDescr,
+ (LiteralRestrictionDescr) restrictionDescr);
+ } else if ( restrictionDescr instanceof VariableRestrictionDescr ) {
+ restriction = build( extractor,
+ fieldConstraintDescr,
+ (VariableRestrictionDescr) restrictionDescr );
+ } else if ( restrictionDescr instanceof ReturnValueRestrictionDescr ) {
+ restriction = build( extractor,
+ fieldConstraintDescr,
+ (ReturnValueRestrictionDescr) restrictionDescr );
+
+ }
+
+ return restriction;
+ }
+
private void build(final Column column,
final FieldBindingDescr fieldBindingDescr) {
Declaration declaration = (Declaration) this.declarations.get( fieldBindingDescr.getIdentifier() );
@@ -478,26 +613,17 @@
}
}
- private void build(final Column column,
- final FieldConstraintDescr fieldConstraintDescr,
- final VariableRestrictionDescr variableRestrictionDescr) {
+ private VariableRestriction build(final FieldExtractor extractor,
+ final FieldConstraintDescr fieldConstraintDescr,
+ final VariableRestrictionDescr variableRestrictionDescr) {
if ( variableRestrictionDescr.getIdentifier() == null || variableRestrictionDescr.getIdentifier().equals( "" ) ) {
this.errors.add( new RuleError( this.rule,
variableRestrictionDescr,
null,
"Identifier not defined for binding field '" + fieldConstraintDescr.getFieldName() + "'" ) );
- return;
+ return null;
}
- final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
-
- final FieldExtractor extractor = getFieldExtractor( variableRestrictionDescr,
- clazz,
- fieldConstraintDescr.getFieldName() );
- if ( extractor == null ) {
- return;
- }
-
final Declaration declaration = (Declaration) this.declarations.get( variableRestrictionDescr.getIdentifier() );
if ( declaration == null ) {
@@ -505,39 +631,28 @@
variableRestrictionDescr,
null,
"Unable to return Declaration for identifier '" + variableRestrictionDescr.getIdentifier() + "'" ) );
- return;
+ return null;
}
final Evaluator evaluator = getEvaluator( variableRestrictionDescr,
extractor.getObjectType().getValueType(),
variableRestrictionDescr.getEvaluator() );
if ( evaluator == null ) {
- return;
+ return null;
}
- column.addConstraint( new VariableConstraint( extractor,
- declaration,
- evaluator ) );
+ return new VariableRestriction( declaration,
+ evaluator );
}
- private void build(final Column column,
- final FieldConstraintDescr fieldConstraintDescr,
- final LiteralRestrictionDescr literalRestrictionDescr) {
-
- final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
-
- final FieldExtractor extractor = getFieldExtractor( literalRestrictionDescr,
- clazz,
- fieldConstraintDescr.getFieldName() );
- if ( extractor == null ) {
- return;
- }
-
+ private LiteralRestriction build(final FieldExtractor extractor,
+ final FieldConstraintDescr fieldConstraintDescr,
+ final LiteralRestrictionDescr literalRestrictionDescr) {
FieldValue field = null;
if ( literalRestrictionDescr.isStaticFieldValue() ) {
final int lastDot = literalRestrictionDescr.getText().lastIndexOf( '.' );
final String className = literalRestrictionDescr.getText().substring( 0,
- lastDot );
+ lastDot );
final String fieldName = literalRestrictionDescr.getText().substring( lastDot + 1 );
try {
final Class staticClass = this.typeResolver.resolveType( className );
@@ -570,17 +685,16 @@
extractor.getObjectType().getValueType(),
literalRestrictionDescr.getEvaluator() );
if ( evaluator == null ) {
- return;
+ return null;
}
- column.addConstraint( new LiteralConstraint( extractor,
- evaluator,
- field ) );
+ return new LiteralRestriction( field,
+ evaluator );
}
- private void build(final Column column,
- final FieldConstraintDescr fieldConstraintDescr,
- final ReturnValueRestrictionDescr returnValueRestrictionDescr) {
+ private ReturnValueRestriction build(final FieldExtractor extractor,
+ final FieldConstraintDescr fieldConstraintDescr,
+ final ReturnValueRestrictionDescr returnValueRestrictionDescr) {
final String classMethodName = "returnValue" + this.counter++;
returnValueRestrictionDescr.setClassMethodName( classMethodName );
@@ -592,25 +706,15 @@
declarations[i] = (Declaration) this.declarations.get( (String) usedIdentifiers[0].get( i ) );
}
- final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
- final FieldExtractor extractor = getFieldExtractor( returnValueRestrictionDescr,
- clazz,
- fieldConstraintDescr.getFieldName() );
- if ( extractor == null ) {
- return;
- }
-
final Evaluator evaluator = getEvaluator( returnValueRestrictionDescr,
extractor.getObjectType().getValueType(),
returnValueRestrictionDescr.getEvaluator() );
if ( evaluator == null ) {
- return;
+ return null;
}
- final ReturnValueConstraint returnValueConstraint = new ReturnValueConstraint( extractor,
- declarations,
- evaluator );
- column.addConstraint( returnValueConstraint );
+ final ReturnValueRestriction returnValueRestriction = new ReturnValueRestriction( declarations,
+ evaluator );
StringTemplate st = RuleBuilder.ruleGroup.getInstanceOf( "returnValueMethod" );
@@ -651,9 +755,11 @@
this.invokers.put( invokerClassName,
st.toString() );
this.invokerLookups.put( invokerClassName,
- returnValueConstraint );
+ returnValueRestriction );
this.descrLookups.put( invokerClassName,
returnValueRestrictionDescr );
+
+ return returnValueRestriction;
}
private void build(final Column column,
@@ -948,7 +1054,8 @@
final String fieldName) {
FieldExtractor extractor = null;
try {
- extractor = classFieldExtractorCache.getExtractor( clazz, fieldName );
+ extractor = classFieldExtractorCache.getExtractor( clazz,
+ fieldName );
} catch ( final RuntimeDroolsException e ) {
this.errors.add( new RuleError( this.rule,
descr,
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Person.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Person.java 2006-07-26 16:52:42 UTC (rev 5295)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Person.java 2006-07-26 18:59:20 UTC (rev 5296)
@@ -14,9 +14,10 @@
private static final long serialVersionUID = 2640286967578736742L;
private final String name;
private final String likes;
- private final int age;
+ private int age;
private BigDecimal bigDecimal;
private BigInteger bigInteger;
+ private String hair;
private char sex;
@@ -79,6 +80,10 @@
public int getAge() {
return this.age;
}
+
+ public void setAge(int age) {
+ this.age = age;
+ }
/* (non-Javadoc)
* @see org.drools.PersonInterface#isAlive()
@@ -107,7 +112,17 @@
public void setSex(final char sex) {
this.sex = sex;
}
+
+
+ public String getHair() {
+ return this.hair;
+ }
+
+ public void setHair(String hair) {
+ this.hair = hair;
+ }
+
public String toString() {
return "[Person name='" + this.name + "']";
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java 2006-07-26 16:52:42 UTC (rev 5295)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java 2006-07-26 18:59:20 UTC (rev 5296)
@@ -95,12 +95,12 @@
final List list = new ArrayList();
workingMemory.setGlobal( "list",
list );
-
+
workingMemory.setGlobal( "string",
- "stilton" );
+ "stilton" );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
@@ -108,17 +108,16 @@
assertEquals( new Integer( 5 ),
list.get( 0 ) );
}
-
-
- public void testFieldBiningsAndEvalSharing() throws Exception {
+
+ public void testFieldBiningsAndEvalSharing() throws Exception {
String drl = "test_FieldBindingsAndEvalSharing.drl";
evalSharingTest( drl );
}
-
- public void testFieldBiningsAndPredicateSharing() throws Exception {
+
+ public void testFieldBiningsAndPredicateSharing() throws Exception {
String drl = "test_FieldBindingsAndPredicateSharing.drl";
evalSharingTest( drl );
- }
+ }
private void evalSharingTest(String drl) throws DroolsParserException,
IOException,
@@ -126,26 +125,25 @@
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( drl ) ) );
final Package pkg = builder.getPackage();
-
+
final RuleBase ruleBase = getRuleBase();
- ruleBase.addPackage( pkg );
+ ruleBase.addPackage( pkg );
WorkingMemory wm = ruleBase.newWorkingMemory();
-
+
List list = new ArrayList();
- wm.setGlobal( "list", list );
-
+ wm.setGlobal( "list",
+ list );
+
TestParam tp1 = new TestParam();
tp1.setValue2( "boo" );
wm.assertObject( tp1 );
-
-
+
wm.fireAllRules();
-
- assertEquals(1, list.size());
+
+ assertEquals( 1,
+ list.size() );
}
-
-
public void testEmptyColumn() throws Exception {
//pre build the package
final PackageBuilder builder = new PackageBuilder();
@@ -163,7 +161,7 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
@@ -173,8 +171,7 @@
}
public void testExplicitAnd() throws Exception {
- final Reader reader = new InputStreamReader(
- getClass().getResourceAsStream( "test_ExplicitAnd.drl" ) );
+ final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_ExplicitAnd.drl" ) );
final DrlParser parser = new DrlParser();
final PackageDescr packageDescr = parser.parse( reader );
@@ -190,13 +187,15 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
List list = new ArrayList();
- workingMemory.setGlobal( "list", list );
- workingMemory.assertObject( new Message("hola") );
-
+ workingMemory.setGlobal( "list",
+ list );
+ workingMemory.assertObject( new Message( "hola" ) );
+
workingMemory.fireAllRules();
- assertEquals(0, list.size());
+ assertEquals( 0,
+ list.size() );
}
-
+
public void testHelloWorld() throws Exception {
//read in the source
@@ -269,7 +268,7 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
@@ -292,8 +291,8 @@
list );
final PersonInterface bill = new Person( "bill",
- null,
- 12 );
+ null,
+ 12 );
bill.setAlive( true );
workingMemory.assertObject( bill );
workingMemory.fireAllRules();
@@ -353,8 +352,8 @@
list );
final PersonInterface bill = new Person( "bill",
- null,
- 12 );
+ null,
+ 12 );
bill.setBigDecimal( new BigDecimal( "42" ) );
workingMemory.assertObject( new BigDecimal( "43" ) );
workingMemory.assertObject( bill );
@@ -395,7 +394,7 @@
list );
final Cheese cheddar = new Cheese( "cheddar",
- 5 );
+ 5 );
final FactHandle h = workingMemory.assertObject( cheddar );
workingMemory.fireAllRules();
@@ -433,7 +432,7 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese stilton = new Cheese( "stinky",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
final QueryResults results = workingMemory.getQueryResults( "simple query" );
assertEquals( 1,
@@ -457,18 +456,18 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
assertEquals( stilton,
list.get( 0 ) );
}
-
+
public void testJaninoEval() throws Exception {
PackageBuilderConfiguration config = new PackageBuilderConfiguration();
config.setCompiler( PackageBuilderConfiguration.JANINO );
- final PackageBuilder builder = new PackageBuilder(config);
+ final PackageBuilder builder = new PackageBuilder( config );
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "eval_rule_test.drl" ) ) );
final Package pkg = builder.getPackage();
@@ -484,13 +483,13 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
assertEquals( stilton,
list.get( 0 ) );
- }
+ }
public void testEvalMore() throws Exception {
final PackageBuilder builder = new PackageBuilder();
@@ -529,12 +528,12 @@
list );
final PersonInterface peter = new Person( "peter",
- null,
- 12 );
+ null,
+ 12 );
workingMemory.assertObject( peter );
final PersonInterface jane = new Person( "jane",
- null,
- 10 );
+ null,
+ 10 );
workingMemory.assertObject( jane );
workingMemory.fireAllRules();
@@ -562,12 +561,12 @@
list );
final PersonInterface peter = new Person( "peter",
- null,
- 12 );
+ null,
+ 12 );
workingMemory.assertObject( peter );
final PersonInterface jane = new Person( "jane",
- null,
- 10 );
+ null,
+ 10 );
workingMemory.assertObject( jane );
workingMemory.fireAllRules();
@@ -592,10 +591,10 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
final FactHandle stiltonHandle = workingMemory.assertObject( stilton );
final Cheese cheddar = new Cheese( "cheddar",
- 7 );
+ 7 );
final FactHandle cheddarHandle = workingMemory.assertObject( cheddar );
workingMemory.fireAllRules();
@@ -632,15 +631,15 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
final FactHandle stiltonHandle = workingMemory.assertObject( stilton );
final Cheese cheddar = new Cheese( "cheddar",
- 7 );
+ 7 );
final FactHandle cheddarHandle = workingMemory.assertObject( cheddar );
final PersonInterface paul = new Person( "paul",
- "stilton",
- 12 );
+ "stilton",
+ 12 );
workingMemory.assertObject( paul );
workingMemory.fireAllRules();
@@ -665,11 +664,11 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final PersonInterface p1 = new Person( "michael",
- "food",
- 40 );
+ "food",
+ 40 );
final PersonInterface p2 = new Person( null,
- "drink",
- 30 );
+ "drink",
+ 30 );
workingMemory.assertObject( p1 );
workingMemory.assertObject( p2 );
@@ -690,8 +689,8 @@
foo );
final PersonInterface p1 = new Person( null,
- "food",
- 40 );
+ "food",
+ 40 );
workingMemory.assertObject( p1 );
@@ -715,7 +714,7 @@
list );
final Cheese cheddar = new Cheese( "cheddar",
- 7 );
+ 7 );
final FactHandle cheddarHandle = workingMemory.assertObject( cheddar );
workingMemory.fireAllRules();
@@ -723,7 +722,7 @@
list.size() );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
final FactHandle stiltonHandle = workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
@@ -731,7 +730,7 @@
list.size() );
final Cheese brie = new Cheese( "brie",
- 5 );
+ 5 );
final FactHandle brieHandle = workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -752,11 +751,15 @@
workingMemory.setGlobal( "list",
list );
- final Cheese cheddar = new Cheese( "cheddar", 7 );
- final Cheese provolone = new Cheese( "provolone", 5);
- final Person edson = new Person("Edson", "cheddar");
- final Person bob = new Person("Bob", "muzzarela");
-
+ final Cheese cheddar = new Cheese( "cheddar",
+ 7 );
+ final Cheese provolone = new Cheese( "provolone",
+ 5 );
+ final Person edson = new Person( "Edson",
+ "cheddar" );
+ final Person bob = new Person( "Bob",
+ "muzzarela" );
+
workingMemory.assertObject( cheddar );
workingMemory.fireAllRules();
assertEquals( 0,
@@ -858,7 +861,7 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
@@ -881,7 +884,7 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
workingMemory.fireAllRules();
@@ -894,7 +897,7 @@
pkg.removeFunction( "addFive" );
final Cheese cheddar = new Cheese( "cheddar",
- 5 );
+ 5 );
workingMemory.assertObject( cheddar );
try {
@@ -910,7 +913,7 @@
ruleBase.addPackage( builder.getPackage() );
final Cheese brie = new Cheese( "brie",
- 5 );
+ 5 );
workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -924,7 +927,7 @@
ruleBase.addPackage( builder.getPackage() );
final Cheese feta = new Cheese( "feta",
- 5 );
+ 5 );
workingMemory.assertObject( feta );
workingMemory.fireAllRules();
@@ -949,7 +952,7 @@
list );
final PersonInterface person = new Person( "michael",
- "cheese" );
+ "cheese" );
person.setStatus( "start" );
workingMemory.assertObject( person );
workingMemory.fireAllRules();
@@ -1066,10 +1069,10 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese mussarela = new Cheese( "Mussarela",
- 35 );
+ 35 );
workingMemory.assertObject( mussarela );
final Cheese provolone = new Cheese( "Provolone",
- 20 );
+ 20 );
workingMemory.assertObject( provolone );
workingMemory.fireAllRules();
@@ -1096,7 +1099,7 @@
list );
final PersonInterface person = new Person( "Edson",
- "cheese" );
+ "cheese" );
workingMemory.assertObject( person );
workingMemory.fireAllRules();
@@ -1127,7 +1130,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -1148,7 +1151,7 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
workingMemory.assertObject( brie );
try {
@@ -1170,7 +1173,7 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
workingMemory.assertObject( brie );
try {
@@ -1192,7 +1195,7 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
try {
workingMemory.assertObject( brie );
@@ -1214,7 +1217,7 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
try {
workingMemory.assertObject( brie );
@@ -1236,7 +1239,7 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
try {
workingMemory.assertObject( brie );
@@ -1248,6 +1251,80 @@
}
}
+ public void testMultiRestrictionFieldConstraint() throws Exception {
+ final PackageBuilder builder = new PackageBuilder();
+ builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_MultiRestrictionFieldConstraint.drl" ) ) );
+ final Package pkg = builder.getPackage();
+
+ final RuleBase ruleBase = getRuleBase();
+ ruleBase.addPackage( pkg );
+ final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
+
+ final List list1 = new ArrayList();
+ workingMemory.setGlobal( "list1",
+ list1 );
+ final List list2 = new ArrayList();
+ workingMemory.setGlobal( "list2",
+ list2 );
+ final List list3 = new ArrayList();
+ workingMemory.setGlobal( "list3",
+ list3 );
+ final List list4 = new ArrayList();
+ workingMemory.setGlobal( "list4",
+ list4 );
+
+ Person youngChili1 = new Person( "young chili1" );
+ youngChili1.setAge( 12 );
+ youngChili1.setHair( "blue" );
+ Person youngChili2 = new Person( "young chili2" );
+ youngChili2.setAge( 25 );
+ youngChili2.setHair( "purple" );
+
+ Person chili1 = new Person( "chili1" );
+ chili1.setAge( 35 );
+ chili1.setHair( "red" );
+
+ Person chili2 = new Person( "chili2" );
+ chili2.setAge( 38 );
+ chili2.setHair( "indigigo" );
+
+ Person oldChili1 = new Person( "old chili2" );
+ oldChili1.setAge( 45 );
+ oldChili1.setHair( "green" );
+
+ Person oldChili2 = new Person( "old chili2" );
+ oldChili2.setAge( 48 );
+ oldChili2.setHair( "blue" );
+
+ workingMemory.assertObject( youngChili1 );
+ workingMemory.assertObject( youngChili2 );
+ workingMemory.assertObject( chili1 );
+ workingMemory.assertObject( chili2 );
+ workingMemory.assertObject( oldChili1 );
+ workingMemory.assertObject( oldChili2 );
+
+ workingMemory.fireAllRules();
+
+ assertEquals( 1,
+ list1.size() );
+ assertTrue( list1.contains( chili1 ) );
+
+ assertEquals( 2,
+ list2.size() );
+ assertTrue( list2.contains( chili1 ) );
+ assertTrue( list2.contains( chili2 ) );
+
+ assertEquals( 2,
+ list3.size() );
+ assertTrue( list3.contains( youngChili1 ) );
+ assertTrue( list3.contains( youngChili2 ) );
+
+ assertEquals( 2,
+ list4.size() );
+ assertTrue( list4.contains( youngChili1 ) );
+ assertTrue( list4.contains( chili1 ) );
+ }
+
public void testAgendaGroups() throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_AgendaGroups.drl" ) ) );
@@ -1262,7 +1339,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -1293,13 +1370,13 @@
assertEquals( "group2",
list.get( 7 ) );
}
-
+
public void testDumpers() throws Exception {
final DrlParser parser = new DrlParser();
- final PackageDescr pkg = parser.parse( new InputStreamReader( getClass().getResourceAsStream( "test_Dumpers.drl" ) ) );
-
+ final PackageDescr pkg = parser.parse( new InputStreamReader( getClass().getResourceAsStream( "test_Dumpers.drl" ) ) );
+
PackageBuilder builder = new PackageBuilder();
- builder.addPackage(pkg );
+ builder.addPackage( pkg );
RuleBase ruleBase = getRuleBase();
ruleBase.addPackage( builder.getPackage() );
@@ -1310,7 +1387,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -1323,12 +1400,12 @@
list.get( 1 ) );
assertEquals( "1 1",
list.get( 2 ) );
-
+
final DrlDumper drlDumper = new DrlDumper();
final String drlResult = drlDumper.dump( pkg );
builder = new PackageBuilder();
builder.addPackageFromDrl( new StringReader( drlResult ) );
-
+
ruleBase = getRuleBase();
ruleBase.addPackage( builder.getPackage() );
workingMemory = ruleBase.newWorkingMemory();
@@ -1348,13 +1425,13 @@
assertEquals( "MAIN",
list.get( 1 ) );
assertEquals( "1 1",
- list.get( 2 ) );
-
+ list.get( 2 ) );
+
final XmlDumper xmlDumper = new XmlDumper();
final String xmlResult = xmlDumper.dump( pkg );
builder = new PackageBuilder();
builder.addPackageFromXml( new StringReader( xmlResult ) );
-
+
ruleBase = getRuleBase();
ruleBase.addPackage( builder.getPackage() );
workingMemory = ruleBase.newWorkingMemory();
@@ -1374,11 +1451,10 @@
assertEquals( "MAIN",
list.get( 1 ) );
assertEquals( "1 1",
- list.get( 2 ) );
+ list.get( 2 ) );
}
-
- public void testXorGroups() throws Exception {
+ public void testActivationGroups() throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_ActivationGroups.drl" ) ) );
final Package pkg = builder.getPackage();
@@ -1392,7 +1468,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
workingMemory.assertObject( brie );
final ActivationGroup activationGroup0 = workingMemory.getAgenda().getActivationGroup( "activation-group-0" );
@@ -1445,7 +1521,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
final FactHandle brieHandle = workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -1477,7 +1553,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
final FactHandle brieHandle = workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -1508,7 +1584,7 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
final FactHandle brieHandle = workingMemory.assertObject( brie );
workingMemory.fireAllRules();
@@ -1542,7 +1618,7 @@
list );
final Cheese stilton = new Cheese( "stilton",
- 12 );
+ 12 );
final FactHandle brieHandle = workingMemory.assertObject( stilton );
final Cheesery cheesery = new Cheesery();
@@ -1605,16 +1681,16 @@
// type node in memory yet, but the rule engine is supposed
// to handle that correctly
final PersonInterface bob = new Person( "bob",
- "stilton" );
+ "stilton" );
bob.setStatus( "Not evaluated" );
workingMemory.assertObject( bob );
final Cheese stilton = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton );
final Cheese cheddar = new Cheese( "cheddar",
- 5 );
+ 5 );
workingMemory.assertObject( cheddar );
workingMemory.fireAllRules();
@@ -1707,24 +1783,24 @@
list );
final PersonInterface bob = new Person( "bob",
- "stilton" );
+ "stilton" );
bob.setStatus( "Not evaluated" );
workingMemory.assertObject( bob );
final Cheese stilton1 = new Cheese( "stilton",
- 5 );
+ 5 );
workingMemory.assertObject( stilton1 );
final Cheese stilton2 = new Cheese( "stilton",
- 3 );
+ 3 );
workingMemory.assertObject( stilton2 );
final Cheese stilton3 = new Cheese( "stilton",
- 1 );
+ 1 );
workingMemory.assertObject( stilton3 );
final Cheese cheddar = new Cheese( "cheddar",
- 5 );
+ 5 );
workingMemory.assertObject( cheddar );
//
// workingMemory.get
@@ -1744,7 +1820,7 @@
"like cheese" );
final Cheese muzzarela = new Cheese( "muzzarela",
- 5 );
+ 5 );
workingMemory.assertObject( muzzarela );
assertEquals( 4,
@@ -1764,7 +1840,7 @@
"like cheese" );
final Cheese muzzarela = new Cheese( "muzzarela",
- 5 );
+ 5 );
workingMemory.assertObject( muzzarela );
assertEquals( 4,
@@ -1854,10 +1930,10 @@
// Adding person with null name and likes attributes
final PersonInterface bob = new Person( null,
- null );
+ null );
bob.setStatus( "P1" );
final PersonInterface pete = new Person( null,
- null );
+ null );
bob.setStatus( "P2" );
workingMemory.assertObject( bob );
workingMemory.assertObject( pete );
@@ -1873,8 +1949,8 @@
}
- public void testSerializable() throws Exception {
-
+ public void testSerializable() throws Exception {
+
final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_Serializable.drl" ) );
final PackageBuilder builder = new PackageBuilder();
@@ -1901,33 +1977,42 @@
assertEquals( "match Person 3",
rules[2].getName() );
assertEquals( "match Integer",
- rules[3].getName() );
-
+ rules[3].getName() );
+
WorkingMemory workingMemory = ruleBase.newWorkingMemory();
-
- workingMemory.setGlobal( "list", new ArrayList() );
-
- workingMemory.assertObject( new Integer(5) );
-
+
+ workingMemory.setGlobal( "list",
+ new ArrayList() );
+
+ workingMemory.assertObject( new Integer( 5 ) );
+
final byte[] wm = serializeOut( workingMemory );
workingMemory = ruleBase.newWorkingMemory( new ByteArrayInputStream( wm ) );
-
- assertEquals( 1, workingMemory.getObjects().size() );
- assertEquals( new Integer( 5 ) , workingMemory.getObjects().get(0) );
-
- assertEquals( 1, workingMemory.getAgenda().agendaSize() );
-
+
+ assertEquals( 1,
+ workingMemory.getObjects().size() );
+ assertEquals( new Integer( 5 ),
+ workingMemory.getObjects().get( 0 ) );
+
+ assertEquals( 1,
+ workingMemory.getAgenda().agendaSize() );
+
workingMemory.fireAllRules();
-
- List list = ( List ) workingMemory.getGlobal( "list" );
-
- assertEquals( 1, list.size() );
- assertEquals( new Integer( 4 ), list.get( 0 ) );
-
- assertEquals( 2, workingMemory.getObjects().size() );
- assertEquals( new Integer( 5 ) , workingMemory.getObjects().get(0) );
- assertEquals( "help" , workingMemory.getObjects().get(1) );
+
+ List list = (List) workingMemory.getGlobal( "list" );
+
+ assertEquals( 1,
+ list.size() );
+ assertEquals( new Integer( 4 ),
+ list.get( 0 ) );
+
+ assertEquals( 2,
+ workingMemory.getObjects().size() );
+ assertEquals( new Integer( 5 ),
+ workingMemory.getObjects().get( 0 ) );
+ assertEquals( "help",
+ workingMemory.getObjects().get( 1 ) );
}
public void testLogicalAssertions() throws Exception {
@@ -1944,11 +2029,11 @@
list );
final Cheese brie = new Cheese( "brie",
- 12 );
+ 12 );
final FactHandle brieHandle = workingMemory.assertObject( brie );
final Cheese provolone = new Cheese( "provolone",
- 12 );
+ 12 );
final FactHandle provoloneHandle = workingMemory.assertObject( provolone );
workingMemory.fireAllRules();
@@ -1980,9 +2065,9 @@
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final Cheese cheese1 = new Cheese( "c",
- 1 );
+ 1 );
final Cheese cheese2 = new Cheese( cheese1.getType(),
- 1 );
+ 1 );
List list;
final FactHandle h1 = workingMemory.assertObject( cheese1 );
@@ -2135,7 +2220,7 @@
events );
final Sensor sensor = new Sensor( 80,
- 80 );
+ 80 );
final FactHandle handle = workingMemory.assertObject( sensor );
// everything should be normal
@@ -2273,11 +2358,11 @@
// workingMemory.addEventListener(new org.drools.event.DebugWorkingMemoryEventListener());
final Cheese c1 = new Cheese( "a",
- 1 );
+ 1 );
final Cheese c2 = new Cheese( "b",
- 2 );
+ 2 );
final Cheese c3 = new Cheese( "c",
- 3 );
+ 3 );
List list;
workingMemory.assertObject( c1 );
@@ -2448,12 +2533,11 @@
assertTrue( list.contains( "fired1" ) );
assertTrue( list.contains( "fired3" ) );
}
-
+
public void testOrWithBinding() throws Exception {
final PackageBuilder builder = new PackageBuilder();
- builder.addPackageFromDrl( new InputStreamReader(
- getClass().getResourceAsStream( "test_OrWithBindings.drl" ) ) );
+ builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_OrWithBindings.drl" ) ) );
final Package pkg = builder.getPackage();
final RuleBase ruleBase = getRuleBase();
@@ -2464,17 +2548,18 @@
workingMemory.setGlobal( "results",
list );
- workingMemory.assertObject( new Integer(42) );
+ workingMemory.assertObject( new Integer( 42 ) );
workingMemory.assertObject( "hola" );
workingMemory.fireAllRules();
assertTrue( list.contains( "hola" ) );
- assertEquals(1, list.size() );
-
+ assertEquals( 1,
+ list.size() );
+
}
protected Object serializeIn(final byte[] bytes) throws IOException,
- ClassNotFoundException {
+ ClassNotFoundException {
final ObjectInput in = new ObjectInputStream( new ByteArrayInputStream( bytes ) );
final Object obj = in.readObject();
in.close();
@@ -2503,84 +2588,92 @@
final RuleBase ruleBase = getRuleBase();
ruleBase.addPackage( pkg1 );
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
-
- final List orderedFacts = new ArrayList();
+
+ final List orderedFacts = new ArrayList();
final List errors = new ArrayList();
-
- workingMemory.setGlobal( "orderedNumbers", orderedFacts );
- workingMemory.setGlobal( "errors", errors );
-
+
+ workingMemory.setGlobal( "orderedNumbers",
+ orderedFacts );
+ workingMemory.setGlobal( "errors",
+ errors );
+
final int MAX = 5;
- for (int i=1 ; i<=MAX; i++) {
- IndexedNumber n = new IndexedNumber(i, MAX - i + 1);
- workingMemory.assertObject(n);
+ for ( int i = 1; i <= MAX; i++ ) {
+ IndexedNumber n = new IndexedNumber( i,
+ MAX - i + 1 );
+ workingMemory.assertObject( n );
}
workingMemory.fireAllRules();
-
- Assert.assertTrue( "Processing generated errors: "+errors.toString(),
- errors.isEmpty());
-
- for(int i=1 ; i<=MAX; i++) {
- IndexedNumber n = (IndexedNumber) orderedFacts.get( i-1 );
- Assert.assertEquals( "Fact is out of order", i, n.getIndex() );
+
+ Assert.assertTrue( "Processing generated errors: " + errors.toString(),
+ errors.isEmpty() );
+
+ for ( int i = 1; i <= MAX; i++ ) {
+ IndexedNumber n = (IndexedNumber) orderedFacts.get( i - 1 );
+ Assert.assertEquals( "Fact is out of order",
+ i,
+ n.getIndex() );
}
}
-
+
public void testRemovePackage() {
try {
final PackageBuilder builder = new PackageBuilder();
- builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_RemovePackage.drl" ) ) );
-
+ builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_RemovePackage.drl" ) ) );
+
final RuleBase ruleBase = getRuleBase();
- String packageName=builder.getPackage().getName();
+ String packageName = builder.getPackage().getName();
ruleBase.addPackage( builder.getPackage() );
-
+
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
-
- workingMemory.assertObject( new Precondition("genericcode","genericvalue"));
+
+ workingMemory.assertObject( new Precondition( "genericcode",
+ "genericvalue" ) );
workingMemory.fireAllRules();
-
- RuleBase ruleBaseWM=workingMemory.getRuleBase();
- ruleBaseWM.removePackage(packageName);
+
+ RuleBase ruleBaseWM = workingMemory.getRuleBase();
+ ruleBaseWM.removePackage( packageName );
final PackageBuilder builder1 = new PackageBuilder();
builder1.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_RemovePackage.drl" ) ) );
- ruleBaseWM.addPackage(builder1.getPackage());
+ ruleBaseWM.addPackage( builder1.getPackage() );
workingMemory.fireAllRules();
} catch ( Exception e ) {
- Assert.fail( "Removing packages should not throw any exception: "+e.getMessage() );
+ Assert.fail( "Removing packages should not throw any exception: " + e.getMessage() );
}
}
-
+
public void testQuery2() {
try {
final PackageBuilder builder = new PackageBuilder();
- builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_Query.drl" ) ) );
-
+ builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_Query.drl" ) ) );
+
final RuleBase ruleBase = getRuleBase();
ruleBase.addPackage( builder.getPackage() );
-
+
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
workingMemory.fireAllRules();
QueryResults results = workingMemory.getQueryResults( "assertedobjquery" );
- if (results==null || !results.iterator().hasNext()) {
- Assert.fail("The stated query should return a result");
+ if ( results == null || !results.iterator().hasNext() ) {
+ Assert.fail( "The stated query should return a result" );
} else {
int counter = 0;
for ( Iterator it = results.iterator(); it.hasNext(); ) {
- QueryResult result = ( QueryResult )it.next();;
- AssertedObject assertedObject=(AssertedObject)result.get( "assertedobj" );
- Assert.assertNotNull( "Query result is not expected to be null", assertedObject );
+ QueryResult result = (QueryResult) it.next();;
+ AssertedObject assertedObject = (AssertedObject) result.get( "assertedobj" );
+ Assert.assertNotNull( "Query result is not expected to be null",
+ assertedObject );
counter++;
}
- Assert.assertEquals( "Expecting a single result from the query", 1, counter );
- }
-
+ Assert.assertEquals( "Expecting a single result from the query",
+ 1,
+ counter );
+ }
+
} catch ( Exception e ) {
- Assert.fail( "Retrieving query results should not throw any exception: "+e.getMessage() );
+ Assert.fail( "Retrieving query results should not throw any exception: " + e.getMessage() );
}
}
-
}
Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultiRestrictionFieldConstraint.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultiRestrictionFieldConstraint.drl 2006-07-26 16:52:42 UTC (rev 5295)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_MultiRestrictionFieldConstraint.drl 2006-07-26 18:59:20 UTC (rev 5296)
@@ -0,0 +1,38 @@
+package org.drools
+
+import org.drools.Person;
+
+global java.util.List list1;
+global java.util.List list2;
+global java.util.List list3;
+global java.util.List list4;
+
+
+rule "& operator with number range"
+ when
+ $person : Person( age > 30 & < 40, hair == "red" )
+ then
+ list1.add( $person );
+end
+
+rule "& operator with ! and strings"
+ when
+ $person : Person( hair != "blue" & != "purple", age > 30 & < 40 )
+ then
+ list2.add( $person );
+end
+
+rule "| operator with == and strings"
+ when
+ $person : Person( hair == "blue" | == "purple", age < 30 )
+ then
+ list3.add( $person );
+end
+
+
+rule "| and & operator with == and != and strings"
+ when
+ $person : Person( age > 30 & < 40 | > 10 & < 20, hair == "red" | == "blue" )
+ then
+ list4.add( $person );
+end
More information about the jboss-svn-commits
mailing list