final RuleBase ruleBase = getRuleBase();
ruleBase.addPackage( pkg );
final WorkingMemory workingMemory = ruleBase.newStatefulSession();
final Person p1 = new Person( "p1",
"stilton",
20 );
p1.setStatus( "europe" );
final FactHandle c1FactHandle = workingMemory.insert( p1 );
final Person p2 = new Person( "p2",
"stilton",
30 );
p2.setStatus( "europe" );
final FactHandle c2FactHandle = workingMemory.insert( p2 );
final Person p3 = new Person( "p3",
"stilton",
40 );
p3.setStatus( "europe" );
final FactHandle c3FactHandle = workingMemory.insert( p3 );
workingMemory.fireAllRules();
QueryResults queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 2,
queryResults.size() );
// europe=[ 1, 2 ], america=[ 3 ]
p3.setStatus( "america" );
workingMemory.update( c3FactHandle,
p3 );
workingMemory.fireAllRules();
queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 1,
queryResults.size() );
// europe=[ 1 ], america=[ 2, 3 ]
p2.setStatus( "america" );
workingMemory.update( c2FactHandle,
p2 );
workingMemory.fireAllRules();
queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 1,
queryResults.size() );
// europe=[ ], america=[ 1, 2, 3 ]
p1.setStatus( "america" );
workingMemory.update( c1FactHandle,
p1 );
workingMemory.fireAllRules();
queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 2,
queryResults.size() );
// europe=[ 2 ], america=[ 1, 3 ]
p2.setStatus( "europe" );
workingMemory.update( c2FactHandle,
p2 );
workingMemory.fireAllRules();
queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 1,
queryResults.size() );
// europe=[ 1, 2 ], america=[ 3 ]
p1.setStatus( "europe" );
workingMemory.update( c1FactHandle,
p1 );
workingMemory.fireAllRules();
queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 1,
queryResults.size() );
// europe=[ 1, 2, 3 ], america=[ ]
p3.setStatus( "europe" );
workingMemory.update( c3FactHandle,
p3 );
workingMemory.fireAllRules();
queryResults = workingMemory.getQueryResults( "2 persons with the same status" );
assertEquals( 2,