Search in sources :

Example 1 with Channel

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();
    }
}
Also used : Globals(org.kie.api.runtime.Globals) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Channel(org.kie.api.runtime.Channel) HashMap(java.util.HashMap) Map(java.util.Map) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 2 with Channel

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();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GameFrame(org.drools.games.GameFrame) MigLayout(net.miginfocom.swing.MigLayout) Channel(org.kie.api.runtime.Channel) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField)

Example 3 with Channel

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

Example 4 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.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.compiler.Cheese) Test(org.junit.Test)

Example 5 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