[jboss-svn-commits] JBL Code SVN: r30076 - in labs/jbossrules/trunk/drools-core/src: test/java/org/drools/rule and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Nov 9 13:47:12 EST 2009
Author: tirelli
Date: 2009-11-09 13:47:12 -0500 (Mon, 09 Nov 2009)
New Revision: 30076
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/LogicTransformer.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java
Log:
JBRULES-2329: eliminating redundant exists during rule compilation
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 2009-11-09 18:32:34 UTC (rev 30075)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/GroupElement.java 2009-11-09 18:47:12 UTC (rev 30076)
@@ -154,6 +154,25 @@
this.children.addAll( group.getChildren() );
}
}
+
+ // if after packing, this is a NOT GE with an EXISTS child
+ // or this is an EXISTS GE with a NOT child, eliminate the redundant
+ // child and make this a NOT GE
+ if ( this.isNot() && this.children.size() == 1 && this.getChildren().get( 0 ) instanceof GroupElement ) {
+ final GroupElement child = (GroupElement) this.getChildren().get( 0 );
+ if ( child.isExists() ) {
+ this.children.clear();
+ this.children.addAll( child.getChildren() );
+ }
+ }
+ if ( this.isExists() && this.children.size() == 1 && this.getChildren().get( 0 ) instanceof GroupElement ) {
+ final GroupElement child = (GroupElement) this.getChildren().get( 0 );
+ if ( child.isNot() ) {
+ this.setType( NOT );
+ this.children.clear();
+ this.children.addAll( child.getChildren() );
+ }
+ }
}
@@ -336,12 +355,11 @@
*/
public static enum Type {
- AND(
- false), OR(
- false), NOT(
- true), EXISTS(
- true), FORALL_NOT(
- true);
+ AND(false),
+ OR(false),
+ NOT(true),
+ EXISTS(true),
+ FORALL_NOT(true);
private final boolean scopeDelimiter;
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 2009-11-09 18:32:34 UTC (rev 30075)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/LogicTransformer.java 2009-11-09 18:47:12 UTC (rev 30076)
@@ -365,7 +365,7 @@
Transformation {
public void transform(final GroupElement parent) throws InvalidPatternException {
- if ( (!(parent.getChildren().get( 0 ) instanceof GroupElement)) && (((GroupElement) parent.getChildren().get( 0 )).isExists()) ) {
+ if ( (!(parent.getChildren().get( 0 ) instanceof GroupElement)) || (!((GroupElement) parent.getChildren().get( 0 )).isOr()) ) {
throw new RuntimeException( "ExistOrTransformation expected 'OR' but instead found '" + parent.getChildren().get( 0 ).getClass().getName() + "'" );
}
@@ -412,7 +412,7 @@
public void transform(final GroupElement parent) throws InvalidPatternException {
- if ( (!(parent.getChildren().get( 0 ) instanceof GroupElement)) && (((GroupElement) parent.getChildren().get( 0 )).isOr()) ) {
+ if ( (!(parent.getChildren().get( 0 ) instanceof GroupElement)) || (!((GroupElement) parent.getChildren().get( 0 )).isOr()) ) {
throw new RuntimeException( "NotOrTransformation expected 'OR' but instead found '" + parent.getChildren().get( 0 ).getClass().getName() + "'" );
}
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 2009-11-09 18:32:34 UTC (rev 30075)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java 2009-11-09 18:47:12 UTC (rev 30076)
@@ -299,6 +299,75 @@
}
/**
+ * Exists inside a not is redundant and should be eliminated
+ *
+ * (Not (exists (A) ) )
+ *
+ * <pre>
+ * Not
+ * |
+ * Exists
+ * |
+ * And
+ * / \
+ * a b
+ * </pre>
+ *
+ * Should become:
+ *
+ * <pre>
+ * Not
+ * |
+ * And
+ * / \
+ * a b
+ * </pre>
+ *
+ *
+ */
+ public void testNotExistsTransformation() throws InvalidPatternException {
+ final ObjectType type = new ClassObjectType( String.class );
+ final Pattern a = new Pattern( 0,
+ type,
+ "a" );
+ final Pattern b = new Pattern( 1,
+ type,
+ "b" );
+
+ final GroupElement not = GroupElementFactory.newNotInstance();
+ final GroupElement exists = GroupElementFactory.newExistsInstance();
+ final GroupElement and = GroupElementFactory.newAndInstance();
+ not.addChild( exists );
+ exists.addChild( and );
+ and.addChild( a );
+ and.addChild( b );
+
+ GroupElement[] transformed = LogicTransformer.getInstance().transform( not );
+ GroupElement wrapper = transformed[0];
+ GroupElement notR = (GroupElement) wrapper.getChildren().get( 0 );
+
+ assertTrue( notR.isNot() );
+ assertEquals( 1,
+ notR.getChildren().size() );
+
+ assertTrue( notR.getChildren().get( 0 ) instanceof GroupElement );
+ GroupElement andR = (GroupElement) notR.getChildren().get( 0 );
+ assertTrue( andR.isAnd() );
+
+ assertEquals( 2,
+ andR.getChildren().size() );
+ assertTrue( andR.getChildren().get( 0 ) instanceof Pattern );
+ assertTrue( andR.getChildren().get( 1 ) instanceof Pattern );
+ final Pattern a1 = (Pattern) andR.getChildren().get( 0 );
+ final Pattern b1 = (Pattern) andR.getChildren().get( 1 );
+
+ assertEquals( a,
+ a1 );
+ assertEquals( b,
+ b1 );
+ }
+
+ /**
* This data structure is now valid (Exists (OR (A B) ) )
*
* <pre>
More information about the jboss-svn-commits
mailing list