Search in sources :

Example 21 with ActionOnEventListener

use of won.bot.framework.eventbot.listener.impl.ActionOnEventListener in project webofneeds by researchstudio-sat.

the class ConversationBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // create needs every trigger execution until 2 needs are created
    this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS);
    bus.subscribe(ActEvent.class, this.needCreator);
    // count until 2 needs were created, then
    // * connect the 2 needs
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_NEEDS, new ConnectFromListToListAction(ctx, ctx.getBotContextWrapper().getNeedCreateListName(), ctx.getBotContextWrapper().getNeedCreateListName(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi, I am the ConversationBot."));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // add a listener that is informed of the connect/open events and that auto-opens
    // subscribe it to:
    // * connect events - so it responds with open
    // * open events - so it responds with open (if the open received was the first open, and we still need to accept the connection)
    this.autoOpener = new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi, I " + "am the ConversationBot, too!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // add a listener that auto-responds to messages by a message
    // after 10 messages, it unsubscribes from all events
    // subscribe it to:
    // * message events - so it responds
    // * open events - so it initiates the chain reaction of responses
    this.autoResponder = new AutomaticMessageResponderListener(ctx, NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(MessageFromOtherNeedEvent.class, this.autoResponder);
    // add a listener that closes the connection after it has seen 10 messages
    this.connectionCloser = new ActionOnceAfterNEventsListener(ctx, NO_OF_MESSAGES, new CloseConnectionAction(ctx, "Bye!"));
    bus.subscribe(MessageFromOtherNeedEvent.class, this.connectionCloser);
    // add a listener that closes the connection when a failureEvent occurs
    EventListener onFailureConnectionCloser = new ActionOnEventListener(ctx, new CloseConnectionAction(ctx, "Bye!"));
    bus.subscribe(FailureResponseEvent.class, onFailureConnectionCloser);
    // add a listener that auto-responds to a close message with a deactivation of both needs.
    // subscribe it to:
    // * close events
    this.needDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(CloseFromOtherNeedEvent.class, this.needDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new SignalWorkDoneAction(ctx));
    bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) CloseConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) AutomaticMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener)

Example 22 with ActionOnEventListener

use of won.bot.framework.eventbot.listener.impl.ActionOnEventListener in project webofneeds by researchstudio-sat.

the class EchoBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // register with WoN nodes, be notified when new needs are created
    RegisterMatcherAction registerMatcherAction = new RegisterMatcherAction(ctx);
    this.matcherRegistrator = new ActionOnEventListener(ctx, registerMatcherAction, 1);
    bus.subscribe(ActEvent.class, this.matcherRegistrator);
    RandomDelayedAction delayedRegistration = new RandomDelayedAction(ctx, registrationMatcherRetryInterval, registrationMatcherRetryInterval, 0, registerMatcherAction);
    ActionOnEventListener matcherRetryRegistrator = new ActionOnEventListener(ctx, delayedRegistration);
    bus.subscribe(MatcherRegisterFailedEvent.class, matcherRetryRegistrator);
    // create the echo need - if we're not reacting to the creation of our own echo need.
    this.needCreator = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), prepareCreateNeedAction(ctx));
    bus.subscribe(NeedCreatedEventForMatcher.class, this.needCreator);
    // as soon as the echo need is created, connect to original
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new RandomDelayedAction(ctx, 5000, 5000, 1, new ConnectWithAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), "Greetings! I am the EchoBot! I will repeat everything you say, which you might " + "find useful for testing purposes.")));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // add a listener that auto-responds to messages by a message
    // after 10 messages, it unsubscribes from all events
    // subscribe it to:
    // * message events - so it responds
    // * open events - so it initiates the chain reaction of responses
    this.autoResponder = new ActionOnEventListener(ctx, new RespondWithEchoToMessageAction(ctx));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(MessageFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new LogAction(ctx, "received close message from remote need.")));
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) RegisterMatcherAction(won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction) NotFilter(won.bot.framework.eventbot.filter.impl.NotFilter) NeedUriInNamedListFilter(won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter) RespondWithEchoToMessageAction(won.bot.framework.eventbot.action.impl.wonmessage.RespondWithEchoToMessageAction) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ConnectWithAssociatedNeedAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectWithAssociatedNeedAction)

Example 23 with ActionOnEventListener

use of won.bot.framework.eventbot.listener.impl.ActionOnEventListener in project webofneeds by researchstudio-sat.

the class Mail2WonBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    mailGenerator.setEventListenerContext(ctx);
    bus = getEventBus();
    BotBehaviour connectBehaviour = new ConnectBehaviour(ctx);
    connectBehaviour.activate();
    BotBehaviour closeBehaviour = new CloseBevahiour(ctx);
    closeBehaviour.activate();
    BotBehaviour connectionMessageBehaviour = new ConnectionMessageBehaviour(ctx);
    connectionMessageBehaviour.activate();
    BotBehaviour deactivateNeedBehaviour = new DeactivateNeedBehaviour(ctx);
    deactivateNeedBehaviour.activate();
    // Mail initiated events
    bus.subscribe(MailReceivedEvent.class, new ActionOnEventListener(ctx, "MailReceived", new MailParserAction(ctx, mailContentExtractor)));
    bus.subscribe(CreateNeedFromMailEvent.class, new ActionOnEventListener(ctx, "CreateNeedFromMailEvent", new CreateNeedFromMailAction(ctx, mailContentExtractor)));
    bus.subscribe(WelcomeMailEvent.class, new ActionOnEventListener(ctx, "WelcomeMailAction", new WelcomeMailAction(mailGenerator, sendEmailChannel)));
    bus.subscribe(MailCommandEvent.class, new ActionOnEventListener(ctx, "MailCommandEvent", new MailCommandAction(ctx, mailContentExtractor)));
    bus.subscribe(SubscribeUnsubscribeEvent.class, new ActionOnEventListener(ctx, "SubscribeUnsubscribeEvent", new SubscribeUnsubscribeAction(ctx)));
    // WON initiated Events
    bus.subscribe(HintFromMatcherEvent.class, new ActionOnEventListener(ctx, "HintReceived", new Hint2MailParserAction(mailGenerator, sendEmailChannel)));
    bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, "ConnectReceived", new Connect2MailParserAction(mailGenerator, sendEmailChannel)));
    bus.subscribe(MessageFromOtherNeedEvent.class, new ActionOnEventListener(ctx, "ReceivedTextMessage", new Message2MailAction(mailGenerator, sendEmailChannel)));
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener)

Example 24 with ActionOnEventListener

use of won.bot.framework.eventbot.listener.impl.ActionOnEventListener in project webofneeds by researchstudio-sat.

the class MatcherProtocolTestBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // subscribe this bot with the WoN nodes' 'new need' topic
    RegisterMatcherAction registerMatcherAction = new RegisterMatcherAction(ctx);
    this.matcherRegistrator = new ActionOnEventListener(ctx, registerMatcherAction, 1);
    bus.subscribe(ActEvent.class, this.matcherRegistrator);
    RandomDelayedAction delayedRegistration = new RandomDelayedAction(ctx, registrationMatcherRetryInterval, registrationMatcherRetryInterval, 0, registerMatcherAction);
    ActionOnEventListener matcherRetryRegistrator = new ActionOnEventListener(ctx, delayedRegistration);
    bus.subscribe(MatcherRegisterFailedEvent.class, matcherRetryRegistrator);
    // create needs every trigger execution until 2 needs are created
    this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS);
    bus.subscribe(MatcherRegisteredEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            getEventListenerContext().getEventBus().subscribe(ActEvent.class, needCreator);
        }
    }, 1));
    this.matcherNotifier = new ActionOnceAfterNEventsListener(ctx, 4, new LogAction(ctx, "Received all events for newly created needs."));
    bus.subscribe(NeedCreatedEventForMatcher.class, matcherNotifier);
    bus.subscribe(NeedCreatedEvent.class, matcherNotifier);
    this.matcher = new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new MatchNeedsAction(ctx));
    // count until 1 need is created, then create a comment facet
    bus.subscribe(NeedCreatedEvent.class, this.matcher);
    this.allNeedsDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(HintFromMatcherEvent.class, this.allNeedsDeactivator);
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, 4, new SignalWorkDoneAction(ctx));
    bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
    bus.subscribe(NeedDeactivatedEventForMatcher.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) RegisterMatcherAction(won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) NeedDeactivatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) MatcherRegisteredEvent(won.bot.framework.eventbot.event.impl.matcher.MatcherRegisteredEvent) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) MatcherRegisterFailedEvent(won.bot.framework.eventbot.event.impl.matcher.MatcherRegisterFailedEvent) MatchNeedsAction(won.bot.framework.eventbot.action.impl.matcher.MatchNeedsAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener)

Example 25 with ActionOnEventListener

use of won.bot.framework.eventbot.listener.impl.ActionOnEventListener in project webofneeds by researchstudio-sat.

the class NeedCreatorBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final EventBus bus = getEventBus();
    final Counter needCreationSuccessfulCounter = new CounterImpl("needsCreated");
    final Counter needCreationFailedCounter = new CounterImpl("needCreationFailed");
    final Counter needCreationStartedCounter = new CounterImpl("creationStarted");
    // create a targeted counter that will publish an event when the target is reached
    // in this case, 0 unfinished need creations means that all needs were created
    final Counter creationUnfinishedCounter = new TargetCounterDecorator(ctx, new CounterImpl("creationUnfinished"), 0);
    // create needs every trigger execution until the need producer is exhausted
    this.groupMemberCreator = new ActionOnEventListener(ctx, "groupMemberCreator", new MultipleActions(ctx, new IncrementCounterAction(ctx, needCreationStartedCounter), new IncrementCounterAction(ctx, creationUnfinishedCounter), new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName())), -1);
    bus.subscribe(ActEvent.class, this.groupMemberCreator);
    bus.subscribe(NeedCreatedEvent.class, new ActionOnEventListener(ctx, "logger", new BaseEventBotAction(ctx) {

        int lastOutput = 0;

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            int cnt = needCreationStartedCounter.getCount();
            int unfinishedCount = creationUnfinishedCounter.getCount();
            int successCnt = needCreationSuccessfulCounter.getCount();
            int failedCnt = needCreationFailedCounter.getCount();
            if (cnt - lastOutput >= 1) {
                logger.info("started creation of {} needs, creation not yet finished for {}. Successful: {}, failed: {}", new Object[] { cnt, unfinishedCount, successCnt, failedCnt });
                lastOutput = cnt;
            }
        }
    }));
    // When the needproducer is exhausted, stop the creator.
    getEventBus().subscribe(NeedProducerExhaustedEvent.class, new ActionOnEventListener(ctx, new UnsubscribeListenerAction(ctx, groupMemberCreator)));
    // also, keep track of what worked and what didn't
    bus.subscribe(NeedCreationFailedEvent.class, new ActionOnEventListener(ctx, new IncrementCounterAction(ctx, needCreationFailedCounter)));
    bus.subscribe(NeedCreatedEvent.class, new ActionOnEventListener(ctx, new IncrementCounterAction(ctx, needCreationSuccessfulCounter)));
    // when a need is created (or it failed), decrement the halfCreatedNeed counter
    EventListener downCounter = new ActionOnEventListener(ctx, "downCounter", new DecrementCounterAction(ctx, creationUnfinishedCounter));
    // count a successful need creation
    bus.subscribe(NeedCreatedEvent.class, downCounter);
    // if a creation failed, we don't want to keep us from keeping the correct count
    bus.subscribe(NeedCreationFailedEvent.class, downCounter);
    // we count the one execution when the creator realizes that the producer is exhausted, we have to count down
    // once for that, too.
    bus.subscribe(NeedProducerExhaustedEvent.class, downCounter);
    EventListener loadTestMonitor = new ActionOnEventListener(ctx, "loadTestMonitor", new MatchingLoadTestMonitorAction(ctx));
    bus.subscribe(NeedCreatedEvent.class, loadTestMonitor);
    bus.subscribe(HintFromMatcherEvent.class, loadTestMonitor);
// wait for the targetCountReached event of the finishedCounter. We don't use
// another target counter, so we don't need to do more filtering.
// this.workDoneSignaller = new ActionOnEventListener(
// ctx, "workDoneSignaller",
// new SignalWorkDoneAction(ctx));
// bus.subscribe(TargetCountReachedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) MatchingLoadTestMonitorAction(won.bot.framework.eventbot.action.impl.monitor.MatchingLoadTestMonitorAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) NeedProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedProducerExhaustedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) UnsubscribeListenerAction(won.bot.framework.eventbot.action.impl.listener.UnsubscribeListenerAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener)

Aggregations

ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)28 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)22 EventBus (won.bot.framework.eventbot.bus.EventBus)19 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)17 EventListener (won.bot.framework.eventbot.listener.EventListener)17 Event (won.bot.framework.eventbot.event.Event)16 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)15 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)14 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)14 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)11 BaseEventListener (won.bot.framework.eventbot.listener.BaseEventListener)11 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)10 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)9 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)8 URI (java.net.URI)6 DeactivateAllNeedsAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction)6 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)6 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)6 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)5 CloseConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction)5