use of won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter 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)));
}
use of won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter 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);
}
use of won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter in project webofneeds by researchstudio-sat.
the class EchoBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
EventBus bus = getEventBus();
// 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 - if we're not reacting to the creation of our own echo need.
this.needCreator = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), prepareCreateNeedAction(ctx));
bus.subscribe(NeedCreatedEventForMatcher.class, this.needCreator);
// as soon as the echo need is created, connect to original
this.needConnector = new ActionOnEventListener(ctx, "needConnector", new RandomDelayedAction(ctx, 5000, 5000, 1, new ConnectWithAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), "Greetings! I am the EchoBot! I will repeat everything you say, which you might " + "find useful for testing purposes.")));
bus.subscribe(NeedCreatedEvent.class, this.needConnector);
// add a listener that auto-responds to messages by a message
// after 10 messages, it unsubscribes from all events
// subscribe it to:
// * message events - so it responds
// * open events - so it initiates the chain reaction of responses
this.autoResponder = new ActionOnEventListener(ctx, new RespondWithEchoToMessageAction(ctx));
bus.subscribe(OpenFromOtherNeedEvent.class, this.autoResponder);
bus.subscribe(MessageFromOtherNeedEvent.class, this.autoResponder);
bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new LogAction(ctx, "received close message from remote need.")));
}
Aggregations