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 java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import org.drools.common.BaseNode;
import org.drools.common.DroolsObjectInputStream;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
import org.drools.common.InternalWorkingMemoryEntryPoint;
import org.drools.common.RuleBasePartitionId;
import org.drools.rule.EntryPoint;
import org.drools.spi.ObjectType;
import org.drools.spi.PropagationContext;
/**
* The Rete-OO network.
*
* The Rete class is the root Object
. All objects are asserted into
* the Rete node where it propagates to all matching ObjectTypeNodes.
*
* The first time an instance of a Class type is asserted it does a full
* iteration of all ObjectTyppeNodes looking for matches, any matches are
* then cached in a HashMap which is used for future assertions.
*
* While Rete extends ObjectSource nad implements ObjectSink it nulls the
* methods attach(), remove() and updateNewNode() as this is the root node
* they are no applicable
*
* @see ObjectTypeNode
*
* @author Mark Proctor
* @author Bob McWhirter
*/
public class Rete extends ObjectSource
implements
Externalizable,
ObjectSink {
// ------------------------------------------------------------
// Instance members
// ------------------------------------------------------------
/**
*
*/
private static final long serialVersionUID = 400L;
private Map entryPoints;
private transient InternalRuleBase ruleBase;
public Rete() {
this( null );
}
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
public Rete(InternalRuleBase ruleBase) {
super( 0, RuleBasePartitionId.MAIN_PARTITION, ruleBase != null ? ruleBase.getConfiguration().isMultithreadEvaluation() : false );
this.entryPoints = Collections.synchronizedMap(new HashMap());
this.ruleBase = ruleBase;
}
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
* This is the entry point into the network for all asserted Facts. Iterates a cache
* of matching ObjectTypdeNode
s asserting the Fact. If the cache does not
* exist it first iteraes and builds the cache.
*
* @param factHandle
* The FactHandle of the fact to assert
* @param context
* The PropagationContext
of the WorkingMemory
action
* @param workingMemory
* The working memory session.
*/
public void assertObject(final InternalFactHandle factHandle,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
EntryPoint entryPoint = context.getEntryPoint();
EntryPointNode node = this.entryPoints.get( entryPoint );
ObjectTypeConf typeConf = ((InternalWorkingMemoryEntryPoint) workingMemory.getWorkingMemoryEntryPoint( entryPoint.getEntryPointId() )).getObjectTypeConfigurationRegistry().getObjectTypeConf( entryPoint,
factHandle.getObject() );
node.assertObject( factHandle,
context,
typeConf,
workingMemory );
}
/**
* Retract a fact object from this RuleBase
and the specified
* WorkingMemory
.
*
* @param handle
* The handle of the fact to retract.
* @param workingMemory
* The working memory session.
*/
public void retractObject(final InternalFactHandle handle,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
EntryPoint entryPoint = context.getEntryPoint();
EntryPointNode node = this.entryPoints.get( entryPoint );
ObjectTypeConf typeConf = ((InternalWorkingMemoryEntryPoint) workingMemory.getWorkingMemoryEntryPoint( entryPoint.getEntryPointId() )).getObjectTypeConfigurationRegistry().getObjectTypeConf( entryPoint,
handle.getObject() );
node.retractObject( handle,
context,
typeConf,
workingMemory );
}
/**
* Adds the ObjectSink
so that it may receive
* Objects
propagated from this ObjectSource
.
*
* @param objectSink
* The ObjectSink
to receive propagated
* Objects
. Rete only accepts ObjectTypeNode
s
* as parameters to this method, though.
*/
protected void addObjectSink(final ObjectSink objectSink) {
final EntryPointNode node = (EntryPointNode) objectSink;
this.entryPoints.put( node.getEntryPoint(),
node );
}
protected void removeObjectSink(final ObjectSink objectSink) {
final EntryPointNode node = (EntryPointNode) objectSink;
this.entryPoints.remove( node.getEntryPoint() );
}
public void attach() {
throw new UnsupportedOperationException( "cannot call attach() from the root Rete node" );
}
public void attach(final InternalWorkingMemory[] workingMemories) {
throw new UnsupportedOperationException( "cannot call attach() from the root Rete node" );
}
public void networkUpdated() {
// nothing to do
}
protected void doRemove(final RuleRemovalContext context,
final ReteooBuilder builder,
final BaseNode node,
final InternalWorkingMemory[] workingMemories) {
final EntryPointNode entryPointNode = (EntryPointNode) node;
removeObjectSink( entryPointNode );
}
public EntryPointNode getEntryPointNode(final EntryPoint entryPoint) {
return this.entryPoints.get( entryPoint );
}
public List getObjectTypeNodes() {
List allNodes = new LinkedList();
for ( EntryPointNode node : this.entryPoints.values() ) {
allNodes.addAll( node.getObjectTypeNodes().values() );
}
return allNodes;
}
public Map getObjectTypeNodes(EntryPoint entryPoint) {
return this.entryPoints.get( entryPoint ).getObjectTypeNodes();
}
public InternalRuleBase getRuleBase() {
return this.ruleBase;
}
public int hashCode() {
return this.entryPoints.hashCode();
}
public boolean equals(final Object object) {
if ( object == this ) {
return true;
}
if ( object == null || !(object instanceof Rete) ) {
return false;
}
final Rete other = (Rete) object;
return this.entryPoints.equals( other.entryPoints );
}
public void updateSink(final ObjectSink sink,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
// nothing to do, since Rete object itself holds no facts to propagate.
}
public boolean isObjectMemoryEnabled() {
throw new UnsupportedOperationException( "Rete has no Object memory" );
}
public void setObjectMemoryEnabled(boolean objectMemoryEnabled) {
throw new UnsupportedOperationException( "ORete has no Object memory" );
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject( entryPoints );
super.writeExternal( out );
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
entryPoints = Collections.synchronizedMap((Map) in.readObject());
ruleBase = ((DroolsObjectInputStream)in).getRuleBase();
super.readExternal( in );
}
public Map getEntryPointNodes() {
return this.entryPoints;
}
}