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;
}
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();
}
}
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"));
}
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));
}
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());
}
Aggregations