Skip to content

Commit

Permalink
[DROOLS-1177] avoid flushing non initialized path during rule removal (
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed May 18, 2016
1 parent abdf21f commit 6be892a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
Expand Up @@ -2781,4 +2781,54 @@ public void testIncrementalCompilationChangeingParentRule() throws Exception {
assertEquals( 2, ksession.fireAllRules() ); assertEquals( 2, ksession.fireAllRules() );
} }


@Test
public void testRemoveRuleWithNonInitializedPath() {
// DROOLS-1177
String drl1 =
"import " + MyEvent.class.getCanonicalName() + "\n" +
"declare MyEvent @role( event ) end\n" +
"rule \"RG_TEST_1\"\n" +
" when\n" +
" $dummy: MyEvent (id == 1)\n" +
" $other: MyEvent (this != $dummy)\n" +
" then\n" +
" retract($other);\n" +
"end\n" +
"rule \"RG_TEST_2\"\n" +
" when\n" +
" $dummy: MyEvent (id == 1)\n" +
" then\n" +
" retract($dummy);\n" +
"end\n";

String drl2 =
"import " + MyEvent.class.getCanonicalName() + "\n" +
"declare IMyEvent @role( event ) end\n" +
"rule \"RG_TEST_2\"\n" +
" when\n" +
" $dummy: MyEvent (id == 1)\n" +
" then\n" +
" retract($dummy);\n" +
"end\n";

KieServices ks = KieServices.Factory.get();

KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel( "KBase1" ).setDefault( true )
.setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true)
.setType(KieSessionModel.KieSessionType.STATEFUL);

ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-upgrade", "1.0.0" );
deployJar( ks, createKJar( ks, kproj, releaseId1, null, drl1 ) );

KieContainer kc = ks.newKieContainer( releaseId1 );
KieSession ksession = kc.newKieSession();

ksession.insert( new MyEvent( 0 ) );

ReleaseId releaseId2 = ks.newReleaseId( "org.kie", "test-upgrade", "1.1.0" );
deployJar( ks, createKJar( ks, kproj, releaseId2, null, drl2 ) );
kc.updateToVersion( releaseId2 );
}
} }
Expand Up @@ -190,7 +190,7 @@ public static void removeRule(TerminalNode tn, InternalWorkingMemory[] wms, Inte
} }
} }


if (tnms.subjectPmem != null && tnms.subjectPmem.getRuleAgendaItem() != null && tnms.subjectPmem.getRuleAgendaItem().isQueued()) { if (tnms.subjectPmem != null && tnms.subjectPmem.isInitialized() && tnms.subjectPmem.getRuleAgendaItem().isQueued()) {
// SubjectPmem can be null, if it was never initialized // SubjectPmem can be null, if it was never initialized
tnms.subjectPmem.getRuleAgendaItem().dequeue(); tnms.subjectPmem.getRuleAgendaItem().dequeue();
} }
Expand Down Expand Up @@ -543,7 +543,7 @@ public Flushed(SegmentMemory segmentMemory, PathMemory pathMemory) {


private static void flushStagedTuples(PathMemory pmem, PathEndNodes pathEndNodes, InternalWorkingMemory wm) { private static void flushStagedTuples(PathMemory pmem, PathEndNodes pathEndNodes, InternalWorkingMemory wm) {
// first flush the subject rule, then flush any staging lists that are part of a merge // first flush the subject rule, then flush any staging lists that are part of a merge
if ( pmem.getRuleAgendaItem() != null ) { if ( pmem.isInitialized() ) {
new RuleNetworkEvaluator().evaluateNetwork(pmem, pmem.getRuleAgendaItem().getRuleExecutor(), wm); new RuleNetworkEvaluator().evaluateNetwork(pmem, pmem.getRuleAgendaItem().getRuleExecutor(), wm);
} }


Expand Down Expand Up @@ -584,7 +584,7 @@ private static void flushStagedTuples(PathMemory pmem, PathEndNodes pathEndNodes
} }


private static void flushStagedTuples(LeftTupleNode splitStartNode, PathMemory pmem, InternalWorkingMemory wm) { private static void flushStagedTuples(LeftTupleNode splitStartNode, PathMemory pmem, InternalWorkingMemory wm) {
if (pmem.getRuleAgendaItem() == null ) { if ( !pmem.isInitialized() ) {
// The rule has never been linked in and evaluated, so there will be nothing to flush. // The rule has never been linked in and evaluated, so there will be nothing to flush.
return; return;
} }
Expand Down Expand Up @@ -618,15 +618,19 @@ public static boolean flushLeftTupleIfNecessary(InternalWorkingMemory wm, Segmen
sm.getPathMemories().get(0) : sm.getPathMemories().get(0) :
sm.getFirstDataDrivenPathMemory(); sm.getFirstDataDrivenPathMemory();


if (pmem == null) {
return false;
}

TupleSets<LeftTuple> leftTupleSets = new TupleSetsImpl<LeftTuple>(); TupleSets<LeftTuple> leftTupleSets = new TupleSetsImpl<LeftTuple>();
if (leftTuple != null) { if (leftTuple != null) {
leftTupleSets.addInsert(leftTuple); leftTupleSets.addInsert(leftTuple);
} }

forceFlushLeftTuple(pmem, sm, wm, leftTupleSets);
return pmem != null && forceFlushLeftTuple(pmem, sm, wm, leftTupleSets); return true;
} }


private static boolean forceFlushLeftTuple(PathMemory pmem, SegmentMemory sm, InternalWorkingMemory wm, TupleSets<LeftTuple> leftTupleSets) { private static void forceFlushLeftTuple(PathMemory pmem, SegmentMemory sm, InternalWorkingMemory wm, TupleSets<LeftTuple> leftTupleSets) {
SegmentMemory[] smems = pmem.getSegmentMemories(); SegmentMemory[] smems = pmem.getSegmentMemories();


LeftTupleNode node; LeftTupleNode node;
Expand All @@ -652,7 +656,6 @@ private static boolean forceFlushLeftTuple(PathMemory pmem, SegmentMemory sm, In
new RuleNetworkEvaluator().outerEval(pmem, node, bit, mem, smems, sm.getPos(), leftTupleSets, wm, new RuleNetworkEvaluator().outerEval(pmem, node, bit, mem, smems, sm.getPos(), leftTupleSets, wm,
new LinkedList<StackEntry>(), new LinkedList<StackEntry>(),
true, rtnPmem.getOrCreateRuleAgendaItem(wm).getRuleExecutor()); true, rtnPmem.getOrCreateRuleAgendaItem(wm).getRuleExecutor());
return true;
} }




Expand Down
Expand Up @@ -179,6 +179,10 @@ public short getNodeType() {
return NodeTypeEnums.RuleTerminalNode; return NodeTypeEnums.RuleTerminalNode;
} }


public boolean isInitialized() {
return agendaItem != null && segmentMemories[0] != null;
}

public SegmentMemory[] getSegmentMemories() { public SegmentMemory[] getSegmentMemories() {
return segmentMemories; return segmentMemories;
} }
Expand Down

0 comments on commit 6be892a

Please sign in to comment.