StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
ksession.setGlobal( "list",
list );
QueryResults results = null;
ksession.fireAllRules();
ksession.insert( "go1" );
ksession = getSerialisedStatefulKnowledgeSession( ksession,
true );
ksession.fireAllRules();
Map<String, Object> map = (Map) list.get( 0 );
assertEquals( "kitchen",
map.get( "place" ) );
List<String> items = (List<String>) map.get( "things" );
assertEquals( 3,
items.size() );
assertContains( new String[]{"apple", "broccoli", "crackers"},
items );
items = (List<String>) map.get( "food" );
assertEquals( 2,
items.size() );
assertContains( new String[]{"apple", "crackers"},
items );
items = (List<String>) map.get( "exits" );
assertEquals( 3,
items.size() );
assertContains( new String[]{"office", "cellar", "dining room"},
items );
ksession.insert( "go2" );
ksession = getSerialisedStatefulKnowledgeSession( ksession,
true );
ksession.fireAllRules();
map = (Map) list.get( 1 );
assertEquals( "office",
map.get( "place" ) );
items = (List<String>) map.get( "things" );
assertEquals( 2,
items.size() );
assertContains( new String[]{"computer", "desk",},
items );
items = (List<String>) map.get( "food" );
assertEquals( 1,
items.size() );
assertContains( new String[]{"apple"},
items ); // notice the apple is on the desk in the office
items = (List<String>) map.get( "exits" );
assertEquals( 2,
items.size() );
assertContains( new String[]{"hall", "kitchen"},
items );
results = ksession.getQueryResults( "isContainedIn",
new Object[]{"key", "office"} );
assertEquals( 1,
results.size() );
QueryResultsRow result = results.iterator().next();
assertEquals( "key",
result.get( "x" ) );
assertEquals( "office",
result.get( "y" ) );
results = ksession.getQueryResults( "isContainedIn",
new Object[]{"key", Variable.v} );
List<List<String>> l = new ArrayList<List<String>>();
for ( QueryResultsRow r : results ) {
l.add( Arrays.asList( new String[]{(String) r.get( "x" ), (String) r.get( "y" )} ) );
}
assertEquals( 3,
results.size() );
assertContains( Arrays.asList( new String[]{"key", "desk"} ),
l );
assertContains( Arrays.asList( new String[]{"key", "office"} ),
l );
assertContains( Arrays.asList( new String[]{"key", "envelope"} ),
l );
results = ksession.getQueryResults( "isContainedIn",
new Object[]{Variable.v, "office"} );
l = new ArrayList<List<String>>();
for ( QueryResultsRow r : results ) {
l.add( Arrays.asList( new String[]{(String) r.get( "x" ), (String) r.get( "y" )} ) );
}
assertEquals( 6,
results.size() );
assertContains( Arrays.asList( new String[]{"desk", "office"} ),
l );
assertContains( Arrays.asList( new String[]{"computer", "office"} ),
l );
assertContains( Arrays.asList( new String[]{"apple", "office"} ),
l );
assertContains( Arrays.asList( new String[]{"envelope", "office"} ),
l );
assertContains( Arrays.asList( new String[]{"flashlight", "office"} ),
l );
assertContains( Arrays.asList( new String[]{"key", "office"} ),
l );
results = ksession.getQueryResults( "isContainedIn",
new Object[]{Variable.v, Variable.v} );
l = new ArrayList<List<String>>();
for ( QueryResultsRow r : results ) {
l.add( Arrays.asList( new String[]{(String) r.get( "x" ), (String) r.get( "y" )} ) );
}
assertEquals( 17,
results.size() );
assertContains( Arrays.asList( new String[]{"apple", "kitchen"} ),
l );
assertContains( Arrays.asList( new String[]{"apple", "desk"} ),
l );
assertContains( Arrays.asList( new String[]{"envelope", "desk"} ),