Java Code Examples for org.kie.api.runtime.KieSession#registerChannel()

The following examples show how to use org.kie.api.runtime.KieSession#registerChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegrationInterfacesTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@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" );
}
 
Example 2
Source File: ShoppingCartServiceImpl.java    From drools-workshop with Apache License 2.0 6 votes vote down vote up
@Override
public String newShoppingCart() throws BusinessException {
    String cartId = UUID.randomUUID().toString();
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    conf.setAssertBehaviour(RuleBaseConfiguration.AssertBehaviour.EQUALITY);
    KieBase kBase = kContainer.newKieBase(conf);
    KieSession kSession = kBase.newKieSession();
    kSession.registerChannel("outboundChannel", new Channel() {

        @Override
        public void send(Object o) {
            System.out.println(o);
        }
    });
    shoppingCarts.put(cartId, kSession);
    return cartId;
}
 
Example 3
Source File: RegisterChannelCommand.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );

    ksession.registerChannel( name,
                              channel );

    return null;
}
 
Example 4
Source File: TestKieSessionUpdate.java    From hacep with Apache License 2.0 4 votes vote down vote up
@Test
public void testContainerUpdateAfterSerializationWindowLength() throws IOException, URISyntaxException {
    ArrayList<Object> globalList = new ArrayList<>();

    KieServices ks = KieServices.Factory.get();

    ReleaseIdImpl releaseIdV1 = new ReleaseIdImpl("it.redhat.jdg.v1", "rules", "1.0.0");
    KieContainer kieContainer = KieAPITestUtils.setupKieContainerFromTemplates(releaseIdV1, "rules/simple-rule.drl");

    KieSession kieSession = kieContainer.newKieSession();
    kieSession.registerChannel("additions", globalList::add);

    Assert.assertEquals(0, globalList.size());

    Fact fact = generateFactTenSecondsAfter(1L, 1L);
    KieSessionUtils.advanceClock(kieSession, fact);
    kieSession.insert(fact);
    kieSession.fireAllRules();

    Assert.assertEquals(1, globalList.size());
    Assert.assertEquals(1L, globalList.get(0));

    fact = generateFactTenSecondsAfter(1L, 2L);
    KieSessionUtils.advanceClock(kieSession, fact);
    kieSession.insert(fact);
    kieSession.fireAllRules();

    Assert.assertEquals(2, globalList.size());
    Assert.assertEquals(1L, globalList.get(0));
    Assert.assertEquals(3L, globalList.get(1));

    KieSessionByteArraySerializer serializer = new KieSessionByteArraySerializer();
    byte[] buffer = serializer.writeObject(createSerializableMarshaller(kieContainer.getKieBase()), kieSession);
    kieSession.dispose();

    ReleaseIdImpl releaseIdV2 = new ReleaseIdImpl("it.redhat.jdg.v2", "rules", "2.0.0");
    KieAPITestUtils.buildReleaseFromTemplates(releaseIdV2, "rules/simple-rule-v2.drl");

    KieSession serializedSession = serializer.readSession(createSerializableMarshaller(kieContainer.getKieBase()), buffer);
    serializedSession.registerChannel("additions", globalList::add);

    //KieContainer update should be done after unmarshall
    kieContainer.updateToVersion(releaseIdV2);

    fact = generateFactTenSecondsAfter(1L, 4L);
    KieSessionUtils.advanceClock(serializedSession, fact);
    serializedSession.insert(fact);
    serializedSession.fireAllRules();

    Assert.assertEquals(3, globalList.size());
    Assert.assertEquals(1L, globalList.get(0));
    Assert.assertEquals(3L, globalList.get(1));
    Assert.assertEquals(14L, globalList.get(2));
}
 
Example 5
Source File: TestSerializedRetractRules.java    From hacep with Apache License 2.0 3 votes vote down vote up
@Test
public void testSessionSerialization() {
    logger.info("Start test serialized rules");

    System.setProperty("grid.buffer", "10");

    RulesConfigurationTestImpl rulesConfigurationTest = RulesConfigurationTestImpl.RulesTestBuilder.buildRulesWithGamePlayRetract();

    RulesManager rulesManager = new RulesManager(rulesConfigurationTest);
    rulesManager.start(null, null, null);

    reset(outcomesChannel);

    KieSession kieSession = rulesManager.newKieSession();
    kieSession.registerChannel("outcomes", outcomesChannel);

    kieSession.insert(generateFactTenSecondsAfter(1));
    kieSession.fireAllRules();

    verify(outcomesChannel, times(1)).send(any());
    verifyNoMoreInteractions(outcomesChannel);

    reset(outcomesChannel);

    byte[] kieSessionBytes = rulesManager.serialize(kieSession);
    Assert.assertTrue(kieSessionBytes.length > 0);
    kieSession.dispose();

    KieSession kieSessionDeserialized = rulesManager.deserializeOrCreate(kieSessionBytes);
    kieSessionDeserialized.registerChannel("outcomes", outcomesChannel);

    kieSessionDeserialized.insert(generateFactTenSecondsAfter(1));
    kieSessionDeserialized.fireAllRules();

    verify(outcomesChannel, times(1)).send(any());
    verifyNoMoreInteractions(outcomesChannel);

    logger.info("End test serialized rules");
    rulesManager.stop();
}