[jboss-svn-commits] JBL Code SVN: r8226 - in labs/jbossrules/trunk: drools-compiler/src/test/resources/org/drools/integrationtests drools-core/src/main/java/org/drools/rule drools-core/src/test/java/org/drools/rule drools-core/src/test/resources
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 11 09:39:51 EST 2006
Author: tirelli
Date: 2006-12-11 09:39:38 -0500 (Mon, 11 Dec 2006)
New Revision: 8226
Modified:
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrWithBindings.drl
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/GroupElement.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/LogicTransformer.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java
labs/jbossrules/trunk/drools-core/src/test/resources/correct_processTree1.dat
labs/jbossrules/trunk/drools-core/src/test/resources/correct_transform1.dat
Log:
JBRULES-218: modifying logical transformations to ensure element order is kept over transformations
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-12-11 13:49:10 UTC (rev 8225)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_OrWithBindings.drl 2006-12-11 14:39:38 UTC (rev 8226)
@@ -11,6 +11,5 @@
(State() || Cheese())
p: Person()
then
- System.out.println("result is " + p);
results.add(p);
end
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/GroupElement.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/GroupElement.java 2006-12-11 13:49:10 UTC (rev 8225)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/GroupElement.java 2006-12-11 14:39:38 UTC (rev 8226)
@@ -70,6 +70,7 @@
* LogicTransformer does further, more complicated, transformations
*/
public void pack() {
+ // we must clone, since we want to iterate only over the original list
Object[] clone = this.children.toArray();
for ( int i = 0; i < clone.length; i++ ) {
// if child is also a group element, there may be
@@ -110,21 +111,32 @@
// then merge this childs with parent childs
if ( parent.getType() == this.getType() ) {
+ // we must keep the order so, save index
+ int index = parent.getChildren().indexOf( this );
parent.getChildren().remove( this );
// for each child, pack it and add it to parent
for ( Iterator childIt = this.children.iterator(); childIt.hasNext(); ) {
Object child = childIt.next();
- parent.addChild( child );
+ // we must keep the order, so add in the same place were parent was before
+ parent.getChildren().add( index++, child );
if ( child instanceof GroupElement ) {
+ int previousSize = parent.getChildren().size();
((GroupElement) child).pack( parent );
+ // in case the child also added elements to the parent,
+ // we need to compensate
+ index += ( parent.getChildren().size() - previousSize );
}
}
// if current node has a single child, then move it to parent and pack it
} else if ( ( ! this.isExists() ) && ( this.children.size() == 1 ) ) {
+ // we must keep the order so, save index
+ int index = parent.getChildren().indexOf( this );
+ parent.getChildren().remove( this );
+
Object child = this.children.get( 0 );
- parent.addChild( child );
- parent.getChildren().remove( this );
+ parent.getChildren().add( index, child );
+
if ( child instanceof GroupElement ) {
((GroupElement) child).pack( parent );
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/LogicTransformer.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/LogicTransformer.java 2006-12-11 13:49:10 UTC (rev 8225)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/LogicTransformer.java 2006-12-11 14:39:38 UTC (rev 8226)
@@ -72,7 +72,7 @@
public GroupElement[] transform(final GroupElement and) throws InvalidPatternException {
GroupElement cloned = (GroupElement) and.clone();
-
+
processTree( cloned );
cloned.pack();
@@ -187,18 +187,21 @@
public void transform(final GroupElement parent) throws InvalidPatternException {
List orsList = new ArrayList();
- List others = new ArrayList();
+ // must keep order, so, using array
+ Object[] others = new Object[parent.getChildren().size()];
// first we split children as OR or not OR
int permutations = 1;
+ int index = 0;
for ( Iterator it = parent.getChildren().iterator(); it.hasNext(); ) {
Object child = it.next();
if ( (child instanceof GroupElement) && ((GroupElement) child).isOr() ) {
permutations *= ((GroupElement) child).getChildren().size();
orsList.add( child );
} else {
- others.add( child );
+ others[index] = child ;
}
+ index++;
}
// transform parent into an OR
@@ -213,19 +216,24 @@
for ( int i = 1; i <= permutations; i++ ) {
GroupElement and = GroupElementFactory.newAndInstance();
- // elements originally outside OR will be in every permutation, so add them
- and.getChildren().addAll( others );
-
// create the actual permutations
int mod = 1;
for ( int j = ors.length - 1; j >= 0; j-- ) {
- and.addChild( ors[j].getChildren().get( indexes[j] ) );
+ // we must insert at the beggining to keep the order
+ and.getChildren().add( 0, ors[j].getChildren().get( indexes[j] ) );
if ( (i % mod) == 0 ) {
indexes[j] = (indexes[j] + 1) % ors[j].getChildren().size();
}
mod *= ors[j].getChildren().size();
}
+ // elements originally outside OR will be in every permutation, so add them
+ // in their original position
+ for( int j = 0; j < others.length; j++ ) {
+ if( others[j] != null ) {
+ and.getChildren().add( j, others[j] );
+ }
+ }
parent.addChild( and );
}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java 2006-12-11 13:49:10 UTC (rev 8225)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java 2006-12-11 14:39:38 UTC (rev 8226)
@@ -54,12 +54,13 @@
final String b = "b";
final String c = "c";
- final GroupElement parent = GroupElementFactory.newAndInstance();
- parent.addChild( c );
final GroupElement or = GroupElementFactory.newOrInstance();
or.addChild( a );
or.addChild( b );
+
+ final GroupElement parent = GroupElementFactory.newAndInstance();
parent.addChild( or );
+ parent.addChild( c );
LogicTransformer.getInstance().applyOrTransformation( parent );
@@ -71,16 +72,19 @@
parent.getChildren().get( 1 ).getClass() );
final GroupElement and1 = (GroupElement) parent.getChildren().get( 0 );
- assertContains( c,
- and1.getChildren() );
- assertContains( a,
- and1.getChildren() );
+ assertTrue( and1.isAnd() );
+ // transformation MUST keep the order
+ assertEquals( a,
+ and1.getChildren().get( 0 ) );
+ assertEquals( c,
+ and1.getChildren().get( 1 ) );
+
final GroupElement and2 = (GroupElement) parent.getChildren().get( 1 );
- assertContains( c,
- and2.getChildren() );
- assertContains( b,
- and2.getChildren() );
+ assertEquals( b,
+ and2.getChildren().get( 0 ) );
+ assertEquals( c,
+ and2.getChildren().get( 1 ) );
}
@@ -156,60 +160,57 @@
parent.getChildren().get( 3 ).getClass() );
GroupElement and1 = (GroupElement) parent.getChildren().get( 0 );
- assertEquals( GroupElement.AND,
- and1.getType() );
+ assertTrue( and1.isAnd() );
assertLength( 4,
and1.getChildren() );
- assertContains( a,
- and1.getChildren() );
- assertContains( c,
- and1.getChildren() );
- assertContains( d,
- and1.getChildren() );
- assertContains( not,
- and1.getChildren() );
+ assertEquals( a,
+ and1.getChildren().get( 0 ) );
+ assertEquals( c,
+ and1.getChildren().get( 1 ) );
+ assertEquals( d,
+ and1.getChildren().get( 2 ) );
+ assertEquals( not,
+ and1.getChildren().get( 3 ) );
and1 = (GroupElement) parent.getChildren().get( 1 );
- assertEquals( GroupElement.AND,
- and1.getType() );
+ assertTrue( and1.isAnd() );
assertLength( 4,
and1.getChildren() );
- assertContains( a,
- and1.getChildren() );
- assertContains( c,
- and1.getChildren() );
- assertContains( e,
- and1.getChildren() );
- assertContains( not,
- and1.getChildren() );
+ assertEquals( a,
+ and1.getChildren().get( 0 ) );
+ assertEquals( c,
+ and1.getChildren().get( 1 ) );
+ assertEquals( e,
+ and1.getChildren().get( 2 ) );
+ assertEquals( not,
+ and1.getChildren().get( 3 ) );
and1 = (GroupElement) parent.getChildren().get( 2 );
- assertEquals( GroupElement.AND,
- and1.getType() );
+ assertTrue( and1.isAnd() );
assertLength( 4,
and1.getChildren() );
- assertContains( b,
- and1.getChildren() );
- assertContains( c,
- and1.getChildren() );
- assertContains( d,
- and1.getChildren() );
- assertContains( not,
- and1.getChildren() );
+ assertEquals( b,
+ and1.getChildren().get( 0 ) );
+ assertEquals( c,
+ and1.getChildren().get( 1 ) );
+ assertEquals( d,
+ and1.getChildren().get( 2 ) );
+ assertEquals( not,
+ and1.getChildren().get( 3 ) );
and1 = (GroupElement) parent.getChildren().get( 3 );
- assertEquals( GroupElement.AND,
- and1.getType() );
+ assertTrue( and1.isAnd() );
assertLength( 4,
and1.getChildren() );
- assertContains( b,
- and1.getChildren() );
- assertContains( c,
- and1.getChildren() );
- assertContains( e,
- and1.getChildren() );
- assertContains( not,
- and1.getChildren() );
+ assertEquals( b,
+ and1.getChildren().get( 0 ) );
+ assertEquals( c,
+ and1.getChildren().get( 1 ) );
+ assertEquals( e,
+ and1.getChildren().get( 2 ) );
+ assertEquals( not,
+ and1.getChildren().get( 3 ) );
+
}
/**
@@ -254,6 +255,7 @@
assertEquals( 2,
parent.getChildren().size() );
+ // we must ensure order
GroupElement b1 = (GroupElement) parent.getChildren().get( 0 );
GroupElement b2 = (GroupElement) parent.getChildren().get( 1 );
assertTrue( b1.isNot() );
@@ -308,6 +310,7 @@
assertEquals( 2,
parent.getChildren().size() );
+ // we must ensure order
GroupElement b1 = (GroupElement) parent.getChildren().get( 0 );
GroupElement b2 = (GroupElement) parent.getChildren().get( 1 );
assertTrue( b1.isExists() );
@@ -350,14 +353,15 @@
result );
assertLength( 4,
result[0].getChildren() );
- assertContains( a,
- result[0].getChildren() );
- assertContains( b,
- result[0].getChildren() );
- assertContains( c,
- result[0].getChildren() );
- assertContains( d,
- result[0].getChildren() );
+ // we must ensure order
+ assertEquals( a,
+ result[0].getChildren().get( 0 ) );
+ assertEquals( b,
+ result[0].getChildren().get( 1 ) );
+ assertEquals( c,
+ result[0].getChildren().get( 2 ) );
+ assertEquals( d,
+ result[0].getChildren().get( 3 ) );
}
@@ -378,6 +382,10 @@
* |
* c
* </pre>
+ *
+ * It is important to ensure that the order of
+ * the elements is not changed after transformation
+ *
* <pre>
* Or
* _/ \__
@@ -388,12 +396,12 @@
* / \__
* | \
* And And
- * /|||| \ \ /||| | \ \
- * abdeh Not Not abde Not Not Exists
- * | | | | |
- * Not i Not i g
- * | |
- * c c
+ * /| | | | | \ /| | | | | \
+ * a b Not d e h Not a b Not d e Exists Not
+ * | | | | |
+ * Not i Not g i
+ * | |
+ * c c
* </pre>
*
* @throws IOException
@@ -410,12 +418,9 @@
final String c = "c";
final String d = "d";
final String e = "e";
- //final String f = "f";
final String g = "g";
final String h = "h";
final String i = "i";
- //final String j = "j";
- //final String k = "notAssertObject";
final GroupElement and1 = GroupElementFactory.newAndInstance();
final GroupElement and2 = GroupElementFactory.newAndInstance();
@@ -435,8 +440,8 @@
and3.addChild( or1 );
final GroupElement exist1 = GroupElementFactory.newExistsInstance();
exist1.addChild( g );
+ or1.addChild( h );
or1.addChild( exist1 );
- or1.addChild( h );
final GroupElement not3 = GroupElementFactory.newNotInstance();
not3.addChild( i );
@@ -458,7 +463,7 @@
// Uncomment this when you need to output a new known correct tree
// result
- //writeTree(result, "correct_processTree1.dat");
+ // writeTree(result, "correct_processTree1.dat");
final ObjectInputStream ois = new ObjectInputStream( this.getClass().getResourceAsStream( "/correct_processTree1.dat" ) );
final GroupElement[] correctResultRoot = (GroupElement[]) ois.readObject();
@@ -538,8 +543,8 @@
*
* And___ And___ And___ And___ And__ And___ And___ And___
* ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \
- * dab Not g dab Not Not dac Not g dac Not Not eab Not g eab Not Not eac Not g eac Not Not
- * | | | | | | | | | | | | |
+ * abd Not g abd Not Not abe Not g abe Not Not acd Not g acd Not Not ace Not g ace Not Not
+ * | | | | | | | | | | | |
* f f h f f h f f h f f h
*
*
@@ -600,7 +605,7 @@
// Uncomment this when you need to output a new known correct tree
// result
- //writeTree(ands, "correct_transform1.dat");
+ // writeTree(ands, "correct_transform1.dat");
// Now check the main tree
Modified: labs/jbossrules/trunk/drools-core/src/test/resources/correct_processTree1.dat
===================================================================
(Binary files differ)
Modified: labs/jbossrules/trunk/drools-core/src/test/resources/correct_transform1.dat
===================================================================
(Binary files differ)
More information about the jboss-svn-commits
mailing list