Search in sources :

Example 1 with HintDebugCommandEvent

use of won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent in project webofneeds by researchstudio-sat.

the class DebugBotIncomingMessageToEventMappingAction method handleTextMessageEvent.

private void handleTextMessageEvent(final ConnectionSpecificEvent messageEvent) {
    if (messageEvent instanceof MessageEvent) {
        EventListenerContext ctx = getEventListenerContext();
        EventBus bus = ctx.getEventBus();
        Connection con = ((BaseNeedAndConnectionSpecificEvent) messageEvent).getCon();
        WonMessage msg = ((MessageEvent) messageEvent).getWonMessage();
        String message = extractTextMessageFromWonMessage(msg);
        try {
            if (message == null) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Whatever you sent me there, it was not a normal text message. I'm expecting a <message> won:hasTextMessage \"Some text\" triple in that message.");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
            } else if (PATTERN_USAGE.matcher(message).matches()) {
                bus.publish(new UsageDebugCommandEvent(con));
            } else if (PATTERN_HINT.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll create a new need and make it send a hint to you.");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                bus.publish(new HintDebugCommandEvent(con));
            } else if (PATTERN_CONNECT.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll create a new need and make it send a connect to you.");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                bus.publish(new ConnectDebugCommandEvent(con));
            } else if (PATTERN_CLOSE.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll close this connection");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                bus.publish(new CloseCommandEvent(con));
            } else if (PATTERN_DEACTIVATE.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll deactivate this need. This will close the connection we are currently talking on.");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                bus.publish(new DeactivateNeedCommandEvent(con.getNeedURI()));
            } else if (PATTERN_CHATTY_ON.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll send you messages spontaneously from time to time.");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                bus.publish(new SetChattinessDebugCommandEvent(con, true));
            } else if (PATTERN_CHATTY_OFF.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, from now on I will be quiet and only respond to your messages.");
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                bus.publish(new SetChattinessDebugCommandEvent(con, false));
            } else if (PATTERN_CACHE_EAGER.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I'll put any message I receive or send into the RDF cache. This slows down message processing in general, but operations that require crawling connection data will be faster.");
                bus.publish(new SetCacheEagernessCommandEvent(true));
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
            } else if (PATTERN_CACHE_LAZY.matcher(message).matches()) {
                Model messageModel = WonRdfUtils.MessageUtils.textMessage("Ok, I won't put messages I receive or send into the RDF cache. This speeds up message processing in general, but operations that require crawling connection data will be slowed down.");
                bus.publish(new SetCacheEagernessCommandEvent(false));
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
            } else if (PATTERN_SEND_N.matcher(message).matches()) {
                Matcher m = PATTERN_SEND_N.matcher(message);
                m.find();
                String nStr = m.group(1);
                int n = Integer.parseInt(nStr);
                bus.publish(new SendNDebugCommandEvent(con, n));
            } else if (PATTERN_VALIDATE.matcher(message).matches()) {
                validate(ctx, bus, con);
            } else if (PATTERN_RETRACT.matcher(message).matches()) {
                Matcher m = PATTERN_RETRACT.matcher(message);
                m.matches();
                boolean useWrongSender = m.group(3) != null;
                boolean retractProposes = m.group(4) != null;
                retract(ctx, bus, con, useWrongSender, retractProposes);
            } else if (PATTERN_REJECT.matcher(message).matches()) {
                Matcher m = PATTERN_REJECT.matcher(message);
                m.matches();
                boolean useWrongSender = m.group(2) != null;
                reject(ctx, bus, con, useWrongSender);
            } else if (PATTERN_PROPOSE.matcher(message).matches()) {
                Matcher m = PATTERN_PROPOSE.matcher(message);
                m.matches();
                boolean my = m.group(3) != null;
                boolean any = m.group(4) != null;
                int count = m.group(5) == null ? 1 : Integer.parseInt(m.group(5));
                propose(ctx, bus, con, any || !my, any || my, count);
            } else if (PATTERN_ACCEPT.matcher(message).matches()) {
                accept(ctx, bus, con);
            } else if (PATTERN_CANCEL.matcher(message).matches()) {
                cancel(ctx, bus, con);
            } else {
                // default: answer with eliza.
                bus.publish(new MessageToElizaEvent(con, message));
            }
        } catch (Exception e) {
            // error: send an error message
            Model messageModel = WonRdfUtils.MessageUtils.textMessage("Did not understand your command '" + message + "': " + e.getClass().getSimpleName() + ":" + e.getMessage());
            bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
        }
    }
}
Also used : UsageDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.UsageDebugCommandEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BaseNeedAndConnectionSpecificEvent(won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent) Matcher(java.util.regex.Matcher) MessageEvent(won.bot.framework.eventbot.event.MessageEvent) Connection(won.protocol.model.Connection) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) SendNDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SendNDebugCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) SetCacheEagernessCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) SetChattinessDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetChattinessDebugCommandEvent) WonMessage(won.protocol.message.WonMessage) Model(org.apache.jena.rdf.model.Model) MessageToElizaEvent(won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Example 2 with HintDebugCommandEvent

use of won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent in project webofneeds by researchstudio-sat.

the class CreateDebugNeedWithFacetsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    String replyText = "";
    URI reactingToNeedUriTmp = null;
    Dataset needDataset = null;
    if (event instanceof NeedSpecificEvent) {
        reactingToNeedUriTmp = ((NeedSpecificEvent) event).getNeedURI();
    } else {
        logger.warn("could not process non-need specific event {}", event);
        return;
    }
    if (event instanceof NeedCreatedEventForMatcher) {
        needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
    } else if (event instanceof HintDebugCommandEvent) {
        reactingToNeedUriTmp = ((HintDebugCommandEvent) event).getRemoteNeedURI();
    } else if (event instanceof ConnectDebugCommandEvent) {
        reactingToNeedUriTmp = ((ConnectDebugCommandEvent) event).getRemoteNeedURI();
    } else {
        logger.error("CreateEchoNeedWithFacetsAction cannot handle " + event.getClass().getName());
        return;
    }
    final URI reactingToNeedUri = reactingToNeedUriTmp;
    String titleString = null;
    boolean createNeed = true;
    if (needDataset != null) {
        DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
        titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
        createNeed = needModelWrapper.hasFlag(WON.USED_FOR_TESTING) && !needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME);
    }
    // if create need is false do not continue the debug need creation
    if (!createNeed)
        return;
    if (titleString != null) {
        if (isInitialForConnect) {
            replyText = "Debugging with initial connect: " + titleString;
        } else if (isInitialForHint) {
            replyText = "Debugging with initial hint: " + titleString;
        } else {
            replyText = "Debugging: " + titleString;
        }
    } else {
        replyText = "Debug Need No. " + counter.increment();
    }
    EventListenerContext ctx = getEventListenerContext();
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    EventBus bus = ctx.getEventBus();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
    needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, replyText);
    needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the DebugBot.");
    for (URI facet : facets) {
        needModelWrapper.addFacetUri(facet.toString());
    }
    final Dataset debugNeedDataset = needModelWrapper.copyDataset();
    final Event origEvent = event;
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(debugNeedDataset), 150));
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, debugNeedDataset);
    // remember the need URI so we can react to success/failure responses
    EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
    EventListener successCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            logger.debug("need creation successful, new need URI is {}", needURI);
            // save the mapping between the original and the reaction in to the context.
            getEventListenerContext().getBotContextWrapper().addUriAssociation(reactingToNeedUri, needURI);
            if ((origEvent instanceof HintDebugCommandEvent) || isInitialForHint) {
                bus.publish(new NeedCreatedEventForDebugHint(needURI, wonNodeUri, debugNeedDataset, null));
            } else if ((origEvent instanceof ConnectDebugCommandEvent) || isInitialForConnect) {
                bus.publish(new NeedCreatedEventForDebugConnect(needURI, wonNodeUri, debugNeedDataset, null));
            } else {
                bus.publish(new NeedCreatedEvent(needURI, wonNodeUri, debugNeedDataset, null));
            }
        }
    };
    EventListener failureCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
            logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
            EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
            bus.publish(new NeedCreationFailedEvent(wonNodeUri));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
    ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
    logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) NeedCreatedEventForDebugConnect(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugConnect) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) NeedCreatedEventForDebugHint(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugHint) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) WonMessage(won.protocol.message.WonMessage) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 ConnectDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent)2 HintDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent)2 WonMessage (won.protocol.message.WonMessage)2 URI (java.net.URI)1 Matcher (java.util.regex.Matcher)1 Dataset (org.apache.jena.query.Dataset)1 Model (org.apache.jena.rdf.model.Model)1 BaseNeedAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent)1 Event (won.bot.framework.eventbot.event.Event)1 MessageEvent (won.bot.framework.eventbot.event.MessageEvent)1 NeedCreationFailedEvent (won.bot.framework.eventbot.event.NeedCreationFailedEvent)1 NeedSpecificEvent (won.bot.framework.eventbot.event.NeedSpecificEvent)1 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)1 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)1 DeactivateNeedCommandEvent (won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent)1 MessageToElizaEvent (won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent)1 NeedCreatedEventForDebugConnect (won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugConnect)1 NeedCreatedEventForDebugHint (won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugHint)1