Search in sources :

Example 6 with OpenConnectionAction

use of won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction 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 7 with OpenConnectionAction

use of won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction in project webofneeds by researchstudio-sat.

the class StandardTwoPhaseCommitNoVoteBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    ParticipantCoordinatorBotContextWrapper botContextWrapper = (ParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), FacetType.ParticipantFacet.getURI()), noOfNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // when done, create one coordinator need
    this.coordinatorNeedCreator = new ActionOnEventListener(ctx, "coordinatorCreator", new FinishedEventFilter(participantNeedCreator), new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCoordinatorListName(), FacetType.CoordinatorFacet.getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", noOfNeeds, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), FacetType.CoordinatorFacet.getURI(), FacetType.ParticipantFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!"));
    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, "autoOpener", new OpenConnectionAction(ctx, "Hi!"));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoOpener);
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    this.autoCloser = new ActionOnceAfterNEventsListener(ctx, "autoCloser", noOfNeeds - 3, new CloseConnectionAction(ctx, "Bye!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoCloser);
    // after the last connect event, all connections are closed!
    this.needDeactivator = new ActionOnEventListener(ctx, new TwoPhaseCommitNoVoteDeactivateAllNeedsAction(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, noOfNeeds, 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) 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) TwoPhaseCommitNoVoteDeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.facet.TwoPhaseCommitNoVoteDeactivateAllNeedsAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)

Example 8 with OpenConnectionAction

use of won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction 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) {
            FailureResponseEvent failureResponseEvent = (FailureResponseEvent) event;
            bus.publish(new TestFailedEvent(DuplicateMessageSendingConversationBot.this, "Message failed: " + failureResponseEvent.getOriginalMessageURI() + ": " + WonRdfUtils.MessageUtils.getTextMessage(failureResponseEvent.getFailureMessage())));
        }
    }));
    // create atoms every trigger execution until 2 atoms are created
    bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateAtomWithSocketsAction(ctx, getBotContextWrapper().getAtomCreateListName()), NO_OF_ATOMS));
    // connect atoms
    bus.subscribe(AtomCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, "atomConnector", NO_OF_ATOMS * 2, new ConnectFromListToListAction(ctx, getBotContextWrapper().getAtomCreateListName(), getBotContextWrapper().getAtomCreateListName(), WXCHAT.ChatSocket.asURI(), WXCHAT.ChatSocket.asURI(), 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(ConnectFromOtherAtomEvent.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(ConnectFromOtherAtomEvent.class, autoResponder);
    bus.subscribe(MessageFromOtherAtomEvent.class, autoResponder);
    // add a listener that closes the connection after it has seen 10 messages
    bus.subscribe(MessageFromOtherAtomEvent.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 atoms.
    // subscribe it to:
    // * close events
    bus.subscribe(CloseFromOtherAtomEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, new DeactivateAllAtomsAction(ctx), new PublishEventAction(ctx, new TestPassedEvent(this))), 1));
    // add a listener that counts two AtomDeactivatedEvents and then tells the
    // framework that the bot's work is done
    bus.subscribe(AtomDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_ATOMS, 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) PublishEventAction(won.bot.framework.eventbot.action.impl.PublishEventAction) 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) CreateAtomWithSocketsAction(won.bot.framework.eventbot.action.impl.atomlifecycle.CreateAtomWithSocketsAction) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) DeactivateAllAtomsAction(won.bot.framework.eventbot.action.impl.atomlifecycle.DeactivateAllAtomsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) ConnectFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherAtomEvent) MessageFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherAtomEvent) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) AtomDeactivatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomDeactivatedEvent) CloseFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherAtomEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) 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) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) AutomaticMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)8 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)8 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)8 EventBus (won.bot.framework.eventbot.bus.EventBus)8 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)8 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)8 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)7 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)7 BaseEventListener (won.bot.framework.eventbot.listener.BaseEventListener)6 EventListener (won.bot.framework.eventbot.listener.EventListener)6 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)5 CloseConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction)5 Event (won.bot.framework.eventbot.event.Event)5 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)5 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)4 DeactivateAllNeedsAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction)3 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)3 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)3 ParticipantCoordinatorBotContextWrapper (won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)2 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)2