Grid grid3 = (Grid) context.getBean( "grid3" );
assertTrue( grid3.get( WhitePages.class ) instanceof WhitePagesImpl );
GridServiceDescription<GridNode> n1Gsd = grid2.get( WhitePages.class ).lookup( "node1" );
GridConnection<GridNode> conn = grid2.get( ConnectionFactoryService.class ).createConnection( n1Gsd );
GridNode remoteN1 = conn.connect();
KnowledgeBuilder kbuilder = remoteN1.get( KnowledgeBuilderFactoryService.class ).newKnowledgeBuilder();
assertNotNull( kbuilder );
String rule = "package test\n"
+ "import org.drools.container.spring.SpringDroolsGridTest.MyObject;\n"
+ "global MyObject myGlobalObj;\n"
+ "rule \"test\""
+ " when"
+ " $o: MyObject()"
+ " then"
+ " System.out.println(\"My Global Object -> \"+myGlobalObj.getName());"
+ " System.out.println(\"Rule Fired! ->\"+$o.getName());"
+ " end";
kbuilder.add( new ByteArrayResource( rule.getBytes() ),
ResourceType.DRL );
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if ( errors != null && errors.size() > 0 ) {
for ( KnowledgeBuilderError error : errors ) {
System.out.println( "Error: " + error.getMessage() );
}
fail( "KnowledgeBase did not build" );
}
KnowledgeBase kbase = remoteN1.get( KnowledgeBaseFactoryService.class ).newKnowledgeBase();
assertNotNull( kbase );
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
assertNotNull( session );
session.setGlobal( "myGlobalObj",
new MyObject( "myGlobalObj" ) );
FactHandle handle = session.insert( new MyObject( "myObj1" ) );
assertNotNull( handle );
int fired = session.fireAllRules();
assertEquals( 1,
fired );
session.retract( handle );
handle = session.insert( new MyObject( "myObj2" ) );
session.update( handle,
new MyObject( "myObj3" ) );
fired = session.fireAllRules();
remoteN1.dispose();
grid1.get( SocketService.class ).close();
}