Author: mar...@jb... Date: 2005-12-18 19:06:15 -0500 (Sun, 18 Dec 2005) New Revision: 1854 Added: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exists.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/PredicateExpressionConstraint.java Removed: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/BooleanConstraint.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exist.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/BooleanExpressionConstraint.java Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/NoSuchFactObjectException.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNode.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNodeBinder.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Builder.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectSource.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Rete.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Column.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/LogicTransformer.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/ClassObjectType.java trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/Tuple.java trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/ConstraintTest.java trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java Log: JBRULES-10 : Rete Builder -Take a given AND Conditional Element and turn it and all its children into a Rete network. Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/NoSuchFactObjectException.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/NoSuchFactObjectException.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/NoSuchFactObjectException.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -55,7 +55,7 @@ * @version $Id: NoSuchFactObjectException.java,v 1.3 2003/11/19 21:31:09 bob * Exp $ */ -public class NoSuchFactObjectException extends FactException +public class NoSuchFactObjectException extends RuntimeException { /** Invalid fact handle. */ private final FactHandle handle; Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -97,4 +97,21 @@ { return new HashSet( ); } + + public boolean equals( Object object ) + { + if ( this == object ) + { + return true; + } + + if ( object == null || getClass( ) != object.getClass( ) ) + { + return false; + } + + AlphaNode other = (AlphaNode) object; + + return this.objectSource.equals( other.objectSource ) && this.constraint.equals( other.constraint ); + } } Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNode.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNode.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNode.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -220,7 +220,7 @@ BetaNode other = (BetaNode) object; - return this.leftInput.equals( other.leftInput ) && this.rightInput.equals( other.rightInput ); + return this.leftInput.equals( other.leftInput ) && this.rightInput.equals( other.rightInput ) && this.joinNodeBinder.equals( other.joinNodeBinder ) ; } public Object createMemory() Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNodeBinder.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNodeBinder.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/BetaNodeBinder.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -1,66 +1,119 @@ package org.drools.reteoo; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + import org.drools.FactHandle; import org.drools.NoSuchFactObjectException; import org.drools.WorkingMemory; +import org.drools.spi.ClassObjectType; import org.drools.spi.Constraint; import org.drools.spi.Tuple; public class BetaNodeBinder { - private final Constraint constraint; + private final Constraint[] constraints; public BetaNodeBinder( ) { - this.constraint = null; + this.constraints = null; } public BetaNodeBinder( Constraint constraint ) { - this.constraint = constraint; + this.constraints = new Constraint[] { constraint }; } + public BetaNodeBinder( Constraint[] constraints ) + { + this.constraints = constraints; + } + boolean isAllowed(Object object, FactHandle handle, Tuple tuple, WorkingMemory workingMemory) { - if ( constraint == null ) + if ( constraints == null ) { return true; } - return this.constraint.isAllowed( object, - handle, - tuple ); + for (int i=0; i < this.constraints.length; i++) + { + if ( ! this.constraints[i].isAllowed( object, + handle, + tuple ) ) + { + return false; + } + } + return true; + } boolean isAllowed(FactHandle handle, Tuple tuple, WorkingMemory workingMemory) { - if ( constraint == null ) + Object object = workingMemory.getObject( handle ); + + return isAllowed(object, handle, tuple, workingMemory); + } + + public Set getRequiredDeclarations() + { + Set declarations = new HashSet(); + for ( int i = 0; i < this.constraints.length; i++ ) { + Collections.addAll(declarations, this.constraints[i].getRequiredDeclarations()); + } + return declarations; + } + + public int hashCode() + { + return this.constraints.hashCode(); + } + + /** + * Determine if another object is equal to this. + * + * @param object The object to test. + * + * @return <code>true</code> if <code>object</code> is equal to this, + * otherwise <code>false</code>. + */ + public boolean equals( Object object ) + { + if ( this == object ) + { return true; } - - boolean isAllowed = false; - try + if ( object == null || getClass( ) != object.getClass( ) ) { - isAllowed = isAllowed( workingMemory.getObject( handle ), - handle, - tuple, - workingMemory ); + return false; } - catch ( NoSuchFactObjectException e ) + + BetaNodeBinder other = ( BetaNodeBinder ) object; + + if ( this.constraints.length != other.constraints.length ) { - //do nothing, as we return false. - //also this should never happen + return false; } - - return isAllowed; - } + for (int i=0; i < this.constraints.length; i++) + { + if ( ! this.constraints[i].equals( other.constraints[i] ) ) + { + return false; + } + } + + return true; + } + } Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Builder.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Builder.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Builder.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -43,6 +43,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -55,8 +56,13 @@ import org.drools.RuleSetIntegrationException; import org.drools.conflict.DefaultConflictResolver; import org.drools.rule.And; +import org.drools.rule.Binding; import org.drools.rule.Column; +import org.drools.rule.ConditionalElement; import org.drools.rule.Declaration; +import org.drools.rule.Exists; +import org.drools.rule.InvalidPatternException; +import org.drools.rule.LiteralConstraint; import org.drools.rule.Not; import org.drools.rule.Rule; import org.drools.rule.RuleSet; @@ -84,16 +90,24 @@ // ------------------------------------------------------------ /** Rete network to build against. */ - private Rete rete; + private Rete rete; /** Rule-sets added. */ - private List ruleSets; + private List ruleSets; /** Nodes that have been attached. */ - private Map attachedNodes; + private Map attachedNodes; - private Map applicationData; - + private Map applicationData; + + private TupleSource tupleSource; + + private ObjectSource objectSource; + + private Map declarations; + + private int id; + // ------------------------------------------------------------ // Constructors // ------------------------------------------------------------ @@ -107,12 +121,10 @@ this.rete = new Rete( ); this.ruleSets = new ArrayList( ); this.attachedNodes = new HashMap( ); - this.applicationData = new HashMap( ); + this.applicationData = new HashMap( ); + this.declarations = new HashMap( ); + } - - this.rete.getOrCreateObjectTypeNode( new ClassObjectType( InitialFact.class ) ); - } - // ------------------------------------------------------------ // Instance methods // ------------------------------------------------------------ @@ -126,58 +138,307 @@ * @throws RuleIntegrationException * if an error prevents complete construction of the network for * the <code>Rule</code>. + * @throws InvalidPatternException */ - void addRule(Rule rule) throws RuleIntegrationException - { - And[] and = rule.getProcessPatterns(); + void addRule(Rule rule) throws InvalidPatternException + { + And[] and = rule.getProcessPatterns( ); for ( int i = 0; i < and.length; i++ ) { - addRule( and[i], rule ); + addRule( and[i], + rule ); } } - - private void addRule( And and, Rule rule ) + + private void addRule(And and, + Rule rule) { - for(Iterator it = and.getChildren().iterator(); it.hasNext(); ) + for ( Iterator it = and.getChildren( ).iterator( ); it.hasNext( ); ) { - Object object = it.next(); - + Object object = it.next( ); + + BetaNodeBinder binder; + Column column; + + if ( object instanceof Column ) + { + column = (Column) object; + + binder = attachColumn( (Column) object, + and ); + + // If a tupleSource does not exist then we need to adapt this into + // a TupleSource using LeftInputAdapterNode + if ( this.tupleSource == null ) + { + this.tupleSource = attachNode( new LeftInputAdapterNode( this.id++, + column.getIndex( ), + this.objectSource ) ); + + // objectSource is created by the attachColumn method, if we adapt this to + // a TupleSource then we need to null the objectSource reference. + this.objectSource = null; + } + } + else + { + // If its not a Column then it can either be a Not or an Exists + ConditionalElement ce = (ConditionalElement) object; + while ( !(ce.getChildren( ).get( 0 ) instanceof Column) ) + { + ce = (ConditionalElement) ce.getChildren( ).get( 0 ); + } + column = (Column) ce.getChildren( ).get( 0 ); + binder = attachColumn( (Column) object, + and ); + + // If a tupleSource does not exist then we need to adapt an + // InitialFact into a a TupleSource using LeftInputAdapterNode + if ( this.tupleSource == null ) + { + ObjectSource objectSource = attachNode( new ObjectTypeNode( this.id++, + new ClassObjectType( InitialFact.class ), + this.rete ) ); + + this.tupleSource = attachNode( new LeftInputAdapterNode( this.id++, + column.getIndex( ), + objectSource ) ); + + } + } + + if ( object instanceof Not ) { - Not not = (Not) object; - Object child = not.getChild(); - - if ( child instanceof Column) - { - - Column column = (Column) child; - if ( !hasDependencies(column.getConstraints() ) ) - { - - } - - } - new NotNode(); + attachNot( this.tupleSource, + (Not) object, + this.objectSource, + binder, + column ); } + else if ( object instanceof Exists ) + { + attachExists( this.tupleSource, + (Exists) object, + this.objectSource, + binder, + column ); + } + else if ( this.objectSource != null ) + { + this.tupleSource = attachNode( new JoinNode( this.id++, + this.tupleSource, + this.objectSource, + column.getIndex( ), + binder ) ); + } } } - - - private boolean hasDependencies(List constraints) + + private BetaNodeBinder attachColumn(Column column, + ConditionalElement parent) { - boolean hasDependencies = false; - for (Iterator it = constraints.iterator(); it.hasNext(); ) + addDeclarations( column ); + + List predicates = attachAlphaNodes( column ); + + BetaNodeBinder binder; + + if ( !predicates.isEmpty( ) ) { - Constraint constraint = (Constraint)it.next(); - if (constraint.getRequiredDeclarations().length > 0) + binder = new BetaNodeBinder( (Constraint[]) predicates.toArray( new Constraint[predicates.size( )] ) ); + } + else + { + binder = new BetaNodeBinder( ); + } + + return binder; + } + + private void addDeclarations(Column column) + { + for ( Iterator it = column.getDeclarations( ).iterator( ); it.hasNext( ); ) + { + Declaration declaration = (Declaration) it.next( ); + this.declarations.put( declaration.getIdentifier( ), + declaration ); + } + + if ( column.getBinding( ) != null ) + { + Binding binding = column.getBinding( ); + this.declarations.put( binding.getIdentifier( ), + binding ); + } + } + + public List attachAlphaNodes(Column column) + { + List constraints = column.getConstraints( ); + + ObjectSource objectSource = attachNode( new ObjectTypeNode( this.id++, + column.getObjectType( ), + this.rete ) ); + + List predicateConstraints = new ArrayList( ); + + for ( Iterator it = constraints.iterator( ); it.hasNext( ); ) + { + Constraint constraint = (Constraint) it.next( ); + if ( constraint instanceof LiteralConstraint ) { - return true; + this.objectSource = attachNode( new AlphaNode( this.id++, + constraint, + true, + objectSource ) ); } - } - return false; + else + { + predicateConstraints.add( constraint ); + } + } + + return predicateConstraints; } + private void attachNot(TupleSource tupleSource, + Not not, + ObjectSource ObjectSource, + BetaNodeBinder binder, + Column column) + { + NotNode notNode = (NotNode) attachNode( new NotNode( this.id++, + tupleSource, + ObjectSource, + column.getIndex( ), + binder ) ); + if ( not.getChild( ) instanceof Not ) + { - + RightInputAdapterNode adapter = (RightInputAdapterNode) attachNode( new RightInputAdapterNode( this.id++, + column.getIndex( ), + notNode ) ); + attachNot( tupleSource, + (Not) not.getChild( ), + adapter, + new BetaNodeBinder( ), + column ); + } + else if ( not.getChild( ) instanceof Exists ) + { + RightInputAdapterNode adapter = (RightInputAdapterNode) attachNode( new RightInputAdapterNode( this.id++, + column.getIndex( ), + notNode ) ); + attachExists( tupleSource, + (Exists) not.getChild( ), + adapter, + new BetaNodeBinder( ), + column ); + } + else + { + this.tupleSource = notNode; + } + } + private void attachExists(TupleSource tupleSource, + Exists exists, + ObjectSource ObjectSource, + BetaNodeBinder binder, + Column column) + { + NotNode notNode = (NotNode) attachNode( new NotNode( this.id++, + tupleSource, + ObjectSource, + column.getIndex( ), + binder ) ); + RightInputAdapterNode adapter = (RightInputAdapterNode) attachNode( new RightInputAdapterNode( this.id++, + column.getIndex( ), + notNode ) ); + notNode = (NotNode) attachNode( new NotNode( this.id++, + tupleSource, + adapter, + column.getIndex( ), + new BetaNodeBinder( ) ) ); + + if ( exists.getChild( ) instanceof Not ) + { + adapter = (RightInputAdapterNode) attachNode( new RightInputAdapterNode( this.id++, + column.getIndex( ), + notNode ) ); + attachNot( tupleSource, + (Not) exists.getChild( ), + adapter, + new BetaNodeBinder( ), + column ); + } + else if ( exists.getChild( ) instanceof Exists ) + { + adapter = (RightInputAdapterNode) attachNode( new RightInputAdapterNode( this.id++, + column.getIndex( ), + notNode ) ); + attachExists( tupleSource, + (Exists) exists.getChild( ), + adapter, + new BetaNodeBinder( ), + column ); + } + else + { + this.tupleSource = notNode; + } + } + + /** + * Attaches a node into the network. If a node already exists that could + * substitute, it is used instead. + * + * @param candidate + * The node to attach. + * @param leafNodes + * The list to which the newly added node will be added. + */ + private TupleSource attachNode(TupleSource candidate) + { + TupleSource node = (TupleSource) this.attachedNodes.get( candidate ); + + if ( node == null ) + { + candidate.attach( ); + + this.attachedNodes.put( candidate, + candidate ); + + node = candidate; + } + else + { + id--; + } + + return node; + } + + private ObjectSource attachNode(ObjectSource candidate) + { + ObjectSource node = (ObjectSource) this.attachedNodes.get( candidate ); + + if ( node == null ) + { + candidate.attach( ); + + this.attachedNodes.put( candidate, + candidate ); + + node = candidate; + } + else + { + id--; + } + + return node; + } + } Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectSource.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectSource.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectSource.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -221,4 +221,11 @@ public abstract void remove(); + public int hashCode() + { + return this.id; + } + + + } Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -237,6 +237,20 @@ { return new PrimitiveLongMap( 32, 8 ); + } + + public boolean equals( Object object ) + { + if ( this == object ) + { + return true; + } + + if ( object == null || getClass( ) != object.getClass( ) ) + { + return false; + } + + return this.objectType.equals( ( ( ObjectTypeNode ) object).getObjectType() ); } - } Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Rete.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Rete.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/Rete.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -50,7 +50,6 @@ import org.drools.FactException; import org.drools.rule.And; -import org.drools.rule.LogicTransformer; import org.drools.rule.Rule; import org.drools.spi.ObjectType; import org.drools.spi.PropagationContext; @@ -188,33 +187,7 @@ return (ObjectTypeNode) this.objectTypeNodes.get( objectType ); } - /** - * Retrieve an <code>ObjectTypeNode</code> keyed by - * <code>ObjectType</code>, creating one, if necessary. - * - * @param objectType - * The <code>ObjectType</code> key. - * - * @return The matching <code>ObjectTypeNode</codeb>. - */ - ObjectTypeNode getOrCreateObjectTypeNode(ObjectType objectType) - { - ObjectTypeNode node = getObjectTypeNode( objectType ); - if ( node == null ) - { - node = new ObjectTypeNode( 0, - objectType, - this ); - - node.attach( ); - } - - this.rulesToUpdate.add( node ); - - return node; - } - /** * Add an <code>ObjectTypeNode</code> child to this <code>Rete</code>. * Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -159,7 +159,7 @@ public Object get(Declaration declaration) { return declaration.getValue( get( declaration.getColumn( ) ) ); - } + } /** * @see Tuple Deleted: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/BooleanConstraint.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/BooleanConstraint.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/BooleanConstraint.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -1,70 +0,0 @@ -package org.drools.rule; - -import org.drools.FactHandle; -import org.drools.spi.BooleanExpressionConstraint; -import org.drools.spi.Constraint; -import org.drools.spi.Tuple; - -public class BooleanConstraint - implements - Constraint -{ - private final Declaration declaration; - - private final BooleanExpressionConstraint booleanExpression; - - private final Declaration[] requiredDeclarations; - - public BooleanConstraint(BooleanExpressionConstraint expression, - Declaration declaration) - { - this( expression, - declaration, - null ); - } - - public BooleanConstraint(BooleanExpressionConstraint expression, - Declaration declaration, - Declaration[] declarations) - { - this.booleanExpression = expression; - - this.declaration = declaration; - - if ( declarations == null ) - { - this.requiredDeclarations = new Declaration[]{}; - } - else - { - this.requiredDeclarations = declarations; - } - } - - public BooleanExpressionConstraint getExpression() - { - return this.booleanExpression; - } - - public Declaration[] getRequiredDeclarations() - { - return requiredDeclarations; - } - - public Declaration getDeclaration() - { - return this.declaration; - } - - public boolean isAllowed(Object object, - FactHandle handle, - Tuple tuple) - { - return this.booleanExpression.isAllowed( object, - handle, - this.declaration, - this.requiredDeclarations, - tuple ); - } - -}; Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Column.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Column.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Column.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -10,6 +10,7 @@ { private final ObjectType objectType; private List constraints = Collections.EMPTY_LIST; + private List declarations = Collections.EMPTY_LIST; private final ColumnBinding binding; private final int index; @@ -24,6 +25,7 @@ public Column(int index, ObjectType objectType, ColumnBinding binding) + { this.index = index; this.objectType = objectType; @@ -34,7 +36,21 @@ { return this.objectType; } + + public List getDeclarations() + { + return Collections.unmodifiableList( this.declarations ); + } + public void addDeclaration(Declaration declaration) + { + if (this.declarations == Collections.EMPTY_LIST) + { + this.declarations = new ArrayList( 1 ); + } + this.declarations.add( declaration ); + } + public List getConstraints() { return Collections.unmodifiableList( this.constraints ); Deleted: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exist.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exist.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exist.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -1,12 +0,0 @@ -package org.drools.rule; - -import java.util.ArrayList; -import java.util.List; - -public class Exist extends ConditionalElement -{ - public Object getChild() - { - return getChildren().get( 0 ); - } -} Copied: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exists.java (from rev 1776, trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exist.java) =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exist.java 2005-12-10 17:37:55 UTC (rev 1776) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/Exists.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -0,0 +1,12 @@ +package org.drools.rule; + +import java.util.ArrayList; +import java.util.List; + +public class Exists extends ConditionalElement +{ + public Object getChild() + { + return getChildren().get( 0 ); + } +} Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/LogicTransformer.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/LogicTransformer.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/LogicTransformer.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -55,14 +55,14 @@ And.class ); addTransformationPair( Or.class, Or.class ); - addTransformationPair( Exist.class, - Exist.class ); + addTransformationPair( Exists.class, + Exists.class ); // these pairs will be transformed addTransformationPair( Not.class, Or.class, new NotOrTransformation( ) ); - addTransformationPair( Exist.class, + addTransformationPair( Exists.class, Or.class, new ExistOrTransformation( ) ); addTransformationPair( And.class, @@ -401,9 +401,9 @@ /** * This data structure is not valid - * (Exist (OR (A B) + * (Exists (OR (A B) * <pre> - * Exist + * Exists * | * or * / \ @@ -418,7 +418,7 @@ public ConditionalElement transform(ConditionalElement exist) throws InvalidPatternException { - throw new InvalidPatternException("You cannot nest an OR within an Exist"); + throw new InvalidPatternException("You cannot nest an OR within an Exists"); } } Copied: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java (from rev 1776, trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/BooleanConstraint.java) =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/BooleanConstraint.java 2005-12-10 17:37:55 UTC (rev 1776) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/rule/PredicateConstraint.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -0,0 +1,72 @@ +package org.drools.rule; + +import org.drools.FactHandle; +import org.drools.spi.PredicateExpressionConstraint; +import org.drools.spi.Constraint; +import org.drools.spi.Tuple; + +public class PredicateConstraint + implements + Constraint +{ + private final Declaration declaration; + + private final PredicateExpressionConstraint booleanExpression; + + private final Declaration[] requiredDeclarations; + + private static final Declaration[] EMPTY_DECLARATIONS = new Declaration[0]; + + public PredicateConstraint(PredicateExpressionConstraint expression, + Declaration declaration) + { + this( expression, + declaration, + null ); + } + + public PredicateConstraint(PredicateExpressionConstraint expression, + Declaration declaration, + Declaration[] declarations) + { + this.booleanExpression = expression; + + this.declaration = declaration; + + if ( declarations == null ) + { + this.requiredDeclarations = PredicateConstraint.EMPTY_DECLARATIONS; + } + else + { + this.requiredDeclarations = declarations; + } + } + + public PredicateExpressionConstraint getExpression() + { + return this.booleanExpression; + } + + public Declaration[] getRequiredDeclarations() + { + return requiredDeclarations; + } + + public Declaration getDeclaration() + { + return this.declaration; + } + + public boolean isAllowed(Object object, + FactHandle handle, + Tuple tuple) + { + return this.booleanExpression.isAllowed( object, + handle, + this.declaration, + this.requiredDeclarations, + tuple ); + } + +}; Deleted: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/BooleanExpressionConstraint.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/BooleanExpressionConstraint.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/BooleanExpressionConstraint.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -1,14 +0,0 @@ -package org.drools.spi; - -import org.drools.FactHandle; -import org.drools.rule.Declaration; - -public interface BooleanExpressionConstraint -{ - - public boolean isAllowed(Object object, - FactHandle handle, - Declaration declaration, - Declaration[] declarations, - Tuple tuple); -} Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/ClassObjectType.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/ClassObjectType.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/ClassObjectType.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -111,6 +111,16 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /** + * Produce the hash of this object. + * + * @return The hash. + */ + public int hashCode() + { + return getType( ).hashCode( ); + } + + /** * Determine if another object is equal to this. * * @param object The object to test. @@ -131,18 +141,9 @@ } return this.objectTypeClass == ( ( ClassObjectType ) object ).objectTypeClass; - } + } + - /** - * Produce the hash of this object. - * - * @return The hash. - */ - public int hashCode() - { - return getType( ).hashCode( ); - } - public String toString() { return getType( ).getName( ); Copied: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/PredicateExpressionConstraint.java (from rev 1776, trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/BooleanExpressionConstraint.java) =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/BooleanExpressionConstraint.java 2005-12-10 17:37:55 UTC (rev 1776) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/PredicateExpressionConstraint.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -0,0 +1,14 @@ +package org.drools.spi; + +import org.drools.FactHandle; +import org.drools.rule.Declaration; + +public interface PredicateExpressionConstraint +{ + + public boolean isAllowed(Object object, + FactHandle handle, + Declaration declaration, + Declaration[] declarations, + Tuple tuple); +} Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/Tuple.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/Tuple.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/spi/Tuple.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -62,7 +62,6 @@ * @return The currently bound <code>Object</code> value. */ Object get(int column); - Object get(Declaration declaration); Modified: trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -4,10 +4,10 @@ import org.drools.DroolsTestCase; import org.drools.FactHandle; import org.drools.RetractionException; -import org.drools.rule.BooleanConstraint; +import org.drools.rule.PredicateConstraint; import org.drools.rule.Declaration; import org.drools.rule.Rule; -import org.drools.spi.BooleanExpressionConstraint; +import org.drools.spi.PredicateExpressionConstraint; import org.drools.spi.ClassObjectType; import org.drools.spi.Extractor; import org.drools.spi.ObjectType; @@ -440,7 +440,7 @@ } /** - * While all the previous tests work with the DefaultJoinNodeBinder, ie joins always succeed. This tests joins with BooleanExpressionConstraint. We only use one constraint, as Constraints are tested more thorougly else where, likewise for this + * While all the previous tests work with the DefaultJoinNodeBinder, ie joins always succeed. This tests joins with PredicateExpressionConstraint. We only use one constraint, as Constraints are tested more thorougly else where, likewise for this * reason we use a very simple constraint. * * @throws Exception @@ -473,7 +473,7 @@ 9 ); /* create the boolean expression check */ - BooleanExpressionConstraint checkString = new BooleanExpressionConstraint() { + PredicateExpressionConstraint checkString = new PredicateExpressionConstraint() { public boolean isAllowed(Object object, FactHandle handle, Declaration declaration, // ?string1 @@ -489,7 +489,7 @@ }; /* create the constraint */ - BooleanConstraint constraint = new BooleanConstraint( checkString, + PredicateConstraint constraint = new PredicateConstraint( checkString, string1Declaration, new Declaration[]{string2Declaration} ); Modified: trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -5,10 +5,10 @@ import org.drools.FactException; import org.drools.FactHandle; import org.drools.RetractionException; -import org.drools.rule.BooleanConstraint; +import org.drools.rule.PredicateConstraint; import org.drools.rule.Declaration; import org.drools.rule.Rule; -import org.drools.spi.BooleanExpressionConstraint; +import org.drools.spi.PredicateExpressionConstraint; import org.drools.spi.ClassObjectType; import org.drools.spi.Extractor; import org.drools.spi.ObjectType; @@ -62,7 +62,7 @@ 9 ); /* create the boolean expression check */ - BooleanExpressionConstraint checkString = new BooleanExpressionConstraint() { + PredicateExpressionConstraint checkString = new PredicateExpressionConstraint() { public boolean isAllowed(Object object, FactHandle handle, Declaration declaration, // ?string1 @@ -78,7 +78,7 @@ }; /* create the constraint */ - BooleanConstraint constraint = new BooleanConstraint( checkString, + PredicateConstraint constraint = new PredicateConstraint( checkString, string1Declaration, new Declaration[]{string2Declaration} ); Modified: trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/ConstraintTest.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/ConstraintTest.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/ConstraintTest.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -5,7 +5,7 @@ import org.drools.FactHandle; import org.drools.reteoo.InstrumentedReteTuple; import org.drools.reteoo.InstrumentedWorkingMemoryImpl; -import org.drools.spi.BooleanExpressionConstraint; +import org.drools.spi.PredicateExpressionConstraint; import org.drools.spi.ClassObjectType; import org.drools.spi.ConstraintComparator; import org.drools.spi.Extractor; @@ -218,7 +218,7 @@ * * </pre> */ - public void testBooleanExpressionConstraint() + public void testPredicateExpressionConstraint() { InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( ); @@ -248,7 +248,7 @@ priceOfCheeseExtractor, 1 ); - BooleanExpressionConstraint isDoubleThePrice = new BooleanExpressionConstraint( ) { + PredicateExpressionConstraint isDoubleThePrice = new PredicateExpressionConstraint( ) { public boolean isAllowed(Object object, FactHandle handle, Declaration declaration, // ?price2 @@ -262,7 +262,7 @@ } }; - BooleanConstraint constraint1 = new BooleanConstraint( isDoubleThePrice, + PredicateConstraint constraint1 = new PredicateConstraint( isDoubleThePrice, price2Declaration, new Declaration[]{price1Declaration} ); Modified: trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java 2005-12-18 23:11:49 UTC (rev 1853) +++ trunk/labs/jbossrules/drools-core/src/test/java/org/drools/rule/LogicTransformerTest.java 2005-12-19 00:06:15 UTC (rev 1854) @@ -234,9 +234,9 @@ /** * This data structure is not valid - * (Exist (OR (A B) + * (Exists (OR (A B) * <pre> - * Exist + * Exists * | * or * / \ @@ -249,7 +249,7 @@ String a = "a"; String b = "b"; - Exist exist = new Exist( ); + Exists exist = new Exists( ); Or or = new Or( ); exist.addChild( or ); @@ -318,7 +318,7 @@ * / | \ / \ | * a And d e Or h * / \ / \ - * b Not f Exist + * b Not f Exists * | | * Not g * | @@ -336,7 +336,7 @@ * / | | \ / \ | * a b d Not And And i * | / \ / | - * Not e f e Exist + * Not e f e Exists * | | * c g * </pre> @@ -379,7 +379,7 @@ and3.addChild( e ); Or or1 = new Or( ); and3.addChild( or1 ); - Exist exist1 = new Exist( ); + Exists exist1 = new Exists( ); exist1.addChild( g ); or1.addChild( exist1 ); or1.addChild( h ); |