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(); } }
/** * This is a factory method for this Clock Type configuration. * The factory method is a best practice for the case where the * actual object construction is changed in the future. * * @param clockType the identifier for the clock type * * @return the actual type safe default clock type configuration. */ public static ClockTypeOption get( String clockType ) { return new ClockTypeOption( clockType ); }
"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 );
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 );
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(Arrays.asList(builder.getPackages())); conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); SessionPseudoClock clock = ( SessionPseudoClock) session.getSessionClock(); clock.advanceTime( 300, TimeUnit.MILLISECONDS ); session.setGlobal("myList", myList); ProcessInstance processInstance = ( ProcessInstance ) session.startProcess("org.drools.timer"); assertEquals(0, myList.size()); assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
"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);
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)); } } }
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; }
protected KieFileSystem createKieFileSystemWithKProject(KieServices ks) { KieModuleModel kproj = ks.newKieModuleModel(); KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase-test") .setDefault(true).addPackage("*") .setEqualsBehavior(EqualityBehaviorOption.EQUALITY) .setEventProcessingMode(EventProcessingOption.STREAM); kieBaseModel1.newKieSessionModel("ksession-test").setDefault(true) .setType(KieSessionModel.KieSessionType.STATEFUL) .setClockType(ClockTypeOption.get("realtime")); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(kproj.toXML()); return kfs; } }
private KieSession marsallStatefulKnowledgeSession(KieSession ksession) throws IOException, ClassNotFoundException { Globals globals = ksession.getGlobals(); KieBase kbase = ksession.getKieBase(); ByteArrayOutputStream out = new ByteArrayOutputStream(); MarshallerFactory.newMarshaller( kbase ).marshall( out, ksession ); KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); Environment env = EnvironmentFactory.newEnvironment(); env.set( EnvironmentName.GLOBALS, globals ); ksession = MarshallerFactory.newMarshaller( kbase ).unmarshall( new ByteArrayInputStream( out.toByteArray() ), ksconf, env ); return ksession; }
@Test public void testStatefulRealTimeClockKieSession() throws Exception { KieSession ksession = context.getBean("statefulSessionRealTime", KieSession.class); assertNotNull(ksession); assertEquals("Session has configured different clock type", ClockTypeOption.get("realtime"), ksession.getSessionConfiguration().getOption(ClockTypeOption.class)); assertTrue(String.format("Session clock not an instance of '~s', but: '~s'.", JDKTimerService.class.getSimpleName(), ksession.getSessionClock().getClass().getSimpleName()), ksession.getSessionClock() instanceof JDKTimerService); }
/** * Initialisation of Drools stateful session without convention and kodmule files. * * @param ruleFiles * DRL files */ public static synchronized void initFromRulesFiles(Set < String > ruleFiles) { if (isInitialized()) { throw new IllegalStateException("This Factory has already be initialized once"); } _instance = new FF4jDroolsService(); _instance.ruleFiles = ruleFiles; KieHelper helper = new KieHelper(); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId())); //ruleFiles.stream().forEach(drlFile -> helper.addContent(loadResourceAsString(drlFile), ResourceType.determineResourceType(drlFile))); for (String drlFile : ruleFiles) { String fileContent = loadResourceAsString(drlFile); ResourceType typeFile = ResourceType.determineResourceType(drlFile); helper.addContent(fileContent, typeFile); } _instance.ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null); }
baseModel.newKieSessionModel("defaultKSession") .setDefault(true) .setClockType(ClockTypeOption.get("pseudo")); ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(); clock = ksession.getSessionClock(); myGlobal = new ArrayList<>(); ksession.setGlobal("myGlobal", myGlobal);