package org.drools.reteoo;
/*
* Copyright 2005 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.
*/
import org.drools.RuleBaseConfiguration;
import org.drools.common.BaseNode;
import org.drools.common.InternalWorkingMemory;
import org.drools.common.NodeMemory;
import org.drools.common.PropagationContextImpl;
import org.drools.reteoo.builder.BuildContext;
import org.drools.rule.EvalCondition;
import org.drools.spi.PropagationContext;
import org.drools.util.Iterator;
import org.drools.util.TupleHashTable;
/**
* Node which filters <code>ReteTuple</code>s.
*
* <p>
* Using a semantic <code>Test</code>, this node may allow or disallow
* <code>Tuples</code> to proceed further through the Rete-OO network.
* </p>
*
* @see EvalConditionNode
* @see Eval
* @see ReteTuple
*
* @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
* @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
*/
public class EvalConditionNode extends TupleSource
implements
TupleSinkNode,
NodeMemory {
// ------------------------------------------------------------
// Instance members
// ------------------------------------------------------------
/**
*
*/
private static final long serialVersionUID = 400L;
/** The semantic <code>Test</code>. */
private final EvalCondition condition;
/** The source of incoming <code>Tuples</code>. */
private final TupleSource tupleSource;
protected boolean tupleMemoryEnabled;
private TupleSinkNode previousTupleSinkNode;
private TupleSinkNode nextTupleSinkNode;
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
/**
* Construct.
*
* @param rule
* The rule
* @param tupleSource
* The source of incoming <code>Tuples</code>.
* @param eval
*/
public EvalConditionNode(final int id,
final TupleSource tupleSource,
final EvalCondition eval,
final BuildContext context) {
super( id );
this.condition = eval;
this.tupleSource = tupleSource;
this.tupleMemoryEnabled = context.isTupleMemoryEnabled();
}
/**
* Attaches this node into the network.
*/
public void attach() {
this.tupleSource.addTupleSink( this );
}
public void attach(final InternalWorkingMemory[] workingMemories) {
attach();
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 );
this.tupleSource.updateSink( this,
propagationContext,
workingMemory );
}
}
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
* Retrieve the <code>Test</code> associated with this node.
*
* @return The <code>Test</code>.
*/
public EvalCondition getCondition() {
return this.condition;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// org.drools.reteoo.impl.TupleSink
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Assert a new <code>Tuple</code>.
*
* @param tuple
* The <code>Tuple</code> being asserted.
* @param workingMemory
* The working memory seesion.
* @throws AssertionException
* If an error occurs while asserting.
*/
public void assertTuple(final ReteTuple tuple,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
final boolean allowed = this.condition.isAllowed( tuple,
workingMemory );
if ( allowed ) {
if ( this.tupleMemoryEnabled ) {
final TupleHashTable memory = (TupleHashTable) workingMemory.getNodeMemory( this );
memory.add( tuple );
}
this.sink.propagateAssertTuple( tuple,
context,
workingMemory );
}
}
public void retractTuple(final ReteTuple tuple,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
final TupleHashTable memory = (TupleHashTable) workingMemory.getNodeMemory( this );
// can we improve that?
final ReteTuple memTuple = memory.remove( tuple );
if ( memTuple != null ) {
this.sink.propagateRetractTuple( memTuple,
context,
workingMemory );
}
}
/**
* Produce a debug string.
*
* @return The debug string.
*/
public String toString() {
return "[EvalConditionNode: cond=" + this.condition + "]";
}
public int hashCode() {
return this.tupleSource.hashCode() ^ this.condition.hashCode();
}
public boolean equals(final Object object) {
if ( this == object ) {
return true;
}
if ( object == null || object.getClass() != EvalConditionNode.class ) {
return false;
}
final EvalConditionNode other = (EvalConditionNode) object;
return this.tupleSource.equals( other.tupleSource ) && this.condition.equals( other.condition );
}
public Object createMemory(final RuleBaseConfiguration config) {
return new TupleHashTable();
}
/* (non-Javadoc)
* @see org.drools.reteoo.BaseNode#updateNewNode(org.drools.reteoo.WorkingMemoryImpl, org.drools.spi.PropagationContext)
*/
public void updateSink(final TupleSink sink,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
final TupleHashTable memory = (TupleHashTable) workingMemory.getNodeMemory( this );
final Iterator it = memory.iterator();
for ( ReteTuple tuple = (ReteTuple) it.next(); tuple != null; tuple = (ReteTuple) it.next() ) {
sink.assertTuple( tuple,
context,
workingMemory );
}
}
public void remove(final BaseNode node,
final InternalWorkingMemory[] workingMemories) {
if ( !node.isInUse() ) {
removeTupleSink( (TupleSink) node );
}
removeShare();
if ( !this.isInUse() ) {
for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
workingMemories[i].clearNodeMemory( this );
}
}
this.tupleSource.remove( this,
workingMemories );
}
public boolean isTupleMemoryEnabled() {
return tupleMemoryEnabled;
}
public void setTupleMemoryEnabled(boolean tupleMemoryEnabled) {
this.tupleMemoryEnabled = tupleMemoryEnabled;
}
/**
* Returns the next node
* @return
* The next TupleSinkNode
*/
public TupleSinkNode getNextTupleSinkNode() {
return this.nextTupleSinkNode;
}
/**
* Sets the next node
* @param next
* The next TupleSinkNode
*/
public void setNextTupleSinkNode(final TupleSinkNode next) {
this.nextTupleSinkNode = next;
}
/**
* Returns the previous node
* @return
* The previous TupleSinkNode
*/
public TupleSinkNode getPreviousTupleSinkNode() {
return this.previousTupleSinkNode;
}
/**
* Sets the previous node
* @param previous
* The previous TupleSinkNode
*/
public void setPreviousTupleSinkNode(final TupleSinkNode previous) {
this.previousTupleSinkNode = previous;
}
}