Search in sources :

Example 1 with DeactivateAllNeedsOfListAction

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

the class BAAtomicAdditionalParticipantsBaseBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final AdditionalParticipantCoordinatorBotContextWrapper botContextWrapper = (AdditionalParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    final EventBus bus = getEventBus();
    logger.info("info1: No of needs: " + noOfNeeds);
    // wait for all needs to be created
    WaitForNEventsListener allNeedsCreatedListener = new WaitForNEventsListener(ctx, "waitForAllNeedsCreated", noOfNeeds);
    bus.subscribe(NeedCreatedEvent.class, allNeedsCreatedListener);
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), getParticipantFacetType().getURI()), noOfNonDelayedNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // create needs every trigger execution until noOfNeeds are created
    this.delayedParticipantNeedCreator = new ActionOnEventListener(ctx, "delayedParticipantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantDelayedListName(), getParticipantFacetType().getURI()), noOfDelayedNeeds);
    bus.subscribe(ActEvent.class, this.delayedParticipantNeedCreator);
    // when done, create one coordinator need
    this.coordinatorNeedCreator = new ActionOnEventListener(ctx, "coordinatorCreator", new FinishedEventFilter(participantNeedCreator), new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCoordinatorListName(), getCoordinatorFacetType().getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    final Iterator<BATestBotScript> firstPhasescriptIterator = firstPhaseScripts.iterator();
    final Iterator<BATestBotScript> firstPhaseScriptWithDelayIterator = firstPhaseScriptsWithDelay.iterator();
    final Iterator<BATestBotScript> secondPhasescriptIterator = secondPhaseScripts.iterator();
    // final Iterator<BATestBotScript> secondPhasescriptWithDelayIterator = secondPhaseScripts.iterator();
    // make a composite filter, with one filter for each testScriptListener that wait
    // for the FinishedEvents the they emit. That filter will be used to shut
    // down all needs after all the scriptListeners have finished.
    final OrFilter firstPhaseScriptListenerFilter = new OrFilter();
    final OrFilter firstPhaseScriptWithDelayListenerFilter = new OrFilter();
    final OrFilter secondPhaseScriptListenerFilter = new OrFilter();
    // final OrFilter secondPhaseScriptWithDelayListenerFilter = new OrFilter();
    // create a callback that gets called immediately before the connection is established
    ConnectFromListToListAction.ConnectHook scriptConnectHook = new ConnectFromListToListAction.ConnectHook() {

        @Override
        public void onConnect(final URI fromNeedURI, final URI toNeedURI) {
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, firstPhasescriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember it so we can check its state later
            firstPhasetestScriptListeners.add(testScriptListener);
            // subscribe it to the relevant events.
            bus.subscribe(ConnectFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(OpenFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(MessageFromOtherNeedEvent.class, testScriptListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptance filter to make extra sure we count each listener only once.
            firstPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
            // now we create the listener that is only active in the second phase
            // remember it so we can check its state later
            BATestScriptListener secondPhaseTestScriptListener = new BATestScriptListener(ctx, secondPhasescriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            secondPhasetestScriptListeners.add(secondPhaseTestScriptListener);
            secondPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(secondPhaseTestScriptListener)));
        }
    };
    ConnectFromListToListAction.ConnectHook scriptConnectWithDelayHook = new ConnectFromListToListAction.ConnectHook() {

        @Override
        public void onConnect(final URI fromNeedURI, final URI toNeedURI) {
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, firstPhaseScriptWithDelayIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember it so we can check its state later
            firstPhasetestScriptWithDelayListeners.add(testScriptListener);
            // subscribe it to the relevant events.
            bus.subscribe(ConnectFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(OpenFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(MessageFromOtherNeedEvent.class, testScriptListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptance filter to make extra sure we count each listener only once.
            firstPhaseScriptWithDelayListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
            // now we create the listener that is only active in the second phase
            // remember it so we can check its state later
            BATestScriptListener secondPhaseTestScriptListener = new BATestScriptListener(ctx, secondPhasescriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            secondPhasetestScriptListeners.add(secondPhaseTestScriptListener);
            secondPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(secondPhaseTestScriptListener)));
        }
    };
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new FinishedEventFilter(allNeedsCreatedListener), new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectHook, "Hi!"), 1);
    bus.subscribe(FinishedEvent.class, this.needConnector);
    // wait until the non-delayed participants are connected and done with their scripts
    BaseEventListener waitForNonDelayedConnectsListener = new WaitForNEventsListener(ctx, firstPhaseScriptListenerFilter, noOfNonDelayedNeeds - 1);
    bus.subscribe(FinishedEvent.class, waitForNonDelayedConnectsListener);
    FinishedEventFilter allNonDelayedConnectedFilter = new FinishedEventFilter(waitForNonDelayedConnectsListener);
    this.needConnectorWithDelay = new ActionOnEventListener(ctx, "needConnectorWithDelay", allNonDelayedConnectedFilter, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantDelayedListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectWithDelayHook, "Hi!"), 1);
    // TODO: MAKE THIS SO URI_LIST_NAME_PARTICIPANT_DELAYED "delayedParticipants" works again
    bus.subscribe(FinishedEvent.class, this.needConnectorWithDelay);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.firstPhaseWithDelayDoneListener = new ActionOnceAfterNEventsListener(ctx, "firstPhaseDoneWithDelayListener", firstPhaseScriptWithDelayListenerFilter, noOfDelayedNeeds, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            logger.debug("starting second phase");
            logger.debug("non-delayed listeners: {}", firstPhasetestScriptListeners.size());
            logger.debug("delayed listeners: {}", firstPhasetestScriptWithDelayListeners.size());
            Iterator<BATestScriptListener> firstPhaseListeners = firstPhasetestScriptListeners.iterator();
            Iterator<BATestScriptListener> firstPhaseWithDelayListeners = firstPhasetestScriptWithDelayListeners.iterator();
            Iterator<BATestScriptListener> combinedFirstPhaseListeners = Iterators.concat(firstPhaseListeners, firstPhaseWithDelayListeners);
            logger.debug("# of listeners in second phase: {}", secondPhasetestScriptListeners.size());
            for (BATestScriptListener listener : secondPhasetestScriptListeners) {
                logger.debug("subscribing second phase listener {}", listener);
                // subscribe it to the relevant events.
                bus.subscribe(MessageFromOtherNeedEvent.class, listener);
                bus.subscribe(SecondPhaseStartedEvent.class, listener);
                BATestScriptListener correspondingFirstPhaseListener = combinedFirstPhaseListeners.next();
                listener.setCoordinatorSideConnectionURI(correspondingFirstPhaseListener.getCoordinatorSideConnectionURI());
                listener.setParticipantSideConnectionURI(correspondingFirstPhaseListener.getParticipantSideConnectionURI());
                listener.updateFilterForBothConnectionURIs();
                bus.publish(new SecondPhaseStartedEvent(correspondingFirstPhaseListener.getCoordinatorURI(), correspondingFirstPhaseListener.getCoordinatorSideConnectionURI(), correspondingFirstPhaseListener.getParticipantURI()));
            }
        }
    });
    bus.subscribe(FinishedEvent.class, this.firstPhaseWithDelayDoneListener);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.scriptsDoneListener = new ActionOnceAfterNEventsListener(ctx, "scriptsDoneListener", secondPhaseScriptListenerFilter, noOfNeeds - 1, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getParticipantListName()));
    bus.subscribe(FinishedEvent.class, this.scriptsDoneListener);
    // When the needs are deactivated, all connections are closed. wait for the close events and signal work done.
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", noOfNeeds - 1, new SignalWorkDoneAction(ctx));
    bus.subscribe(CloseFromOtherNeedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) AdditionalParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.AdditionalParticipantCoordinatorBotContextWrapper) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) WaitForNEventsListener(won.bot.framework.eventbot.listener.impl.WaitForNEventsListener) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) BATestScriptListener(won.bot.framework.eventbot.listener.baStateBots.BATestScriptListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) FinishedEvent(won.bot.framework.eventbot.event.impl.listener.FinishedEvent) 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) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) AcceptOnceFilter(won.bot.framework.eventbot.filter.impl.AcceptOnceFilter) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter)

Example 2 with DeactivateAllNeedsOfListAction

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

the class GroupingBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    GroupBotContextWrapper botContextWrapper = (GroupBotContextWrapper) getBotContextWrapper();
    EventBus bus = getEventBus();
    // for each created need (in the group), add a listener that will auto-respond to messages directed at that need
    // create a filter that only accepts events for needs in the group:
    NeedUriInNamedListFilter groupMemberFilter = new NeedUriInNamedListFilter(ctx, botContextWrapper.getGroupMembersListName());
    // remember the auto-responders in a list
    this.autoResponders = new ArrayList<BaseEventListener>();
    // remember the listeners that wait for all messages
    this.messageCounters = new ArrayList<BaseEventListener>();
    // make a composite filter, with one filter for each autoResponder that wait for the FinishedEvents the responders emit.
    // that filter will be used to shut down all needs after all the autoResponders have finished.
    final OrFilter mainAutoResponderFilter = new OrFilter();
    // listen to NeedCreatedEvents
    this.autoResponderCreator = new ActionOnEventListener(ctx, groupMemberFilter, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            // create a listener that automatically answers messages, only for that need URI. We let it send NO_OF_MESSAGES messages
            logger.debug("created auto responder");
            AutomaticMessageResponderListener listener = new AutomaticMessageResponderListener(ctx, "autoResponder", NeedUriEventFilter.forEvent(event), NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
            // create a listener that publishes a FinishedEvent after having received all messages from the group
            WaitForNEventsListener waitForMessagesListener = new WaitForNEventsListener(ctx, "messageCounter", NeedUriEventFilter.forEvent(event), NO_OF_MESSAGES * (NO_OF_GROUPMEMBERS - 1));
            messageCounters.add(waitForMessagesListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptonce filter to make extra sure we count each listener only once.
            mainAutoResponderFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(waitForMessagesListener)));
            ActionOnEventListener debugger = new ActionOnEventListener(ctx, NeedUriEventFilter.forEvent(event), new BaseEventBotAction(ctx) {

                @Override
                protected void doRun(Event event, EventListener executingListener) throws Exception {
                    if (event instanceof MessageFromOtherNeedEvent) {
                        MessageFromOtherNeedEvent msg = (MessageFromOtherNeedEvent) event;
                        logger.debug("processing event {} wonMessage {} - text message '{}', sent by {} to {}", new Object[] { event.toString(), msg.getWonMessage().getMessageURI(), WonRdfUtils.MessageUtils.getTextMessage(msg.getWonMessage()), msg.getRemoteNeedURI(), msg.getNeedURI() });
                    }
                }
            });
            getEventBus().subscribe(MessageFromOtherNeedEvent.class, debugger);
            // finally, subscribe to the message events
            getEventBus().subscribe(MessageFromOtherNeedEvent.class, waitForMessagesListener);
            getEventBus().subscribe(MessageFromOtherNeedEvent.class, listener);
        }
    });
    getEventBus().subscribe(NeedCreatedEvent.class, this.autoResponderCreator);
    // count until N needs were created, then create need with group facet (the others will connect to that facet)
    this.groupCreator = new ActionOnceAfterNEventsListener(ctx, "groupCreator", NO_OF_GROUPMEMBERS, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getGroupListName(), FacetType.GroupFacet.getURI()));
    bus.subscribe(NeedCreatedEvent.class, this.groupCreator);
    // wait for N+1 needCreatedEvents, then connect the members with the group facet of the third need
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_GROUPMEMBERS + 1, new ConnectFromListToListAction(ctx, botContextWrapper.getGroupListName(), botContextWrapper.getGroupMembersListName(), FacetType.GroupFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi from the " + "GroupingBot!"));
    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 from the GroupingBot!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // now, once all connections have been opened, make 1 bot send a message to the group, the subsequent listener will cause let wild chatting to begin
    this.conversationStarter = new ActionOnceAfterNEventsListener(ctx, "conversationStarter", NO_OF_GROUPMEMBERS, new RespondToMessageAction(ctx, MILLIS_BETWEEN_MESSAGES));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.conversationStarter);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.messagesDoneListener = new ActionOnceAfterNEventsListener(ctx, "messagesDoneListener", mainAutoResponderFilter, NO_OF_GROUPMEMBERS, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getGroupMembersListName()));
    bus.subscribe(FinishedEvent.class, this.messagesDoneListener);
    // When the group facet need is deactivated, all connections are closed. wait for the close events and signal work done.
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", NO_OF_GROUPMEMBERS, new SignalWorkDoneAction(ctx));
    bus.subscribe(CloseFromOtherNeedEvent.class, this.workDoneSignaller);
    // start the whole thing:
    // create needs every trigger execution until N needs are created
    this.groupMemberCreator = new ActionOnEventListener(ctx, "groupMemberCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getGroupMembersListName(), FacetType.OwnerFacet.getURI()), NO_OF_GROUPMEMBERS);
    bus.subscribe(ActEvent.class, this.groupMemberCreator);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) RespondToMessageAction(won.bot.framework.eventbot.action.impl.wonmessage.RespondToMessageAction) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) WaitForNEventsListener(won.bot.framework.eventbot.listener.impl.WaitForNEventsListener) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) GroupBotContextWrapper(won.bot.framework.bot.context.GroupBotContextWrapper) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) FinishedEvent(won.bot.framework.eventbot.event.impl.listener.FinishedEvent) 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) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) AutomaticMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener)

Example 3 with DeactivateAllNeedsOfListAction

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

the class StandardTwoPhaseCommitBot 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);
    // wait for N NeedCreatedEvents
    creationWaiter = new WaitForNEventsListener(ctx, noOfNeeds);
    bus.subscribe(NeedCreatedEvent.class, creationWaiter);
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new FinishedEventFilter(creationWaiter), new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), FacetType.CoordinatorFacet.getURI(), FacetType.ParticipantFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!"), 1);
    bus.subscribe(FinishedEvent.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 NeedUriInNamedListFilter(ctx, botContextWrapper.getParticipantListName()), new OpenConnectionAction(ctx, "Hi!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // after the last connect event, all connections are closed!
    this.participantDeactivator = new ActionOnEventListener(ctx, "participantDeactivator", new NeedUriInNamedListFilter(ctx, botContextWrapper.getParticipantListName()), new TwoPhaseCommitDeactivateOnCloseAction(ctx), noOfNeeds - 1);
    bus.subscribe(CloseFromOtherNeedEvent.class, this.participantDeactivator);
    coordinatorDeactivator = new ActionOnEventListener(ctx, "coordinatorDeactivator", new FinishedEventFilter(participantDeactivator), new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getCoordinatorListName()), 1);
    bus.subscribe(FinishedEvent.class, coordinatorDeactivator);
    // 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) TwoPhaseCommitDeactivateOnCloseAction(won.bot.framework.eventbot.action.impl.facet.TwoPhaseCommitDeactivateOnCloseAction) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) NeedUriInNamedListFilter(won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter) 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) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) WaitForNEventsListener(won.bot.framework.eventbot.listener.impl.WaitForNEventsListener) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)

Example 4 with DeactivateAllNeedsOfListAction

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

the class BAAtomicBaseBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final 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(), getParticipantFacetType().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(), getCoordinatorFacetType().getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    FinishedEventFilter coordinatorCreatorFilter = new FinishedEventFilter(coordinatorNeedCreator);
    final Iterator<TwoPhaseScript> scriptIterator = scripts.iterator();
    // make a composite filter, with one filter for each testScriptListener that wait
    // for the FinishedEvents the they emit. That filter will be used to shut
    // down all needs after all the scriptListeners have finished.
    final OrFilter firstPhaseScriptListenerFilter = new OrFilter();
    final OrFilter secondPhaseScriptListenerFilter = new OrFilter();
    // create a callback that gets called immediately before the connection is established
    ConnectFromListToListAction.ConnectHook scriptConnectHook = new ConnectFromListToListAction.ConnectHook() {

        @Override
        public void onConnect(final URI fromNeedURI, final URI toNeedURI) {
            TwoPhaseScript script = scriptIterator.next();
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, script.getFirstPhaseScript(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // subscribe it to the relevant events.
            bus.subscribe(ConnectFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(OpenFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(MessageFromOtherNeedEvent.class, testScriptListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptance filter to make extra sure we count each listener only once.
            firstPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
            // now we create the listener that is only active in the second phase
            // remember it so we can check its state later
            BATestScriptListener secondPhaseTestScriptListener = new BATestScriptListener(ctx, script.getSecondPhaseScript(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember both listeners as a pair as we'll need them together later
            TwoPhaseScriptListener twoPhaseScriptListener = new TwoPhaseScriptListener(testScriptListener, secondPhaseTestScriptListener);
            scriptListeners.add(twoPhaseScriptListener);
            secondPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(secondPhaseTestScriptListener)));
        }
    };
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", noOfNeeds, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectHook, "Hi!"));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.firstPhaseDoneListener = new ActionOnceAfterNEventsListener(ctx, "firstPhaseDoneListener", firstPhaseScriptListenerFilter, noOfNeeds - 1, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            logger.debug("starting second phase");
            for (TwoPhaseScriptListener listener : scriptListeners) {
                logger.debug("subscribing second phase listener {}", listener);
                // subscribe it to the relevant events.
                bus.subscribe(MessageFromOtherNeedEvent.class, listener.getSecondPhaseListener());
                bus.subscribe(SecondPhaseStartedEvent.class, listener.getSecondPhaseListener());
                listener.getSecondPhaseListener().setCoordinatorSideConnectionURI(listener.getFirstPhaseListener().getCoordinatorSideConnectionURI());
                listener.getSecondPhaseListener().setParticipantSideConnectionURI(listener.getFirstPhaseListener().getParticipantSideConnectionURI());
                listener.getSecondPhaseListener().updateFilterForBothConnectionURIs();
                bus.publish(new SecondPhaseStartedEvent(listener.getFirstPhaseListener().getCoordinatorURI(), listener.getFirstPhaseListener().getCoordinatorSideConnectionURI(), listener.getFirstPhaseListener().getParticipantURI()));
            }
        }
    });
    bus.subscribe(FinishedEvent.class, this.firstPhaseDoneListener);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.scriptsDoneListener = new ActionOnceAfterNEventsListener(ctx, "scriptsDoneListener", secondPhaseScriptListenerFilter, noOfNeeds - 1, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getParticipantListName()));
    bus.subscribe(FinishedEvent.class, this.scriptsDoneListener);
    // When the needs are deactivated, all connections are closed. wait for the close events and signal work done.
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", noOfNeeds - 1, new SignalWorkDoneAction(ctx));
    bus.subscribe(CloseFromOtherNeedEvent.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) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) URI(java.net.URI) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BATestScriptListener(won.bot.framework.eventbot.listener.baStateBots.BATestScriptListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) FinishedEvent(won.bot.framework.eventbot.event.impl.listener.FinishedEvent) 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) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) AcceptOnceFilter(won.bot.framework.eventbot.filter.impl.AcceptOnceFilter) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)

Example 5 with DeactivateAllNeedsOfListAction

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

the class BABaseBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final 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(), getParticipantFacetType().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(), getCoordinatorFacetType().getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    final Iterator<BATestBotScript> scriptIterator = scripts.iterator();
    // make a composite filter, with one filter for each testScriptListener that wait
    // for the FinishedEvents the they emit. That filter will be used to shut
    // down all needs after all the scriptListeners have finished.
    final OrFilter mainScriptListenerFilter = new OrFilter();
    // create a callback that gets called immediately before the connection is established
    ConnectFromListToListAction.ConnectHook scriptConnectHook = new ConnectFromListToListAction.ConnectHook() {

        @Override
        public void onConnect(final URI fromNeedURI, final URI toNeedURI) {
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, scriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember it so we can check its state later
            testScriptListeners.add(testScriptListener);
            // subscribe it to the relevant events.
            bus.subscribe(ConnectFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(OpenFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(MessageFromOtherNeedEvent.class, testScriptListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptance filter to make extra sure we count each listener only once.
            mainScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
        }
    };
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", noOfNeeds, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectHook, "Hi!"));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.scriptsDoneListener = new ActionOnceAfterNEventsListener(ctx, "scriptsDoneListener", mainScriptListenerFilter, noOfNeeds - 1, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getParticipantListName()));
    bus.subscribe(FinishedEvent.class, this.scriptsDoneListener);
    // When the needs are deactivated, all connections are closed. wait for the close events and signal work done.
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", noOfNeeds - 1, new SignalWorkDoneAction(ctx));
    bus.subscribe(CloseFromOtherNeedEvent.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) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) URI(java.net.URI) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BATestScriptListener(won.bot.framework.eventbot.listener.baStateBots.BATestScriptListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) AcceptOnceFilter(won.bot.framework.eventbot.filter.impl.AcceptOnceFilter) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)5 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)5 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)5 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)5 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)5 EventBus (won.bot.framework.eventbot.bus.EventBus)5 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)5 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)5 FinishedEventFilter (won.bot.framework.eventbot.filter.impl.FinishedEventFilter)4 URI (java.net.URI)3 ParticipantCoordinatorBotContextWrapper (won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)3 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)3 Event (won.bot.framework.eventbot.event.Event)3 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)3 FinishedEvent (won.bot.framework.eventbot.event.impl.listener.FinishedEvent)3 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)3 CloseFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent)3 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)3 MessageFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent)3 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)3