Search in sources :

Example 1 with CrawlConnectionCommandEvent

use of won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent 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 CrawlConnectionCommandEvent

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

the class CrawlConnectionDataBehaviour method onActivate.

@Override
protected void onActivate(Optional<Object> message) {
    logger.debug("activating crawling connection data for connection {}", command.getConnectionURI());
    logger.debug("will deactivate autmatically after " + abortTimeout);
    LinkedDataSource linkedDataSource = context.getLinkedDataSource();
    if (linkedDataSource instanceof CachingLinkedDataSource) {
        URI toInvalidate = WonLinkedDataUtils.getEventContainerURIforConnectionURI(command.getConnectionURI(), linkedDataSource);
        ((CachingLinkedDataSource) linkedDataSource).invalidate(toInvalidate);
        ((CachingLinkedDataSource) linkedDataSource).invalidate(toInvalidate, command.getNeedURI());
        URI remoteConnectionUri = WonLinkedDataUtils.getRemoteConnectionURIforConnectionURI(command.getConnectionURI(), linkedDataSource);
        toInvalidate = WonLinkedDataUtils.getEventContainerURIforConnectionURI(remoteConnectionUri, linkedDataSource);
        ((CachingLinkedDataSource) linkedDataSource).invalidate(toInvalidate);
        ((CachingLinkedDataSource) linkedDataSource).invalidate(toInvalidate, command.getNeedURI());
    }
    context.getTaskScheduler().schedule(new Runnable() {

        @Override
        public void run() {
            deactivate();
        }
    }, new Date(System.currentTimeMillis() + abortTimeout.toMillis()));
    ;
    List<Path> propertyPaths = new ArrayList<>();
    PrefixMapping pmap = new PrefixMappingImpl();
    pmap.withDefaultMappings(PrefixMapping.Standard);
    pmap.setNsPrefix("won", WON.getURI());
    pmap.setNsPrefix("msg", WONMSG.getURI());
    propertyPaths.add(PathParser.parse("won:hasEventContainer", pmap));
    propertyPaths.add(PathParser.parse("won:hasEventContainer/rdfs:member", pmap));
    CrawlCommandEvent crawlNeedCommandEvent = new CrawlCommandEvent(command.getNeedURI(), command.getNeedURI(), propertyPaths, 10000, 5);
    propertyPaths = new ArrayList();
    propertyPaths.add(PathParser.parse("won:hasEventContainer", pmap));
    propertyPaths.add(PathParser.parse("won:hasEventContainer/rdfs:member", pmap));
    propertyPaths.add(PathParser.parse("won:hasEventContainer/rdfs:member/msg:hasCorrespondingRemoteMessage", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteNeed", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteNeed/won:hasEventContainer", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteNeed/won:hasEventContainer/rdfs:member", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteConnection", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteConnection/won:hasEventContainer", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteConnection/won:hasEventContainer/rdfs:member", pmap));
    propertyPaths.add(PathParser.parse("won:hasRemoteConnection/won:hasEventContainer/rdfs:member/msg:hasCorrespondingRemoteMessage", pmap));
    CrawlCommandEvent crawlConnectionCommandEvent = new CrawlCommandEvent(command.getNeedURI(), command.getConnectionURI(), propertyPaths, 10000, 5);
    Dataset crawledData = DatasetFactory.createGeneral();
    // add crawlcommand listener
    this.subscribeWithAutoCleanup(CrawlCommandEvent.class, new ActionOnEventListener(context, new OrFilter(new SameEventFilter(crawlNeedCommandEvent), new SameEventFilter(crawlConnectionCommandEvent)), new CrawlAction(context)));
    // when the first crawl succeeds, start the second
    this.subscribeWithAutoCleanup(CrawlCommandSuccessEvent.class, new ActionOnEventListener(context, new CommandResultFilter(crawlNeedCommandEvent), new BaseEventBotAction(context) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            logger.debug("finished crawling need data. ");
            Dataset dataset = ((CrawlCommandSuccessEvent) event).getCrawledData();
            RdfUtils.addDatasetToDataset(crawledData, dataset);
            // now crawl connection data
            context.getEventBus().publish(crawlConnectionCommandEvent);
        }
    }));
    // when we're done crawling, validate:
    this.subscribeWithAutoCleanup(CrawlCommandSuccessEvent.class, new ActionOnEventListener(context, new CommandResultFilter(crawlConnectionCommandEvent), new BaseEventBotAction(context) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            logger.debug("finished crawling need data for connection {}", command.getConnectionURI());
            Dataset dataset = ((CrawlCommandSuccessEvent) event).getCrawledData();
            RdfUtils.addDatasetToDataset(crawledData, dataset);
            context.getEventBus().publish(new CrawlConnectionCommandSuccessEvent(command, crawledData));
            deactivate();
        }
    }));
    // when something goes wrong, abort
    this.subscribeWithAutoCleanup(CrawlCommandFailureEvent.class, new ActionOnFirstEventListener(context, new OrFilter(new CommandResultFilter(crawlConnectionCommandEvent), new CommandResultFilter(crawlNeedCommandEvent)), new BaseEventBotAction(context) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            CrawlCommandFailureEvent failureEvent = (CrawlCommandFailureEvent) event;
            logger.debug("crawling failed for connection {}, message: {}", command.getConnectionURI(), failureEvent.getMessage());
            context.getEventBus().publish(new CrawlConnectionCommandFailureEvent(failureEvent.getMessage(), command));
            deactivate();
        }
    }));
    // start crawling the need  - connection will be crawled when need crawling is done
    context.getEventBus().publish(crawlNeedCommandEvent);
}
Also used : SameEventFilter(won.bot.framework.eventbot.filter.impl.SameEventFilter) CrawlAction(won.bot.framework.eventbot.action.impl.crawl.CrawlAction) CrawlConnectionCommandSuccessEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandSuccessEvent) ArrayList(java.util.ArrayList) CrawlCommandSuccessEvent(won.bot.framework.eventbot.event.impl.crawl.CrawlCommandSuccessEvent) CommandResultFilter(won.bot.framework.eventbot.filter.impl.CommandResultFilter) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) URI(java.net.URI) CrawlCommandEvent(won.bot.framework.eventbot.event.impl.crawl.CrawlCommandEvent) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) Path(org.apache.jena.sparql.path.Path) CrawlCommandFailureEvent(won.bot.framework.eventbot.event.impl.crawl.CrawlCommandFailureEvent) Dataset(org.apache.jena.query.Dataset) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) CrawlConnectionCommandFailureEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandFailureEvent) Date(java.util.Date) PrefixMapping(org.apache.jena.shared.PrefixMapping) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) CrawlCommandEvent(won.bot.framework.eventbot.event.impl.crawl.CrawlCommandEvent) CrawlConnectionCommandFailureEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandFailureEvent) CrawlCommandFailureEvent(won.bot.framework.eventbot.event.impl.crawl.CrawlCommandFailureEvent) CommandResultEvent(won.bot.framework.eventbot.event.impl.cmd.CommandResultEvent) CrawlCommandSuccessEvent(won.bot.framework.eventbot.event.impl.crawl.CrawlCommandSuccessEvent) CrawlConnectionCommandSuccessEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandSuccessEvent) Event(won.bot.framework.eventbot.event.Event) CrawlConnectionCommandEvent(won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener)

Example 3 with CrawlConnectionCommandEvent

use of won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent 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

CrawlConnectionCommandEvent (won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandEvent)3 Model (org.apache.jena.rdf.model.Model)2 StopWatch (org.springframework.util.StopWatch)2 CrawlConnectionDataBehaviour (won.bot.framework.eventbot.behaviour.CrawlConnectionDataBehaviour)2 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)2 CrawlConnectionCommandSuccessEvent (won.bot.framework.eventbot.event.impl.crawlconnection.CrawlConnectionCommandSuccessEvent)2 URI (java.net.URI)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Dataset (org.apache.jena.query.Dataset)1 PrefixMapping (org.apache.jena.shared.PrefixMapping)1 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)1 Path (org.apache.jena.sparql.path.Path)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 CrawlAction (won.bot.framework.eventbot.action.impl.crawl.CrawlAction)1 Event (won.bot.framework.eventbot.event.Event)1 CommandResultEvent (won.bot.framework.eventbot.event.impl.cmd.CommandResultEvent)1 CrawlCommandEvent (won.bot.framework.eventbot.event.impl.crawl.CrawlCommandEvent)1 CrawlCommandFailureEvent (won.bot.framework.eventbot.event.impl.crawl.CrawlCommandFailureEvent)1