[jboss-svn-commits] JBL Code SVN: r6130 - in labs/jbossrules/trunk: drools-compiler/src/main/java/org/drools/semantics/java drools-compiler/src/main/resources/org/drools/semantics/java drools-compiler/src/test/java/org/drools drools-compiler/src/test/java/org/drools/integrationtests drools-compiler/src/test/java/org/drools/semantics/java drools-compiler/src/test/resources/org/drools/integrationtests drools-core/src/main/java/org/drools/common drools-core/src/main/java/org/drools/reteoo drools-core/src/main/java/org/drools/rule drools-core/src/main/java/org/drools/spi drools-core/src/test/java/org/drools/reteoo
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Sep 8 15:53:45 EDT 2006
Author: tirelli
Date: 2006-09-08 15:53:13 -0400 (Fri, 08 Sep 2006)
New Revision: 6130
Added:
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AccumulateModify.drl
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaInvokers.stg
labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaRule.stg
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Cheesery.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
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/LeapsTest.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/AccumulateTemplateTest.java
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/HelloWorld.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/errors_in_rule.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/evalmodify.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/not_with_bindings_rule_test.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/null_behaviour.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Accumulate.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AgendaGroups.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_FunctionInConsequence.drl
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrWithBindings.drl
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Accumulate.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/Accumulator.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockAccumulator.java
Log:
JBRULES-217: add Accumulate Conditional Element
* JBRULES-336:
- Fixing minor bugs
- Updating test cases
* JBRULES-337:
- Fixing minor bugs
- Adding integration tests
* Removing System.out from test cases
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-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -132,7 +132,7 @@
private final TypeResolver typeResolver;
- private Map notDeclarations;
+ private Map innerDeclarations;
private static final StringTemplateGroup ruleGroup = new StringTemplateGroup( new InputStreamReader( RuleBuilder.class.getResourceAsStream( "javaRule.stg" ) ),
AngleBracketTemplateLexer.class );
@@ -256,7 +256,6 @@
if ( object instanceof ConditionalElementDescr ) {
if ( object.getClass() == AndDescr.class ) {
final And and = new And();
- this.columnCounter.setParent( and );
build( this.rule,
(ConditionalElementDescr) object,
and,
@@ -265,7 +264,6 @@
this.rule.addPattern( and );
} else if ( object.getClass() == OrDescr.class ) {
final Or or = new Or();
- this.columnCounter.setParent( or );
build( this.rule,
(ConditionalElementDescr) object,
or,
@@ -274,9 +272,8 @@
this.rule.addPattern( or );
} else if ( object.getClass() == NotDescr.class ) {
// We cannot have declarations created inside a not visible outside it, so track no declarations so they can be removed
- this.notDeclarations = new HashMap();
+ this.innerDeclarations = new HashMap();
final Not not = new Not();
- this.columnCounter.setParent( not );
build( this.rule,
(ConditionalElementDescr) object,
not,
@@ -285,27 +282,26 @@
this.rule.addPattern( not );
// remove declarations bound inside not node
- for ( final Iterator notIt = this.notDeclarations.keySet().iterator(); notIt.hasNext(); ) {
+ for ( final Iterator notIt = this.innerDeclarations.keySet().iterator(); notIt.hasNext(); ) {
this.declarations.remove( notIt.next() );
}
- this.notDeclarations = null;
+ this.innerDeclarations = null;
} else if ( object.getClass() == ExistsDescr.class ) {
// We cannot have declarations created inside a not visible outside it, so track no declarations so they can be removed
- this.notDeclarations = new HashMap();
+ this.innerDeclarations = new HashMap();
final Exists exists = new Exists();
- this.columnCounter.setParent( exists );
build( this.rule,
(ConditionalElementDescr) object,
exists,
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(); ) {
+ for ( final Iterator notIt = this.innerDeclarations.keySet().iterator(); notIt.hasNext(); ) {
this.declarations.remove( notIt.next() );
}
- this.notDeclarations = null;
+ this.innerDeclarations = null;
this.rule.addPattern( exists );
} else if ( object.getClass() == EvalDescr.class ) {
final EvalCondition eval = build( (EvalDescr) object );
@@ -346,7 +342,6 @@
if ( object instanceof ConditionalElementDescr ) {
if ( object.getClass() == AndDescr.class ) {
final And and = new And();
- this.columnCounter.setParent( and );
build( rule,
(ConditionalElementDescr) object,
and,
@@ -355,7 +350,6 @@
ce.addChild( and );
} else if ( object.getClass() == OrDescr.class ) {
final Or or = new Or();
- this.columnCounter.setParent( or );
build( rule,
(ConditionalElementDescr) object,
or,
@@ -364,7 +358,6 @@
ce.addChild( or );
} else if ( object.getClass() == NotDescr.class ) {
final Not not = new Not();
- this.columnCounter.setParent( not );
build( rule,
(ConditionalElementDescr) object,
not,
@@ -373,7 +366,6 @@
ce.addChild( not );
} else if ( object.getClass() == ExistsDescr.class ) {
final Exists exists = new Exists();
- this.columnCounter.setParent( exists );
build( rule,
(ConditionalElementDescr) object,
exists,
@@ -443,9 +435,9 @@
this.declarations.put( column.getDeclaration().getIdentifier(),
column.getDeclaration() );
- if ( this.notDeclarations != null ) {
- this.notDeclarations.put( column.getDeclaration().getIdentifier(),
- column.getDeclaration() );
+ if ( this.innerDeclarations != null ) {
+ this.innerDeclarations.put( column.getDeclaration().getIdentifier(),
+ column.getDeclaration() );
}
} else {
column = new Column( this.columnCounter.getNext(),
@@ -653,9 +645,9 @@
this.declarations.put( declaration.getIdentifier(),
declaration );
- if ( this.notDeclarations != null ) {
- this.notDeclarations.put( declaration.getIdentifier(),
- declaration );
+ if ( this.innerDeclarations != null ) {
+ this.innerDeclarations.put( declaration.getIdentifier(),
+ declaration );
}
}
@@ -828,9 +820,9 @@
this.declarations.put( declaration.getIdentifier(),
declaration );
- if ( this.notDeclarations != null ) {
- this.notDeclarations.put( declaration.getIdentifier(),
- declaration );
+ if ( this.innerDeclarations != null ) {
+ this.innerDeclarations.put( declaration.getIdentifier(),
+ declaration );
}
final List[] usedIdentifiers = getUsedIdentifiers( predicateDescr,
@@ -1040,7 +1032,17 @@
}
private Accumulate build(final AccumulateDescr accumDescr) {
+ this.innerDeclarations = new HashMap();
+
Column sourceColumn = build( accumDescr.getSourceColumn() );
+ // remove declarations bound inside source column
+ this.declarations.keySet().removeAll( this.innerDeclarations.keySet() );
+ Map sourceDeclarations = this.innerDeclarations;
+ this.innerDeclarations = null;
+
+ // removing declaration as it is not a real declaration
+ this.declarations.remove( sourceColumn.getDeclaration().getIdentifier() );
+
// decrementing offset as accumulate fills only one column
this.columnOffset--;
Column resultColumn = build( accumDescr.getResultColumn() );
@@ -1058,11 +1060,17 @@
final List requiredDeclarations = new ArrayList( usedIdentifiers1[0] );
requiredDeclarations.addAll( usedIdentifiers2[0] );
requiredDeclarations.addAll( usedIdentifiers3[0] );
+
final List requiredGlobals = new ArrayList( usedIdentifiers1[1] );
requiredGlobals.addAll( usedIdentifiers2[1] );
requiredGlobals.addAll( usedIdentifiers3[1] );
- final Declaration[] declarations = (Declaration[]) requiredDeclarations.toArray( new Declaration[requiredDeclarations.size()] );
+ final Declaration[] declarations = new Declaration[requiredDeclarations.size()];
+ for ( int i = 0, size = requiredDeclarations.size(); i < size; i++ ) {
+ declarations[i] = (Declaration) this.declarations.get( (String) requiredDeclarations.get( i ) );
+ }
+ final Declaration[] sourceDeclArr = (Declaration[]) sourceDeclarations.values().toArray( new Declaration[sourceDeclarations.size()] );
+
final String[] globals = (String[]) requiredGlobals.toArray( new String[requiredGlobals.size()] );
StringTemplate st = RuleBuilder.ruleGroup.getInstanceOf( "accumulateMethod" );
@@ -1072,6 +1080,8 @@
globals,
null );
+ st.setAttribute( "innerDeclarations",
+ sourceDeclArr );
st.setAttribute( "methodName",
className );
@@ -1085,14 +1095,6 @@
st.setAttribute( "resultCode",
resultCode );
- String columnType = null;
- // TODO: Need to change this...
- if ( sourceColumn.getObjectType() instanceof ClassObjectType ) {
- columnType = ((ClassObjectType) sourceColumn.getObjectType()).getClassType().getName();
- } else {
- columnType = sourceColumn.getObjectType().getValueType().getClassType().getName();
- }
-
String resultType = null;
// TODO: Need to change this...
if ( resultColumn.getObjectType() instanceof ClassObjectType ) {
@@ -1100,11 +1102,7 @@
} else {
resultType = resultColumn.getObjectType().getValueType().getClassType().getName();
}
-
- st.setAttribute( "columnType",
- columnType );
- st.setAttribute( "columnDeclaration",
- sourceColumn.getDeclaration() );
+
st.setAttribute( "resultType",
resultType );
@@ -1131,7 +1129,8 @@
Accumulate accumulate = new Accumulate( sourceColumn,
resultColumn,
- declarations);
+ declarations,
+ sourceDeclArr);
final String invokerClassName = this.pkg.getName() + "." + this.ruleDescr.getClassName() + ucFirst( className ) + "Invoker";
this.invokers.put( invokerClassName,
st.toString() );
@@ -1350,12 +1349,6 @@
// we start with -1 so that we can ++this.value - otherwise the first element has a lower value than the second in an 'or'
private int value = -1;
- private GroupElement ge;
-
- public void setParent(final GroupElement ge) {
- this.ge = ge;
- }
-
public int getNext() {
return ++this.value;
}
Modified: labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaInvokers.stg
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaInvokers.stg 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaInvokers.stg 2006-09-08 19:53:13 UTC (rev 6130)
@@ -157,12 +157,14 @@
public Object accumulate(org.drools.reteoo.ReteTuple tuple,
java.util.List matchingObjects,
org.drools.rule.Declaration[] declarations,
+ org.drools.rule.Declaration[] innerDeclarations,
org.drools.WorkingMemory workingMemory) throws Exception {
<declarationTypes, declarations:{ type, declr| <type> <declr.identifier> = ( <type> ) declarations[<i0>].getValue( ( (org.drools.common.InternalFactHandle) tuple.get( declarations[<i0>] ) ).getObject() );<\n>}>
<globalTypes, globals:{ type, identifier | <type> <identifier> = ( <type> ) workingMemory.getGlobal( "<identifier>" );<\n>}>
return <ruleClassName>.<methodName>(
+ innerDeclarations,
matchingObjects<if(declarations)>,<endif>
<declarations:{ declr | <declr.identifier>}; separator=",\n"><if(globals)>,<endif>
<globals:{ identifier | <identifier>}; separator=",\n"> );
Modified: labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaRule.stg
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaRule.stg 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/main/resources/org/drools/semantics/java/javaRule.stg 2006-09-08 19:53:13 UTC (rev 6130)
@@ -18,11 +18,12 @@
}
>>
-accumulateMethod(declarations, declarationTypes, globals, globalTypes, methodName, columnType, columnDeclaration, resultType, initCode, actionCode, resultCode) ::= <<
-public static <resultType> <methodName>(java.util.List matchingObjects<if(declarations)>, <endif><declarationTypes, declarations:{ type, declr | <type> <declr.identifier>}; separator=","><if(globals)>,<endif> <globalTypes, globals:{ type, identifier | <type> <identifier>}; separator=","> ) throws Exception {
+accumulateMethod(declarations, declarationTypes, innerDeclarations, globals, globalTypes, methodName, columnType, columnDeclaration, resultType, initCode, actionCode, resultCode) ::= <<
+public static <resultType> <methodName>(org.drools.rule.Declaration[] innerDeclarations, java.util.List matchingObjects<if(declarations)>, <endif><declarationTypes, declarations:{ type, declr | <type> <declr.identifier>}; separator=","><if(globals)>,<endif> <globalTypes, globals:{ type, identifier | <type> <identifier>}; separator=","> ) throws Exception {
<initCode>
for( java.util.Iterator it = matchingObjects.iterator(); it.hasNext(); ) {
- <columnType> <columnDeclaration.identifier> = (<columnType>) it.next();
+ Object __itNextElem__ = it.next();
+ <innerDeclarations:{ declr | <declr.extractor.extractToClass.name> <declr.identifier> = (<declr.extractor.extractToClass.name>) innerDeclarations[<i0>].getValue( __itNextElem__ );}; separator="\n">
<actionCode>
}
return ( <resultCode> );
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Cheesery.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Cheesery.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Cheesery.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -33,6 +33,7 @@
private final List cheeses = new ArrayList();
private int status;
+ private int totalAmount;
public List getCheeses() {
return this.cheeses;
@@ -40,6 +41,7 @@
public void addCheese(Cheese cheese) {
this.cheeses.add( cheese );
+ this.totalAmount += cheese.getPrice();
}
public void setStatus(final int status) {
@@ -49,4 +51,12 @@
public int getStatus() {
return this.status;
}
+
+ public int getTotalAmount() {
+ return totalAmount;
+ }
+
+ public void setTotalAmount(int totalAmount) {
+ this.totalAmount = totalAmount;
+ }
}
\ No newline at end of file
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-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Person.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -12,18 +12,18 @@
*
*/
private static final long serialVersionUID = 2640286967578736742L;
- private final String name;
- private final String likes;
- private int age;
- private BigDecimal bigDecimal;
- private BigInteger bigInteger;
- private String hair;
+ private final String name;
+ private String likes;
+ private int age;
+ private BigDecimal bigDecimal;
+ private BigInteger bigInteger;
+ private String hair;
- private char sex;
+ private char sex;
- private boolean alive;
+ private boolean alive;
- private String status;
+ private String status;
public Person(final String name) {
this( name,
@@ -80,7 +80,7 @@
public int getAge() {
return this.age;
}
-
+
public void setAge(int age) {
this.age = age;
}
@@ -112,8 +112,6 @@
public void setSex(final char sex) {
this.sex = sex;
}
-
-
public String getHair() {
return this.hair;
@@ -200,4 +198,8 @@
public void setBigInteger(final BigInteger bigInteger) {
this.bigInteger = bigInteger;
}
+
+ public void setLikes(String likes) {
+ this.likes = likes;
+ }
}
\ No newline at end of file
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-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -28,7 +28,6 @@
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -62,12 +61,6 @@
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.compiler.ParserError;
import org.drools.compiler.RuleError;
-import org.drools.event.ActivationCancelledEvent;
-import org.drools.event.ActivationCreatedEvent;
-import org.drools.event.AfterActivationFiredEvent;
-import org.drools.event.AgendaEventListener;
-import org.drools.event.BeforeActivationFiredEvent;
-import org.drools.event.DefaultAgendaEventListener;
import org.drools.facttemplates.Fact;
import org.drools.facttemplates.FactTemplate;
import org.drools.integrationtests.helloworld.Message;
@@ -3106,10 +3099,85 @@
wm.assertObject( new Cheese("stilton", 10) );
wm.assertObject( new Cheese("brie", 5) );
+ wm.assertObject( new Cheese("provolone", 150) );
+ wm.assertObject( new Person("Bob", "stilton") );
+ wm.assertObject( new Person("Mark", "provolone") );
wm.fireAllRules();
- Assert.assertTrue( results.contains( new Integer(15) ) );
+ Assert.assertEquals(new Integer(165), results.get(0));
+ Assert.assertEquals(new Integer(10), results.get(1));
+ Assert.assertEquals(new Integer(150), results.get(2));
+ Assert.assertEquals(new Integer(10), results.get(3));
}
+
+ public void testAccumulateModify() throws Exception {
+ //read in the source
+ final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_AccumulateModify.drl" ) );
+ final DrlParser parser = new DrlParser();
+ final PackageDescr packageDescr = parser.parse( reader );
+ //pre build the package
+ final PackageBuilder builder = new PackageBuilder();
+ builder.addPackage( packageDescr );
+ final Package pkg = builder.getPackage();
+
+ //add the package to a rulebase
+ final RuleBase ruleBase = getRuleBase();
+ ruleBase.addPackage( pkg );
+ //load up the rulebase
+
+ final WorkingMemory wm = ruleBase.newWorkingMemory();
+ List results = new ArrayList();
+
+ wm.setGlobal( "results", results );
+
+ Cheese[] cheese = new Cheese[] {
+ new Cheese("stilton", 10),
+ new Cheese("stilton", 2),
+ new Cheese("stilton", 5),
+ new Cheese("brie", 15),
+ new Cheese("brie", 16),
+ new Cheese("provolone", 8)
+ };
+ Person bob = new Person("Bob", "stilton");
+
+ FactHandle[] cheeseHandles = new FactHandle[cheese.length];
+ for( int i = 0; i < cheese.length; i++ ) {
+ cheeseHandles[i] = wm.assertObject( cheese[i] );
+ }
+ FactHandle bobHandle = wm.assertObject( bob );
+
+ // ---------------- 1st scenario
+ wm.fireAllRules();
+ // no fire, as per rule constraints
+ Assert.assertEquals( 0, results.size() );
+
+ // ---------------- 2nd scenario
+ int index = 1;
+ cheese[index].setPrice( 9 );
+ wm.modifyObject( cheeseHandles[index], cheese[index] );
+ wm.fireAllRules();
+
+ // 1 fire
+ Assert.assertEquals( 1, results.size());
+ Assert.assertEquals( 24, ((Cheesery)results.get( results.size()-1 )).getTotalAmount() );
+
+ // ---------------- 3rd scenario
+ bob.setLikes( "brie" );
+ wm.modifyObject( bobHandle, bob );
+ wm.fireAllRules();
+
+ // 2 fires
+ Assert.assertEquals( 2, results.size());
+ Assert.assertEquals( 31, ((Cheesery)results.get( results.size()-1 )).getTotalAmount() );
+
+ // ---------------- 4th scenario
+ wm.retractObject( cheeseHandles[3] );
+ wm.fireAllRules();
+
+ // should not have fired as per constraint
+ Assert.assertEquals( 2, results.size());
+
+ }
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/LeapsTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/LeapsTest.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/LeapsTest.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -431,4 +431,8 @@
// FIXME
}
+ public void testAccumulateModify() throws Exception {
+ // FIXME
+ }
+
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/AccumulateTemplateTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/AccumulateTemplateTest.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/AccumulateTemplateTest.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -9,7 +9,11 @@
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
+import org.drools.Cheese;
+import org.drools.base.ClassFieldExtractor;
+import org.drools.base.ClassObjectType;
import org.drools.rule.Declaration;
+import org.drools.spi.ColumnExtractor;
public class AccumulateTemplateTest extends TestCase {
@@ -29,9 +33,16 @@
final String[] declarationTypes = new String[]{"String", "Integer"};
final Declaration[] declarations = new Declaration[]{new Declaration( "name",
null,
- null ), new Declaration( "age",
- null,
- null )};
+ null ),
+ new Declaration( "age",
+ null,
+ null )};
+ final Declaration[] inner = new Declaration[]{new Declaration( "cheese",
+ new ColumnExtractor(new ClassObjectType(Cheese.class)),
+ null ),
+ new Declaration( "price",
+ new ClassFieldExtractor(Cheese.class, "price"),
+ null )};
final String[] globals = new String[]{"aGlobal", "anotherGlobal"};
final List globalTypes = Arrays.asList( new String[]{"String", "String"} );
@@ -39,6 +50,8 @@
declarations );
accMethod.setAttribute( "declarationTypes",
declarationTypes );
+ accMethod.setAttribute( "innerDeclarations",
+ inner );
accMethod.setAttribute( "globals",
globals );
accMethod.setAttribute( "globalTypes",
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/HelloWorld.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/HelloWorld.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/HelloWorld.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -19,7 +19,7 @@
int a = 0;
}
try {
- System.out.println("hello world with collections " + $m.getMessage());
+ //System.out.println("hello world with collections " + $m.getMessage());
} catch ( Exception e ) {
} finally {
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/errors_in_rule.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/errors_in_rule.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/errors_in_rule.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -20,5 +20,5 @@
when
String()
then
- System.out.println("hello");
+ //System.out.println("hello");
end
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/evalmodify.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/evalmodify.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/evalmodify.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -9,7 +9,7 @@
cell2 : Cell(value2:value < value1)
eval (true)
then
- System.out.println("setting value to " + (value2.intValue() + 1));
+ //System.out.println("setting value to " + (value2.intValue() + 1));
cell2.setValue(value2.intValue() + 1);
modify(cell2);
end
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/not_with_bindings_rule_test.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/not_with_bindings_rule_test.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/not_with_bindings_rule_test.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -13,7 +13,7 @@
$person : Person( $likes:likes )
not Cheese( type == $likes )
then
- System.out.println( "help" );
+ //System.out.println( "help" );
list.add( $person );
end
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/null_behaviour.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/null_behaviour.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/null_behaviour.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -7,5 +7,5 @@
p1 : Person($name : name)
p2 : Person(name == $name)
then
- System.out.println("Persons detected: ");
+ //System.out.println("Persons detected: ");
end
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Accumulate.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Accumulate.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Accumulate.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -1,16 +1,55 @@
package org.drools.test;
import org.drools.Cheese;
+import org.drools.Person;
+import org.drools.Cheesery;
global java.util.List results;
-rule "AccumulateTest"
+rule "AccumulateTest" salience 100
when
$totalAmount : Integer() from accumulate( $cheese : Cheese( ),
init( int total = 0; ),
action( total += $cheese.getPrice(); ),
result( new Integer( total ) ) );
then
- //System.out.println("Total amount = "+$totalAmount );
+ //System.out.println("Total amount = US$ "+$totalAmount );
results.add($totalAmount);
-end
\ No newline at end of file
+end
+
+rule "Accumulate with Bindings" salience 90
+ when
+ $person : Person( name == "Bob", $likes : likes )
+ $totalAmount : Integer() from accumulate( $cheese : Cheese( type == $likes ),
+ init( int total = 0; ),
+ action( total += $cheese.getPrice(); ),
+ result( new Integer( total ) ) );
+ then
+ //System.out.println($person.getName() +" will spend US$ "+ $totalAmount + " buying cheese");
+ results.add($totalAmount);
+end
+
+rule "Constraints everywhere" salience 80
+ when
+ $person : Person( $likes : likes )
+ $cheesery : Cheesery( totalAmount > 100 )
+ from accumulate( $cheese : Cheese( type == $likes ),
+ init( Cheesery cheesery = new Cheesery(); ),
+ action( cheesery.addCheese( $cheese ); ),
+ result( cheesery ) );
+ then
+ //System.out.println($person.getName() +" is spending a lot buying cheese ( US$ "+$cheesery.getTotalAmount()+" )!");
+ results.add(new Integer($cheesery.getTotalAmount()));
+end
+
+rule "Source column binds" salience 70
+ when
+ $person : Person( name == "Bob", $likes : likes )
+ $totalAmount : Integer() from accumulate( $cheese : Cheese( type == $likes, $price: price ),
+ init( int total = 0; ),
+ action( total += $price.intValue(); ),
+ result( new Integer( total ) ) );
+ then
+ //System.out.println($person.getName() +" will spend US$ "+ $totalAmount + " buying cheese");
+ results.add($totalAmount);
+end
Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AccumulateModify.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AccumulateModify.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AccumulateModify.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -0,0 +1,20 @@
+package org.drools.test;
+
+import org.drools.Cheese;
+import org.drools.Cheesery;
+import org.drools.Person;
+
+global java.util.List results;
+
+rule "Constraints everywhere" salience 80
+ when
+ $person : Person( $likes : likes )
+ $cheesery : Cheesery( totalAmount > 20 )
+ from accumulate( $cheese : Cheese( type == $likes ),
+ init( Cheesery cheesery = new Cheesery(); ),
+ action( cheesery.addCheese( $cheese ); ),
+ result( cheesery ) );
+ then
+ //System.out.println($person.getName() +" is spending a lot buying cheese ( US$ "+$cheesery.getTotalAmount()+" )!");
+ results.add( $cheesery );
+end
Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AccumulateModify.drl
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:eol-style
+ native
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AgendaGroups.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AgendaGroups.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_AgendaGroups.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -41,7 +41,6 @@
when
Cheese( )
then
- System.out.println("help");
list.add( "group2" );
end
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_FunctionInConsequence.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_FunctionInConsequence.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_FunctionInConsequence.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -16,5 +16,5 @@
then
addFive( list );
// Calling methods does not mess with function calls
- System.out.println(list);
+ // System.out.println(list);
end
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrWithBindings.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrWithBindings.drl 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrWithBindings.drl 2006-09-08 19:53:13 UTC (rev 6130)
@@ -8,6 +8,6 @@
Long())
s: String()
then
- System.out.println("result is " + s);
+ //System.out.println("result is " + s);
results.add(s);
end
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -436,7 +436,7 @@
activation.getRule().getConsequence().evaluate( knowledgeHelper,
this.workingMemory );
} catch ( final Exception e ) {
- e.printStackTrace();
+ //e.printStackTrace();
throw new ConsequenceException( e,
activation.getRule() );
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -198,16 +198,19 @@
}
}
- // Need to store the accumulate result object for later disposal
- InternalFactHandle[] handles = ((ReteTuple)((LinkedListObjectWrapper)leftTuple.getLinkedTuples().getFirst()).getObject()).getFactHandles();
- InternalFactHandle lastHandle = handles[handles.length-1];
-
- propagateRetractTuple( leftTuple,
- context,
- workingMemory );
-
- // Destroying the acumulate result object
- workingMemory.getFactHandleFactory().destroyFactHandle( lastHandle );
+ // if tuple was propagated
+ if((leftTuple.getLinkedTuples() != null) && (leftTuple.getLinkedTuples().size() > 0)) {
+ // Need to store the accumulate result object for later disposal
+ InternalFactHandle[] handles = ((ReteTuple)((LinkedListObjectWrapper)leftTuple.getLinkedTuples().getFirst()).getObject()).getFactHandles();
+ InternalFactHandle lastHandle = handles[handles.length-1];
+
+ propagateRetractTuple( leftTuple,
+ context,
+ workingMemory );
+
+ // Destroying the acumulate result object
+ workingMemory.getFactHandleFactory().destroyFactHandle( lastHandle );
+ }
}
/**
@@ -257,9 +260,19 @@
public void modifyObject(DefaultFactHandle handle,
PropagationContext context,
ReteooWorkingMemory workingMemory) {
- this.retractObject( handle,
- context,
- workingMemory );
+ final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
+
+ // Remove the FactHandle from memory
+ final ObjectMatches objectMatches = memory.remove( workingMemory,
+ handle );
+
+ // remove references from tuple to the handle
+ for ( TupleMatch tupleMatch = objectMatches.getFirstTupleMatch(); tupleMatch != null; tupleMatch = (TupleMatch) tupleMatch.getNext() ) {
+ final ReteTuple leftTuple = tupleMatch.getTuple();
+ leftTuple.removeMatch( handle );
+ }
+
+ // reassert object modifying appropriate tuples
this.assertObject( handle,
context,
workingMemory );
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -210,7 +210,7 @@
public void addTupleMatch(final DefaultFactHandle handle,
final TupleMatch node) {
if ( this.matches == Collections.EMPTY_MAP ) {
- this.matches = new HashMap(1);
+ this.matches = new HashMap(2);
}
this.matches.put( handle,
node );
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Accumulate.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Accumulate.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Accumulate.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -34,6 +34,7 @@
private Column sourceColumn;
private Column resultColumn;
private Declaration[] requiredDeclarations;
+ private Declaration[] innerDeclarations;
public Accumulate(final Column sourceColumn,
final Column resultColumn) {
@@ -41,27 +42,32 @@
this( sourceColumn,
resultColumn,
new Declaration[0],
+ new Declaration[0],
null);
}
public Accumulate(final Column sourceColumn,
final Column resultColumn,
- final Declaration[] requiredDeclarations) {
+ final Declaration[] requiredDeclarations,
+ final Declaration[] innerDeclarations) {
this( sourceColumn,
resultColumn,
requiredDeclarations,
+ innerDeclarations,
null );
}
public Accumulate(final Column sourceColumn,
final Column resultColumn,
final Declaration[] requiredDeclarations,
+ final Declaration[] innerDeclarations,
final Accumulator accumulator) {
this.sourceColumn = sourceColumn;
this.resultColumn = resultColumn;
this.requiredDeclarations = requiredDeclarations;
+ this.innerDeclarations = innerDeclarations;
this.accumulator = accumulator;
}
@@ -80,6 +86,7 @@
return this.accumulator.accumulate( leftTuple,
matchingObjects,
this.requiredDeclarations,
+ this.innerDeclarations,
workingMemory );
} catch ( final Exception e ) {
throw new RuntimeDroolsException( e );
@@ -90,6 +97,7 @@
return new Accumulate( this.sourceColumn,
this.resultColumn,
this.requiredDeclarations,
+ this.innerDeclarations,
this.accumulator );
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/Accumulator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/Accumulator.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/Accumulator.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -37,6 +37,7 @@
public Object accumulate(ReteTuple leftTuple,
List matchingObjects,
Declaration[] declarations,
+ Declaration[] innerDeclarations,
WorkingMemory workingMemory) throws Exception;
}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AccumulateNodeTest.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -19,7 +19,6 @@
import junit.framework.Assert;
-import org.drools.Cheese;
import org.drools.DroolsTestCase;
import org.drools.RuleBaseFactory;
import org.drools.base.ClassObjectType;
@@ -80,6 +79,7 @@
this.accumulate = new Accumulate( sourceColumn,
resultColumn,
new Declaration[0],
+ new Declaration[0],
this.accumulator );
this.node = new AccumulateNode( 15,
@@ -410,9 +410,9 @@
this.workingMemory );
assertEquals( 2,
this.memory.getRightObjectMemory().size() );
- assertEquals( 3,
+ assertEquals( 2,
this.sink.getAsserted().size() );
- assertEquals( 2,
+ assertEquals( 1,
this.sink.getRetracted().size() );
Assert.assertEquals( "Wrong number of elements in matching objects list ",
2,
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockAccumulator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockAccumulator.java 2006-09-08 16:50:52 UTC (rev 6129)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockAccumulator.java 2006-09-08 19:53:13 UTC (rev 6130)
@@ -44,6 +44,7 @@
public Object accumulate(ReteTuple leftTuple,
List matchingObjects,
Declaration[] declarations,
+ Declaration[] inner,
WorkingMemory workingMemory) {
this.leftTuple = leftTuple;
this.matchingObjects = matchingObjects;
More information about the jboss-svn-commits
mailing list