Search in sources :

Example 1 with MultipleActions

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

the class DebugBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    String welcomeMessage = "Greetings! \nI am the DebugBot. I " + "can simulate multiple other users so you can test things. I understand a few commands. \nTo see which ones, " + "type 'usage'.";
    String welcomeHelpMessage = "When connecting with me, you can say 'ignore', or 'deny' to make me ignore or deny requests, and 'wait N' to make me wait N seconds (max 99) before reacting.";
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // eagerly cache RDF data
    BotBehaviour eagerlyCacheBehaviour = new EagerlyPopulateCacheBehaviour(ctx);
    eagerlyCacheBehaviour.activate();
    // react to a bot command activating/deactivating eager caching
    bus.subscribe(SetCacheEagernessCommandEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (event instanceof SetCacheEagernessCommandEvent) {
                if (((SetCacheEagernessCommandEvent) event).isEager()) {
                    eagerlyCacheBehaviour.activate();
                } else {
                    eagerlyCacheBehaviour.deactivate();
                }
            }
        }
    }));
    // react to a message that was not identified as a debug command
    BotBehaviour connectionMessageBehaviour = new ConnectionMessageBehaviour(ctx);
    connectionMessageBehaviour.activate();
    // react to the debug deactivate command (deactivate my need)
    BotBehaviour deactivateNeedBehaviour = new DeactivateNeedBehaviour(ctx);
    deactivateNeedBehaviour.activate();
    // react to the close behaviour
    BotBehaviour closeBehaviour = new CloseBevahiour(ctx);
    closeBehaviour.activate();
    // 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 for debug initial connect - if we're not reacting to the creation of our own echo need.
    CreateDebugNeedWithFacetsAction needForInitialConnectAction = new CreateDebugNeedWithFacetsAction(ctx, true, true);
    needForInitialConnectAction.setIsInitialForConnect(true);
    ActionOnEventListener initialConnector = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), needForInitialConnectAction);
    bus.subscribe(NeedCreatedEventForMatcher.class, initialConnector);
    // create the echo need for debug initial hint - if we're not reacting to the creation of our own echo need.
    CreateDebugNeedWithFacetsAction initialHinter = new CreateDebugNeedWithFacetsAction(ctx, true, true);
    initialHinter.setIsInitialForHint(true);
    ActionOnEventListener needForInitialHintListener = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), initialHinter);
    bus.subscribe(NeedCreatedEventForMatcher.class, needForInitialHintListener);
    // as soon as the echo need triggered by debug connect created, connect to original
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new RandomDelayedAction(ctx, CONNECT_DELAY_MILLIS, CONNECT_DELAY_MILLIS, 1, new ConnectWithAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), welcomeMessage + " " + welcomeHelpMessage)));
    bus.subscribe(NeedCreatedEventForDebugConnect.class, this.needConnector);
    // as soon as the echo need triggered by debug hint command created, hint to original
    this.needHinter = new ActionOnEventListener(ctx, "needHinter", new RandomDelayedAction(ctx, CONNECT_DELAY_MILLIS, CONNECT_DELAY_MILLIS, 1, new HintAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), matcherUri)));
    bus.subscribe(NeedCreatedEventForDebugHint.class, this.needHinter);
    // if the original need wants to connect - always open
    this.autoOpener = new ActionOnEventListener(ctx, new MultipleActions(ctx, new OpenConnectionDebugAction(ctx, welcomeMessage, welcomeHelpMessage), new PublishSetChattinessEventAction(ctx, true)));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    EventBotAction userCommandAction = new DebugBotIncomingMessageToEventMappingAction(ctx);
    // if the remote side opens, send a greeting and set to chatty.
    bus.subscribe(OpenFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, userCommandAction, new PublishSetChattinessEventAction(ctx, true))));
    // if the bot receives a text message - try to map the command of the text message to a DebugEvent
    messageFromOtherNeedListener = new ActionOnEventListener(ctx, userCommandAction);
    bus.subscribe(MessageFromOtherNeedEvent.class, messageFromOtherNeedListener);
    // react to usage command event
    this.usageMessageSender = new ActionOnEventListener(ctx, new SendMultipleMessagesAction(ctx, DebugBotIncomingMessageToEventMappingAction.USAGE_MESSAGES));
    bus.subscribe(UsageDebugCommandEvent.class, usageMessageSender);
    bus.subscribe(CloseCommandSuccessEvent.class, new ActionOnEventListener(ctx, "chattiness off", new PublishSetChattinessEventAction(ctx, false)));
    // react to close event: set connection to not chatty
    bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new PublishSetChattinessEventAction(ctx, false)));
    // react to the hint and connect commands by creating a need (it will fire correct need created for connect/hint
    // events)
    needCreator = new ActionOnEventListener(ctx, new CreateDebugNeedWithFacetsAction(ctx, true, true));
    bus.subscribe(HintDebugCommandEvent.class, needCreator);
    bus.subscribe(ConnectDebugCommandEvent.class, needCreator);
    bus.subscribe(SendNDebugCommandEvent.class, new ActionOnEventListener(ctx, new SendNDebugMessagesAction(ctx, DELAY_BETWEEN_N_MESSAGES, DebugBotIncomingMessageToEventMappingAction.N_MESSAGES)));
    MessageTimingManager timingManager = new MessageTimingManager(ctx, 20);
    // on every actEvent there is a chance we send a chatty message
    bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new SendChattyMessageAction(ctx, CHATTY_MESSAGE_PROBABILITY, timingManager, DebugBotIncomingMessageToEventMappingAction.RANDOM_MESSAGES, DebugBotIncomingMessageToEventMappingAction.LAST_MESSAGES)));
    // set the chattiness of the connection
    bus.subscribe(SetChattinessDebugCommandEvent.class, new ActionOnEventListener(ctx, new SetChattinessAction(ctx)));
    // process eliza messages with eliza
    bus.subscribe(MessageToElizaEvent.class, new ActionOnEventListener(ctx, new AnswerWithElizaAction(ctx, 20)));
    // remember when we sent the last message
    bus.subscribe(WonMessageSentOnConnectionEvent.class, new ActionOnEventListener(ctx, new RecordMessageSentTimeAction(ctx, timingManager)));
    // remember when we got the last message
    bus.subscribe(WonMessageReceivedOnConnectionEvent.class, new ActionOnEventListener(ctx, new RecordMessageReceivedTimeAction(ctx, timingManager)));
    // initialize the sent timestamp when the open message is received
    bus.subscribe(OpenFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new RecordMessageSentTimeAction(ctx, timingManager)));
    // initialize the sent timestamp when the connect message is received
    bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new RecordMessageSentTimeAction(ctx, timingManager)));
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) CreateDebugNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.debugbot.CreateDebugNeedWithFacetsAction) SendMultipleMessagesAction(won.bot.framework.eventbot.action.impl.wonmessage.SendMultipleMessagesAction) NeedUriInNamedListFilter(won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter) SendNDebugMessagesAction(won.bot.framework.eventbot.action.impl.debugbot.SendNDebugMessagesAction) EventBus(won.bot.framework.eventbot.bus.EventBus) HintAssociatedNeedAction(won.bot.framework.eventbot.action.impl.wonmessage.HintAssociatedNeedAction) MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) PublishSetChattinessEventAction(won.bot.framework.eventbot.action.impl.debugbot.PublishSetChattinessEventAction) DebugBotIncomingMessageToEventMappingAction(won.bot.framework.eventbot.action.impl.debugbot.DebugBotIncomingMessageToEventMappingAction) OpenConnectionDebugAction(won.bot.framework.eventbot.action.impl.debugbot.OpenConnectionDebugAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) RandomDelayedAction(won.bot.framework.eventbot.action.impl.RandomDelayedAction) CloseBevahiour(won.bot.framework.eventbot.behaviour.CloseBevahiour) ConnectWithAssociatedNeedAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectWithAssociatedNeedAction) BotBehaviour(won.bot.framework.eventbot.behaviour.BotBehaviour) RecordMessageSentTimeAction(won.bot.framework.eventbot.action.impl.debugbot.RecordMessageSentTimeAction) SetChattinessAction(won.bot.framework.eventbot.action.impl.debugbot.SetChattinessAction) AnswerWithElizaAction(won.bot.framework.eventbot.action.impl.debugbot.AnswerWithElizaAction) EagerlyPopulateCacheBehaviour(won.bot.framework.eventbot.behaviour.EagerlyPopulateCacheBehaviour) RecordMessageReceivedTimeAction(won.bot.framework.eventbot.action.impl.debugbot.RecordMessageReceivedTimeAction) SetCacheEagernessCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent) RegisterMatcherAction(won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction) ConnectionMessageBehaviour(won.bot.framework.eventbot.behaviour.ConnectionMessageBehaviour) NotFilter(won.bot.framework.eventbot.filter.impl.NotFilter) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) UsageDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.UsageDebugCommandEvent) MatcherRegisterFailedEvent(won.bot.framework.eventbot.event.impl.matcher.MatcherRegisterFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) OpenFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent) Event(won.bot.framework.eventbot.event.Event) SendNDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SendNDebugCommandEvent) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) CloseCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandSuccessEvent) SetChattinessDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetChattinessDebugCommandEvent) WonMessageReceivedOnConnectionEvent(won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) MessageToElizaEvent(won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) WonMessageSentOnConnectionEvent(won.bot.framework.eventbot.event.impl.wonmessage.WonMessageSentOnConnectionEvent) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) SetCacheEagernessCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent) DeactivateNeedBehaviour(won.bot.framework.eventbot.behaviour.DeactivateNeedBehaviour) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) MessageTimingManager(won.bot.framework.eventbot.action.impl.debugbot.MessageTimingManager) SendChattyMessageAction(won.bot.framework.eventbot.action.impl.debugbot.SendChattyMessageAction) EventBotAction(won.bot.framework.eventbot.action.EventBotAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction)

Example 2 with MultipleActions

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

the class InitFactoryAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof InitializeEvent) || !(getEventListenerContext().getBotContextWrapper() instanceof FactoryBotContextWrapper)) {
        logger.error("InitFactoryAction can only handle InitializeEvent with FactoryBotContextWrapper");
        return;
    }
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = ctx.getEventBus();
    FactoryBotContextWrapper botContextWrapper = (FactoryBotContextWrapper) ctx.getBotContextWrapper();
    // create a targeted counter that will publish an event when the target is
    // reached
    // in this case, 0 unfinished atom creations means that all atoms were created
    final TargetCounterDecorator creationUnfinishedCounter = new TargetCounterDecorator(ctx, new CounterImpl("creationUnfinished"), 0);
    BotTrigger createFactoryAtomTrigger = new BotTrigger(ctx, Duration.ofMillis(FACTORYATOMCREATION_DURATION_INMILLIS));
    createFactoryAtomTrigger.activate();
    bus.subscribe(StartFactoryAtomCreationEvent.class, new ActionOnFirstEventListener(ctx, new PublishEventAction(ctx, new StartBotTriggerCommandEvent(createFactoryAtomTrigger))));
    bus.subscribe(BotTriggerEvent.class, new ActionOnTriggerEventListener(ctx, createFactoryAtomTrigger, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (isTooManyMessagesInFlight(messagesInFlightCounter)) {
                return;
            }
            adjustTriggerInterval(createFactoryAtomTrigger, messagesInFlightCounter);
            // defined via spring
            AtomProducer atomProducer = ctx.getAtomProducer();
            Dataset dataset = atomProducer.create();
            if (dataset == null && atomProducer.isExhausted()) {
                bus.publish(new AtomProducerExhaustedEvent());
                bus.unsubscribe(executingListener);
                return;
            }
            URI atomUriFromProducer = null;
            Resource atomResource = WonRdfUtils.AtomUtils.getAtomResource(dataset);
            if (atomResource.isURIResource()) {
                atomUriFromProducer = URI.create(atomResource.getURI());
            }
            if (atomUriFromProducer != null) {
                URI atomURI = botContextWrapper.getURIFromInternal(atomUriFromProducer);
                if (atomURI != null) {
                    bus.publish(new FactoryAtomCreationSkippedEvent());
                } else {
                    bus.publish(new CreateAtomCommandEvent(dataset, botContextWrapper.getFactoryListName()));
                }
            }
        }
    }));
    bus.subscribe(CreateAtomCommandSuccessEvent.class, // decrease the
    new DecrementCounterAction(ctx, creationUnfinishedCounter), // count a successful atom
    new IncrementCounterAction(ctx, atomCreationSuccessfulCounter), // creation
    new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (event instanceof CreateAtomCommandSuccessEvent) {
                CreateAtomCommandSuccessEvent atomCreatedEvent = (CreateAtomCommandSuccessEvent) event;
                botContextWrapper.addInternalIdToUriReference(atomCreatedEvent.getAtomUriBeforeCreation(), atomCreatedEvent.getAtomURI());
            }
        }
    });
    bus.subscribe(CreateAtomCommandEvent.class, // execute the atom creation for the atom in the event
    new ExecuteCreateAtomCommandAction(ctx), // increase the
    new IncrementCounterAction(ctx, atomCreationStartedCounter), // increase the
    new IncrementCounterAction(ctx, creationUnfinishedCounter));
    // if an atom is already created we skip the recreation of it and increase the
    // atomCreationSkippedCounter
    bus.subscribe(FactoryAtomCreationSkippedEvent.class, new IncrementCounterAction(ctx, atomCreationSkippedCounter));
    // if a creation failed, we don't want to keep us from keeping the correct count
    bus.subscribe(CreateAtomCommandFailureEvent.class, // decrease the
    new DecrementCounterAction(ctx, creationUnfinishedCounter), // count an unsuccessful atom
    new IncrementCounterAction(ctx, atomCreationFailedCounter));
    // when the atomproducer is exhausted, we stop the creator (trigger) and we have
    // to wait until all unfinished atom creations finish
    // when they do, the InitFactoryFinishedEvent is published
    bus.subscribe(AtomProducerExhaustedEvent.class, new ActionOnFirstEventListener(ctx, new MultipleActions(ctx, new PublishEventAction(ctx, new StopBotTriggerCommandEvent(createFactoryAtomTrigger)), new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            // when we're called, there probably are atom creations
            // unfinished, but there may not be
            // a)
            // first, prepare for the case when there are unfinished
            // atom creations:
            // we register a listener, waiting for the unfinished
            // counter to reach 0
            EventListener waitForUnfinishedAtomsListener = bus.subscribe(TargetCountReachedEvent.class, new ActionOnFirstEventListener(ctx, new TargetCounterFilter(creationUnfinishedCounter), new PublishEventAction(ctx, new InitFactoryFinishedEvent())));
            // now, we can check if we've already reached the target
            if (creationUnfinishedCounter.getCount() <= 0) {
                // ok, turned out we didn't need that listener
                bus.unsubscribe(waitForUnfinishedAtomsListener);
                bus.publish(new InitFactoryFinishedEvent());
            }
        }
    })));
    bus.subscribe(InitFactoryFinishedEvent.class, new ActionOnFirstEventListener(ctx, "factoryCreateStatsLogger", new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            logger.info("FactoryAtomCreation finished: total:{}, successful: {}, failed: {}, skipped: {}", new Object[] { atomCreationStartedCounter.getCount(), atomCreationSuccessfulCounter.getCount(), atomCreationFailedCounter.getCount(), atomCreationSkippedCounter.getCount() });
        }
    }));
    // MessageInFlight counter handling *************************
    bus.subscribe(MessageCommandEvent.class, new IncrementCounterAction(ctx, messagesInFlightCounter));
    bus.subscribe(MessageCommandResultEvent.class, new DecrementCounterAction(ctx, messagesInFlightCounter));
    // if we receive a
    bus.subscribe(MessageCommandFailureEvent.class, new LogMessageCommandFailureAction(ctx));
    // message command
    // failure, log it
    // Start the atom creation stuff
    bus.publish(new StartFactoryAtomCreationEvent());
}
Also used : InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) PublishEventAction(won.bot.framework.eventbot.action.impl.PublishEventAction) StartFactoryAtomCreationEvent(won.bot.framework.eventbot.event.impl.factory.StartFactoryAtomCreationEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CounterImpl(won.bot.framework.eventbot.action.impl.counter.CounterImpl) StopBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StopBotTriggerCommandEvent) URI(java.net.URI) LogMessageCommandFailureAction(won.bot.framework.eventbot.action.impl.wonmessage.execCommand.LogMessageCommandFailureAction) ExecuteCreateAtomCommandAction(won.bot.framework.eventbot.action.impl.wonmessage.execCommand.ExecuteCreateAtomCommandAction) MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) ActionOnTriggerEventListener(won.bot.framework.eventbot.action.impl.trigger.ActionOnTriggerEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) IncrementCounterAction(won.bot.framework.eventbot.action.impl.counter.IncrementCounterAction) AtomProducer(won.bot.framework.component.atomproducer.AtomProducer) InitFactoryFinishedEvent(won.bot.framework.eventbot.event.impl.factory.InitFactoryFinishedEvent) AtomProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent) TargetCounterFilter(won.bot.framework.eventbot.filter.impl.TargetCounterFilter) Dataset(org.apache.jena.query.Dataset) FactoryAtomCreationSkippedEvent(won.bot.framework.eventbot.event.impl.factory.FactoryAtomCreationSkippedEvent) Resource(org.apache.jena.rdf.model.Resource) TargetCountReachedEvent(won.bot.framework.eventbot.action.impl.counter.TargetCountReachedEvent) FactoryBotContextWrapper(won.bot.framework.bot.context.FactoryBotContextWrapper) StartBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StartBotTriggerCommandEvent) ActionOnTriggerEventListener(won.bot.framework.eventbot.action.impl.trigger.ActionOnTriggerEventListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) StartBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StartBotTriggerCommandEvent) StartFactoryAtomCreationEvent(won.bot.framework.eventbot.event.impl.factory.StartFactoryAtomCreationEvent) CreateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent) StopBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StopBotTriggerCommandEvent) AtomProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent) BotTriggerEvent(won.bot.framework.eventbot.action.impl.trigger.BotTriggerEvent) MessageCommandResultEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandResultEvent) MessageCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent) FactoryAtomCreationSkippedEvent(won.bot.framework.eventbot.event.impl.factory.FactoryAtomCreationSkippedEvent) InitFactoryFinishedEvent(won.bot.framework.eventbot.event.impl.factory.InitFactoryFinishedEvent) Event(won.bot.framework.eventbot.event.Event) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) TargetCountReachedEvent(won.bot.framework.eventbot.action.impl.counter.TargetCountReachedEvent) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) DecrementCounterAction(won.bot.framework.eventbot.action.impl.counter.DecrementCounterAction) BotTrigger(won.bot.framework.eventbot.action.impl.trigger.BotTrigger) TargetCounterDecorator(won.bot.framework.eventbot.action.impl.counter.TargetCounterDecorator)

Example 3 with MultipleActions

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

the class DuplicateAtomURIFailureBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // create atoms every trigger execution until 2 atoms are created
    bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateAtomWithSocketsAction(// message
    new ConstantNewAtomURIDecorator(ctx, "constantAtomURI" + System.currentTimeMillis()), getBotContextWrapper().getAtomCreateListName()), 2));
    // log error if we can create 2 atoms
    bus.subscribe(AtomCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogErrorAction(ctx, "Should not have been able to create 2 atoms with identical URI"), new PublishEventAction(ctx, new TestFailedEvent(this, "Should not have been able to create 2 atoms with identical URI")))));
    // log success if we could create 1 atom
    bus.subscribe(AtomCreatedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: could create one atom"), new PublishEventAction(ctx, new SuccessEvent()))));
    // log success if we got an error for 2nd atom
    bus.subscribe(AtomCreationFailedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: atom creation failed for 2nd atom."), new PublishEventAction(ctx, new SuccessEvent()))));
    // when we have 2 SuccessEvents, we're done. Deactivate the atoms and signal
    // we're finished
    bus.subscribe(SuccessEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogAction(ctx, "Test passed."), new PublishEventAction(ctx, new TestPassedEvent(this)), new DeactivateAllAtomsAction(ctx))));
    // when we have a FailureEvent, we're done, too. Deactivate the atoms and signal
    // we're finished
    bus.subscribe(TestFailedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Test failed."), new DeactivateAllAtomsAction(ctx))));
    // wait for the atomDeactivated event, then say we're done.
    bus.subscribe(AtomDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new SignalWorkDoneAction(ctx, this)));
// TODO: fix: bot runs forever even if test fails.
// TODO: fix: atom 1 is not deactivated if test fails.
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) PublishEventAction(won.bot.framework.eventbot.action.impl.PublishEventAction) LogErrorAction(won.bot.framework.eventbot.action.impl.LogErrorAction) EventBus(won.bot.framework.eventbot.bus.EventBus) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) CreateAtomWithSocketsAction(won.bot.framework.eventbot.action.impl.atomlifecycle.CreateAtomWithSocketsAction) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) ActionOnFirstNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstNEventsListener) ConstantNewAtomURIDecorator(won.bot.integrationtest.failsim.ConstantNewAtomURIDecorator) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) LogAction(won.bot.framework.eventbot.action.impl.LogAction) DeactivateAllAtomsAction(won.bot.framework.eventbot.action.impl.atomlifecycle.DeactivateAllAtomsAction) SuccessEvent(won.bot.framework.eventbot.event.impl.test.SuccessEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener)

Example 4 with MultipleActions

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

the class DuplicateMessageURIFailureBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // log error if we can create 2 atoms
    bus.subscribe(AtomCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogErrorAction(ctx, "Should not have been able to create 2 atoms using message with identical URIs"), new PublishEventAction(ctx, new TestFailedEvent(this, "Should not have been able to create 2 atoms with identical URI")))));
    // log success if we could create 1 atom
    bus.subscribe(AtomCreatedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: could create one atom"), new PublishEventAction(ctx, new SuccessEvent()))));
    // log success if we got an error for 2nd atom
    bus.subscribe(AtomCreationFailedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: atom creation failed for 2nd atom."), new PublishEventAction(ctx, new SuccessEvent()))));
    // when we have 2 SuccessEvents, we're done. Deactivate the atoms and signal
    // we're finished
    bus.subscribe(SuccessEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogAction(ctx, "Test passed."), new PublishEventAction(ctx, new TestPassedEvent(this)), new DeactivateAllAtomsAction(ctx))));
    // when we have a FailureEvent, we're done, too. Deactivate the atoms and signal
    // we're finished
    bus.subscribe(TestFailedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Test failed."), new DeactivateAllAtomsAction(ctx))));
    // wait for the atomDeactivated event, then say we're done.
    bus.subscribe(AtomDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new SignalWorkDoneAction(ctx, this)));
// TODO: fix: bot runs forever even if test fails.
// TODO: fix: atom 1 is not deactivated if test fails.
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) PublishEventAction(won.bot.framework.eventbot.action.impl.PublishEventAction) LogErrorAction(won.bot.framework.eventbot.action.impl.LogErrorAction) EventBus(won.bot.framework.eventbot.bus.EventBus) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) ActionOnFirstNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstNEventsListener) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) LogAction(won.bot.framework.eventbot.action.impl.LogAction) DeactivateAllAtomsAction(won.bot.framework.eventbot.action.impl.atomlifecycle.DeactivateAllAtomsAction) SuccessEvent(won.bot.framework.eventbot.event.impl.test.SuccessEvent)

Example 5 with MultipleActions

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

MultipleActions (won.bot.framework.eventbot.action.impl.MultipleActions)6 EventBus (won.bot.framework.eventbot.bus.EventBus)6 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)5 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)4 PublishEventAction (won.bot.framework.eventbot.action.impl.PublishEventAction)4 Event (won.bot.framework.eventbot.event.Event)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)4 DeactivateAllAtomsAction (won.bot.framework.eventbot.action.impl.atomlifecycle.DeactivateAllAtomsAction)3 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)3 TestFailedEvent (won.bot.framework.eventbot.event.impl.test.TestFailedEvent)3 TestPassedEvent (won.bot.framework.eventbot.event.impl.test.TestPassedEvent)3 LogAction (won.bot.framework.eventbot.action.impl.LogAction)2 LogErrorAction (won.bot.framework.eventbot.action.impl.LogErrorAction)2 CreateAtomWithSocketsAction (won.bot.framework.eventbot.action.impl.atomlifecycle.CreateAtomWithSocketsAction)2 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)2 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)2 URI (java.net.URI)1 Dataset (org.apache.jena.query.Dataset)1 Resource (org.apache.jena.rdf.model.Resource)1