org.kie.api.event.rule.RuleRuntimeEventListener Java Examples

The following examples show how to use org.kie.api.event.rule.RuleRuntimeEventListener. 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: InjectionHelper.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private static void wireListeners(BeanCreator beanCreator, BeanCreator fallbackBeanCreator, ClassLoader cl, List<ListenerModel> listenerModels, KieRuntimeEventManager kRuntimeEventManager) {
	for (ListenerModel listenerModel : listenerModels) {
        Object listener = createListener( beanCreator, fallbackBeanCreator, cl, listenerModel );
        switch(listenerModel.getKind()) {
            case AGENDA_EVENT_LISTENER:
                kRuntimeEventManager.addEventListener((AgendaEventListener)listener);
                break;
            case RULE_RUNTIME_EVENT_LISTENER:
                kRuntimeEventManager.addEventListener((RuleRuntimeEventListener)listener);
                break;
            case PROCESS_EVENT_LISTENER:
                kRuntimeEventManager.addEventListener((ProcessEventListener)listener);
                break;
        }
    }
}
 
Example #2
Source File: RuleConfigGenerator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private MethodDeclaration generateMergeEventListenerConfigMethod() {
    BlockStmt body = new BlockStmt().addStatement(new ReturnStmt(newObject(CachedRuleEventListenerConfig.class,
            callMerge(
                    VAR_RULE_EVENT_LISTENER_CONFIGS,
                    RuleEventListenerConfig.class, "agendaListeners",
                    VAR_AGENDA_EVENT_LISTENERS
            ),
            callMerge(
                    VAR_RULE_EVENT_LISTENER_CONFIGS,
                    RuleEventListenerConfig.class, "ruleRuntimeListeners",
                    VAR_RULE_RUNTIME_EVENT_LISTENERS
            )
    )));

    return method(Modifier.Keyword.PRIVATE, RuleEventListenerConfig.class, METHOD_MERGE_RULE_EVENT_LISTENER_CONFIG,
            NodeList.nodeList(
                    new Parameter().setType(genericType(Collection.class, RuleEventListenerConfig.class)).setName(VAR_RULE_EVENT_LISTENER_CONFIGS),
                    new Parameter().setType(genericType(Collection.class, AgendaEventListener.class)).setName(VAR_AGENDA_EVENT_LISTENERS),
                    new Parameter().setType(genericType(Collection.class, RuleRuntimeEventListener.class)).setName(VAR_RULE_RUNTIME_EVENT_LISTENERS)
            ),
            body);
}
 
Example #3
Source File: RuleConfigGenerator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public List<BodyDeclaration<?>> members() {

        if (annotator != null) {
            FieldDeclaration relcFieldDeclaration = annotator.withOptionalInjection(new FieldDeclaration().addVariable(new VariableDeclarator(genericType(annotator.multiInstanceInjectionType(), RuleEventListenerConfig.class), VAR_RULE_EVENT_LISTENER_CONFIGS)));
            members.add(relcFieldDeclaration);

            FieldDeclaration aelFieldDeclaration = annotator.withOptionalInjection(new FieldDeclaration().addVariable(new VariableDeclarator(genericType(annotator.multiInstanceInjectionType(), AgendaEventListener.class), VAR_AGENDA_EVENT_LISTENERS)));
            members.add(aelFieldDeclaration);

            FieldDeclaration rrelFieldDeclaration = annotator.withOptionalInjection(new FieldDeclaration().addVariable(new VariableDeclarator(genericType(annotator.multiInstanceInjectionType(), RuleRuntimeEventListener.class), VAR_RULE_RUNTIME_EVENT_LISTENERS)));
            members.add(rrelFieldDeclaration);

            members.add(generateExtractEventListenerConfigMethod());
            members.add(generateMergeEventListenerConfigMethod());
        } else {
            FieldDeclaration defaultRelcFieldDeclaration = new FieldDeclaration()
                    .setModifiers(Modifier.Keyword.PRIVATE)
                    .addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, RuleEventListenerConfig.class.getCanonicalName()), VAR_DEFAULT_RULE_EVENT_LISTENER_CONFIG, newObject(DefaultRuleEventListenerConfig.class)));
            members.add(defaultRelcFieldDeclaration);
        }

        return members;
    }
 
Example #4
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private void registerListeners( StatefulKnowledgeSessionImpl wm ) {
    if ( listeners.isEmpty()) {
        return;
    }
    for (ListnerHolder listnerHolder : listeners ) {
        switch (listnerHolder.type) {
            case AGENDA:
                wm.addEventListener( (AgendaEventListener)listnerHolder.listener );
                break;
            case RUNTIME:
                wm.addEventListener( (RuleRuntimeEventListener)listnerHolder.listener );
                break;
            case PROCESS:
                wm.addEventListener( (ProcessEventListener)listnerHolder.listener );
                break;
        }
    }
}
 
Example #5
Source File: RuleRuntimeEventSupport.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void fireObjectInserted(final PropagationContext propagationContext,
                               final FactHandle handle,
                               final Object object,
                               final InternalWorkingMemory workingMemory) {
    final Iterator<RuleRuntimeEventListener> iter = getEventListenersIterator();

    if (iter.hasNext()) {
        ObjectInsertedEventImpl event = new ObjectInsertedEventImpl(workingMemory,
                                                                    propagationContext,
                                                                    handle,
                                                                    object);

        do {
            iter.next().objectInserted(event);
        } while (iter.hasNext());
    }
}
 
Example #6
Source File: RuleRuntimeEventSupport.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void fireObjectUpdated(final PropagationContext propagationContext,
                              final FactHandle handle,
                              final Object oldObject,
                              final Object object,
                              final InternalWorkingMemory workingMemory) {
    final Iterator<RuleRuntimeEventListener> iter = getEventListenersIterator();

    if (iter.hasNext()) {
        ObjectUpdatedEventImpl event = new ObjectUpdatedEventImpl(workingMemory,
                                                                  propagationContext,
                                                                  handle,
                                                                  oldObject,
                                                                  object);

        do {
            iter.next().objectUpdated(event);
        } while (iter.hasNext());
    }
}
 
Example #7
Source File: RuleRuntimeEventSupport.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void fireObjectRetracted(final PropagationContext propagationContext,
                                final FactHandle handle,
                                final Object oldObject,
                                final InternalWorkingMemory workingMemory) {
    final Iterator<RuleRuntimeEventListener> iter = getEventListenersIterator();

    if (iter.hasNext()) {
        ObjectDeletedEventImpl event = new ObjectDeletedEventImpl(workingMemory,
                                                                  propagationContext,
                                                                  handle,
                                                                  oldObject);

        do {
            iter.next().objectDeleted(event);
        } while (iter.hasNext());
    }
}
 
Example #8
Source File: TruthMaintenanceTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogicalInsertOrder() throws Exception {
    // JBRULES-1602
    // "rule 1" is never logical inserted, as it's rule is unmatched prior to calling logical insert
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( ResourceFactory.newClassPathResource( "test_LogicalInsertOrder.drl",
                                                        getClass() ),
                  ResourceType.DRL );
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    kbase.addPackages( kbuilder.getKnowledgePackages() );
    kbase = SerializationHelper.serializeObject(kbase);

    final KieSession session = createKnowledgeSession(kbase);
    try {
        RuleRuntimeEventListener wmel = mock( RuleRuntimeEventListener.class );
        session.addEventListener( wmel );

        Person bob = new Person( "bob" );
        bob.setStatus( "hungry" );
        Person mark = new Person( "mark" );
        mark.setStatus( "thirsty" );

        session.insert( bob );
        session.insert( mark );

        int count = session.fireAllRules();

        assertEquals(2, count);

        assertEquals(2, session.getObjects().size());

        TruthMaintenanceSystem tms =  ((NamedEntryPoint)session.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()) ).getTruthMaintenanceSystem();
        assertTrue(tms.getEqualityKeyMap().isEmpty());
    } finally {
        session.dispose();
    }
}
 
Example #9
Source File: RuleRuntimeEventTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testEventModel() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_EventModel.drl"));
    final KieSession wm = createKnowledgeSession(kbase);

    final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
    wm.addEventListener(wmel);

    final Cheese stilton = new Cheese("stilton", 15);

    final FactHandle stiltonHandle = wm.insert(stilton);

    final ArgumentCaptor<ObjectInsertedEvent> oic = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectInsertedEvent.class);
    verify(wmel).objectInserted(oic.capture());
    assertSame(stiltonHandle, oic.getValue().getFactHandle());

    wm.update(stiltonHandle, stilton);
    final ArgumentCaptor<org.kie.api.event.rule.ObjectUpdatedEvent> ouc = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
    verify(wmel).objectUpdated(ouc.capture());
    assertSame(stiltonHandle, ouc.getValue().getFactHandle());

    wm.delete(stiltonHandle);
    final ArgumentCaptor<ObjectDeletedEvent> orc = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
    verify(wmel).objectDeleted(orc.capture());
    assertSame(stiltonHandle, orc.getValue().getFactHandle());

}
 
Example #10
Source File: WorkingMemoryLogger.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new working memory logger for the given working memory.
 * 
 * @param workingMemory
 */
public WorkingMemoryLogger(final WorkingMemory workingMemory) {
    workingMemory.addEventListener( (RuleRuntimeEventListener) this );
    workingMemory.addEventListener( (AgendaEventListener) this );
    setProcessRuntimeEventListener( (InternalWorkingMemory) workingMemory );
    workingMemory.addEventListener( (KieBaseEventListener) this );
}
 
Example #11
Source File: StatefulKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
    return Collections.unmodifiableCollection(ruleRuntimeEventSupport.getEventListeners());
}
 
Example #12
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
    return (Collection<RuleRuntimeEventListener>) getListeners(ListnerHolder.Type.RUNTIME);
}
 
Example #13
Source File: StatefulKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void addEventListener(final RuleRuntimeEventListener listener) {
    this.ruleRuntimeEventSupport.addEventListener( listener );
}
 
Example #14
Source File: StatefulKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void removeEventListener(final RuleRuntimeEventListener listener) {
    this.ruleRuntimeEventSupport.removeEventListener( listener );
}
 
Example #15
Source File: GetRuleRuntimeEventListenersCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Collection<RuleRuntimeEventListener> execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    return ksession.getRuleRuntimeEventListeners();
}
 
Example #16
Source File: RemoveEventListenerCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public RemoveEventListenerCommand(RuleRuntimeEventListener listener) {
    this.ruleRuntimeEventlistener = listener;
}
 
Example #17
Source File: AddEventListenerCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public AddEventListenerCommand(RuleRuntimeEventListener listener) {
    this.ruleRuntimeEventlistener = listener;
}
 
Example #18
Source File: CommandBasedStatefulKnowledgeSession.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void addEventListener(RuleRuntimeEventListener listener) {
    runner.execute(new AddEventListenerCommand(listener));
}
 
Example #19
Source File: CommandBasedStatefulKnowledgeSession.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
    return runner.execute( new GetRuleRuntimeEventListenersCommand() );
}
 
Example #20
Source File: CommandBasedStatefulKnowledgeSession.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void removeEventListener(RuleRuntimeEventListener listener) {
    runner.execute( new RemoveEventListenerCommand( listener ) );
}
 
Example #21
Source File: DummyKnowledgeRuntime.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
    return null;
}
 
Example #22
Source File: DummyWorkingMemory.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
    return null;
}
 
Example #23
Source File: MockKieSession.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
    return null;
}
 
Example #24
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void removeEventListener(RuleRuntimeEventListener listener) {
    listeners.remove( new ListnerHolder( ListnerHolder.Type.RUNTIME, listener ) );
}
 
Example #25
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void addEventListener(RuleRuntimeEventListener listener) {
    listeners.add( new ListnerHolder( ListnerHolder.Type.RUNTIME, listener ) );
}
 
Example #26
Source File: CachedRuleEventListenerConfig.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public List<RuleRuntimeEventListener> ruleRuntimeListeners() {        
    return ruleRuntimeListeners;
}
 
Example #27
Source File: CachedRuleEventListenerConfig.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public CachedRuleEventListenerConfig register(RuleRuntimeEventListener listener) {
    ruleRuntimeListeners.add(listener);
    return this;
}
 
Example #28
Source File: CachedRuleEventListenerConfig.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public CachedRuleEventListenerConfig(List<AgendaEventListener> agendaListeners, List<RuleRuntimeEventListener> ruleRuntimeListeners) {
    this.agendaListeners = agendaListeners;
    this.ruleRuntimeListeners = ruleRuntimeListeners;
}
 
Example #29
Source File: DefaultRuleEventListenerConfig.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public DefaultRuleEventListenerConfig(RuleRuntimeEventListener... listeners) {
    for (RuleRuntimeEventListener listener : listeners) {
        register(listener);
    }
}
 
Example #30
Source File: WrappedStatefulKnowledgeSessionForRHS.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public void addEventListener(RuleRuntimeEventListener listener) {
    delegate.addEventListener(listener);
}