Skip to content

Commit

Permalink
[DROOLS-828] add test case + code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jun 30, 2015
1 parent c62c63b commit 66b87a1
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 99 deletions.
Expand Up @@ -7308,4 +7308,38 @@ public void testInsertAndDelete() {


assertEquals( 1, list.size() ); assertEquals( 1, list.size() );
} }

@Test
public void testClearActivationGroupCommand() {
// DROOLS-828
String drl =
"package org.kie.test\n" +
"global java.util.List list\n" +
"rule \"Rule in first agenda group\" @Propagation(IMMEDIATE)\n" +
"agenda-group \"first-agenda\"\n" +
"salience 10\n" +
"when\n" +
"then\n" +
"list.add(\"Rule in first agenda group executed\");\n" +
"end\n" +
"rule \"Rule without agenda group\" @Propagation(IMMEDIATE)\n" +
"salience 100\n" +
"when\n" +
"then\n" +
"list.add(\"Rule without agenda group executed\");\n" +
"end\n";

KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL)
.build()
.newKieSession();

ksession.setGlobal("list", new ArrayList<String>());
ksession.getAgenda().getAgendaGroup("first-agenda").setFocus();
ksession.getAgenda().getAgendaGroup("first-agenda").clear();
ksession.fireAllRules();

ArrayList<String> list = (ArrayList<String>)ksession.getGlobal("list");
assertEquals(1, list.size());
assertEquals("Rule without agenda group executed", list.get(0));
}
} }
Expand Up @@ -232,18 +232,11 @@ public RuleAgendaItem createRuleAgendaItem(final int salience,
return lazyAgendaItem; return lazyAgendaItem;
} }


@Override public AgendaItem createAgendaItem(RuleTerminalNodeLeftTuple rtnLeftTuple,
public long getNextActivationCounter() {
return activationCounter++;
}

public AgendaItem createAgendaItem(final LeftTuple tuple,
final int salience, final int salience,
final PropagationContext context, final PropagationContext context,
final TerminalNode rtn,
RuleAgendaItem ruleAgendaItem, RuleAgendaItem ruleAgendaItem,
InternalAgendaGroup agendaGroup) { InternalAgendaGroup agendaGroup) {
RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) tuple;
rtnLeftTuple.init(activationCounter++, rtnLeftTuple.init(activationCounter++,
salience, salience,
context, context,
Expand All @@ -252,13 +245,6 @@ public AgendaItem createAgendaItem(final LeftTuple tuple,
return rtnLeftTuple; return rtnLeftTuple;
} }


public ScheduledAgendaItem createScheduledAgendaItem(final LeftTuple tuple,
final PropagationContext context,
final TerminalNode rtn,
InternalAgendaGroup agendaGroup) {
throw new UnsupportedOperationException("rete only");
}

public void setWorkingMemory(final InternalWorkingMemory workingMemory) { public void setWorkingMemory(final InternalWorkingMemory workingMemory) {
this.workingMemory = workingMemory; this.workingMemory = workingMemory;
RuleBaseConfiguration rbc = this.workingMemory.getKnowledgeBase().getConfiguration(); RuleBaseConfiguration rbc = this.workingMemory.getKnowledgeBase().getConfiguration();
Expand Down Expand Up @@ -856,7 +842,7 @@ public void clearAndCancel() {
* @see org.kie.common.AgendaI#clearAgendaGroup(java.lang.String) * @see org.kie.common.AgendaI#clearAgendaGroup(java.lang.String)
*/ */
public void clearAndCancelAgendaGroup(final String name) { public void clearAndCancelAgendaGroup(final String name) {
final AgendaGroup agendaGroup = this.agendaGroups.get( name ); InternalAgendaGroup agendaGroup = this.agendaGroups.get( name );
if ( agendaGroup != null ) { if ( agendaGroup != null ) {
clearAndCancelAgendaGroup( agendaGroup ); clearAndCancelAgendaGroup( agendaGroup );
} }
Expand All @@ -867,20 +853,16 @@ public void clearAndCancelAgendaGroup(final String name) {
* *
* @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl) * @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl)
*/ */
public void clearAndCancelAgendaGroup(final AgendaGroup agendaGroup) { public void clearAndCancelAgendaGroup(InternalAgendaGroup agendaGroup) {
final EventSupport eventsupport = (EventSupport) this.workingMemory; final EventSupport eventsupport = (EventSupport) this.workingMemory;


((InternalAgendaGroup) agendaGroup).setClearedForRecency( this.workingMemory.getFactHandleFactory().getRecency() ); agendaGroup.setClearedForRecency( this.workingMemory.getFactHandleFactory().getRecency() );


// this is thread safe for BinaryHeapQueue // this is thread safe for BinaryHeapQueue
// Binary Heap locks while it returns the array and reset's it's own internal array. Lock is released afer getAndClear() // Binary Heap locks while it returns the array and reset's it's own internal array. Lock is released afer getAndClear()
List<RuleAgendaItem> lazyItems = new ArrayList<RuleAgendaItem>(); List<RuleAgendaItem> lazyItems = new ArrayList<RuleAgendaItem>();
for ( Activation aQueueable : ((InternalAgendaGroup) agendaGroup).getAndClear() ) { for ( Activation aQueueable : ((InternalAgendaGroup) agendaGroup).getAndClear() ) {
final AgendaItem item = (AgendaItem) aQueueable; final AgendaItem item = (AgendaItem) aQueueable;
if ( item == null ) {
continue;
}

if ( item.isRuleAgendaItem() ) { if ( item.isRuleAgendaItem() ) {
lazyItems.add( (RuleAgendaItem) item ); lazyItems.add( (RuleAgendaItem) item );
((RuleAgendaItem) item).getRuleExecutor().cancel(workingMemory, eventsupport); ((RuleAgendaItem) item).getRuleExecutor().cancel(workingMemory, eventsupport);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.drools.core.phreak.RuleAgendaItem; import org.drools.core.phreak.RuleAgendaItem;
import org.drools.core.reteoo.LeftTuple; import org.drools.core.reteoo.LeftTuple;
import org.drools.core.reteoo.PathMemory; import org.drools.core.reteoo.PathMemory;
import org.drools.core.reteoo.RuleTerminalNodeLeftTuple;
import org.drools.core.reteoo.TerminalNode; import org.drools.core.reteoo.TerminalNode;
import org.drools.core.spi.Activation; import org.drools.core.spi.Activation;
import org.drools.core.spi.AgendaGroup; import org.drools.core.spi.AgendaGroup;
Expand Down Expand Up @@ -117,7 +118,7 @@ public interface InternalAgenda
* Clears all Activations from an Agenda Group. Any Activations that are also in an Xor Group are removed the * Clears all Activations from an Agenda Group. Any Activations that are also in an Xor Group are removed the
* the Xor Group. * the Xor Group.
*/ */
void clearAndCancelAgendaGroup(AgendaGroup agendaGroup); void clearAndCancelAgendaGroup(InternalAgendaGroup agendaGroup);


/** /**
* Clears all Activations from an Activation-Group. Any Activations that are also in an Agenda Group are removed * Clears all Activations from an Activation-Group. Any Activations that are also in an Agenda Group are removed
Expand Down Expand Up @@ -160,17 +161,11 @@ public interface InternalAgenda


void scheduleItem(final ScheduledAgendaItem item, InternalWorkingMemory workingMemory); void scheduleItem(final ScheduledAgendaItem item, InternalWorkingMemory workingMemory);


AgendaItem createAgendaItem(final LeftTuple tuple, AgendaItem createAgendaItem(RuleTerminalNodeLeftTuple rtnLeftTuple,
final int salience, int salience,
final PropagationContext context, PropagationContext context,
final TerminalNode rtn, RuleAgendaItem ruleAgendaItem,
RuleAgendaItem ruleAgendaItem, InternalAgendaGroup agendaGroup);
InternalAgendaGroup agendaGroup);

ScheduledAgendaItem createScheduledAgendaItem(final LeftTuple tuple,
final PropagationContext context,
final TerminalNode rtn,
InternalAgendaGroup agendaGroup);


boolean createActivation(final LeftTuple tuple, boolean createActivation(final LeftTuple tuple,
final PropagationContext context, final PropagationContext context,
Expand Down Expand Up @@ -297,8 +292,6 @@ RuleAgendaItem createRuleAgendaItem(final int salience,
void addQueryAgendaItem(final RuleAgendaItem item); void addQueryAgendaItem(final RuleAgendaItem item);
void removeQueryAgendaItem(final RuleAgendaItem item); void removeQueryAgendaItem(final RuleAgendaItem item);


long getNextActivationCounter();

/* /*
* (non-Javadoc) * (non-Javadoc)
* *
Expand Down
Expand Up @@ -60,7 +60,7 @@ public interface InternalAgendaGroup extends AgendaGroup {


void removeNodeInstance(Long processInstanceId, String nodeInstanceId); void removeNodeInstance(Long processInstanceId, String nodeInstanceId);


public Activation[] getActivations(); Activation[] getActivations();


Map<Long, String> getNodeInstances(); Map<Long, String> getNodeInstances();


Expand All @@ -70,10 +70,9 @@ public interface InternalAgendaGroup extends AgendaGroup {


InternalWorkingMemory getWorkingMemory(); InternalWorkingMemory getWorkingMemory();



void hasRuleFlowListener(boolean hasRuleFlowLister); void hasRuleFlowListener(boolean hasRuleFlowLister);


boolean isRuleFlowListener(); boolean isRuleFlowListener();


public boolean isSequential(); boolean isSequential();
} }
Expand Up @@ -49,7 +49,7 @@ public void doLeftInserts(TerminalNode rtnNode,
InternalWorkingMemory wm, InternalWorkingMemory wm,
LeftTupleSets srcLeftTuples, LeftTupleSets srcLeftTuples,
RuleExecutor executor) { RuleExecutor executor) {
InternalAgenda agenda = ( InternalAgenda ) wm.getAgenda(); InternalAgenda agenda = wm.getAgenda();
RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem(); RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem();


int salienceInt = 0; int salienceInt = 0;
Expand All @@ -60,7 +60,7 @@ public void doLeftInserts(TerminalNode rtnNode,
} }


if ( rtnNode.getRule().getAutoFocus() && !ruleAgendaItem.getAgendaGroup().isActive() ) { if ( rtnNode.getRule().getAutoFocus() && !ruleAgendaItem.getAgendaGroup().isActive() ) {
((InternalAgenda)wm.getAgenda()).setFocus(ruleAgendaItem.getAgendaGroup()); wm.getAgenda().setFocus( ruleAgendaItem.getAgendaGroup() );
} }


for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) { for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
Expand Down Expand Up @@ -89,8 +89,8 @@ public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor
} }


RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) leftTuple; RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) leftTuple;
rtnLeftTuple.init(agenda.getNextActivationCounter(), salienceInt, pctx, ruleAgendaItem, ruleAgendaItem.getAgendaGroup()); agenda.createAgendaItem( rtnLeftTuple, salienceInt, pctx, ruleAgendaItem, ruleAgendaItem.getAgendaGroup() );
rtnLeftTuple.setObject( rtnLeftTuple );
EventSupport es = (EventSupport) wm; EventSupport es = (EventSupport) wm;
es.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, wm); es.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, wm);


Expand All @@ -109,7 +109,7 @@ public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor
return; return;
} }


((InternalAgenda)wm.getAgenda()).addItemToActivationGroup( rtnLeftTuple ); wm.getAgenda().addItemToActivationGroup( rtnLeftTuple );


executor.addLeftTuple(leftTuple); executor.addLeftTuple(leftTuple);
leftTuple.increaseActivationCountForEvents(); // increased here, decreased in Agenda's cancelActivation and fireActivation leftTuple.increaseActivationCountForEvents(); // increased here, decreased in Agenda's cancelActivation and fireActivation
Expand All @@ -122,11 +122,9 @@ public void doLeftUpdates(TerminalNode rtnNode,
InternalWorkingMemory wm, InternalWorkingMemory wm,
LeftTupleSets srcLeftTuples, LeftTupleSets srcLeftTuples,
RuleExecutor executor) { RuleExecutor executor) {
InternalAgenda agenda = ( InternalAgenda ) wm.getAgenda();

RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem(); RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem();
if ( rtnNode.getRule().getAutoFocus() && !ruleAgendaItem.getAgendaGroup().isActive() ) { if ( rtnNode.getRule().getAutoFocus() && !ruleAgendaItem.getAgendaGroup().isActive() ) {
((InternalAgenda)wm.getAgenda()).setFocus(ruleAgendaItem.getAgendaGroup()); wm.getAgenda().setFocus(ruleAgendaItem.getAgendaGroup());
} }


int salienceInt = 0; int salienceInt = 0;
Expand All @@ -140,7 +138,7 @@ public void doLeftUpdates(TerminalNode rtnNode,
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) { for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext(); LeftTuple next = leftTuple.getStagedNext();


doLeftTupleUpdate(rtnNode, executor, agenda, salienceInt, salience, leftTuple, wm); doLeftTupleUpdate(rtnNode, executor, wm.getAgenda(), salienceInt, salience, leftTuple, wm);


leftTuple.clearStaged(); leftTuple.clearStaged();
leftTuple = next; leftTuple = next;
Expand Down Expand Up @@ -228,12 +226,11 @@ public static void doLeftDelete(InternalWorkingMemory wm, RuleExecutor executor,
Activation activation = (Activation) leftTuple; Activation activation = (Activation) leftTuple;
activation.setMatched( false ); activation.setMatched( false );


InternalAgenda agenda = (InternalAgenda) wm.getAgenda(); wm.getAgenda().cancelActivation( leftTuple,
agenda.cancelActivation( leftTuple, pctx,
pctx, wm,
wm, activation,
activation, rtnLt.getTerminalNode() );
rtnLt.getTerminalNode() );


if ( leftTuple.getMemory() != null && (pctx.getType() != PropagationContext.EXPIRATION ) ) { if ( leftTuple.getMemory() != null && (pctx.getType() != PropagationContext.EXPIRATION ) ) {
// Expiration propagations should not be removed from the list, as they still need to fire // Expiration propagations should not be removed from the list, as they still need to fire
Expand Down
Expand Up @@ -121,7 +121,8 @@ public void clear() {
} }


public Activation[] getAndClear() { public Activation[] getAndClear() {
Activation[] queue = this.elements; Activation[] queue = new Activation[size];
System.arraycopy( this.elements, 1, queue, 0, size );
this.elements = new Activation[this.elements.length]; // for gc this.elements = new Activation[this.elements.length]; // for gc
this.size = 0; this.size = 0;
return queue; return queue;
Expand Down
Expand Up @@ -219,18 +219,11 @@ public RuleAgendaItem createRuleAgendaItem(final int salience,
return lazyAgendaItem; return lazyAgendaItem;
} }


@Override public AgendaItem createAgendaItem(RuleTerminalNodeLeftTuple rtnLeftTuple,
public long getNextActivationCounter() {
return activationCounter++;
}

public AgendaItem createAgendaItem(final LeftTuple tuple,
final int salience, final int salience,
final PropagationContext context, final PropagationContext context,
final TerminalNode rtn,
RuleAgendaItem ruleAgendaItem, RuleAgendaItem ruleAgendaItem,
InternalAgendaGroup agendaGroup) { InternalAgendaGroup agendaGroup) {
RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) tuple;
rtnLeftTuple.init(activationCounter++, rtnLeftTuple.init(activationCounter++,
salience, salience,
context, context,
Expand Down Expand Up @@ -562,8 +555,7 @@ public boolean createActivation(final LeftTuple tuple,
// ControlRules for now re-use the same PropagationContext // ControlRules for now re-use the same PropagationContext
if ( rtn.isFireDirect() ) { if ( rtn.isFireDirect() ) {
// Fire RunLevel == 0 straight away. agenda-groups, rule-flow groups, salience are ignored // Fire RunLevel == 0 straight away. agenda-groups, rule-flow groups, salience are ignored
AgendaItem item = createAgendaItem( tuple, 0, context, AgendaItem item = createAgendaItem( (RuleTerminalNodeLeftTuple)tuple, 0, context, null, null );
rtn, null, null );
tuple.setObject( item ); tuple.setObject( item );
if ( activationsFilter != null && !activationsFilter.accept( item, if ( activationsFilter != null && !activationsFilter.accept( item,
workingMemory, workingMemory,
Expand Down Expand Up @@ -606,10 +598,9 @@ public boolean createActivation(final LeftTuple tuple,
return false; return false;
} }


item = createAgendaItem( tuple, item = createAgendaItem( (RuleTerminalNodeLeftTuple)tuple,
0, 0,
context, context,
rtn,
null, null,
agendaGroup agendaGroup
); );
Expand Down Expand Up @@ -654,10 +645,9 @@ public boolean createPostponedActivation(final LeftTuple tuple,
return false; return false;
} }


item = createAgendaItem( tuple, item = createAgendaItem( (RuleTerminalNodeLeftTuple)tuple,
0, 0,
context, context,
rtn,
null, null,
agendaGroup agendaGroup
); );
Expand Down Expand Up @@ -1097,7 +1087,7 @@ public void clearAndCancel() {
* @see org.kie.common.AgendaI#clearAgendaGroup(java.lang.String) * @see org.kie.common.AgendaI#clearAgendaGroup(java.lang.String)
*/ */
public void clearAndCancelAgendaGroup(final String name) { public void clearAndCancelAgendaGroup(final String name) {
final AgendaGroup agendaGroup = this.agendaGroups.get( name ); InternalAgendaGroup agendaGroup = this.agendaGroups.get( name );
if ( agendaGroup != null ) { if ( agendaGroup != null ) {
clearAndCancelAgendaGroup( agendaGroup ); clearAndCancelAgendaGroup( agendaGroup );
} }
Expand All @@ -1108,7 +1098,7 @@ public void clearAndCancelAgendaGroup(final String name) {
* *
* @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl) * @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl)
*/ */
public void clearAndCancelAgendaGroup(final AgendaGroup agendaGroup) { public void clearAndCancelAgendaGroup(InternalAgendaGroup agendaGroup) {
final EventSupport eventsupport = (EventSupport) this.workingMemory; final EventSupport eventsupport = (EventSupport) this.workingMemory;


((InternalAgendaGroup) agendaGroup).setClearedForRecency( this.workingMemory.getFactHandleFactory().getRecency() ); ((InternalAgendaGroup) agendaGroup).setClearedForRecency( this.workingMemory.getFactHandleFactory().getRecency() );
Expand Down

0 comments on commit 66b87a1

Please sign in to comment.