use of org.kie.api.runtime.Channel in project drools by kiegroup.
the class StatelessSessionTest method testChannels.
@Test
public void testChannels() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import org.drools.mvel.compiler.Cheese \n";
str += "rule rule1 \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " \n";
str += " then \n";
str += " channels[\"x\"].send( $c ); \n";
str += "end\n";
final Cheese stilton = new Cheese("stilton", 5);
final Channel channel = Mockito.mock(Channel.class);
final StatelessKieSession ksession = getSession2(ResourceFactory.newByteArrayResource(str.getBytes()));
ksession.registerChannel("x", channel);
assertEquals(1, ksession.getChannels().size());
assertEquals(channel, ksession.getChannels().get("x"));
ksession.execute(stilton);
Mockito.verify(channel).send(stilton);
ksession.unregisterChannel("x");
assertEquals(0, ksession.getChannels().size());
assertNull(ksession.getChannels().get("x"));
}
use of org.kie.api.runtime.Channel in project drools by kiegroup.
the class WumpusWorldMain method init.
public void init(final KieContainer kc, boolean exitOnClose) {
final KieSession serverKsession = kc.newKieSession("WumpusMainKS");
final KieSession clientKsession = kc.newKieSession("WumpusClientKS");
serverKsession.getChannels().put("sensors", new Channel() {
public void send(Object object) {
clientKsession.insert(object);
clientKsession.fireAllRules();
}
});
clientKsession.getChannels().put("commands", new Channel() {
public void send(Object object) {
serverKsession.insert(object);
serverKsession.fireAllRules();
}
});
WumpusWorldConfiguration wumpusWorldConfiguration = new WumpusWorldConfiguration();
wumpusWorldConfiguration.setExitOnClose(exitOnClose);
serverKsession.setGlobal("wumpusWorldConfiguration", wumpusWorldConfiguration);
serverKsession.setGlobal("randomInteger", new java.util.Random());
GameUI gameUI = new GameUI(serverKsession, wumpusWorldConfiguration);
serverKsession.insert(gameUI);
serverKsession.insert(gameUI.getGameView());
new Thread(new Runnable() {
public void run() {
serverKsession.fireUntilHalt();
}
}).start();
new Thread(new Runnable() {
public void run() {
clientKsession.fireUntilHalt();
}
}).start();
}
Aggregations