[jboss-svn-commits] JBL Code SVN: r8677 - in labs/jbossrules/trunk/drools-core: . src/main/java/org/drools/base src/main/java/org/drools/rule src/main/java/org/drools/spi src/test/java/org/drools/rule src/test/resources
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jan 3 16:57:35 EST 2007
Author: tirelli
Date: 2007-01-03 16:57:09 -0500 (Wed, 03 Jan 2007)
New Revision: 8677
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/RuleConditionElement.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AvailableVariables.java
Modified:
labs/jbossrules/trunk/drools-core/.project
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldExtractor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Accumulate.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Collect.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Column.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ConditionalElement.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/EvalCondition.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/From.java
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/main/java/org/drools/rule/Rule.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ColumnExtractor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FunctionResolver.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: adding support to variable scope resolution in core
Modified: labs/jbossrules/trunk/drools-core/.project
===================================================================
--- labs/jbossrules/trunk/drools-core/.project 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/.project 2007-01-03 21:57:09 UTC (rev 8677)
@@ -1,28 +1,29 @@
-<projectDescription>
- <name>drools-core</name>
- <comment/>
- <projects/>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.wst.common.modulecore.ComponentStructuralBuilder</name>
- <arguments/>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments/>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.wst.validation.validationbuilder</name>
- <arguments/>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.wst.common.modulecore.ComponentStructuralBuilderDependencyResolver</name>
- <arguments/>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
- <nature>org.eclipse.jdt.core.javanature</nature>
- <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
- </natures>
-</projectDescription>
\ No newline at end of file
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>drools-core</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
+ <triggers>full,incremental,</triggers>
+ <arguments>
+ <dictionary>
+ <key>LaunchConfigHandle</key>
+ <value><project>/.externalToolBuilders/org.eclipse.wst.validation.validationbuilder.launch</value>
+ </dictionary>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
+ </natures>
+</projectDescription>
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldExtractor.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/BaseClassFieldExtractor.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -74,6 +74,6 @@
return false;
}
final BaseClassFieldExtractor other = (BaseClassFieldExtractor) object;
- return this.fieldType == other.fieldType && this.index == other.index;
+ return this.fieldType == other.fieldType && this.index == other.index && this.valueType.equals( other.valueType );
}
}
\ No newline at end of file
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 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Accumulate.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -17,6 +17,7 @@
package org.drools.rule;
import java.util.List;
+import java.util.Map;
import org.drools.RuntimeDroolsException;
import org.drools.WorkingMemory;
@@ -109,4 +110,12 @@
return this.sourceColumn;
}
+ public Map getInnerDeclarations() {
+ return this.sourceColumn.getInnerDeclarations();
+ }
+
+ public Map getOuterDeclarations() {
+ return this.resultColumn.getOuterDeclarations();
+ }
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Collect.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Collect.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Collect.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -17,6 +17,7 @@
package org.drools.rule;
import java.util.Collection;
+import java.util.Map;
import org.drools.RuntimeDroolsException;
import org.drools.base.ClassObjectType;
@@ -68,5 +69,12 @@
e );
}
}
+
+ public Map getInnerDeclarations() {
+ return this.sourceColumn.getInnerDeclarations();
+ }
+ public Map getOuterDeclarations() {
+ return this.resultColumn.getOuterDeclarations();
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Column.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Column.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Column.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -16,11 +16,12 @@
* limitations under the License.
*/
-import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import org.drools.spi.ColumnExtractor;
import org.drools.spi.Constraint;
@@ -29,7 +30,7 @@
public class Column
implements
- Serializable {
+ RuleConditionElement {
/**
*
*/
@@ -37,6 +38,7 @@
private final ObjectType objectType;
private List constraints = Collections.EMPTY_LIST;
final Declaration declaration;
+ private Map declarations;
private final int index;
// this is the negative offset of the related fact inside a tuple. i.e:
@@ -71,6 +73,8 @@
this.declaration = new Declaration( identifier,
new ColumnExtractor( objectType ),
this );
+ this.declarations = new HashMap(2); // default to avoid immediate resize
+ this.declarations.put( this.declaration.getIdentifier(), this.declaration );
} else {
this.declaration = null;
}
@@ -100,6 +104,10 @@
extractor,
this );
this.constraints.add( declaration );
+ if( this.declarations == null ) {
+ this.declarations = new HashMap(2); // default to avoid immediate resize
+ }
+ this.declarations.put( declaration.getIdentifier(), declaration );
return declaration;
}
@@ -145,6 +153,14 @@
}
}
+ public Map getInnerDeclarations() {
+ return ( this.declarations != null ) ? this.declarations : Collections.EMPTY_MAP;
+ }
+
+ public Map getOuterDeclarations() {
+ return ( this.declarations != null ) ? this.declarations : Collections.EMPTY_MAP;
+ }
+
public String toString() {
return "Column type='" + ((this.objectType == null) ? "null" : this.objectType.toString()) + "', index='" + this.index + "', offset='" + this.getOffset() + "', identifer='" + ((this.declaration == null) ? "" : this.declaration.toString()) + "'";
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ConditionalElement.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ConditionalElement.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/ConditionalElement.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -16,13 +16,12 @@
* limitations under the License.
*/
-import java.io.Serializable;
public abstract class ConditionalElement
implements
- Serializable,
+ RuleConditionElement,
Cloneable {
public abstract Object clone();
-
+
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/EvalCondition.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/EvalCondition.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/EvalCondition.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -16,6 +16,9 @@
* limitations under the License.
*/
+import java.util.Collections;
+import java.util.Map;
+
import org.drools.RuntimeDroolsException;
import org.drools.WorkingMemory;
import org.drools.spi.EvalExpression;
@@ -110,4 +113,12 @@
return this.expression.equals( other.expression );
}
+
+ public Map getInnerDeclarations() {
+ return Collections.EMPTY_MAP;
+ }
+
+ public Map getOuterDeclarations() {
+ return Collections.EMPTY_MAP;
+ }
};
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/From.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/From.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/From.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -1,12 +1,16 @@
package org.drools.rule;
import java.io.Serializable;
+import java.util.Map;
import org.drools.spi.DataProvider;
public class From extends ConditionalElement
implements
Serializable {
+
+ private static final long serialVersionUID = -2640290731776949513L;
+
private Column column;
private DataProvider dataProvider;
@@ -29,4 +33,12 @@
// TODO Auto-generated method stub
return null;
}
+
+ public Map getInnerDeclarations() {
+ return this.column.getInnerDeclarations();
+ }
+
+ public Map getOuterDeclarations() {
+ return this.column.getOuterDeclarations();
+ }
}
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 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/GroupElement.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -18,8 +18,11 @@
import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import org.drools.RuntimeDroolsException;
@@ -51,7 +54,7 @@
*
* @param child
*/
- public void addChild(final Object child) {
+ public void addChild(final RuleConditionElement child) {
if ( (this.isNot() || this.isExists()) && (this.children.size() > 0) ) {
throw new RuntimeDroolsException( this.type.toString() + " can have only a single child element. Either a single Pattern or another CE." );
}
@@ -63,6 +66,20 @@
}
/**
+ * @inheritDoc
+ */
+ public Map getInnerDeclarations() {
+ return this.type.getInnerDeclarations( this.children );
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public Map getOuterDeclarations() {
+ return this.type.getOuterDeclarations( this.children );
+ }
+
+ /**
* Optimize the group element subtree by removing redundancies
* like an AND inside another AND, OR inside OR, single branches
* AND/OR, etc.
@@ -85,27 +102,27 @@
// child GE, then clone child into current node eliminating child
if ( (this.isAnd() || this.isOr()) && (this.children.size() == 1) ) {
Object child = this.getChildren().get( 0 );
- if( child instanceof GroupElement ) {
+ if ( child instanceof GroupElement ) {
GroupElement group = (GroupElement) child;
this.type = group.getType();
this.children.clear();
this.children.addAll( group.getChildren() );
}
- }
+ }
}
/**
* @param parent
*/
private void pack(GroupElement parent) {
- if( this.children.size() == 0 ) {
+ if ( this.children.size() == 0 ) {
// if there is no child, just remove this node
parent.children.remove( this );
return;
}
-
+
// If this is an AND or OR or EXISTS, there are some possible merges
- if ( this.isAnd() || this.isOr() || this.isExists()) {
+ if ( this.isAnd() || this.isOr() || this.isExists() ) {
// if parent is of the same type as current node,
// then merge this childs with parent childs
@@ -118,25 +135,27 @@
for ( Iterator childIt = this.children.iterator(); childIt.hasNext(); ) {
Object child = childIt.next();
// we must keep the order, so add in the same place were parent was before
- parent.getChildren().add( index++, child );
+ 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 );
+ 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 ) ) {
+ } 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.getChildren().add( index, child );
-
+ parent.getChildren().add( index,
+ child );
+
if ( child instanceof GroupElement ) {
((GroupElement) child).pack( parent );
}
@@ -146,7 +165,7 @@
this.pack();
}
- // also pack itself if it is a NOT
+ // also pack itself if it is a NOT
} else {
this.pack();
}
@@ -173,7 +192,7 @@
// Now try a recurse manual check
final GroupElement e2 = (GroupElement) object;
- if ( ! this.type.equals( e2.type ) ) {
+ if ( !this.type.equals( e2.type ) ) {
return false;
}
@@ -186,23 +205,7 @@
for ( int i = 0; i < e1Children.size(); i++ ) {
final Object e1Object1 = e1Children.get( i );
final Object e2Object1 = e2Children.get( i );
- if ( e1Object1 instanceof GroupElement ) {
- if ( e1Object1.getClass().isInstance( e2Object1 ) ) {
- if ( !e1Object1.equals( e2Object1 ) ) {
- //System.out.println( e1Object1.getClass().getName() + " did not have identical children" );
- return false;
- }
- } else {
- //System.out.println( "Should be the equal Conditionalelements but instead was '" + e1Object1.getClass().getName() + "', '" + e2Object1.getClass().getName() + "'" );
- return false;
- }
- } else if ( e1Object1 instanceof String ) {
- if ( !e1Object1.equals( e2Object1 ) ) {
- //System.out.println( "Should be the equal Strings but instead was '" + e1Object1 + "', '" + e2Object1 + "'" );
- return false;
- }
- } else {
- //System.out.println( "Objects are neither instances of ConditionalElement or String" );
+ if ( !e1Object1.equals( e2Object1 ) ) {
return false;
}
}
@@ -232,16 +235,15 @@
} catch ( final IllegalAccessException e ) {
throw new RuntimeException( "Could not clone '" + this.getClass().getName() + "'" );
}
-
+
cloned.setType( this.getType() );
for ( final Iterator it = this.children.iterator(); it.hasNext(); ) {
- Object object = it.next();
- if ( object instanceof GroupElement ) {
- object = ((GroupElement) object).clone();
+ RuleConditionElement re = (RuleConditionElement) it.next();
+ if ( re instanceof GroupElement ) {
+ re = (RuleConditionElement) ((GroupElement) re).clone();
}
- cloned.addChild( object );
-
+ cloned.addChild( re );
}
return cloned;
@@ -270,15 +272,17 @@
public boolean isExists() {
return this.type.isExists();
}
-
+
public String toString() {
- return this.type.toString()+this.children.toString();
+ return this.type.toString() + this.children.toString();
}
/**
* A public interface for CE types
*/
- public static interface Type extends Serializable {
+ public static interface Type
+ extends
+ Serializable {
/**
* Returns true if this CE type is an AND
@@ -299,14 +303,69 @@
* Returns true if this CE type is an EXISTS
*/
public boolean isExists();
+
+ /**
+ * Returns a map of declarations that are
+ * visible inside of an element of this type
+ */
+ public Map getInnerDeclarations(List children);
+
+ /**
+ * Returns a map of declarations that are
+ * visible outside of an element of this type
+ */
+ public Map getOuterDeclarations(List children);
}
+ private static abstract class AbstractType
+ implements
+ Type {
+
+ /**
+ * @inheritDoc
+ */
+ public Map getInnerDeclarations(List children) {
+ Map declarations = null;
+
+ if ( children.isEmpty() ) {
+ declarations = Collections.EMPTY_MAP;
+ } else if ( children.size() == 1 ) {
+ RuleConditionElement re = (RuleConditionElement) children.get( 0 );
+ declarations = re.getOuterDeclarations();
+ } else {
+ declarations = new HashMap();
+ for ( Iterator it = children.iterator(); it.hasNext(); ) {
+ declarations.putAll( ((RuleConditionElement) it.next()).getOuterDeclarations() );
+ }
+ }
+ return declarations;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public Map getOuterDeclarations(List children) {
+ Map declarations = null;
+
+ if ( children.isEmpty() ) {
+ declarations = Collections.EMPTY_MAP;
+ } else if ( children.size() == 1 ) {
+ RuleConditionElement re = (RuleConditionElement) children.get( 0 );
+ declarations = re.getOuterDeclarations();
+ } else {
+ declarations = new HashMap();
+ for ( Iterator it = children.iterator(); it.hasNext(); ) {
+ declarations.putAll( ((RuleConditionElement) it.next()).getOuterDeclarations() );
+ }
+ }
+ return declarations;
+ }
+ }
+
/**
* An AND CE type
*/
- private static class AndType
- implements
- Type {
+ private static class AndType extends AbstractType {
private static final long serialVersionUID = -669797012452495460L;
@@ -343,14 +402,13 @@
public String toString() {
return "AND";
}
+
}
/**
* An OR CE type
*/
- private static class OrType
- implements
- Type {
+ private static class OrType extends AbstractType {
private static final long serialVersionUID = 8108203371968455372L;
@@ -392,9 +450,7 @@
/**
* A NOT CE type
*/
- private static class NotType
- implements
- Type {
+ private static class NotType extends AbstractType {
private static final long serialVersionUID = -7873159668081968617L;
@@ -417,6 +473,13 @@
return false;
}
+ /**
+ * @inheritDoc
+ */
+ public Map getOuterDeclarations(List children) {
+ return Collections.EMPTY_MAP;
+ }
+
public boolean equals(Object obj) {
if ( !(obj instanceof NotType) ) {
return false;
@@ -431,15 +494,12 @@
public String toString() {
return "NOT";
}
-
}
/**
* An EXISTS CE type
*/
- private static class ExistsType
- implements
- Type {
+ private static class ExistsType extends AbstractType {
private static final long serialVersionUID = -1528071451996382861L;
@@ -462,6 +522,13 @@
return false;
}
+ /**
+ * @inheritDoc
+ */
+ public Map getOuterDeclarations(List children) {
+ return Collections.EMPTY_MAP;
+ }
+
public boolean equals(Object obj) {
if ( !(obj instanceof ExistsType) ) {
return false;
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 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/LogicTransformer.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -86,7 +86,7 @@
ands = new GroupElement[cloned.getChildren().size()];
int i = 0;
for( Iterator it = cloned.getChildren().iterator(); it.hasNext(); ) {
- Object branch = it.next();
+ RuleConditionElement branch = (RuleConditionElement) it.next();
if( ( branch instanceof GroupElement ) && (((GroupElement)branch).isAnd()) ){
ands[i++] = (GroupElement) branch;
} else {
@@ -281,7 +281,7 @@
parent.getChildren().clear();
for ( Iterator it = or.getChildren().iterator(); it.hasNext(); ) {
GroupElement newExists = GroupElementFactory.newExistsInstance();
- newExists.addChild( it.next() );
+ newExists.addChild( (RuleConditionElement) it.next() );
parent.addChild( newExists );
}
parent.pack();
@@ -328,7 +328,7 @@
parent.getChildren().clear();
for ( Iterator it = or.getChildren().iterator(); it.hasNext(); ) {
GroupElement newNot = GroupElementFactory.newNotInstance();
- newNot.addChild( it.next() );
+ newNot.addChild( (RuleConditionElement) it.next() );
parent.addChild( newNot );
}
parent.pack();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -17,7 +17,6 @@
*/
import java.io.Serializable;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -45,48 +44,48 @@
/**
*
*/
- private static final long serialVersionUID = 320;
+ private static final long serialVersionUID = 320;
/** */
// ------------------------------------------------------------
// Instance members
// ------------------------------------------------------------
/** The parent pkg */
- private String pkg;
+ private String pkg;
/** Name of the rule. */
- private final String name;
+ private final String name;
/** Salience value. */
- private int salience;
+ private int salience;
- private final Map declarations = new HashMap();
+ private boolean dirty = false;
+ private Map declarations;
+ private Declaration[] declarationArray;
- private Declaration[] declarationArray;
-
private final GroupElement lhsRoot = new GroupElement( GroupElement.AND );
- private String agendaGroup;
+ private String agendaGroup;
/** Consequence. */
- private Consequence consequence;
+ private Consequence consequence;
/** Truthness duration. */
- private Duration duration;
+ private Duration duration;
/** Load order in Package */
- private long loadOrder;
+ private long loadOrder;
/** Is recursion of this rule allowed */
- private boolean noLoop;
+ private boolean noLoop;
/** makes the rule's much the current focus */
- private boolean autoFocus;
+ private boolean autoFocus;
- private String ActivationGroup;
+ private String ActivationGroup;
/** indicates that the rule is semantically correct. */
- private boolean semanticallyValid = true;
+ private boolean semanticallyValid = true;
// ------------------------------------------------------------
// Constructors
@@ -268,6 +267,11 @@
* the <code>identifier</code>.
*/
public Declaration getDeclaration(final String identifier) {
+ if ( dirty || (this.declarations == null) ) {
+ this.declarations = this.lhsRoot.getOuterDeclarations();
+ this.declarationArray = (Declaration[]) this.declarations.values().toArray( new Declaration[this.declarations.values().size()] );
+ this.dirty = false;
+ }
return (Declaration) this.declarations.get( identifier );
}
@@ -279,8 +283,10 @@
* <i>root fact objects</i>.
*/
public Declaration[] getDeclarations() {
- if ( this.declarationArray == null ) {
+ if ( dirty || (this.declarationArray == null) ) {
+ this.declarations = this.lhsRoot.getOuterDeclarations();
this.declarationArray = (Declaration[]) this.declarations.values().toArray( new Declaration[this.declarations.values().size()] );
+ this.dirty = false;
}
return this.declarationArray;
}
@@ -293,54 +299,11 @@
* The <code>Test</code> to add.
* @throws InvalidRuleException
*/
- public void addPattern(final ConditionalElement ce) {
- if ( ce instanceof GroupElement ) {
- addDeclarations( (GroupElement) ce );
- } else if ( ce.getClass() == From.class ) {
- addDeclarations( ((From) ce).getColumn() );
- } else if ( ce.getClass() == Accumulate.class ) {
- addDeclarations( ((Accumulate) ce).getResultColumn() );
- } else if ( ce.getClass() == Collect.class ) {
- addDeclarations( ((Collect) ce).getResultColumn() );
- }
- this.lhsRoot.addChild( ce );
+ public void addPattern(final RuleConditionElement element) {
+ this.dirty = true;
+ this.lhsRoot.addChild( element );
}
- public void addPattern(final Column column) {
- addDeclarations( column );
- this.lhsRoot.addChild( column );
- }
-
- private void addDeclarations(final Column column) {
- // Check if the column is bound and if so add it as a declaration
- if ( column.isBound() ) {
- final Declaration declaration = column.getDeclaration();
- this.declarations.put( declaration.getIdentifier(),
- declaration );
- }
-
- // Check if there are any bound fields and if so add it as a declaration
- for ( final Iterator it = column.getConstraints().iterator(); it.hasNext(); ) {
- final Object object = it.next();
- if ( object instanceof Declaration ) {
- final Declaration declaration = (Declaration) object;
- this.declarations.put( declaration.getIdentifier(),
- declaration );
- }
- }
- }
-
- private void addDeclarations(final GroupElement ce) {
- for ( final Iterator it = ce.getChildren().iterator(); it.hasNext(); ) {
- final Object object = it.next();
- if ( object instanceof Column ) {
- addDeclarations( (Column) object );
- } else if ( object instanceof GroupElement ) {
- addDeclarations( (GroupElement) object );
- }
- }
- }
-
/**
* Retrieve the <code>List</code> of <code>Conditions</code> for this
* rule.
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/RuleConditionElement.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/RuleConditionElement.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/RuleConditionElement.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2006 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.rule;
+
+import java.util.Map;
+
+import org.drools.spi.RuleComponent;
+
+/**
+ * @author etirelli
+ *
+ */
+public interface RuleConditionElement
+ extends
+ RuleComponent {
+
+ /**
+ * Returns a Map of declarations that are
+ * visible inside this conditional element
+ *
+ * @return
+ */
+ public abstract Map getInnerDeclarations();
+
+ /**
+ * Returns a Map of declarations that are visible
+ * outside this conditional element.
+ *
+ * @return
+ */
+ public abstract Map getOuterDeclarations();
+
+}
Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/RuleConditionElement.java
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:keywords
+ id author date revision
Name: svn:eol-style
+ native
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AvailableVariables.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AvailableVariables.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AvailableVariables.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -1,36 +0,0 @@
-package org.drools.spi;
-
-import java.util.Map;
-
-import org.drools.rule.Declaration;
-
-public class AvailableVariables {
- private Map[] maps;
-
- public AvailableVariables(final Map[] maps) {
- this.maps = maps;
- }
-
- public Class getType(final String name) {
- for ( int i = 0, length = this.maps.length; i < length; i++ ) {
- final Object object = this.maps[i].get( name );
- if ( object != null ) {
- if ( object.getClass() == Declaration.class ) {
- return ((Declaration) object).getExtractor().getExtractToClass();
- } else {
- return (Class) object;
- }
- }
- }
- return null;
- }
-
- public boolean available(final String name) {
- for ( int i = 0, length = this.maps.length; i < length; i++ ) {
- if ( this.maps[i].containsKey( (name) ) ) {
- return true;
- }
- }
- return false;
- }
-}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ColumnExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ColumnExtractor.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ColumnExtractor.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -128,4 +128,19 @@
public int getHashCode(Object object) {
return getValue( object ).hashCode();
}
+
+ public int hashCode() {
+ return this.objectType.hashCode();
+ }
+
+ public boolean equals(Object obj) {
+ if( this == obj ) {
+ return true;
+ }
+ if( ! ( obj instanceof ColumnExtractor ) ) {
+ return false;
+ }
+ ColumnExtractor other = (ColumnExtractor) obj;
+ return this.objectType.equals( other.objectType );
+ }
}
\ No newline at end of file
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java (from rev 8603, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AvailableVariables.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AvailableVariables.java 2007-01-02 11:09:57 UTC (rev 8603)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -0,0 +1,88 @@
+package org.drools.spi;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+
+import org.drools.rule.Declaration;
+import org.drools.rule.RuleConditionElement;
+
+public class DeclarationScopeResolver {
+ private static final Stack EMPTY_STACK = new Stack( );
+ private Map[] maps;
+ private Stack buildStack;
+
+ public DeclarationScopeResolver(final Map[] maps ) {
+ this( maps, EMPTY_STACK );
+ }
+
+ public DeclarationScopeResolver(final Map[] maps, final Stack buildStack ) {
+ this.maps = maps;
+ if( buildStack == null ) {
+ this.buildStack = EMPTY_STACK;
+ } else {
+ this.buildStack = buildStack;
+ }
+ }
+
+ public Class getType(final String name) {
+ for ( int i = 0, length = this.maps.length; i < length; i++ ) {
+ final Object object = this.maps[i].get( name );
+ if ( object != null ) {
+ if ( object.getClass() == Declaration.class ) {
+ return ((Declaration) object).getExtractor().getExtractToClass();
+ } else {
+ return (Class) object;
+ }
+ }
+ }
+ for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
+ Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
+ if( declaration != null ) {
+ return declaration.getExtractor().getExtractToClass();
+ }
+ }
+ return null;
+ }
+
+ public Declaration getDeclaration( final String name ) {
+ for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
+ Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
+ if( declaration != null ) {
+ return declaration;
+ }
+ }
+ return null;
+ }
+
+ public boolean available(final String name) {
+ for ( int i = 0, length = this.maps.length; i < length; i++ ) {
+ if ( this.maps[i].containsKey( (name) ) ) {
+ return true;
+ }
+ }
+ for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
+ Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
+ if( declaration != null ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Return all declarations scoped to the current
+ * RuleConditionElement in the build stack
+ *
+ * @return
+ */
+ public Map getDeclarations() {
+ Map declarations = new HashMap();
+ for( int i = 0; i < this.buildStack.size(); i++ ) {
+ // this may be optimized in the future to only re-add elements at
+ // scope breaks, like "NOT" and "EXISTS"
+ declarations.putAll( ((RuleConditionElement) this.buildStack.get( i )).getInnerDeclarations() );
+ }
+ return declarations;
+ }
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FunctionResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FunctionResolver.java 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FunctionResolver.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -12,6 +12,6 @@
public String resolveFunction(String functionName,
String params,
- AvailableVariables variables);
+ DeclarationScopeResolver variables);
}
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 2007-01-03 21:45:31 UTC (rev 8676)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java 2007-01-03 21:57:09 UTC (rev 8677)
@@ -23,6 +23,8 @@
import java.io.ObjectOutputStream;
import org.drools.DroolsTestCase;
+import org.drools.base.ClassObjectType;
+import org.drools.spi.ObjectType;
public class LogicTransformerTest extends DroolsTestCase {
/**
@@ -50,9 +52,10 @@
* </pre>
*/
public void testSingleOrAndOrTransformation() throws InvalidPatternException {
- final String a = "a";
- final String b = "b";
- final String c = "c";
+ final ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
+ final Column c = new Column( 2, type, "c");
final GroupElement or = GroupElementFactory.newOrInstance();
or.addChild( a );
@@ -119,12 +122,13 @@
* </pre>
*/
public void testMultipleOrAndOrTransformation() throws InvalidPatternException {
- final String a = "a";
- final String b = "b";
- final String c = "c";
- final String d = "d";
- final String e = "e";
- final String f = "f";
+ final ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
+ final Column c = new Column( 2, type, "c");
+ final Column d = new Column( 3, type, "d");
+ final Column e = new Column( 4, type, "e");
+ final Column f = new Column( 5, type, "f");
final GroupElement parent = GroupElementFactory.newAndInstance();
final GroupElement or = GroupElementFactory.newOrInstance();
@@ -239,8 +243,9 @@
*
*/
public void testNotOrTransformation() throws InvalidPatternException {
- final String a = "a";
- final String b = "b";
+ final ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
final GroupElement parent = GroupElementFactory.newNotInstance();
final GroupElement or = GroupElementFactory.newOrInstance();
@@ -294,8 +299,9 @@
* </pre>
*/
public void testExistOrTransformation() throws InvalidPatternException {
- final String a = "a";
- final String b = "b";
+ final ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
final GroupElement parent = GroupElementFactory.newExistsInstance();
final GroupElement or = GroupElementFactory.newOrInstance();
@@ -329,10 +335,11 @@
}
public void testEliminateEmptyBranchesAndDuplications() throws InvalidRuleException {
- final String a = "a";
- final String b = "b";
- final String c = "c";
- final String d = "d";
+ final ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
+ final Column c = new Column( 2, type, "c");
+ final Column d = new Column( 3, type, "d");
final GroupElement and1 = GroupElementFactory.newAndInstance();
and1.addChild( a );
@@ -413,14 +420,15 @@
public void testProcessTree() throws IOException,
ClassNotFoundException,
InvalidPatternException {
- final String a = "a";
- final String b = "b";
- final String c = "c";
- final String d = "d";
- final String e = "e";
- final String g = "g";
- final String h = "h";
- final String i = "i";
+ final ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
+ final Column c = new Column( 2, type, "c");
+ final Column d = new Column( 3, type, "d");
+ final Column e = new Column( 4, type, "e");
+ final Column g = new Column( 5, type, "g");
+ final Column h = new Column( 6, type, "h");
+ final Column i = new Column( 7, type, "i");
final GroupElement and1 = GroupElementFactory.newAndInstance();
final GroupElement and2 = GroupElementFactory.newAndInstance();
@@ -476,14 +484,15 @@
}
public void testCloneable() {
- final String a = "a";
- final String b = "b";
- 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 ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
+ final Column c = new Column( 2, type, "c");
+ final Column d = new Column( 3, type, "d");
+ final Column e = new Column( 4, type, "e");
+ final Column f = new Column( 5, type, "f");
+ final Column g = new Column( 6, type, "g");
+ final Column h = new Column( 7, type, "h");
// Test against a known false tree
final GroupElement and = GroupElementFactory.newAndInstance();
@@ -563,14 +572,15 @@
public void testTransform() throws IOException,
ClassNotFoundException,
InvalidPatternException {
- final String a = "a";
- final String b = "b";
- 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 ObjectType type = new ClassObjectType(String.class);
+ final Column a = new Column( 0, type, "a");
+ final Column b = new Column( 1, type, "b");
+ final Column c = new Column( 2, type, "c");
+ final Column d = new Column( 3, type, "d");
+ final Column e = new Column( 4, type, "e");
+ final Column f = new Column( 5, type, "f");
+ final Column g = new Column( 6, type, "g");
+ final Column h = new Column( 7, type, "h");
final GroupElement and = GroupElementFactory.newAndInstance();
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