ksession = getSerialisedStatefulKnowledgeSession( ksession,
true );
// Check logical Insertions where made for c2 and c3
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
assertEquals( 2,
list.size() );
assertFalse( list.contains( new Person( c1.getType() ) ) );
assertTrue( list.contains( new Person( c2.getType() ) ) );
assertTrue( list.contains( new Person( c3.getType() ) ) );
// this rule will make a logical assertion for c1 too
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newClassPathResource( "test_LogicalInsertionsDynamicRule2.drl",
getClass() ),
ResourceType.DRL );
if ( kbuilder.hasErrors() ) {
fail( kbuilder.getErrors().toString() );
}
Collection<KnowledgePackage> kpkgs2 = kbuilder.getKnowledgePackages();
kbase.addKnowledgePackages( kpkgs2 );
kbase = SerializationHelper.serializeObject( kbase );
ksession.fireAllRules();
ksession = getSerialisedStatefulKnowledgeSession( ksession,
true );
kbase = ksession.getKnowledgeBase();
// check all now have just one logical assertion each
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
assertEquals( 3,
list.size() );
assertTrue( list.contains( new Person( c1.getType() ) ) );
assertTrue( list.contains( new Person( c2.getType() ) ) );
assertTrue( list.contains( new Person( c3.getType() ) ) );
ksession = getSerialisedStatefulKnowledgeSession( ksession,
true );
// check the packages are correctly populated
assertEquals( 2, kbase.getKnowledgePackages().size() );
KnowledgePackage test = null, test2 = null;
// different JVMs return the package list in different order
for( KnowledgePackage kpkg : kbase.getKnowledgePackages() ) {
if( kpkg.getName().equals( "org.drools.test" )) {
test = kpkg;
} else if( kpkg.getName().equals( "org.drools.test2" )) {
test2 = kpkg;
}
}
assertNotNull( test );
assertNotNull( test2 );
assertEquals( "rule1",
test.getRules().iterator().next().getName() );
assertEquals( "rule2",
test2.getRules().iterator().next().getName() );
// now remove the first rule
kbase.removeRule( test.getName(),
test.getRules().iterator().next().getName() );
// different JVMs return the package list in different order
for( KnowledgePackage kpkg : kbase.getKnowledgePackages() ) {
if( kpkg.getName().equals( "org.drools.test" )) {
test = kpkg;
} else if( kpkg.getName().equals( "org.drools.test2" )) {
test2 = kpkg;
}
}
assertNotNull( test );
assertNotNull( test2 );
// Check the rule was correctly remove
assertEquals( 0,
test.getRules().size() );
assertEquals( 1,
test2.getRules().size() );
assertEquals( "rule2",
test2.getRules().iterator().next().getName() );
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
assertEquals( "removal of the rule should result in retraction of c3's logical assertion",
2,
list.size() );
assertTrue( "c1's logical assertion should not be retracted",
list.contains( new Person( c1.getType() ) ) );
assertTrue( "c2's logical assertion should not be retracted",
list.contains( new Person( c2.getType() ) ) );
assertFalse( "c3's logical assertion should be retracted",
list.contains( new Person( c3.getType() ) ) );
c2.setPrice( 3 );
h = getFactHandle( h, ksession );
ksession.update( h,
c2 );
ksession = getSerialisedStatefulKnowledgeSession( ksession,
true );
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
assertEquals( "c2 now has a higher price, its logical assertion should be cancelled",
1,
list.size() );
assertFalse( "The logical assertion cor c2 should have been retracted",
list.contains( new Person( c2.getType() ) ) );
assertTrue( "The logical assertion for c1 should exist",
list.contains( new Person( c1.getType() ) ) );
// different JVMs return the package list in different order
for( KnowledgePackage kpkg : kbase.getKnowledgePackages() ) {
if( kpkg.getName().equals( "org.drools.test" )) {
test = kpkg;
} else if( kpkg.getName().equals( "org.drools.test2" )) {
test2 = kpkg;
}
}
assertNotNull( test );
assertNotNull( test2 );
kbase.removeRule( test2.getName(),
test2.getRules().iterator().next().getName() );
kbase = SerializationHelper.serializeObject( kbase );
// different JVMs return the package list in different order
for( KnowledgePackage kpkg : kbase.getKnowledgePackages() ) {
if( kpkg.getName().equals( "org.drools.test" )) {
test = kpkg;
} else if( kpkg.getName().equals( "org.drools.test2" )) {
test2 = kpkg;
}
}
assertNotNull( test );
assertNotNull( test2 );
assertEquals( 0,
test.getRules().size() );
assertEquals( 0,
test2.getRules().size() );
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
assertEquals( 0,
list.size() );
}