org.kie.api.event.rule.AfterMatchFiredEvent Java Examples
The following examples show how to use
org.kie.api.event.rule.AfterMatchFiredEvent.
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: TrackingAgendaEventListener.java From qzr with Apache License 2.0 | 6 votes |
@Override public void afterMatchFired(AfterMatchFiredEvent event) { Rule rule = event.getMatch().getRule(); String ruleName = rule.getName(); Map<String, Object> ruleMetaDataMap = rule.getMetaData(); addActivation(new Activation(ruleName)); StringBuilder sb = new StringBuilder("Rule fired: " + ruleName); if (ruleMetaDataMap.size() > 0) { sb.append("\n With [" + ruleMetaDataMap.size() + "] meta-data:"); for (String key : ruleMetaDataMap.keySet()) { sb.append("\n key=" + key + ", value=" + ruleMetaDataMap.get(key)); } } log.debug(sb.toString()); }
Example #2
Source File: KieLoggersTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testKieConsoleLoggerStateless() throws Exception { String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end"; // get the resource Resource dt = ResourceFactory.newByteArrayResource( drl.getBytes() ).setTargetPath("org/drools/integrationtests/hello.drl"); // create the builder StatelessKieSession ksession = getStatelessKieSession(dt); KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newConsoleLogger( ksession ); AgendaEventListener ael = mock( AgendaEventListener.class ); ksession.addEventListener( ael ); ksession.execute( new Message("Hello World") ); verify( ael ).afterMatchFired( any(AfterMatchFiredEvent.class) ); logger.close(); }
Example #3
Source File: PseudoClockEventsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testEvenFirePseudoClockRulesAB() throws Exception { AgendaEventListener ael = mock(AgendaEventListener.class); processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA + evalFirePseudoClockRuleB); final int expectedActivationCount = evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2 + evalFirePseudoClockStockCount - 1; verify(ael, times(expectedActivationCount)).afterMatchFired( any(AfterMatchFiredEvent.class)); }
Example #4
Source File: PrometheusMetricsDroolsListener.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Override public void afterMatchFired(AfterMatchFiredEvent event) { AfterActivationFiredEventImpl afterImpl = getAfterImpl(event); BeforeActivationFiredEventImpl beforeImpl = getBeforeImpl(afterImpl.getBeforeMatchFiredEvent()); long startTime = beforeImpl.getTimestamp(); long elapsed = System.nanoTime() - startTime; String ruleName = event.getMatch().getRule().getName(); getDroolsEvaluationTimeHistogram() .labels(identifier, ruleName) .observe(elapsed); if (logger.isDebugEnabled()) { logger.debug("Elapsed time: " + elapsed); } }
Example #5
Source File: BusinessRuleTaskTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testBasicBusinessRuleTaskWithAgendaListener() throws Exception { Application app = generateCode(Collections.singletonList("ruletask/BusinessRuleTask.bpmn2"), Collections.singletonList("ruletask/BusinessRuleTask.drl")); assertThat(app).isNotNull(); final AtomicInteger counter = new AtomicInteger(); ((DefaultRuleEventListenerConfig)app.config().rule().ruleEventListeners()).register(new DefaultAgendaEventListener() { @Override public void afterMatchFired(AfterMatchFiredEvent event) { counter.incrementAndGet(); } }); Process<? extends Model> p = app.processes().processById("BusinessRuleTask"); Model m = p.createModel(); m.fromMap(Collections.singletonMap("person", new Person("john", 25))); ProcessInstance<?> processInstance = p.createInstance(m); processInstance.start(); assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); Model result = (Model)processInstance.variables(); assertThat(result.toMap()).hasSize(1).containsKey("person"); assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true); assertThat(counter.get()).isEqualTo(1); }
Example #6
Source File: BusinessRuleUnitTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@ParameterizedTest @MethodSource("processes") public void testBasicBusinessRuleUnitWithAgendaListener(String bpmnPath) throws Exception { Application app = generateCode(Collections.singletonList(bpmnPath), Collections.singletonList("org/kie/kogito/codegen/tests/BusinessRuleUnit.drl")); assertThat(app).isNotNull(); final AtomicInteger counter = new AtomicInteger(); ((DefaultRuleEventListenerConfig)app.config().rule().ruleEventListeners()).register(new DefaultAgendaEventListener() { @Override public void afterMatchFired(AfterMatchFiredEvent event) { counter.incrementAndGet(); } }); Process<? extends Model> p = app.processes().processById("BusinessRuleUnit"); Model m = p.createModel(); m.fromMap(Collections.singletonMap("person", new Person("john", 25))); ProcessInstance<?> processInstance = p.createInstance(m); processInstance.start(); assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); Model result = (Model)processInstance.variables(); assertThat(result.toMap()).hasSize(1).containsKey("person"); assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true); assertThat(counter.get()).isEqualTo(1); }
Example #7
Source File: AgendaEventSupport.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public void fireAfterActivationFired(final Activation activation, final InternalWorkingMemory workingMemory, BeforeMatchFiredEvent beforeMatchFiredEvent) { Iterator<AgendaEventListener> iter = getEventListenersIterator(); AfterMatchFiredEvent event = new AfterActivationFiredEventImpl(activation, getKRuntime(workingMemory), beforeMatchFiredEvent); if (iter.hasNext()) { do{ iter.next().afterMatchFired(event); } while (iter.hasNext()); } }
Example #8
Source File: ComplianceCheckAgendaEventListener.java From cia with Apache License 2.0 | 5 votes |
@Override public void afterMatchFired(final AfterMatchFiredEvent event) { super.afterMatchFired(event); final AbstractComplianceCheckImpl check = (AbstractComplianceCheckImpl) ((DefaultFactHandle) event.getMatch() .getFactHandles().stream().findFirst().orElse(null)).getObject(); complianceChecks.put(check.getId(), check); }
Example #9
Source File: GenericKieSessionMonitoringImpl.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public void afterMatchFired(AfterMatchFiredEvent event) { AgendaStatsData data = getRuleStatsInstance( event.getMatch().getRule().getName() ); this.consolidated.stopFireClock(); data.stopFireClock(); this.consolidated.matchesFired.incrementAndGet(); data.matchesFired.incrementAndGet(); }
Example #10
Source File: WorkingMemoryLogger.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * @see org.kie.api.event.rule.AgendaEventListener */ public void afterMatchFired(final AfterMatchFiredEvent event) { filterLogEvent( new ActivationLogEvent( LogEvent.AFTER_ACTIVATION_FIRE, getActivationId( event.getMatch() ), event.getMatch().getRule().getName(), extractDeclarations( event.getMatch() ), ((RuleImpl)event.getMatch().getRule()).getRuleFlowGroup(), extractFactHandleIds( (Activation) event.getMatch() ) ) ); }
Example #11
Source File: PublishingAgendaEventListener.java From qzr with Apache License 2.0 | 5 votes |
/** * Whenever a rule's LHS matches and causes the rule to activate, this * method will publish the name of that rule to the Stomp message broker, * using the {@link SimpMessagingTemplate}. */ @Override public void afterMatchFired(AfterMatchFiredEvent event) { Rule rule = event.getMatch().getRule(); System.out.println("Publishing: " + rule.getName()); AgendaEventMessage msg = new AgendaEventMessage(rule.getName()); this.template.convertAndSend("/queue/agendaevents/", msg); }
Example #12
Source File: MapConstraintTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMapNullConstraint() throws Exception { final KieBase kbase = loadKnowledgeBase("test_mapNullConstraints.drl"); final KieSession ksession = createKnowledgeSession(kbase); final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class); ksession.addEventListener(ael); new WorkingMemoryConsoleLogger((WorkingMemory) ksession); final Map addresses = new HashMap(); addresses.put("home", new Address("home street")); final Person bob = new Person("Bob"); bob.setNamedAddresses(addresses); ksession.insert(bob); ksession.fireAllRules(); final ArgumentCaptor<AfterMatchFiredEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class); verify(ael, times(4)).afterMatchFired(arg.capture()); org.kie.api.event.rule.AfterMatchFiredEvent aaf = arg.getAllValues().get(0); assertThat(aaf.getMatch().getRule().getName()).isEqualTo("1. home != null"); aaf = arg.getAllValues().get(1); assertThat(aaf.getMatch().getRule().getName()).isEqualTo("2. not home == null"); aaf = arg.getAllValues().get(2); assertThat(aaf.getMatch().getRule().getName()).isEqualTo("7. work == null"); aaf = arg.getAllValues().get(3); assertThat(aaf.getMatch().getRule().getName()).isEqualTo("8. not work != null"); }
Example #13
Source File: PseudoClockEventsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testEvenFirePseudoClockRuleB() throws Exception { AgendaEventListener ael = mock(AgendaEventListener.class); processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleB); verify(ael, times(evalFirePseudoClockStockCount - 1)).afterMatchFired( any(AfterMatchFiredEvent.class)); }
Example #14
Source File: PseudoClockEventsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testEvenFirePseudoClockRuleA() throws Exception { AgendaEventListener ael = mock(AgendaEventListener.class); processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA); verify(ael, times(evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2)).afterMatchFired( any(AfterMatchFiredEvent.class)); }
Example #15
Source File: LoggingAgendaEventListener.java From qzr with Apache License 2.0 | 5 votes |
@Override public void afterMatchFired(AfterMatchFiredEvent event) { Rule rule = event.getMatch().getRule(); StringBuilder sb = new StringBuilder("Rule fired: " + rule.getName()); log.debug(sb.toString()); }
Example #16
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testOrWithVariableResolution2() throws Exception { // KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl"); // KieSession workingMemory = createKnowledgeSession(kbase); final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); final KieSession ksession = createKnowledgeSession(kbase); final AgendaEventListener al = mock( AgendaEventListener.class ); ksession.addEventListener( al ); ksession.insert( new FactA( "a" ) ); ksession.insert( new FactB( "b" ) ); ksession.insert( new FactC( "c" ) ); ksession.fireAllRules(); verify( al, times( 8 ) ).afterMatchFired(any(AfterMatchFiredEvent.class)); }
Example #17
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testOrWithVariableResolution() throws Exception { // KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl"); // KieSession workingMemory = createKnowledgeSession(kbase); final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_OrCEFollowedByMultipleEval.drl", FirstOrderLogicTest.class ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); final KieSession ksession = createKnowledgeSession(kbase); final AgendaEventListener al = mock( AgendaEventListener.class ); ksession.addEventListener( al ); ksession.insert( new FactA( "a" ) ); ksession.insert( new FactB( "b" ) ); ksession.insert( new FactC( "c" ) ); ksession.fireAllRules(); verify( al, times( 6 ) ).afterMatchFired(any(AfterMatchFiredEvent.class)); }
Example #18
Source File: WorkingMemoryActionsSerializationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@BeforeEach public void before() { ruleCalls.clear(); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase( ); kbase.addPackages( kbuilder.getKnowledgePackages() ); ksession = kbase.newKieSession(); ksession.addEventListener(new DefaultAgendaEventListener() { @Override public void afterMatchFired(final AfterMatchFiredEvent event) { final String name = event.getMatch().getRule().getName(); System.out.println(name + " fired!"); synchronized (ruleCalls) { Integer calls = ruleCalls.get(name); if (calls == null) { calls = 1; } else { calls++; } ruleCalls.put(name, calls); } } }); // Using fire until halt. If firaAllRules is called it works. But for other reasons, I need to run fireUntilHalt new Thread(ksession::fireUntilHalt).start(); }
Example #19
Source File: DebugAgendaEventListener.java From servicemix with Apache License 2.0 | 4 votes |
/** * @see AgendaEventListener#afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent) */ @Override public void afterMatchFired(AfterMatchFiredEvent event) { log.info("{}", event); }
Example #20
Source File: CepEngineImpl.java From hawkular-alerts with Apache License 2.0 | 4 votes |
@Override public void afterMatchFired(AfterMatchFiredEvent event) { log.debug(event); }
Example #21
Source File: DebugAgendaEventListener.java From servicemix with Apache License 2.0 | 4 votes |
/** * @see AgendaEventListener#afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent) */ @Override public void afterMatchFired(AfterMatchFiredEvent event) { log.info("{}", event); }
Example #22
Source File: DebugAgendaEventListener.java From servicemix with Apache License 2.0 | 4 votes |
/** * @see AgendaEventListener#afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent) */ @Override public void afterMatchFired(AfterMatchFiredEvent event) { log.info("{}", event); }
Example #23
Source File: RuleCoverageListener.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public void afterMatchFired(AfterMatchFiredEvent event) { rules.remove(event.getMatch().getRule().getName()); }
Example #24
Source File: PrometheusMetricsDroolsListener.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public AfterActivationFiredEventImpl getAfterImpl(AfterMatchFiredEvent e) { return (AfterActivationFiredEventImpl)e; }
Example #25
Source File: DebugAgendaEventListener.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public void afterMatchFired(AfterMatchFiredEvent event) { logger.info( event.toString() ); }
Example #26
Source File: DefaultAgendaEventListener.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public void afterMatchFired(AfterMatchFiredEvent event) { // intentionally left blank }
Example #27
Source File: DescrBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testTopLevelAccumulate() throws InstantiationException, IllegalAccessException { PackageDescr pkg = DescrFactory.newPackage() .name( "org.drools.compiler" ) .newRule().name( "r1" ) .lhs() .accumulate() .source() .pattern("StockTick").constraint( "company == \"RHT\"" ).bind( "$p", "price", false ).end() .end() .function( "sum", "$sum", false, "$p" ) .function( "count", "$cnt", false, "$p" ) .end() .end() .rhs( "// some comment" ) .end() .getDescr(); KiePackage kpkg = compilePkgDescr( pkg ); assertEquals( "org.drools.compiler", kpkg.getName() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( Collections.singletonList( kpkg ) ); KieSession ksession = kbase.newKieSession(); AgendaEventListener ael = mock( AgendaEventListener.class ); ksession.addEventListener( ael ); ksession.insert( new StockTick(1, "RHT", 80, 1 ) ); ksession.insert( new StockTick(2, "RHT", 100, 10 ) ); int rules = ksession.fireAllRules(); assertEquals( 1, rules ); ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass( AfterMatchFiredEvent.class ); verify( ael ).afterMatchFired(cap.capture()); assertThat((Number)cap.getValue().getMatch().getDeclarationValue( "$sum" )).isEqualTo(180.0); assertThat((Number)cap.getValue().getMatch().getDeclarationValue( "$cnt" )).isEqualTo(2L); }
Example #28
Source File: DynamicRulesTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDynamicRulesWithTypeDeclarations() { String type = "package com.sample\n" + "declare type Foo\n" + " id : int\n" + "end\n"; String r1 = "package com.sample\n" + "rule R1 when\n" + " not Foo()\n" + "then\n" + " insert( new Foo(1) );\n" + "end\n"; String r2 = "package com.sample\n" + "rule R2 when\n" + " $f : Foo()\n" + "then\n" + " $f.setId( 2 );\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( type.getBytes() ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase( ); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = kbase.newKieSession(); AgendaEventListener ael = mock( AgendaEventListener.class ); ksession.addEventListener( ael ); ksession.fireAllRules(); verify( ael, never() ).afterMatchFired( any( AfterMatchFiredEvent.class ) ); kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( kbase ); kbuilder.add( ResourceFactory.newByteArrayResource( r1.getBytes() ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); ksession.fireAllRules(); ArgumentCaptor<AfterMatchFiredEvent> capt = ArgumentCaptor.forClass( AfterMatchFiredEvent.class ); verify( ael, times(1) ).afterMatchFired( capt.capture() ); assertThat( "R1").isEqualTo(capt.getValue().getMatch().getRule().getName()); kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( kbase ); kbuilder.add( ResourceFactory.newByteArrayResource( r2.getBytes() ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); ksession.fireAllRules(); verify( ael, times(2) ).afterMatchFired( capt.capture() ); assertThat( "R2").isEqualTo(capt.getAllValues().get( 2 ).getMatch().getRule().getName()); ksession.dispose(); }
Example #29
Source File: ListenersTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Override public void afterMatchFired(final AfterMatchFiredEvent event) { super.afterMatchFired(event); this.fired.compareAndSet(false, true); }
Example #30
Source File: JBRULESTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testJBRULES3111() { final String str = "package org.drools.compiler\n" + "declare Bool123\n" + " bool1 : boolean\n" + " bool2 : boolean\n" + " bool3 : boolean\n" + "end\n" + "declare Thing\n" + " name : String\n" + " bool123 : Bool123\n" + "end\n" + "rule kickOff\n" + "when\n" + "then\n" + " insert( new Thing( \"one\", new Bool123( true, false, false ) ) );\n" + " insert( new Thing( \"two\", new Bool123( false, false, false ) ) );\n" + " insert( new Thing( \"three\", new Bool123( false, false, false ) ) );\n" + "end\n" + "rule r1\n" + "when\n" + " $t: Thing( bool123.bool1 == true )\n" + "then\n" + "end\n" + "rule r2\n" + "when\n" + " $t: Thing( bool123.bool2 == true )\n" + "then\n" + "end\n" + "rule r3\n" + "when\n" + " $t: Thing( bool123.bool3 == true )\n" + "then\n" + "end"; final KieBase kbase = loadKnowledgeBaseFromString(str); final KieSession ksession = createKnowledgeSession(kbase); final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class); ksession.addEventListener(ael); final int rulesFired = ksession.fireAllRules(); assertEquals(2, rulesFired); final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class); verify(ael, times(2)).afterMatchFired(captor.capture()); final List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues(); assertThat(aafe.get(0).getMatch().getRule().getName()).isEqualTo("kickOff"); assertThat(aafe.get(1).getMatch().getRule().getName()).isEqualTo("r1"); final Object value = aafe.get(1).getMatch().getDeclarationValue("$t"); final String name = (String) MVEL.eval("$t.name", Collections.singletonMap("$t", value)); assertThat(name).isEqualTo("one"); }