Search in sources :

Example 16 with SessionConfiguration

use of org.drools.core.SessionConfiguration in project drools by kiegroup.

the class ProtobufMarshaller method unmarshall.

public StatefulKnowledgeSession unmarshall(final InputStream stream, KieSessionConfiguration config, Environment environment) throws IOException, ClassNotFoundException {
    if (config == null) {
        config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    }
    if (environment == null) {
        environment = KieServices.get().newEnvironment();
    }
    MarshallerReaderContext context = new MarshallerReaderContext(stream, (KnowledgeBaseImpl) kbase, RuleBaseNodes.getNodeMap((KnowledgeBaseImpl) kbase), this.strategyStore, TIMER_READERS, this.marshallingConfig.isMarshallProcessInstances(), this.marshallingConfig.isMarshallWorkItems(), environment);
    int id = ((KnowledgeBaseImpl) this.kbase).nextWorkingMemoryCounter();
    RuleBaseConfiguration conf = ((KnowledgeBaseImpl) this.kbase).getConfiguration();
    StatefulKnowledgeSessionImpl session = ProtobufInputMarshaller.readSession(context, id, environment, (SessionConfiguration) config, initializer);
    context.close();
    if (((SessionConfiguration) config).isKeepReference()) {
        ((KnowledgeBaseImpl) this.kbase).addStatefulSession(session);
    }
    return session;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 17 with SessionConfiguration

use of org.drools.core.SessionConfiguration in project drools by kiegroup.

the class KnowledgeBaseImpl method newKieSession.

public KieSession newKieSession(KieSessionConfiguration conf, Environment environment) {
    // NOTE if you update here, you'll also need to update the JPAService
    if (conf == null) {
        conf = getSessionConfiguration();
    }
    SessionConfiguration sessionConfig = (SessionConfiguration) conf;
    if (environment == null) {
        environment = EnvironmentFactory.newEnvironment();
    }
    if (this.getConfiguration().isSequential()) {
        throw new RuntimeException("Cannot have a stateful rule session, with sequential configuration set to true");
    }
    readLock();
    try {
        WorkingMemoryFactory wmFactory = kieComponentFactory.getWorkingMemoryFactory();
        StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) wmFactory.createWorkingMemory(nextWorkingMemoryCounter(), this, sessionConfig, environment);
        if (sessionConfig.isKeepReference()) {
            addStatefulSession(session);
        }
        return session;
    } finally {
        readUnlock();
    }
}
Also used : WorkingMemoryFactory(org.drools.core.common.WorkingMemoryFactory) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 18 with SessionConfiguration

use of org.drools.core.SessionConfiguration in project drools by kiegroup.

the class CustomWorkItemHandlerTest method testRegisterHandlerWithKsessionUsingConfiguration.

@Test
public void testRegisterHandlerWithKsessionUsingConfiguration() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    Properties props = new Properties();
    props.setProperty("drools.workItemHandlers", "CustomWorkItemHandlers.conf");
    KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(props);
    KieSession ksession = kbase.newKieSession(config, EnvironmentFactory.newEnvironment());
    assertNotNull(ksession);
    // this test would fail on creation of the work item manager if injecting session is not supported
    WorkItemManager manager = ksession.getWorkItemManager();
    assertNotNull(manager);
    Map<String, WorkItemHandler> handlers = ((SessionConfiguration) config).getWorkItemHandlers();
    assertNotNull(handlers);
    assertEquals(1, handlers.size());
    assertTrue(handlers.containsKey("Custom"));
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Properties(java.util.Properties) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) Test(org.junit.Test)

Example 19 with SessionConfiguration

use of org.drools.core.SessionConfiguration in project drools by kiegroup.

the class RuleTest method testTimeMachine.

@Test
public void testTimeMachine() {
    SessionConfiguration conf = SessionConfiguration.newInstance();
    conf.setClockType(ClockType.PSEUDO_CLOCK);
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession(conf, null);
    final Calendar future = Calendar.getInstance();
    ((PseudoClockScheduler) wm.getSessionClock()).setStartupTime(future.getTimeInMillis());
    final RuleImpl rule = new RuleImpl("myrule");
    rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    future.setTimeInMillis(future.getTimeInMillis() + 100000000);
    rule.setDateEffective(future);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    ((PseudoClockScheduler) wm.getSessionClock()).advanceTime(1000000000000L, TimeUnit.MILLISECONDS);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) SessionConfiguration(org.drools.core.SessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 20 with SessionConfiguration

use of org.drools.core.SessionConfiguration in project drools by kiegroup.

the class CronJobTest method testCronTriggerJob.

@Test
public void testCronTriggerJob() throws Exception {
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.PSEUDO_CLOCK);
    PseudoClockScheduler timeService = (PseudoClockScheduler) TimerServiceFactory.getTimerService(config);
    timeService.advanceTime(0, TimeUnit.MILLISECONDS);
    CronTrigger trigger = new CronTrigger(0, null, null, -1, "15 * * * * ?", null, null);
    HelloWorldJobContext ctx = new HelloWorldJobContext("hello world", timeService);
    timeService.scheduleJob(new HelloWorldJob(), ctx, trigger);
    assertEquals(0, ctx.getList().size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(0, ctx.getList().size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(1, ctx.getList().size());
    timeService.advanceTime(30, TimeUnit.SECONDS);
    assertEquals(1, ctx.getList().size());
    timeService.advanceTime(30, TimeUnit.SECONDS);
    assertEquals(2, ctx.getList().size());
}
Also used : HelloWorldJobContext(org.drools.core.time.impl.JDKTimerServiceTest.HelloWorldJobContext) HelloWorldJob(org.drools.core.time.impl.JDKTimerServiceTest.HelloWorldJob) SessionConfiguration(org.drools.core.SessionConfiguration) Test(org.junit.Test)

Aggregations

SessionConfiguration (org.drools.core.SessionConfiguration)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)17 KieSession (org.kie.api.runtime.KieSession)12 Test (org.junit.Test)11 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 KieBase (org.kie.api.KieBase)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Properties (java.util.Properties)3 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)3 TimerService (org.drools.core.time.TimerService)3 Trigger (org.drools.core.time.Trigger)3 ArrayList (java.util.ArrayList)2 WorkingMemory (org.drools.core.WorkingMemory)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)2 Ignore (org.junit.Ignore)2 KieHelper (org.kie.internal.utils.KieHelper)2 IOException (java.io.IOException)1