Search in sources :

Example 6 with Channel

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"));
}
Also used : Channel(org.kie.api.runtime.Channel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 7 with Channel

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();
}
Also used : GameUI(org.drools.games.wumpus.view.GameUI) Channel(org.kie.api.runtime.Channel) KieSession(org.kie.api.runtime.KieSession)

Aggregations

Channel (org.kie.api.runtime.Channel)7 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)3 Cheese (org.drools.compiler.Cheese)2 Cheese (org.drools.mvel.compiler.Cheese)2 KieBase (org.kie.api.KieBase)2 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 JTextField (javax.swing.JTextField)1 MigLayout (net.miginfocom.swing.MigLayout)1 SessionConfiguration (org.drools.core.SessionConfiguration)1 GameFrame (org.drools.games.GameFrame)1 GameUI (org.drools.games.wumpus.view.GameUI)1 Globals (org.kie.api.runtime.Globals)1 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1