Search in sources :

Example 1 with ConnectionMessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.

the class DebugBotIncomingMessageToEventMappingAction method referToEarlierMessages.

private void referToEarlierMessages(EventListenerContext ctx, EventBus bus, Connection con, String crawlAnnouncement, MessageFinder messageFinder, MessageReferrer messageReferrer, TextMessageMaker textMessageMaker) {
    Model messageModel = WonRdfUtils.MessageUtils.textMessage(crawlAnnouncement);
    bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
    // initiate crawl behaviour
    CrawlConnectionCommandEvent command = new CrawlConnectionCommandEvent(con.getNeedURI(), con.getConnectionURI());
    CrawlConnectionDataBehaviour crawlConnectionDataBehaviour = new CrawlConnectionDataBehaviour(ctx, command, Duration.ofSeconds(60));
    final StopWatch crawlStopWatch = new StopWatch();
    crawlStopWatch.start("crawl");
    AgreementProtocolState state = WonConversationUtils.getAgreementProtocolState(con.getConnectionURI(), ctx.getLinkedDataSource());
    crawlStopWatch.stop();
    Duration crawlDuration = Duration.ofMillis(crawlStopWatch.getLastTaskTimeMillis());
    messageModel = WonRdfUtils.MessageUtils.textMessage("Finished crawl in " + getDurationString(crawlDuration) + " seconds. The dataset has " + state.getConversationDataset().asDatasetGraph().size() + " rdf graphs.");
    getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(con, messageModel));
    messageModel = makeReferringMessage(state, messageFinder, messageReferrer, textMessageMaker);
    getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(con, messageModel));
    crawlConnectionDataBehaviour.activate();
}
Also used : CrawlConnectionDataBehaviour(won.bot.framework.eventbot.behaviour.CrawlConnectionDataBehaviour) Model(org.apache.jena.rdf.model.Model) Duration(java.time.Duration) AgreementProtocolState(won.protocol.agreement.AgreementProtocolState) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) CrawlConnectionCommandEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent) StopWatch(org.springframework.util.StopWatch)

Example 2 with ConnectionMessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.

the class SendMessageOnCrawlResultAction method onCrawlFailure.

@Override
protected final void onCrawlFailure(CrawlConnectionCommandFailureEvent failureEvent) {
    Model messageModel = makeFailureMessage(failureEvent);
    if (messageModel == null)
        return;
    getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(con, messageModel));
}
Also used : Model(org.apache.jena.rdf.model.Model) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Example 3 with ConnectionMessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.

the class AnswerWithElizaAction method doRun.

@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
    if (event instanceof MessageToElizaEvent) {
        MessageToElizaEvent messageToElizaEvent = (MessageToElizaEvent) event;
        String elizaResponse;
        synchronized (eliza) {
            elizaResponse = eliza.processInput(((MessageToElizaEvent) event).getMessage());
        }
        Model messageModel = WonRdfUtils.MessageUtils.textMessage(elizaResponse);
        getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(messageToElizaEvent.getCon(), messageModel));
    }
}
Also used : 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 4 with ConnectionMessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent 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 5 with ConnectionMessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.

the class DebugBotIncomingMessageToEventMappingAction method validate.

private void validate(EventListenerContext ctx, EventBus bus, Connection con) {
    Model messageModel = WonRdfUtils.MessageUtils.textMessage("ok, I'll validate the connection - but I'll need to crawl the connection data first, please be patient.");
    bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
    // initiate crawl behaviour
    CrawlConnectionCommandEvent command = new CrawlConnectionCommandEvent(con.getNeedURI(), con.getConnectionURI());
    CrawlConnectionDataBehaviour crawlConnectionDataBehaviour = new CrawlConnectionDataBehaviour(ctx, command, Duration.ofSeconds(60));
    final StopWatch crawlStopWatch = new StopWatch();
    crawlStopWatch.start("crawl");
    crawlConnectionDataBehaviour.onResult(new SendMessageReportingCrawlResultAction(ctx, con, crawlStopWatch));
    crawlConnectionDataBehaviour.onResult(new SendMessageOnCrawlResultAction(ctx, con) {

        @Override
        protected Model makeSuccessMessage(CrawlConnectionCommandSuccessEvent successEvent) {
            try {
                logger.debug("validating data of connection {}", command.getConnectionURI());
                // TODO: use one validator for all invocations
                WonConnectionValidator validator = new WonConnectionValidator();
                StringBuilder message = new StringBuilder();
                boolean valid = validator.validate(successEvent.getCrawledData(), message);
                String successMessage = "Connection " + command.getConnectionURI() + " is valid: " + valid + " " + message.toString();
                return WonRdfUtils.MessageUtils.textMessage(successMessage);
            } catch (Exception e) {
                return WonRdfUtils.MessageUtils.textMessage("Caught exception during validation: " + e);
            }
        }
    });
    crawlConnectionDataBehaviour.activate();
}
Also used : CrawlConnectionCommandSuccessEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandSuccessEvent) CrawlConnectionDataBehaviour(won.bot.framework.eventbot.behaviour.CrawlConnectionDataBehaviour) StopWatch(org.springframework.util.StopWatch) WonConnectionValidator(won.protocol.validation.WonConnectionValidator) Model(org.apache.jena.rdf.model.Model) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) CrawlConnectionCommandEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent)

Aggregations

Model (org.apache.jena.rdf.model.Model)9 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)9 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)4 Connection (won.protocol.model.Connection)4 URI (java.net.URI)3 Dataset (org.apache.jena.query.Dataset)3 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)3 EventBus (won.bot.framework.eventbot.bus.EventBus)3 Event (won.bot.framework.eventbot.event.Event)3 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)3 EventListener (won.bot.framework.eventbot.listener.EventListener)3 ConnectionModelMapper (won.protocol.model.ConnectionModelMapper)3 RdfUtils (won.protocol.util.RdfUtils)3 WonRdfUtils (won.protocol.util.WonRdfUtils)3 StopWatch (org.springframework.util.StopWatch)2 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)2 ConnectCommandEvent (won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent)2 DeactivateNeedCommandEvent (won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent)2 IOException (java.io.IOException)1 AccessControlException (java.security.AccessControlException)1