Skip to content

Commit

Permalink
JBRULES-3148 Dynamic Declarative Conflict Resolution
Browse files Browse the repository at this point in the history
JBRULES-3149 ObjectTypeNodes are not handled correctly during Marshalling
(cherry picked from commit aadcfda)
  • Loading branch information
mdproctor committed Sep 14, 2011
1 parent 15cebe1 commit 7daf224
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2389,8 +2389,8 @@ public void testBasicFrom() throws Exception {
Person p = new Person( "stilton" );
ksession.insert( p );

// ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession( ksession,
// true );
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession( ksession,
true );
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession( ksession,
true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.drools.core.util.Iterator;
import org.drools.core.util.ObjectHashSet;
import org.drools.core.util.ObjectHashSet.ObjectEntry;
import org.drools.impl.KnowledgeBaseImpl;
import org.drools.impl.StatefulKnowledgeSessionImpl;
import org.drools.reteoo.AccumulateNode;
import org.drools.reteoo.AccumulateNode.AccumulateMemory;
Expand Down Expand Up @@ -61,6 +62,11 @@ private ActivationIterator(InternalWorkingMemory wm,
}
}

public static ActivationIterator iterator(InternalWorkingMemory wm) {
return new ActivationIterator( wm,
new KnowledgeBaseImpl( wm.getRuleBase() ) );
}

public static ActivationIterator iterator(StatefulKnowledgeSession ksession) {
return new ActivationIterator( ((StatefulKnowledgeSessionImpl) ksession).getInternalWorkingMemory(),
ksession.getKnowledgeBase() );
Expand Down
16 changes: 14 additions & 2 deletions drools-core/src/main/java/org/drools/common/DefaultAgenda.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
import org.drools.event.rule.ActivationCancelledCause;
import org.drools.reteoo.LeftTuple;
import org.drools.reteoo.LeftTuple;
import org.drools.reteoo.ObjectTypeConf;
import org.drools.reteoo.RuleTerminalNode;
import org.drools.rule.Declaration;
import org.drools.rule.EntryPoint;
import org.drools.rule.GroupElement;
import org.drools.rule.Rule;
import org.drools.runtime.process.ProcessInstance;
Expand Down Expand Up @@ -115,6 +117,8 @@ public class DefaultAgenda
protected volatile AtomicBoolean halt = new AtomicBoolean( false );

private int activationCounter;

private ObjectTypeConf activationObjectTypeConf;

// ------------------------------------------------------------
// Constructors
Expand Down Expand Up @@ -333,8 +337,16 @@ public ActivationGroup getStageActivationsGroup() {
return stagedActivations;
}

public boolean addActivation(final AgendaItem activation) {
InternalFactHandle factHandle = workingMemory.getFactHandleFactory().newFactHandle( activation, null, workingMemory, null );


public boolean addActivation(final AgendaItem activation) {
if ( activationObjectTypeConf == null ) {
EntryPoint ep = workingMemory.getEntryPoint();
activationObjectTypeConf = ((InternalWorkingMemoryEntryPoint) workingMemory.getWorkingMemoryEntryPoint( ep.getEntryPointId() )).getObjectTypeConfigurationRegistry().getObjectTypeConf( ep,
activation );
}

InternalFactHandle factHandle = workingMemory.getFactHandleFactory().newFactHandle( activation, activationObjectTypeConf, workingMemory, workingMemory );
workingMemory.getEntryPointNode().assertActivation( factHandle, activation.getPropagationContext(), workingMemory );
activation.setFactHandle( factHandle );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.drools.base.ClassObjectType;
import org.drools.facttemplates.Fact;
import org.drools.reteoo.ClassObjectTypeConf;
import org.drools.reteoo.FactTemplateTypeConf;
import org.drools.reteoo.ObjectTypeConf;
import org.drools.rule.EntryPoint;
import org.drools.spi.Activation;

public class ObjectTypeConfigurationRegistry implements Serializable {
private static final long serialVersionUID = 510l;
Expand Down Expand Up @@ -55,7 +57,8 @@ public ObjectTypeConf getObjectTypeConf(EntryPoint entrypoint,

// first see if it's a ClassObjectTypeConf
ObjectTypeConf objectTypeConf = null;
Object key = ( object instanceof Fact ) ? ((Fact) object).getFactTemplate().getName() : object.getClass();
Class cls = (object instanceof Activation) ? ClassObjectType.Activation_ObjectType.getClassType() : object.getClass();
Object key = ( object instanceof Fact ) ? ((Fact) object).getFactTemplate().getName() : cls;
objectTypeConf = this.typeConfMap.get( key );

// it doesn't exist, so create it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalRuleFlowGroup;
import org.drools.common.InternalWorkingMemory;
import org.drools.common.InternalWorkingMemoryEntryPoint;
import org.drools.common.NodeMemory;
import org.drools.common.PropagationContextImpl;
import org.drools.common.QueryElementFactHandle;
Expand Down Expand Up @@ -356,22 +357,6 @@ public static void readFactHandles(MarshallerReaderContext context) throws IOExc
context );
}

EntryPointNode node = ruleBase.getRete().getEntryPointNode( EntryPoint.DEFAULT );
Map<ObjectType, ObjectTypeNode> objectTypeNodes = node.getObjectTypeNodes();

// add handles to object type nodes
for ( InternalFactHandle handle : handles ) {
Object object = handle.getObject();

ClassObjectType objectType = new ClassObjectType( ( object != null ) ? object.getClass() : AgendaItem.class );
ObjectTypeNode objectTypeNode = objectTypeNodes.get( objectType );
if (objectTypeNode != null) {
ObjectHashSet set = (ObjectHashSet) context.wm.getNodeMemory( objectTypeNode );
set.add( handle,
false );
}
}

InternalFactHandle handle = wm.getInitialFactHandle();
while ( stream.readShort() == PersisterEnums.LEFT_TUPLE ) {
LeftTupleSink sink = (LeftTupleSink) context.sinks.get( stream.readInt() );
Expand All @@ -386,7 +371,22 @@ public static void readFactHandles(MarshallerReaderContext context) throws IOExc

readPropagationContexts( context );

readActivations( context );
readActivations( context );

// add handles to object type nodes
for ( InternalFactHandle factHandle : handles ) {
Object object = factHandle.getObject();

EntryPoint ep = ((InternalWorkingMemoryEntryPoint) factHandle.getEntryPoint()).getEntryPoint();

ObjectTypeConf typeConf = ((InternalWorkingMemoryEntryPoint) factHandle.getEntryPoint()).getObjectTypeConfigurationRegistry().getObjectTypeConf( ep, object );
ObjectTypeNode[] cachedNodes = typeConf.getObjectTypeNodes();
for ( int i = 0, length = cachedNodes.length; i < length; i++ ) {
ObjectHashSet set = (ObjectHashSet) wm.getNodeMemory( cachedNodes[i] );
set.add( factHandle,
false );
}
}
}

public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public class MarshallerWriteContext extends ObjectOutputStream {
public final boolean marshalProcessInstances;
public final boolean marshalWorkItems;
public final Environment env;
public List<InternalFactHandle> matchFactHandles;



public MarshallerWriteContext(OutputStream stream,
InternalRuleBase ruleBase,
InternalWorkingMemory wm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.drools.InitialFact;
import org.drools.base.ClassObjectType;
import org.drools.common.ActivationGroupNode;
import org.drools.common.ActivationIterator;
import org.drools.common.ActiveActivationIterator;
import org.drools.common.AgendaItem;
import org.drools.common.DefaultAgenda;
Expand Down Expand Up @@ -265,13 +266,14 @@ public static void writeFactHandles(MarshallerWriteContext context) throws IOExc



ActiveActivationIterator it = ActiveActivationIterator.iterator( wm );
List<InternalFactHandle> list = new ArrayList<InternalFactHandle>(100);
ActivationIterator it = ActivationIterator.iterator( wm );
List<InternalFactHandle> matchFactHandles = new ArrayList<InternalFactHandle>(100);
for ( Activation item = (Activation) it.next(); item != null; item = (Activation) it.next() ) {
list.add( item.getFactHandle() );
matchFactHandles.add( item.getFactHandle() );
}
context.matchFactHandles = matchFactHandles;

stream.writeInt( wm.getObjectStore().size() + list.size() );
stream.writeInt( wm.getObjectStore().size() + matchFactHandles.size() );

// Write out FactHandles
for ( InternalFactHandle handle : orderFacts( wm.getObjectStore() ) ) {
Expand All @@ -286,7 +288,7 @@ public static void writeFactHandles(MarshallerWriteContext context) throws IOExc
context );
}

for ( InternalFactHandle handle : list) {
for ( InternalFactHandle handle : orderFacts( matchFactHandles ) ) {
Object object = handle.getObject();
handle.setObject( null ); // we must set it to null as we don't want to write out the Activation
writeFactHandle( context,
Expand Down Expand Up @@ -371,6 +373,16 @@ public static InternalFactHandle[] orderFacts(ObjectStore objectStore) {

return handles;
}

public static InternalFactHandle[] orderFacts(List<InternalFactHandle> handlesList) {
// this method is just needed for testing purposes, to allow round tripping
int size = handlesList.size();
InternalFactHandle[] handles = handlesList.toArray( new InternalFactHandle[ size ] );
Arrays.sort( handles,
new HandleSorter() );

return handles;
}

public static class HandleSorter
implements
Expand Down Expand Up @@ -471,6 +483,24 @@ public static void writeLeftTuples(MarshallerWriteContext context) throws IOExce
true );
}
}


for ( InternalFactHandle handle : orderFacts( context.matchFactHandles ) ) {
//InternalFactHandle handle = (InternalFactHandle) it.next();

for ( LeftTuple leftTuple = handle.getFirstLeftTuple(); leftTuple != null; leftTuple = (LeftTuple) leftTuple.getLeftParentNext() ) {
stream.writeShort( PersisterEnums.LEFT_TUPLE );

stream.writeInt( leftTuple.getLeftTupleSink().getId() );
stream.writeInt( handle.getId() );

//context.out.println( "LeftTuple sinkId:" + leftTuple.getLeftTupleSink().getId() + " handleId:" + handle.getId() );
writeLeftTuple( leftTuple,
context,
true );
}
}

stream.writeShort( PersisterEnums.END );
//context.out.println( "LeftTuples End" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.drools.reteoo.builder.PatternBuilder;
import org.drools.rule.EntryPoint;
import org.drools.rule.TypeDeclaration;
import org.drools.spi.Activation;
import org.drools.spi.ObjectType;

public class ClassObjectTypeConf
Expand Down Expand Up @@ -62,8 +63,8 @@ public ClassObjectTypeConf() {

public ClassObjectTypeConf(final EntryPoint entryPoint,
final Class< ? > clazz,
final InternalRuleBase ruleBase) {
this.cls = clazz;
final InternalRuleBase ruleBase) {
this.cls = (Activation.class.isAssignableFrom( clazz ) ) ? ClassObjectType.Activation_ObjectType.getClassType() : clazz;
this.ruleBase = ruleBase;
this.entryPoint = entryPoint;
this.typeDecl = ruleBase.getTypeDeclaration( clazz );
Expand Down Expand Up @@ -206,5 +207,9 @@ public boolean isTMSEnabled() {
public void enableTMS() {
this.tmsEnabled = true;
}

public EntryPoint getEntryPoint() {
return entryPoint;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.drools.reteoo;

import org.drools.rule.EntryPoint;
import org.drools.rule.TypeDeclaration;

public interface ObjectTypeConf {
Expand Down Expand Up @@ -44,5 +45,7 @@ public interface ObjectTypeConf {
* Enable TMS for this object type.
* */
public void enableTMS();

public EntryPoint getEntryPoint();

}
13 changes: 11 additions & 2 deletions drools-core/src/main/java/org/drools/reteoo/QueryElementNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.drools.common.BaseNode;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
import org.drools.common.InternalWorkingMemoryEntryPoint;
import org.drools.common.PropagationContextImpl;
import org.drools.common.QueryElementFactHandle;
import org.drools.core.util.RightTupleList;
Expand Down Expand Up @@ -65,6 +66,8 @@ public class QueryElementNode extends LeftTupleSource
private boolean tupleMemoryEnabled;

private boolean openQuery;

private ObjectTypeConf queryObjectTypeConf;

public QueryElementNode() {
// for serialization
Expand Down Expand Up @@ -220,10 +223,16 @@ public void assertLeftTuple(LeftTuple leftTuple,
collector,
openQuery );

if ( queryObjectTypeConf == null ) {
EntryPoint ep = workingMemory.getEntryPoint();
queryObjectTypeConf = ((InternalWorkingMemoryEntryPoint) workingMemory.getWorkingMemoryEntryPoint( ep.getEntryPointId() )).getObjectTypeConfigurationRegistry().getObjectTypeConf( ep,
queryObject );
}

InternalFactHandle handle = workingMemory.getFactHandleFactory().newFactHandle( queryObject,
null,
queryObjectTypeConf,
workingMemory,
null );
workingMemory );

collector.setFactHandle( handle );

Expand Down

0 comments on commit 7daf224

Please sign in to comment.