Search in sources :

Example 11 with BaseEventBotAction

use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.

the class DuplicateMessageSendingConversationBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getDuplicateMessageSenderDecorator(getEventListenerContext());
    final EventBus bus = getEventBus();
    // we're not expecting any failure messages in this test:
    bus.subscribe(FailureResponseEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            FailureResponseEvent failureResponseEvent = (FailureResponseEvent) event;
            bus.publish(new TestFailedEvent(DuplicateMessageSendingConversationBot.this, "Message failed: " + failureResponseEvent.getOriginalMessageURI() + ": " + WonRdfUtils.MessageUtils.getTextMessage(failureResponseEvent.getFailureMessage())));
        }
    }));
    // create needs every trigger execution until 2 needs are created
    bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS));
    // connect needs
    bus.subscribe(NeedCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_NEEDS * 2, new ConnectFromListToListAction(ctx, getBotContextWrapper().getNeedCreateListName(), getBotContextWrapper().getNeedCreateListName(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!")));
    // 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)
    bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi!")));
    // 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
    BaseEventListener autoResponder = new AutomaticMessageResponderListener(ctx, NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
    bus.subscribe(OpenFromOtherNeedEvent.class, autoResponder);
    bus.subscribe(MessageFromOtherNeedEvent.class, autoResponder);
    // add a listener that closes the connection after it has seen 10 messages
    bus.subscribe(MessageFromOtherNeedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_MESSAGES, new CloseConnectionAction(ctx, "Bye!")));
    // 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
    bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, new DeactivateAllNeedsAction(ctx), new PublishEventAction(ctx, new TestPassedEvent(this))), 1));
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    bus.subscribe(NeedDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new SignalWorkDoneAction(ctx)));
}
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) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) NeedDeactivatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) AutomaticMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener)

Example 12 with BaseEventBotAction

use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.

the class BehaviourBarrier method onActivate.

@Override
protected void onActivate(Optional<Object> message) {
    Set<BotBehaviour> deactivatedBehaviours = Collections.synchronizedSet(new HashSet<>());
    subscribeWithAutoCleanup(BotBehaviourDeactivatedEvent.class, new ActionOnEventListener(context, new EventFilter() {

        @Override
        public boolean accept(Event event) {
            if (!(event instanceof BotBehaviourDeactivatedEvent))
                return false;
            return behavioursToWaitFor.contains(((BotBehaviourDeactivatedEvent) event).getBehaviour());
        }
    }, new BaseEventBotAction(context) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            synchronized (behavioursToWaitFor) {
                deactivatedBehaviours.add(((BotBehaviourDeactivatedEvent) event).getBehaviour());
                if (deactivatedBehaviours.containsAll(behavioursToWaitFor)) {
                    behavioursToStart.forEach(behaviour -> behaviour.activate());
                    deactivate();
                }
            }
        }
    }));
}
Also used : Event(won.bot.framework.eventbot.event.Event) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) EventFilter(won.bot.framework.eventbot.filter.EventFilter)

Example 13 with BaseEventBotAction

use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.

the class BotTrigger method activate.

public synchronized void activate() {
    if (active.get())
        return;
    // make the stop listener
    this.stopListener = new ActionOnFirstEventListener(this.context, new BotTriggerFilter(this), new BaseEventBotAction(BotTrigger.this.context) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            // unregister all listeners
            BotTrigger.this.context.getEventBus().unsubscribe(BotTrigger.this.startListener);
            BotTrigger.this.context.getEventBus().unsubscribe(BotTrigger.this.stopListener);
            BotTrigger.this.cancelableTask.cancel(true);
            BotTrigger.this.active.set(false);
        }
    });
    // make the start listener
    this.startListener = new ActionOnFirstEventListener(this.context, new BotTriggerFilter(this), new BaseEventBotAction(BotTrigger.this.context) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            reschedule();
        }
    });
    // register both listeners
    context.getEventBus().subscribe(StopBotTriggerCommandEvent.class, stopListener);
    context.getEventBus().subscribe(StartBotTriggerCommandEvent.class, startListener);
    active.set(true);
}
Also used : BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) Event(won.bot.framework.eventbot.event.Event) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener)

Example 14 with BaseEventBotAction

use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.

the class CoordinationBehaviour method onActivate.

@Override
protected void onActivate(Optional<Object> message) {
    EventBotAction actionToExecute = null;
    if (typeB == CoordinationType.ACTIVATE) {
        actionToExecute = new BaseEventBotAction(context) {

            @Override
            protected void doRun(Event event, EventListener executingListener) throws Exception {
                BotBehaviourEvent botBehaviourEvent = (BotBehaviourEvent) event;
                behaviourB.activate(botBehaviourEvent.getMessage());
                deactivate(botBehaviourEvent.getMessage());
            }
        };
    } else {
        actionToExecute = new BaseEventBotAction(context) {

            @Override
            protected void doRun(Event event, EventListener executingListener) throws Exception {
                BotBehaviourEvent botBehaviourEvent = (BotBehaviourEvent) event;
                behaviourB.deactivate(botBehaviourEvent.getMessage());
                deactivate(botBehaviourEvent.getMessage());
            }
        };
    }
    Class<? extends Event> eventClazz = null;
    if (typeA == CoordinationType.ACTIVATE) {
        eventClazz = BotBehaviourActivatedEvent.class;
    } else {
        eventClazz = BotBehaviourDeactivatedEvent.class;
    }
    subscribeWithAutoCleanup(eventClazz, new ActionOnEventListener(context, new EventFilter() {

        @Override
        public boolean accept(Event event) {
            return ((BotBehaviourEvent) event).getBehaviour() == behaviourA;
        }
    }, actionToExecute));
}
Also used : BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) Event(won.bot.framework.eventbot.event.Event) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventFilter(won.bot.framework.eventbot.filter.EventFilter) EventBotAction(won.bot.framework.eventbot.action.EventBotAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction)

Example 15 with BaseEventBotAction

use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.

the class CommentBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    final EventBus bus = getEventBus();
    CommentBotContextWrapper botContextWrapper = (CommentBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until 2 needs are created
    this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getNeedCreateListName()), NO_OF_NEEDS);
    bus.subscribe(ActEvent.class, this.needCreator);
    // count until 1 need is created, then create a comment facet
    this.commentFacetCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCommentListName(), FacetType.CommentFacet.getURI()), 1);
    bus.subscribe(NeedCreatedEvent.class, this.commentFacetCreator);
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, 2, new ConnectFromListToListAction(ctx, botContextWrapper.getNeedCreateListName(), botContextWrapper.getCommentListName(), FacetType.OwnerFacet.getURI(), FacetType.CommentFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi, I am the " + "CommentBot."));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    this.autoOpener = new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi!"));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoOpener);
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    BaseEventListener assertionRunner = new ActionOnceAfterNEventsListener(ctx, 1, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            executeAssertionsForEstablishedConnectionInternal(bus);
        }
    });
    bus.subscribe(OpenFromOtherNeedEvent.class, assertionRunner);
    // deactivate all needs when the assertion was executed
    this.allNeedsDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(AssertionsExecutedEvent.class, this.allNeedsDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, 2, 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) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) CommentBotContextWrapper(won.bot.framework.bot.context.CommentBotContextWrapper) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) BaseEvent(won.bot.framework.eventbot.event.BaseEvent) NeedDeactivatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) OpenFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent) 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) 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

BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)18 Event (won.bot.framework.eventbot.event.Event)18 EventListener (won.bot.framework.eventbot.listener.EventListener)18 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)17 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)13 EventBus (won.bot.framework.eventbot.bus.EventBus)12 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)11 BaseEventListener (won.bot.framework.eventbot.listener.BaseEventListener)10 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)9 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)9 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)8 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)8 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)7 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)7 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)6 MessageFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent)6 URI (java.net.URI)5 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)5 CloseFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent)4 ActionOnFirstEventListener (won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener)4