protected KieSession restoreSession(KieSession ksession, boolean noCache) { if (sessionPersistence) { long id = ksession.getIdentifier(); KieBase kbase = ksession.getKieBase(); Environment env = null; if (noCache) { env = createEnvironment(emf); } else { env = ksession.getEnvironment(); } if( pessimisticLocking ) { env.set(USE_PESSIMISTIC_LOCKING, true); } KieSessionConfiguration config = ksession.getSessionConfiguration(); config.setOption(ForceEagerActivationOption.YES); ksession.dispose(); StatefulKnowledgeSession result = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, config, env); AuditLoggerFactory.newInstance(Type.JPA, result, null); return result; } else { return ksession; } }
@Test public void testClockTypeConfiguration() { // setting the option using the type safe method config.setOption( ClockTypeOption.get("pseudo") ); // checking the type safe getOption() method assertEquals( ClockTypeOption.get("pseudo"), config.getOption( ClockTypeOption.class ) ); // checking the string based getProperty() method assertEquals( "pseudo", config.getProperty( ClockTypeOption.PROPERTY_NAME ) ); // setting the options using the string based setProperty() method config.setProperty( ClockTypeOption.PROPERTY_NAME, "realtime" ); // checking the type safe getOption() method assertEquals( ClockTypeOption.get("realtime"), config.getOption( ClockTypeOption.class ) ); // checking the string based getProperty() method assertEquals( "realtime", config.getProperty( ClockTypeOption.PROPERTY_NAME ) ); }
private void testEventsExpiredInThePast(final String drl) { final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); final KieHelper helper = new KieHelper(); helper.addContent( drl, ResourceType.DRL ); final KieBase kieBase = helper.build( EventProcessingOption.STREAM ); final KieSession kieSession = kieBase.newKieSession( sessionConfig, null ); PseudoClockScheduler clock = kieSession.getSessionClock(); final long currentTime = clock.getCurrentTime(); clock.advanceTime(100, TimeUnit.MILLISECONDS); kieSession.insert(new BasicEvent(new Date(currentTime + 20), 10L, "20ms-30ms")); clock.advanceTime(1, TimeUnit.MILLISECONDS); kieSession.insert(new BasicEvent(new Date(currentTime + 20), 20L, "20ms-40ms")); clock.advanceTime(100, TimeUnit.MILLISECONDS); Assertions.assertThat(kieSession.fireAllRules()).isEqualTo(1); clock.advanceTime(10, TimeUnit.MILLISECONDS); Assertions.assertThat(kieSession.getObjects()).isEmpty(); } }
"end\n"; KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM ); KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newKieSession( ksconf, null ); ksession.setGlobal( "list", list ); EntryPoint aep = ksession.getEntryPoint( "a-ep" ); aep.insert( new A() ); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 3, TimeUnit.SECONDS );
"end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); .newKieSession( sessionConfig, null ); PseudoClockScheduler sessionClock = ksession.getSessionClock(); sessionClock.setStartupTime( 0 ); ksession.setGlobal( "counter", counter ); new Thread( () -> ksession.fireUntilHalt() ).start();
"end"; final KieBaseConfiguration kbconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kbconfig.setOption(EventProcessingOption.STREAM); final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption(ClockTypeOption.get("REALTIME")); final KieSession ksession = kbase.newKieSession(conf, null); ksession.setGlobal("list", list); ksession.fireAllRules(); () -> { for (int j = 0; j < FACTS_PER_POLL; j++) { ksession.insert(new MyFact());
ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newStatefulKnowledgeSession( ksconf, null ); ksession.setGlobal( "list", list ); EntryPoint aep = ksession.getEntryPoint( "a-ep" ); aep.insert( new A() ); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 3, TimeUnit.SECONDS );
final KieBase kbase = helper.build(EventProcessingOption.STREAM); final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration(); ksconf.setOption(TimedRuleExecutionOption.YES); final KieSession ksession = kbase.newKieSession(ksconf, null); ksession.insert("xxx"); ksession.dispose(); ksession.halt();
private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString) throws Exception { KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption(EventProcessingOption.STREAM); KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString); KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksessionConfig.setOption(ClockTypeOption.get("pseudo")); ksessionConfig.setProperty("keep.reference", "true"); final KieSession ksession = kbase.newKieSession(ksessionConfig, null); ksession.addEventListener(agendaEventListener); PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock(); 100 * stIndex, 100 * stIndex); ksession.insert(st); Thread.sleep( 100 );
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption( ForceEagerActivationOption.YES ); KieSession ksession = kbase.newKieSession( conf, null ); try { ksession.insert( asList(new Person() ) ); ksession.insert("test"); assertEquals( 1, ksession.fireAllRules() ); } finally { ksession.dispose(); try { ksession = kbase.newKieSession( conf, null ); ksession.insert( asList(new Person() ) ); ksession.insert("test");
"end\n"; KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM ); KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newKieSession( ksconf, null ); ksession.insert( new A() ); ksession.insert( new B() ); ksession.fireAllRules(); assertEquals( 2, ksession.getObjects().size() );
KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel( KSESSION1 ).setDefault( true ) .setType( KieSessionModel.KieSessionType.STATEFUL ) .setClockType( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); KieBase kiebase = kc.getKieBase( KBASE1 ); KieSessionConfiguration sessionConf = ks.newKieSessionConfiguration(); sessionConf.setOption(ForceEagerActivationOption.YES); KieSession ksession = kc.newKieSession(KSESSION1, sessionConf); assertFalse( aggrMonitor.getStatsByRule().containsKey("ND2") ); ksession.insert("Ciao"); ksession.fireAllRules(); print(aggrMonitor); checkAgendaTotals(aggrMonitor, 2, 1, 1); checkStatsByRule(aggrMonitor,"ND2",1,0,1); ksession.fireAllRules(); print(aggrMonitor); checkAgendaTotals(aggrMonitor, 2, 1, 1);
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption(ForceEagerActivationOption.YES); .addContent(str, ResourceType.DRL) .build() .newKieSession(conf, null); try { final List list = new ArrayList(); ksession.addEventListener(agendaEventListener); ksession.insert("test"); assertEquals(2, list.size()); } finally { ksession.dispose();
final KieBase kbase = helper.build(EventProcessingOption.STREAM); final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration(); ksconf.setOption(TimedRuleExecutionOption.YES); final KieSession ksession = kbase.newKieSession(ksconf, null); LOG.info("before: while."); while (true) { ksession.insert("" + i++); ksession.fireAllRules();
config.setOption(ClockTypeOption.get(getClockType())); final KieSession ret = kbase.newKieSession(config, null); ret.setGlobal("epc", _context); ret.fireAllRules();
ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newStatefulKnowledgeSession( ksconf, null ); ksession.insert( new A() ); ksession.insert( new B() ); ksession.fireAllRules(); assertEquals( 2, ksession.getObjects().size() );
@Before public void setUp() throws Exception { baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); // use stream mode to enable proper event processing (see Drools Fusion 5.5.0 Doc "Event Processing Modes") baseConfig.setOption( EventProcessingOption.STREAM ); kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig); // config sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); // use a pseudo clock, which starts at 0 and can be advanced manually sessionConfig.setOption( ClockTypeOption.get("pseudo") ); // create and return session session = kbase.newKieSession(sessionConfig, null); clock = session.getSessionClock(); }
@Test public void testSimulationCommand() { PathFinder finder = PathFinderFactory.getInstance(this.getClass().getResourceAsStream("/BPMN2-ExclusiveSplit.bpmn2")); List<SimulationPath> paths = finder.findPaths(new SimulationFilterPathFormatConverter()); SimulationContext context = SimulationContextFactory.newContext(new HardCodedSimulationDataProvider()); KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newClassPathResource("BPMN2-ExclusiveSplit.bpmn2"), ResourceType.BPMN2); KieBase kbase = builder.newKieBase(); KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); config.setOption(ClockTypeOption.get("pseudo") ); for (SimulationPath sp : paths) { KieSession session = kbase.newKieSession(config, KieServices.get().newEnvironment()); ((SessionPseudoClock) session.getSessionClock()).advanceTime(System.currentTimeMillis(), TimeUnit.MILLISECONDS); session.execute(new SimulateProcessPathCommand("com.sample.test", context, sp)); } } }
@Test(timeout=10000) public void testEagerEvaluation() throws Exception { String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n"; KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption( ForceEagerActivationOption.YES ); KieBase kbase = loadKnowledgeBaseFromString(str); KieSession ksession = createKnowledgeSession(kbase, conf); final List list = new ArrayList(); AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() { public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) { list.add("activated"); } }; ksession.addEventListener(agendaEventListener); ksession.insert("test"); assertEquals(2, list.size()); }
public static KieSession createSession(String process) { KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newClassPathResource(process), ResourceType.BPMN2); KieBase kbase = builder.newKieBase(); KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); config.setOption(ClockTypeOption.get("pseudo") ); KieSession session = kbase.newKieSession(config, EnvironmentFactory.newEnvironment()); session.getEnvironment().set("NodeInstanceFactoryRegistry", SimulationNodeInstanceFactoryRegistry.getInstance()); return session; }