Skip to content

Instantly share code, notes, and snippets.

@leogomes
Created November 15, 2010 12:31
Show Gist options
  • Save leogomes/700306 to your computer and use it in GitHub Desktop.
Save leogomes/700306 to your computer and use it in GitHub Desktop.
MiscTest changes
public void testRuleRemovalWithJoinedRootPattern() {
String str = "";
str += "package org.drools \n";
str += "rule rule1 \n";
str += "when \n";
str += " String() \n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule rule2 \n";
str += "when \n";
str += " String() \n";
str += " Cheese() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ),
ResourceType.DRL );
if ( kbuilder.hasErrors() ) {
System.out.println( kbuilder.getErrors() );
assertTrue( kbuilder.hasErrors() );
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
DefaultFactHandle stringHandle = (DefaultFactHandle) ksession.insert( "hello" );
// Left side is initially unlinked, so the tuple wasn't generated.
assertNull( stringHandle.getFirstLeftTuple() );
DefaultFactHandle personHandle = (DefaultFactHandle) ksession.insert( new Person() );
LeftTuple stringLT = stringHandle.getFirstLeftTuple();
// Now there should be a left tuple there.
assertNotNull(stringLT);
// I need a Cheese for this to be propagated
assertNull( stringLT.getLeftParentNext() );
// Add the Cheese
DefaultFactHandle cheeseHandle = (DefaultFactHandle) ksession.insert( new Cheese() );
// Now the tuple was propagated
assertNotNull( stringLT.getLeftParentNext() );
// Removing the rule will remove the node and its tuples
kbase.removeRule( "org.drools",
"rule2" );
// Now the second tuple for the string should have been removed.
assertNotNull( stringLT );
assertNull( stringLT.getLeftParentNext() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment