[jboss-svn-commits] JBL Code SVN: r26141 - in labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo: builder and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Apr 19 10:38:51 EDT 2009


Author: dsotty
Date: 2009-04-19 10:38:51 -0400 (Sun, 19 Apr 2009)
New Revision: 26141

Added:
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/AndOperatorInstaller.java
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImperfectReteooBuilder.java
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImplicationObjectSource.java
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OperatorInstaller.java
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OrOperatorInstaller.java
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectGroupElementBuilder.java
   labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectReteooRuleBuilder.java
Log:
inter-pattern operators

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/AndOperatorInstaller.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/AndOperatorInstaller.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/AndOperatorInstaller.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,45 @@
+package org.drools.reteoo;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+
+import org.drools.RuntimeDroolsException;
+import org.drools.common.BaseNode;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.PropagationContextImpl;
+import org.drools.common.RuleBasePartitionId;
+import org.drools.degrees.factory.IDegreeFactory;
+import org.drools.reteoo.AccumulateNode.AccumulateMemory;
+import org.drools.reteoo.CollectNode.CollectMemory;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.Tuple;
+import org.drools.util.Iterator;
+
+public class AndOperatorInstaller 
+	extends OperatorInstaller 
+	implements LeftTupleSink {
+	
+	protected LeftTupleSource source;
+
+	
+	public AndOperatorInstaller(LeftTupleSource source, int arity, int id, RuleBasePartitionId partitionId, boolean isPartitionEnabled) {
+		super(source, arity, id, partitionId, isPartitionEnabled);		
+	}
+	
+	
+	
+
+
+
+
+	
+	
+	
+	
+	
+
+	
+	
+}

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImperfectReteooBuilder.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImperfectReteooBuilder.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImperfectReteooBuilder.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,38 @@
+package org.drools.reteoo;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.drools.RuleBaseConfiguration.EventProcessingMode;
+import org.drools.common.BaseNode;
+import org.drools.common.InternalRuleBase;
+import org.drools.reteoo.ReteooBuilder.IdGenerator;
+import org.drools.reteoo.builder.BuildContext;
+import org.drools.reteoo.builder.ImperfectReteooRuleBuilder;
+import org.drools.reteoo.builder.ReteooRuleBuilder;
+import org.drools.rule.GroupElement;
+import org.drools.rule.InvalidPatternException;
+import org.drools.rule.Rule;
+import org.drools.time.TemporalDependencyMatrix;
+
+public class ImperfectReteooBuilder extends ReteooBuilder {
+
+	
+		
+	public ImperfectReteooBuilder(final InternalRuleBase ruleBase) {
+        this.setRuleBase(ruleBase);
+        setRules(new HashMap<Rule, BaseNode[]>());
+
+        //Set to 1 as Rete node is set to 0
+        setIdGenerator(new IdGenerator( 1 ));
+        setRuleBuilder(new ImperfectReteooRuleBuilder());
+    }
+	
+	
+	
+	
+	
+	
+
+}

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImplicationObjectSource.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImplicationObjectSource.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/ImplicationObjectSource.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,10 @@
+package org.drools.reteoo;
+
+import org.drools.common.BaseNode;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.spi.PropagationContext;
+
+public class ImplicationObjectSource extends AlphaNode {
+
+	
+}

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OperatorInstaller.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OperatorInstaller.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OperatorInstaller.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,181 @@
+package org.drools.reteoo;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Collections;
+
+
+import org.drools.RuntimeDroolsException;
+import org.drools.common.BaseNode;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.PropagationContextImpl;
+import org.drools.common.RuleBasePartitionId;
+import org.drools.degrees.factory.IDegreeFactory;
+import org.drools.degrees.operators.IDegreeCombiner;
+import org.drools.degrees.operators.IMergeStrategy;
+import org.drools.degrees.operators.INullHandlingStrategy;
+import org.drools.reteoo.AccumulateNode.AccumulateMemory;
+import org.drools.reteoo.CollectNode.CollectMemory;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.Tuple;
+import org.drools.util.Iterator;
+
+public abstract class OperatorInstaller 
+	extends LeftTupleSource 
+	implements LeftTupleSink {
+	
+	protected LeftTupleSource source;
+	
+	protected int arity;
+	
+	private CompositeEvaluationTemplate template;
+
+	public OperatorInstaller(LeftTupleSource source, int arity, int id, RuleBasePartitionId partitionId, boolean isPartitionEnabled) {
+		super(id, partitionId, isPartitionEnabled);
+		this.arity = arity;
+		this.source = source;
+		
+	}
+	
+	public EvaluationTemplate buildEvaluationTemplate(IDegreeCombiner operator, IMergeStrategy mergeStrat, INullHandlingStrategy nullStrat) {
+		template = new CompositeEvaluationTemplate( this.getId(),
+													new DynamicConstraintKey(operator.getName()),
+													null,
+													getArity(),
+													operator,
+													mergeStrat,
+													nullStrat
+													);
+			
+		return template;
+	}
+	
+	public CompositeEvaluationTemplate getTemplate() {
+		return template;
+	}
+	
+	
+	public void updateSink(LeftTupleSink sink, PropagationContext context,
+			InternalWorkingMemory workingMemory) {
+		//DO nothing as this is only a transit node?
+	}
+	
+	
+	protected int getArity() {
+		return arity;
+	}
+
+	public void attach() {
+		this.source.addTupleSink(this);
+	}
+
+	public void networkUpdated() {
+		this.source.networkUpdated();
+	}
+
+		
+	public void attach(InternalWorkingMemory[] workingMemories) {
+		for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
+            final InternalWorkingMemory workingMemory = workingMemories[i];
+            final PropagationContext propagationContext = new PropagationContextImpl( 
+            		workingMemory.getNextPropagationIdCounter(),
+            		PropagationContext.RULE_ADDITION,
+            		null,
+            		null,
+            		null );
+                                   
+            this.source.updateSink( this,
+                                    propagationContext,
+                                    workingMemory );
+        }
+	}
+
+	
+	protected void doRemove(RuleRemovalContext context, ReteooBuilder builder,
+			BaseNode node, InternalWorkingMemory[] workingMemories) {
+		
+		context.visitTupleSource( this );
+        if ( !node.isInUse() ) {
+            removeTupleSink( (LeftTupleSink) node );
+        }
+                        
+        if ( !context.alreadyVisited( this.source ) ) {
+            this.source.remove( context,
+            		builder,
+            		this,
+            		workingMemories );
+        }
+		
+	}
+
+	
+	
+	
+	
+	
+	
+	
+	public void assertLeftTuple(LeftTuple leftTuple,
+			PropagationContext context, InternalWorkingMemory workingMemory) {
+		throw new RuntimeDroolsException("Operator nodes are supported only with imperfect reasoning");
+	}
+	
+	
+	public short getType() {
+		return NodeTypeEnums.Imperfect_OperatorNode;
+	}
+
+	
+
+	
+
+		
+
+	public void assertLeftTuple(ImperfectLeftTuple leftTuple,
+			PropagationContext context, InternalWorkingMemory workingMemory,
+			IDegreeFactory factory) {
+		EvalRecord record = leftTuple.getRecord();
+		int M = record.getOperands().length;
+		int N = getArity();
+						
+		record.replace((CompositeEvaluation) getTemplate().spawn(N),N);
+		
+		
+		this.getSinkPropagator().propagateAssertLeftTuple(
+				leftTuple, 
+				context, 
+				workingMemory, 
+				factory,
+				record,
+				false);
+	}
+
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	public boolean isLeftTupleMemoryEnabled() {	
+		return false;
+	}
+			
+	public void retractLeftTuple(LeftTuple leftTuple,
+			PropagationContext context, InternalWorkingMemory workingMemory) {
+		//TODO
+		//Nothing to do yet
+	}
+
+	public void setLeftTupleMemoryEnabled(boolean tupleMemoryEnabled) {
+		//TODO
+		//Nothing to do, no memory here. YET		
+	}
+
+	
+	
+}

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OrOperatorInstaller.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OrOperatorInstaller.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/OrOperatorInstaller.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,64 @@
+package org.drools.reteoo;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+
+import org.drools.RuntimeDroolsException;
+import org.drools.common.BaseNode;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.PropagationContextImpl;
+import org.drools.common.RuleBasePartitionId;
+import org.drools.degrees.factory.IDegreeFactory;
+import org.drools.reteoo.AccumulateNode.AccumulateMemory;
+import org.drools.reteoo.CollectNode.CollectMemory;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.Tuple;
+import org.drools.util.Iterator;
+
+public class OrOperatorInstaller 
+	extends OperatorInstaller 
+	implements LeftTupleSink {
+	
+	protected LeftTupleSource source;
+
+	
+	public OrOperatorInstaller(LeftTupleSource source, int arity, int id, RuleBasePartitionId partitionId, boolean isPartitionEnabled) {
+		super(source, arity, id, partitionId, isPartitionEnabled);		
+	}
+	
+	
+	
+//
+//	public void assertLeftTuple(ImperfectLeftTuple leftTuple,
+//			PropagationContext context, InternalWorkingMemory workingMemory,
+//			IDegreeFactory factory) {
+//		EvalRecord record = leftTuple.getRecord();
+//			record.setOperator(factory.getOrOperator());
+//			record.setNodeId(this.getId());
+//		
+//		this.getSinkPropagator().propagateAssertLeftTuple(
+//				leftTuple, 
+//				context, 
+//				workingMemory, 
+//				factory,
+//				record,
+//				false);
+//	}
+
+	
+	
+	
+
+	
+
+	
+	
+	
+	
+	
+
+	
+	
+}

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectGroupElementBuilder.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectGroupElementBuilder.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectGroupElementBuilder.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,416 @@
+package org.drools.reteoo.builder;
+
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.common.BetaConstraints;
+import org.drools.common.TupleStartEqualsConstraint;
+import org.drools.degrees.factory.IDegreeFactory;
+import org.drools.degrees.operators.IDegreeCombiner;
+import org.drools.reteoo.AndOperatorInstaller;
+import org.drools.reteoo.ExistsNode;
+import org.drools.reteoo.ImperfectRuleBase;
+import org.drools.reteoo.JoinNode;
+import org.drools.reteoo.LeftInputAdapterNode;
+import org.drools.reteoo.LeftTupleSource;
+import org.drools.reteoo.NotNode;
+import org.drools.reteoo.ObjectSource;
+import org.drools.reteoo.OperatorInstaller;
+import org.drools.reteoo.OrOperatorInstaller;
+import org.drools.reteoo.RightInputAdapterNode;
+import org.drools.rule.Behavior;
+import org.drools.rule.GroupElement;
+import org.drools.rule.RuleConditionElement;
+
+public class ImperfectGroupElementBuilder extends GroupElementBuilder implements
+		ReteooComponentBuilder {
+	
+	
+	public ImperfectGroupElementBuilder() {
+		
+        this.getGeBuilders().put( GroupElement.AND,
+                             new ImperfectAndBuilder() );
+        this.getGeBuilders().put( GroupElement.OR,
+                             new ImperfectOrBuilder() );
+        this.getGeBuilders().put( GroupElement.NOT,
+                             new ImperfectNotBuilder() );
+        this.getGeBuilders().put( GroupElement.EXISTS,
+                             new ImperfectExistsBuilder() );
+    }
+
+	
+	
+	
+	
+	
+	
+	
+		
+	
+
+	private static class ImperfectAndBuilder
+        implements
+        ReteooComponentBuilder {
+
+        /**
+         * @inheritDoc
+         * 
+         * And group elements just iterate over their children
+         * selecting and calling the build procedure for each one
+         * 
+         */
+        public void build(final BuildContext context,
+                          final BuildUtils utils,
+                          final RuleConditionElement rce) {
+
+        	
+        	
+            final GroupElement ge = (GroupElement) rce;
+
+            // iterate over each child and build it
+            for ( final Iterator it = ge.getChildren().iterator(); it.hasNext(); ) {
+
+                final RuleConditionElement child = (RuleConditionElement) it.next();
+
+                final ReteooComponentBuilder builder = utils.getBuilderFor( child );
+
+                builder.build( context,
+                               utils,
+                               child );
+
+                // if a previous object source was bound, but no tuple source
+                if ( context.getObjectSource() != null && context.getTupleSource() == null ) {
+                    // adapt it to a Tuple source                    
+                    context.setTupleSource( (LeftTupleSource) utils.attachNode( context,
+                                                                                new LeftInputAdapterNode( context.getNextId(),
+                                                                                                          context.getObjectSource(),
+                                                                                                          context ) ) );
+
+                    context.setObjectSource( null );
+                }
+
+                // if there was a previous tuple source, then a join node is needed
+                if ( context.getObjectSource() != null && context.getTupleSource() != null ) {
+                    // so, create the tuple source and clean up the constraints and object source
+                    final BetaConstraints betaConstraints = utils.createBetaNodeConstraint( context,
+                                                                                            context.getBetaconstraints(),
+                                                                                            false );
+
+                    Behavior[] behaviors = createBehaviorArray( context );
+
+                    context.setTupleSource( (LeftTupleSource) utils.attachNode( context,
+                    														    new JoinNode( context.getNextId(),
+                    														    		context.getTupleSource(),
+                    														    		context.getObjectSource(),
+                    														    		betaConstraints,
+                    														    		behaviors,
+                    														    		context ) ) );
+                    context.setBetaconstraints( null );
+                    context.setObjectSource( null );
+                }
+            }
+            
+            
+            OperatorInstaller instNode = new AndOperatorInstaller(
+														context.getTupleSource(),
+														ge.getChildren().size(),
+														context.getNextId(),
+														context.getPartitionId(),
+														context.getRuleBase().getConfiguration().isMultithreadEvaluation());
+            IDegreeFactory factory = ((ImperfectRuleBase) context.getRuleBase()).getDegreeFactory();
+            
+            instNode.buildEvaluationTemplate(factory.getAndOperator(),factory.getMergeStrategy(),factory.getNullHandlingStrategy());
+            
+            context.setTupleSource((LeftTupleSource) utils.attachNode(
+            							context,
+            							instNode
+            							));
+            
+            
+            
+        }
+
+        public boolean requiresLeftActivation(final BuildUtils utils,
+                                              final RuleConditionElement rce) {
+            final GroupElement and = (GroupElement) rce;
+
+            // need to check this because in the case of an empty rule, the root AND
+            // will have no child
+            if ( and.getChildren().isEmpty() ) {
+                return true;
+            }
+
+            final RuleConditionElement child = (RuleConditionElement) and.getChildren().get( 0 );
+            final ReteooComponentBuilder builder = utils.getBuilderFor( child );
+
+            return builder.requiresLeftActivation( utils,
+                                                   child );
+        }
+    }
+
+    private static class ImperfectOrBuilder
+        implements
+        ReteooComponentBuilder {
+
+       
+    	
+    	 public void build(final BuildContext context,
+                 final BuildUtils utils,
+                 final RuleConditionElement rce) {
+
+	
+    		 final GroupElement ge = (GroupElement) rce;
+
+    		 // 	iterate over each child and build it
+    		 for ( final Iterator it = ge.getChildren().iterator(); it.hasNext(); ) {
+    			 
+    			 final RuleConditionElement child = (RuleConditionElement) it.next();
+
+    			 final ReteooComponentBuilder builder = utils.getBuilderFor( child );
+
+    			 builder.build( context,
+    					 utils,
+    					 child );
+
+       // 	if a previous object source was bound, but no tuple source
+    			 if ( context.getObjectSource() != null && context.getTupleSource() == null ) {
+           // 	adapt it to a Tuple source                    
+    				 context.setTupleSource( (LeftTupleSource) utils.attachNode( context,
+                                                                       new LeftInputAdapterNode( context.getNextId(),
+                                                                                                 context.getObjectSource(),
+                                                                                                 context ) ) );
+
+    				 context.setObjectSource( null );
+    			 }
+
+    			 // if there was a previous tuple source, then a join node is needed
+    			 if ( context.getObjectSource() != null && context.getTupleSource() != null ) {
+           // 	so, create the tuple source and clean up the constraints and object source
+    				 final BetaConstraints betaConstraints = utils.createBetaNodeConstraint( context,
+    						 context.getBetaconstraints(),
+    						 false );
+
+    				 Behavior[] behaviors = createBehaviorArray( context );
+
+    				 context.setTupleSource( (LeftTupleSource) utils.attachNode( context,
+    						 new JoinNode( context.getNextId(),
+    								 context.getTupleSource(),
+    								 context.getObjectSource(),
+    								 betaConstraints,
+    								 behaviors,
+    								 context ) ) );
+    				 context.setBetaconstraints( null );
+    				 context.setObjectSource( null );
+    			 }
+    		 }
+    		     		              
+    		 OperatorInstaller instNode = new OrOperatorInstaller(
+						context.getTupleSource(),
+						ge.getChildren().size(),
+						context.getNextId(),
+						context.getPartitionId(),
+						context.getRuleBase().getConfiguration().isMultithreadEvaluation());
+    		 
+    		 IDegreeFactory factory = ((ImperfectRuleBase) context.getRuleBase()).getDegreeFactory();
+
+    		 instNode.buildEvaluationTemplate(factory.getOrOperator(),factory.getMergeStrategy(),factory.getNullHandlingStrategy());
+    		 
+    		 context.setTupleSource((LeftTupleSource) utils.attachNode(
+						context,
+						instNode
+						));
+             
+    	 }
+
+    	 public boolean requiresLeftActivation(final BuildUtils utils,
+    			 final RuleConditionElement rce) {
+    		 final GroupElement and = (GroupElement) rce;
+
+    		 // need to check this because in the case of an empty rule, the root AND
+   // 	will have no child
+    		 if ( and.getChildren().isEmpty() ) {
+    			 return true;
+    		 }
+
+    		 final RuleConditionElement child = (RuleConditionElement) and.getChildren().get( 0 );
+    		 final ReteooComponentBuilder builder = utils.getBuilderFor( child );
+
+    		 return builder.requiresLeftActivation( utils,
+                                          child );
+    	 }
+    	
+    }
+
+    private static class ImperfectNotBuilder
+        implements
+        ReteooComponentBuilder {
+
+        /**
+         * @inheritDoc
+         * 
+         * Not must verify what is the class of its child:
+         * 
+         * If it is a pattern, a simple NotNode is added to the rulebase
+         * If it is a group element, than a subnetwork must be created
+         */
+        public void build(final BuildContext context,
+                          final BuildUtils utils,
+                          final RuleConditionElement rce) {
+            final GroupElement not = (GroupElement) rce;
+
+            // NOT must save some context info to restore it later
+            final int currentPatternIndex = context.getCurrentPatternOffset();
+            final LeftTupleSource tupleSource = context.getTupleSource();
+
+            // get child
+            final RuleConditionElement child = (RuleConditionElement) not.getChildren().get( 0 );
+
+            // get builder for child
+            final ReteooComponentBuilder builder = utils.getBuilderFor( child );
+
+            // builds the child
+            builder.build( context,
+                           utils,
+                           child );
+
+            // if it is a subnetwork
+            if ( context.getObjectSource() == null && context.getTupleSource() != null ) {
+
+                // attach right input adapter node to convert tuple source into an object source
+                context.setObjectSource( (ObjectSource) utils.attachNode( context,
+                                                                          new RightInputAdapterNode( context.getNextId(),
+                                                                                                     context.getTupleSource(),
+                                                                                                     context ) ) );
+
+                // restore tuple source from before the start of the sub network
+                context.setTupleSource( tupleSource );
+
+                // create a tuple start equals constraint and set it in the context
+                final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
+                final List predicates = new ArrayList();
+                predicates.add( constraint );
+                context.setBetaconstraints( predicates );
+
+            }
+
+            final BetaConstraints betaConstraints = utils.createBetaNodeConstraint( context,
+                                                                                    context.getBetaconstraints(),
+                                                                                    false );
+            Behavior[] behaviors = createBehaviorArray( context );
+
+            // then attach the NOT node. It will work both as a simple not node
+            // or as subnetwork join node as the context was set appropriatelly
+            // in each case
+            context.setTupleSource( (LeftTupleSource) utils.attachNode( context,
+                                                                        new NotNode( context.getNextId(),
+                                                                                     context.getTupleSource(),
+                                                                                     context.getObjectSource(),
+                                                                                     betaConstraints,
+                                                                                     behaviors,
+                                                                                     context ) ) );
+            context.setBetaconstraints( null );
+            context.setObjectSource( null );
+
+            // restore pattern index
+            context.setCurrentPatternOffset( currentPatternIndex );
+        }
+
+        public boolean requiresLeftActivation(final BuildUtils utils,
+                                              final RuleConditionElement rce) {
+            return true;
+        }
+    }
+
+    private static class ImperfectExistsBuilder
+        implements
+        ReteooComponentBuilder {
+
+        /**
+         * @inheritDoc
+         * 
+         * Exists must verify what is the class of its child:
+         * 
+         * If it is a pattern, a simple ExistsNode is added to the rulebase
+         * If it is a group element, than a subnetwork must be created
+         */
+        public void build(final BuildContext context,
+                          final BuildUtils utils,
+                          final RuleConditionElement rce) {
+            final GroupElement exists = (GroupElement) rce;
+
+            // EXISTS must save some context info to restore it later
+            final int currentPatternIndex = context.getCurrentPatternOffset();
+            final LeftTupleSource tupleSource = context.getTupleSource();
+
+            // get child
+            final RuleConditionElement child = (RuleConditionElement) exists.getChildren().get( 0 );
+
+            // get builder for child
+            final ReteooComponentBuilder builder = utils.getBuilderFor( child );
+
+            // builds the child
+            builder.build( context,
+                           utils,
+                           child );
+
+            // if it is a subnetwork
+            if ( context.getObjectSource() == null && context.getTupleSource() != null ) {
+
+                // attach right input adapter node to convert tuple source into an object source
+                context.setObjectSource( (ObjectSource) utils.attachNode( context,
+                                                                          new RightInputAdapterNode( context.getNextId(),
+                                                                                                     context.getTupleSource(),
+                                                                                                     context ) ) );
+
+                // restore tuple source from before the start of the sub network
+                context.setTupleSource( tupleSource );
+
+                // create a tuple start equals constraint and set it in the context
+                final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
+                final List predicates = new ArrayList();
+                predicates.add( constraint );
+                context.setBetaconstraints( predicates );
+
+            }
+
+            final BetaConstraints betaConstraints = utils.createBetaNodeConstraint( context,
+                                                                                    context.getBetaconstraints(),
+                                                                                    false );
+
+            Behavior[] behaviors = createBehaviorArray( context );
+
+            // then attach the EXISTS node. It will work both as a simple exists node
+            // or as subnetwork join node as the context was set appropriatelly
+            // in each case
+            context.setTupleSource( (LeftTupleSource) utils.attachNode( context,
+                                                                        new ExistsNode( context.getNextId(),
+                                                                                        context.getTupleSource(),
+                                                                                        context.getObjectSource(),
+                                                                                        betaConstraints,
+                                                                                        behaviors,
+                                                                                        context ) ) );
+            context.setBetaconstraints( null );
+            context.setObjectSource( null );
+
+            // restore pattern index
+            context.setCurrentPatternOffset( currentPatternIndex );
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public boolean requiresLeftActivation(final BuildUtils utils,
+                                              final RuleConditionElement rce) {
+            return true;
+        }
+    }
+
+	
+	
+
+}

Added: labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectReteooRuleBuilder.java
===================================================================
--- labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectReteooRuleBuilder.java	                        (rev 0)
+++ labs/jbossrules/branches/DroolsChance/drools-core/src/main/java/org/drools/reteoo/builder/ImperfectReteooRuleBuilder.java	2009-04-19 14:38:51 UTC (rev 26141)
@@ -0,0 +1,129 @@
+package org.drools.reteoo.builder;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.RuleBase;
+import org.drools.RuntimeDroolsException;
+import org.drools.RuleBaseConfiguration.EventProcessingMode;
+import org.drools.common.BaseNode;
+import org.drools.common.InternalRuleBase;
+import org.drools.degrees.factory.IDegreeFactory;
+import org.drools.reteoo.ImperfectRuleBase;
+import org.drools.reteoo.ModusPonensNode;
+import org.drools.reteoo.QueryTerminalNode;
+import org.drools.reteoo.ReteooBuilder;
+import org.drools.reteoo.RuleTerminalNode;
+import org.drools.reteoo.TerminalNode;
+import org.drools.rule.Accumulate;
+import org.drools.rule.Behavior;
+import org.drools.rule.Collect;
+import org.drools.rule.EntryPoint;
+import org.drools.rule.EvalCondition;
+import org.drools.rule.Forall;
+import org.drools.rule.From;
+import org.drools.rule.GroupElement;
+import org.drools.rule.InvalidPatternException;
+import org.drools.rule.Pattern;
+import org.drools.rule.Query;
+import org.drools.rule.Rule;
+import org.drools.rule.RuleConditionElement;
+import org.drools.time.TemporalDependencyMatrix;
+
+public class ImperfectReteooRuleBuilder extends ReteooRuleBuilder {
+	
+	
+	
+	public ImperfectReteooRuleBuilder() {
+        this.setUtils(new BuildUtils());
+
+        this.getUtils().addBuilder( GroupElement.class,
+                               new ImperfectGroupElementBuilder() );
+        this.getUtils().addBuilder( Pattern.class,
+                               new PatternBuilder() );
+        this.getUtils().addBuilder( EvalCondition.class,
+                               new EvalBuilder() );
+        this.getUtils().addBuilder( From.class,
+                               new FromBuilder() );
+        this.getUtils().addBuilder( Collect.class,
+                               new CollectBuilder() );
+        this.getUtils().addBuilder( Accumulate.class,
+                               new AccumulateBuilder() );
+        this.getUtils().addBuilder( Forall.class,
+                               new ForallBuilder() );
+        this.getUtils().addBuilder( EntryPoint.class,
+                               new EntryPointBuilder() );
+    }
+	
+	
+	/**
+	 * Rules are no longer SPLIT! Instead, a full tree is computed
+	 */
+	public List<TerminalNode> addRule(final Rule rule,
+            final InternalRuleBase rulebase,
+            final ReteooBuilder.IdGenerator idGenerator) throws InvalidPatternException {
+
+		
+		// the list of terminal nodes
+		final List<TerminalNode> nodes = new ArrayList<TerminalNode>();
+
+		//GroupElement ruleRoot = (GroupElement) rule.getLhs().getChildren().get(0);
+		GroupElement ruleRoot = rule.getLhs();
+		
+			
+
+			final BuildContext context = new BuildContext( rulebase,
+					idGenerator );
+				context.setRule( rule );
+
+				// if running in STREAM mode, calculate temporal distance for events
+				if( EventProcessingMode.STREAM.equals( rulebase.getConfiguration().getEventProcessingMode() ) ) {
+					TemporalDependencyMatrix temporal = getUtils().calculateTemporalDistance( ruleRoot );
+					context.setTemporalDistance( temporal );
+				}
+
+				if ( rulebase.getConfiguration().isSequential() ) {
+					context.setTupleMemoryEnabled( false );
+					context.setObjectTypeNodeMemoryEnabled( false );
+					context.setTerminalNodeMemoryEnabled( false );
+					context.setAlphaNodeMemoryAllowed( false );
+				} else {
+					context.setTupleMemoryEnabled( true );
+					context.setObjectTypeNodeMemoryEnabled( true );
+					context.setTerminalNodeMemoryEnabled( true );
+					context.setAlphaNodeMemoryAllowed( true );                
+				}
+
+			// 	adds subrule
+			final TerminalNode node = this.addSubRule( context,
+                                           ruleRoot,
+                                           rule );
+
+			// adds the terminal node to the list of terminal nodes
+			nodes.add( node );
+
+			return nodes;
+	}
+	
+	
+	
+	
+	
+	
+
+
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+}




More information about the jboss-svn-commits mailing list