Search in sources :

Example 1 with DeactivateAllAtomsAction

use of won.bot.framework.eventbot.action.impl.atomlifecycle.DeactivateAllAtomsAction 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 2 with DeactivateAllAtomsAction

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

Example 3 with DeactivateAllAtomsAction

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

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)3 MultipleActions (won.bot.framework.eventbot.action.impl.MultipleActions)3 PublishEventAction (won.bot.framework.eventbot.action.impl.PublishEventAction)3 DeactivateAllAtomsAction (won.bot.framework.eventbot.action.impl.atomlifecycle.DeactivateAllAtomsAction)3 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)3 EventBus (won.bot.framework.eventbot.bus.EventBus)3 TestFailedEvent (won.bot.framework.eventbot.event.impl.test.TestFailedEvent)3 TestPassedEvent (won.bot.framework.eventbot.event.impl.test.TestPassedEvent)3 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)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 SuccessEvent (won.bot.framework.eventbot.event.impl.test.SuccessEvent)2 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)2 ActionOnFirstNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnFirstNEventsListener)2 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 CloseConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction)1 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)1 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)1 Event (won.bot.framework.eventbot.event.Event)1