use of won.protocol.agreement.AgreementProtocolState 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.protocol.agreement.AgreementProtocolState in project webofneeds by researchstudio-sat.
the class DebugBotIncomingMessageToEventMappingAction method reject.
private void reject(EventListenerContext ctx, EventBus bus, Connection con, boolean useWrongSender) {
String whose = useWrongSender ? "my" : "your";
referToEarlierMessages(ctx, bus, con, "ok, I'll reject " + whose + " latest rejectable message - but I'll need to crawl the connection data first, please be patient.", state -> {
URI uri = state.getLatestProposesMessageSentByNeed(useWrongSender ? con.getNeedURI() : con.getRemoteNeedURI());
return uri == null ? Collections.EMPTY_LIST : Arrays.asList(uri);
}, (messageModel, uris) -> WonRdfUtils.MessageUtils.addRejects(messageModel, uris), (Duration queryDuration, AgreementProtocolState state, URI... uris) -> {
if (uris == null || uris.length == 0 || uris[0] == null) {
return "Sorry, I cannot reject any of " + whose + " messages - I did not find any suitable message.";
}
Optional<String> retractedString = state.getTextMessage(uris[0]);
String finalRetractedString = (retractedString.isPresent()) ? ", which read, '" + retractedString.get() + "'" : ", which had no text message";
return "Ok, I am hereby rejecting " + whose + " message" + finalRetractedString + " (uri: " + uris[0] + ")." + "\n The query for finding that message took " + getDurationString(queryDuration) + " seconds.";
});
}
use of won.protocol.agreement.AgreementProtocolState in project webofneeds by researchstudio-sat.
the class WonConversationUtilsTest method main.
public static void main(String... args) throws Exception {
Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.INFO);
// Dataset input = loadDataset("/won/utils/agreement/input/one-agreement-one-cancellation.trig");
Dataset input = loadDataset("/won/utils/conversationutils/input/longer-conversation.trig");
SparqlSelectFunction<QuerySolution> selectfunction = new SparqlSelectFunction<>("/conversation/messagesForHighlevelProtocols.rq", x -> x).addOrderBy("distance", Query.ORDER_ASCENDING).addInitialBinding("senderOfFirstMessage", new ResourceImpl("https://localhost:8443/won/resource/need/4517796920802783000"));
StopWatch sw = new StopWatch();
sw.start();
selectfunction.apply(input).forEach(x -> System.out.println("solution:" + x.toString()));
sw.stop();
System.out.println("query took: " + sw.getLastTaskTimeMillis() / 1000d + " seconds ");
// Thread.currentThread().sleep(30000);
sw.start();
AgreementProtocolState state = AgreementProtocolState.of(input);
sw.stop();
RDFDataMgr.write(System.out, state.getAgreements(), Lang.TRIG);
System.out.println("Analyzing the state of agreement protocol took: " + sw.getLastTaskTimeMillis() / 1000d + " seconds ");
System.out.println(state.getAgreementProtocolUris());
System.out.println(state.getEffects(URI.create("https://localhost:8443/won/resource/event/grj35bbhnkcrlfc72pqo")));
/*
RdfUtils.Pair<Dataset> diff = RdfUtils.diff(input, output);
if (!(diff.getFirst().isEmpty() && diff.getSecond().isEmpty())) {
System.out.println("diff - only in input:");
RDFDataMgr.write(System.out, diff.getFirst(), Lang.TRIG);
System.out.println("diff - only in output:");
RDFDataMgr.write(System.out, diff.getSecond(), Lang.TRIG);
} else {
System.out.println("input and output are equal");
}
*/
// Dataset output = HighlevelProtocols.getProposals(input);
// RDFDataMgr.write(System.out, output, Lang.TRIG);
// initialBinding.add("senderNeed", new ResourceImpl("https://localhost:8443/won/resource/need/7820503869697675000"));
// initialBinding.add("senderConnection", new ResourceImpl("https://localhost:8443/won/resource/connection/4t9deo4t6bqx83jxk5ex"));
// URI uri = WonConversationUtils.getLatestMessageOfNeed(input, URI.create("https://localhost:8443/won/resource/need/7820503869697675000"));
// URI uri = WonConversationUtils.getLatestMessageOfNeed(input, URI.create("https://192.168.124.49:8443/won/resource/need/z35td13ftmn5k1hzella"));
// System.out.println("uri: " + uri);
/*
List<QuerySolution> actual = WonConversationQueryBuilder.getBuilder((x -> x))
//.senderNeed(URI.create("https://localhost:8443/won/resource/need/7820503869697675000"))
.newestFirst()
.noResponses()
.build()
.apply(input);
*/
// System.out.println("actual: \n" + actual.stream().map(x -> x.toString()).collect(Collectors.joining("\n")));
/*
URI uri = WonConversationUtils.getLatestProposesMessageOfNeed(input, URI.create("https://localhost:8443/won/resource/need/7820503869697675000"));
System.out.println("uri: " + uri);
uri = WonConversationUtils.getLatestAcceptsMessageOfNeed(input, URI.create("https://localhost:8443/won/resource/need/7820503869697675000"));
System.out.println("uri: " + uri);*/
}
use of won.protocol.agreement.AgreementProtocolState in project webofneeds by researchstudio-sat.
the class DebugBotIncomingMessageToEventMappingAction method retract.
private void retract(EventListenerContext ctx, EventBus bus, Connection con, boolean useWrongSender, boolean onlyProposes) {
String whose = useWrongSender ? "your" : "my";
String which = onlyProposes ? "proposal " : "";
referToEarlierMessages(ctx, bus, con, "ok, I'll retract " + whose + " latest " + which + "message - but 'll need to crawl the connection data first, please be patient.", state -> {
URI uri = state.getNthLatestMessage(m -> onlyProposes ? (m.isProposesMessage() || m.isProposesToCancelMessage()) && m.getEffects().stream().anyMatch(e -> e.isProposes()) : true && useWrongSender ? m.getSenderNeedURI().equals(con.getRemoteNeedURI()) : m.getSenderNeedURI().equals(con.getNeedURI()), 0);
return uri == null ? Collections.EMPTY_LIST : Arrays.asList(uri);
}, (messageModel, uris) -> WonRdfUtils.MessageUtils.addRetracts(messageModel, uris), (Duration queryDuration, AgreementProtocolState state, URI... uris) -> {
if (uris == null || uris.length == 0 || uris[0] == null) {
return "Sorry, I cannot retract any messages - I did not find any.";
}
Optional<String> retractedString = state.getTextMessage(uris[0]);
String finalRetractedString = (retractedString.isPresent()) ? ", which read, '" + retractedString.get() + "'" : ", which had no text message";
return "Ok, I am hereby retracting " + whose + " message" + finalRetractedString + " (uri: " + uris[0] + ")." + "\n The query for finding that message took " + getDurationString(queryDuration) + " seconds.";
});
}
Aggregations