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();
}
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));
}
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));
}
}
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));
}
}
}
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();
}
Aggregations