}
@Test
public void remoteNodeRetractUpdateGlobalsTest() {
WhitePages wp = new WhitePagesImpl();
Grid grid1 = new GridImpl( new HashMap<String, Object>() );
configureGrid1( grid1,
8000,
wp );
Grid grid2 = new GridImpl( new HashMap<String, Object>() );
configureGrid1( grid2,
-1,
wp );
GridNode n1 = grid1.createGridNode( "n1" );
grid1.get( SocketService.class ).addService( "n1", 8000, n1 );
GridServiceDescription<GridNode> n1Gsd = grid2.get( WhitePages.class ).lookup( "n1" );
GridConnection<GridNode> conn = grid2.get( ConnectionFactoryService.class ).createConnection( n1Gsd );
GridNode remoteN1 = conn.connect();
KnowledgeBuilder kbuilder = remoteN1.get( KnowledgeBuilderFactoryService.class ).newKnowledgeBuilder();
Assert.assertNotNull( kbuilder );
String rule = "package test\n"
+ "import org.drools.grid.NodeTests.MyObject;\n"
+ "global MyObject myGlobalObj;\n"
+ "global org.drools.grid.Grid grid; \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();
Assert.assertNotNull( kbase );
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
Assert.assertNotNull( session );
session.setGlobal("myGlobalObj", new MyObject("myGlobalObj"));
FactHandle handle = session.insert( new MyObject("myObj1") );
Assert.assertNotNull( handle );
int fired = session.fireAllRules();
Assert.assertEquals( 1,
fired );
session.retract(handle);
handle = session.insert(new MyObject("myObj2"));
session.update(handle, new MyObject("myObj3"));
fired = session.fireAllRules();
grid1.dispose();
grid2.dispose();
assertTrue( remoteN1.isDisposed() );
assertTrue( n1.isDisposed() );
}