Search in sources :

Example 1 with CachingLinkedDataSource

use of won.protocol.util.linkeddata.CachingLinkedDataSource in project webofneeds by researchstudio-sat.

the class BATestScriptListener method assertCorrectConnectionState.

private void assertCorrectConnectionState(final URI fromCon, final BATestScriptAction action) {
    LinkedDataSource linkedDataSource = getEventListenerContext().getLinkedDataSource();
    if (linkedDataSource instanceof CachingLinkedDataSource) {
        ((CachingLinkedDataSource) linkedDataSource).invalidate(fromCon);
    }
    logger.debug("fromCon {}, stateOfSenderBeforeSending{}", fromCon, action.getStateOfSenderBeforeSending());
    Dataset dataModel = linkedDataSource.getDataForResource(fromCon);
    logger.debug("crawled dataset for fromCon {}: {}", fromCon, RdfUtils.toString(dataModel));
    String sparqlPrefix = "PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>" + "PREFIX geo:   <http://www.w3.org/2003/01/geo/wgs84_pos#>" + "PREFIX xsd:   <http://www.w3.org/2001/XMLSchema#>" + "PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "PREFIX won:   <http://purl.org/webofneeds/model#>" + "PREFIX wontx:   <http://purl.org/webofneeds/tx/model#>" + "PREFIX gr:    <http://purl.org/goodrelations/v1#>" + "PREFIX sioc:  <http://rdfs.org/sioc/ns#>" + "PREFIX ldp:   <http://www.w3.org/ns/ldp#>";
    String queryString = sparqlPrefix + "ASK WHERE { ?con wontx:hasBAState ?state }";
    QuerySolutionMap binding = new QuerySolutionMap();
    binding.add("con", new ResourceImpl(fromCon.toString()));
    binding.add("state", new ResourceImpl(action.getStateOfSenderBeforeSending().toString()));
    Query query = QueryFactory.create(queryString);
    QueryExecution qExec = QueryExecutionFactory.create(query, dataModel, binding);
    boolean result = qExec.execAsk();
    // check if the connection is really in the state required for the action
    if (result)
        return;
    // we detected an error. Throw an exception.
    // query again, this time fetch the state so we can display an informaitive error message
    queryString = sparqlPrefix + "SELECT ?state WHERE { ?con wontx:hasBAState ?state }";
    binding = new QuerySolutionMap();
    binding.add("con", new ResourceImpl(fromCon.toString()));
    query = QueryFactory.create(queryString);
    qExec = QueryExecutionFactory.create(query, dataModel, binding);
    ResultSet res = qExec.execSelect();
    if (!res.hasNext()) {
        throw new IllegalStateException("connection state of connection " + fromCon + " does " + "not allow next action " + action + ". Could not determine actual connection state: not found");
    }
    QuerySolution solution = res.next();
    RDFNode state = solution.get("state");
    throw new IllegalStateException("connection state " + state + " of connection " + fromCon + " does " + "not allow next action " + action);
}
Also used : LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 2 with CachingLinkedDataSource

use of won.protocol.util.linkeddata.CachingLinkedDataSource 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 CachingLinkedDataSource

use of won.protocol.util.linkeddata.CachingLinkedDataSource in project webofneeds by researchstudio-sat.

the class WonConversationUtils method refreshDataForConnection.

private static void refreshDataForConnection(URI connectionUri, URI needUri, LinkedDataSource linkedDataSource) {
    // we throw the exception on:
    if (!(linkedDataSource instanceof CachingLinkedDataSource)) {
        return;
    }
    if (connectionUri == null) {
        return;
    }
    invalidate(connectionUri, needUri, linkedDataSource);
    URI connectionEventContainerUri = WonLinkedDataUtils.getEventContainerURIforConnectionURI(connectionUri, linkedDataSource);
    invalidate(connectionEventContainerUri, needUri, linkedDataSource);
    URI remoteConnectionUri = WonLinkedDataUtils.getRemoteConnectionURIforConnectionURI(connectionUri, linkedDataSource);
    if (remoteConnectionUri == null) {
        return;
    }
    invalidate(remoteConnectionUri, needUri, linkedDataSource);
    URI remoteConnectionEventContainerUri = WonLinkedDataUtils.getEventContainerURIforConnectionURI(remoteConnectionUri, linkedDataSource);
    invalidate(remoteConnectionEventContainerUri, needUri, linkedDataSource);
}
Also used : CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) URI(java.net.URI)

Example 4 with CachingLinkedDataSource

use of won.protocol.util.linkeddata.CachingLinkedDataSource in project webofneeds by researchstudio-sat.

the class EagerlyCachePopulatingMessageProcessor method process.

@Override
public WonMessage process(WonMessage message) throws WonMessageProcessingException {
    if (this.linkedDataSourceOnBehalfOfNeed != null && this.linkedDataSourceOnBehalfOfNeed instanceof CachingLinkedDataSource) {
        logger.debug("eagerly fetching delivery chain for mesasge {} into cache", message.getMessageURI());
        URI requester = message.getReceiverNeedURI();
        ((CachingLinkedDataSource) linkedDataSourceOnBehalfOfNeed).addToCache(message.getCompleteDataset(), message.getMessageURI(), requester);
        // load the original message(s) into cache, too
        Set<URI> toLoad = new HashSet<URI>();
        addIfNotNull(toLoad, message.getIsRemoteResponseToMessageURI());
        addIfNotNull(toLoad, message.getIsResponseToMessageURI());
        addIfNotNull(toLoad, message.getCorrespondingRemoteMessageURI());
        List<URI> previous = WonRdfUtils.MessageUtils.getPreviousMessageUrisIncludingRemote(message);
        addIfNotNull(toLoad, previous);
        parallelRequestsThreadpool.submit(() -> toLoad.parallelStream().forEach(uri -> linkedDataSourceOnBehalfOfNeed.getDataForResource(uri, requester)));
    }
    return message;
}
Also used : LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) Logger(org.slf4j.Logger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) Set(java.util.Set) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) WonMessage(won.protocol.message.WonMessage) WonRdfUtils(won.protocol.util.WonRdfUtils) HashSet(java.util.HashSet) List(java.util.List) WonMessageProcessor(won.protocol.message.processor.WonMessageProcessor) URI(java.net.URI) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) URI(java.net.URI) HashSet(java.util.HashSet)

Example 5 with CachingLinkedDataSource

use of won.protocol.util.linkeddata.CachingLinkedDataSource in project webofneeds by researchstudio-sat.

the class LinkedDataCacheUpdater method process.

@Override
public WonMessage process(WonMessage message) throws WonMessageProcessingException {
    if (this.linkedDataSourceOnBehalfOfNeed != null && this.linkedDataSourceOnBehalfOfNeed instanceof CachingLinkedDataSource) {
        logger.debug("putting message {} into cache", message.getMessageURI());
        URI requester = message.getReceiverNeedURI();
        ((CachingLinkedDataSource) linkedDataSourceOnBehalfOfNeed).addToCache(message.getCompleteDataset(), message.getMessageURI(), requester);
    }
    return message;
}
Also used : CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) URI(java.net.URI)

Aggregations

CachingLinkedDataSource (won.protocol.util.linkeddata.CachingLinkedDataSource)5 URI (java.net.URI)4 LinkedDataSource (won.protocol.util.linkeddata.LinkedDataSource)3 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 Dataset (org.apache.jena.query.Dataset)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 ResourceImpl (org.apache.jena.rdf.model.impl.ResourceImpl)1 PrefixMapping (org.apache.jena.shared.PrefixMapping)1 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)1 Path (org.apache.jena.sparql.path.Path)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 CrawlAction (won.bot.framework.eventbot.action.impl.crawl.CrawlAction)1