Skip to content

Commit

Permalink
[JBRULES-3656] fix node memories cleanup on node removal
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Oct 24, 2012
1 parent 32d723b commit 674d5e0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

Expand Down Expand Up @@ -11639,7 +11642,7 @@ public void testDeterministicOTNOrdering() throws Exception {
ksession.fireAllRules();
}

@Test
@Test(timeout = 10000)
public void testRemoveBigRule() throws Exception {
// JBRULES-3496
String str =
Expand Down Expand Up @@ -11796,4 +11799,77 @@ public void testAlphaHashingWithConstants() {
ksession.insert(new Person("Mario", 38));
assertEquals(3, ksession.fireAllRules());
}

@Test
public void testMemoriesCCEWhenAddRemoveAddRule() {
// JBRULES-3656
String rule1 =
"import org.drools.integrationtests.MiscTest.*\n" +
"import java.util.Date\n" +
"rule \"RTR - 28717 retract\"\n" +
"when\n" +
" $listMembership0 : SimpleMembership( $listMembershipPatientSpaceIdRoot : patientSpaceId,\n" +
" ( listId != null && listId == \"28717\" ) ) and not ($patient0 : SimplePatient( $patientSpaceIdRoot : spaceId, spaceId != null &&\n" +
" spaceId == $listMembershipPatientSpaceIdRoot ) and\n" +
" (($ruleTime0 : RuleTime( $ruleTimeStartOfDay4_1 : startOfDay, $ruleTimeTime4_1 : time ) and $patient1 :\n" +
" SimplePatient( spaceId != null && spaceId == $patientSpaceIdRoot, birthDate != null && (birthDate after[0s,1d] $ruleTimeStartOfDay4_1) ) ) ) )\n" +
"then\n" +
"end";

String rule2 =
"import org.drools.integrationtests.MiscTest.*\n" +
"import java.util.Date\n" +
"rule \"RTR - 28717 retract\"\n" +
"when $listMembership0 : SimpleMembership( $listMembershipPatientSpaceIdRoot : patientSpaceId, ( listId != null && listId == \"28717\" ) )\n" +
" and not ($patient0 : SimplePatient( $patientSpaceIdRoot : spaceId, spaceId != null && spaceId == $listMembershipPatientSpaceIdRoot )\n" +
" and ( ($ruleTime0 : RuleTime( $ruleTimeStartOfDay4_1 : startOfDay, $ruleTimeTime4_1 : time )\n" +
" and $patient1 : SimplePatient( spaceId != null && spaceId == $patientSpaceIdRoot, birthDate != null && (birthDate not after[0s,1d] $ruleTimeStartOfDay4_1) ) ) ) )\n" +
"then\n" +
"end";

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource(rule1.getBytes()), ResourceType.DRL );

KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSession knowledgeSession = kbase.newStatefulKnowledgeSession();

Collection<KnowledgePackage> knowledgePackages = kbuilder.getKnowledgePackages();
kbase.addKnowledgePackages( knowledgePackages );

for (KnowledgePackage kPackage : knowledgePackages) {
kbase.removeKnowledgePackage(kPackage.getName());
}

kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource(rule2.getBytes()), ResourceType.DRL );
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
}

public static class RuleTime {
public Date getTime() {
return new Date();
}
public Date getStartOfDay(){
return new Date();
}
}
public static class SimpleMembership {
public String getListId() {
return "";
}
public String getPatientSpaceId() {
return "";
}
}
public class SimplePatient {
public String getSpaceId() {
return "";
}
public String getFactHandleString() {
return "";
}
public Date getBirthDate() {
return new Date();
}
}
}
16 changes: 15 additions & 1 deletion drools-core/src/main/java/org/drools/common/BaseNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public void remove(RuleRemovalContext context,
BaseNode node,
InternalWorkingMemory[] workingMemories) {

if (!context.addRemovedNode(this) && !(this instanceof LeftTupleSource)) {
if (!context.addRemovedNode(this) && !(this instanceof LeftTupleSource) ) {
node.internalCleanUp(builder, workingMemories);
return;
}

Expand All @@ -120,6 +121,19 @@ public void remove(RuleRemovalContext context,
}
}

private void internalCleanUp(ReteooBuilder builder, InternalWorkingMemory[] workingMemories) {
if ( !this.isInUse() && this instanceof NodeMemory ) {
if (this instanceof NodeMemory) {
for( InternalWorkingMemory workingMemory : workingMemories ) {
workingMemory.clearNodeMemory( (NodeMemory) this );
}
}
if ( !(this instanceof EntryPointNode) ) {
builder.getIdGenerator().releaseId( this.getId() );
}
}
}

/**
* Removes the node from teh network. Usually from the parent <code>ObjectSource</code> or <code>TupleSource</code>
* @param builder
Expand Down
2 changes: 1 addition & 1 deletion drools-core/src/main/java/org/drools/reteoo/NodeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public List<BaseNode> getNodes() {

public boolean add(BaseNode node) {
if (nodeIds.add(node.getId())) {
getNodes().add(node);
nodes.add(node);
return true;
}
return false;
Expand Down

0 comments on commit 674d5e0

Please sign in to comment.