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
KnowledgePackage[] pkgs = (KnowledgePackage[]) kbase.getKnowledgePackages().toArray( new KnowledgePackage[]{} );
assertEquals( "org.drools.test",
pkgs[0].getName() );
assertEquals( "org.drools.test2",
pkgs[1].getName() );
assertEquals( "rule1",
pkgs[0].getRules().iterator().next().getName() );
assertEquals( "rule2",
pkgs[1].getRules().iterator().next().getName() );
// now remove the first rule
kbase.removeRule( pkgs[0].getName(),
pkgs[0].getRules().iterator().next().getName() );
pkgs = (KnowledgePackage[]) kbase.getKnowledgePackages().toArray( new KnowledgePackage[]{} );
// Check the rule was correctly remove
assertEquals( 0,
pkgs[0].getRules().size() );
assertEquals( 1,
pkgs[1].getRules().size() );
assertEquals( "org.drools.test2",
pkgs[1].getName() );
assertEquals( "rule2",
pkgs[1].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() ) ) );
pkgs = (KnowledgePackage[]) kbase.getKnowledgePackages().toArray( new KnowledgePackage[]{} );
kbase.removeRule( pkgs[1].getName(),
pkgs[1].getRules().iterator().next().getName() );
kbase = SerializationHelper.serializeObject( kbase );
pkgs = (KnowledgePackage[]) kbase.getKnowledgePackages().toArray( new KnowledgePackage[]{} );
assertEquals( 0,
pkgs[0].getRules().size() );
assertEquals( 0,
pkgs[1].getRules().size() );
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
assertEquals( 0,
list.size() );
}