Search in sources :

Example 1 with AgreementProtocolState

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

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.";
    });
}
Also used : Duration(java.time.Duration) AgreementProtocolState(won.protocol.agreement.AgreementProtocolState) URI(java.net.URI)

Example 3 with AgreementProtocolState

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);*/
}
Also used : ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) SparqlSelectFunction(won.protocol.util.SparqlSelectFunction) AgreementProtocolState(won.protocol.agreement.AgreementProtocolState) Logger(ch.qos.logback.classic.Logger) StopWatch(org.springframework.util.StopWatch)

Example 4 with AgreementProtocolState

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.";
    });
}
Also used : Arrays(java.util.Arrays) Connection(won.protocol.model.Connection) CrawlConnectionDataBehaviour(won.bot.framework.eventbot.behaviour.CrawlConnectionDataBehaviour) WonConversationUtils(won.protocol.util.WonConversationUtils) SetChattinessDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetChattinessDebugCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) StringUtils(org.apache.commons.lang3.StringUtils) ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) WonMessage(won.protocol.message.WonMessage) Model(org.apache.jena.rdf.model.Model) UsageDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.UsageDebugCommandEvent) Matcher(java.util.regex.Matcher) Duration(java.time.Duration) BaseNeedAndConnectionSpecificEvent(won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) MessageEvent(won.bot.framework.eventbot.event.MessageEvent) DecimalFormat(java.text.DecimalFormat) StopWatch(org.springframework.util.StopWatch) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) MessageToElizaEvent(won.bot.framework.eventbot.event.impl.debugbot.MessageToElizaEvent) WonRdfUtils(won.protocol.util.WonRdfUtils) CrawlConnectionCommandSuccessEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandSuccessEvent) AgreementProtocolState(won.protocol.agreement.AgreementProtocolState) List(java.util.List) Event(won.bot.framework.eventbot.event.Event) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) SendNDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SendNDebugCommandEvent) WonConnectionValidator(won.protocol.validation.WonConnectionValidator) Optional(java.util.Optional) CrawlConnectionCommandEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent) SetCacheEagernessCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.SetCacheEagernessCommandEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) Duration(java.time.Duration) AgreementProtocolState(won.protocol.agreement.AgreementProtocolState) URI(java.net.URI)

Aggregations

AgreementProtocolState (won.protocol.agreement.AgreementProtocolState)4 Duration (java.time.Duration)3 StopWatch (org.springframework.util.StopWatch)3 URI (java.net.URI)2 Dataset (org.apache.jena.query.Dataset)2 Model (org.apache.jena.rdf.model.Model)2 CrawlConnectionDataBehaviour (won.bot.framework.eventbot.behaviour.CrawlConnectionDataBehaviour)2 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)2 CrawlConnectionCommandEvent (won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent)2 Logger (ch.qos.logback.classic.Logger)1 DecimalFormat (java.text.DecimalFormat)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 StringUtils (org.apache.commons.lang3.StringUtils)1 QuerySolution (org.apache.jena.query.QuerySolution)1 ResourceImpl (org.apache.jena.rdf.model.impl.ResourceImpl)1