use of org.kie.api.runtime.Channel in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method newWorkingMemory.
public StatefulKnowledgeSession newWorkingMemory() {
this.kBase.readLock();
try {
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) wmFactory.createWorkingMemory(this.kBase.nextWorkingMemoryCounter(), this.kBase, (SessionConfiguration) this.conf, this.environment);
StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
((Globals) ksessionImpl.getGlobalResolver()).setDelegate(this.sessionGlobals);
registerListeners(ksessionImpl);
for (Map.Entry<String, Channel> entry : this.channels.entrySet()) {
ksession.registerChannel(entry.getKey(), entry.getValue());
}
wmCreated.incrementAndGet();
return ksession;
} finally {
this.kBase.readUnlock();
}
}
use of org.kie.api.runtime.Channel in project drools by kiegroup.
the class GameUI method initialize.
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new GameFrame("Wumpus World");
frame.getContentPane().setBackground(Color.WHITE);
frame.setDefaultCloseOperation(wumpusWorldConfiguration.isExitOnClose() ? JFrame.EXIT_ON_CLOSE : JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new MigLayout("", "[540px:n][grow,fill]", "[30px,top][300px,top][100px,top][grow]"));
frame.setSize(926, 603);
// Center in screen
frame.setLocationRelativeTo(null);
JPanel scorePanel = new JPanel();
FlowLayout flowLayout = (FlowLayout) scorePanel.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
scorePanel.setBackground(Color.WHITE);
frame.getContentPane().add(scorePanel, "cell 0 0,grow");
JLabel lblScore = new JLabel("Score");
scorePanel.add(lblScore);
final JTextField txtScore = new JTextField();
gameView.getKsession().getChannels().put("score", new Channel() {
public void send(Object object) {
txtScore.setText("" + ((Score) object).getValue());
}
});
txtScore.setEditable(false);
scorePanel.add(txtScore);
txtScore.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, "cell 1 0 1 4,grow");
JPanel actionPanel = new JPanel();
actionPanel.setBackground(Color.WHITE);
frame.getContentPane().add(actionPanel, "cell 0 1,grow");
actionPanel.setLayout(new MigLayout("", "[200px,left][320px:n]", "[grow]"));
JPanel controls = new JPanel();
controls.setBackground(Color.WHITE);
controls.setLayout(new MigLayout("", "[grow,fill]", "[::100px,top][200px,top]"));
controls.add(drawActionPanel(), "cell 0 0,alignx left,aligny top");
controls.add(drawMovePanel(), "cell 0 1,alignx left,growy");
actionPanel.add(controls, "cell 0 0,grow");
cavePanel = drawCave();
actionPanel.add(cavePanel, "cell 1 0,grow");
sensorPanel = drawSensorPanel();
frame.getContentPane().add(sensorPanel, "cell 0 2,grow");
JPanel blank = new JPanel();
blank.setBackground(Color.WHITE);
frame.add(blank, "cell 0 3,grow");
frame.setVisible(true);
cavePanel.getBufferedImage();
sensorPanel.getBufferedImage();
repaint();
}
use of org.kie.api.runtime.Channel in project drools by kiegroup.
the class IntegrationInterfacesTest method testChannels.
@Test
public void testChannels() throws IOException, ClassNotFoundException {
KieBase kbase = getKnowledgeBase("test_Channels.drl");
KieSession ksession = createKnowledgeSession(kbase);
Channel someChannel = mock(Channel.class);
ksession.registerChannel("someChannel", someChannel);
ksession.insert(new Cheese("brie", 30));
ksession.insert(new Cheese("stilton", 5));
ksession.fireAllRules();
verify(someChannel).send("brie");
verify(someChannel, never()).send("stilton");
}
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.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