Search in sources :

Example 1 with NeedModelWrapper

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

the class TurtleFileNeedProducer method readNeedFromFile.

@Override
public synchronized Dataset readNeedFromFile(final File file) throws IOException {
    logger.debug("processing as turtle file: {} ", file);
    try (FileInputStream fis = new FileInputStream(file)) {
        Model model = ModelFactory.createDefaultModel();
        RDFDataMgr.read(model, fis, RDFFormat.TURTLE.getLang());
        // Use needmodelwrapper to ensure that the sysinfo graph is added already
        NeedModelWrapper needModelWrapper = new NeedModelWrapper(model, null);
        return needModelWrapper.copyDataset();
    } catch (Exception e) {
        logger.error("could not parse turtle from file {} ", file, e);
        throw e;
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) NeedModelWrapper(won.protocol.util.NeedModelWrapper) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 2 with NeedModelWrapper

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

the class ExecuteCreateNeedCommandAction method createWonMessage.

private WonMessage createWonMessage(WonNodeInformationService wonNodeInformationService, URI needURI, URI wonNodeURI, Dataset needDataset, final boolean usedForTesting, final boolean doNotMatch) throws WonMessageBuilderException {
    RdfUtils.replaceBaseURI(needDataset, needURI.toString());
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(needDataset);
    if (doNotMatch) {
        needModelWrapper.addFlag(WON.NO_HINT_FOR_ME);
        needModelWrapper.addFlag(WON.NO_HINT_FOR_COUNTERPART);
    }
    if (usedForTesting) {
        needModelWrapper.addFlag(WON.USED_FOR_TESTING);
    }
    return WonMessageBuilder.setMessagePropertiesForCreate(wonNodeInformationService.generateEventURI(wonNodeURI), needURI, wonNodeURI).addContent(needModelWrapper.copyDataset()).build();
}
Also used : NeedModelWrapper(won.protocol.util.NeedModelWrapper)

Example 3 with NeedModelWrapper

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

the class WorkerCrawlerActor method crawlUri.

private void crawlUri(CrawlUriMessage uriMsg) {
    Dataset ds = null;
    List<String> etags = null;
    Lock lock = null;
    try {
        // check if resource is already downloaded
        if (uriMsg instanceof ResourceCrawlUriMessage) {
            ResourceCrawlUriMessage resMsg = ((ResourceCrawlUriMessage) uriMsg);
            if (resMsg.getSerializedResource() != null && resMsg.getSerializationFormat() != null) {
                // TODO: this should be optimized, why deserialize the resource here when we just want to save it in the RDF
                // store? How to insert this serialized resource into the SPARQL endpoint?
                ds = SparqlService.deserializeDataset(resMsg.getSerializedResource(), resMsg.getSerializationFormat());
            }
        }
        // download resource if not already downloaded
        if (ds == null) {
            // use ETag/If-None-Match Headers to make the process more efficient
            HttpHeaders httpHeaders = new HttpHeaders();
            if (uriMsg.getResourceETagHeaderValues() != null && !uriMsg.getResourceETagHeaderValues().isEmpty()) {
                String ifNoneMatchHeaderValue = StringUtils.collectionToDelimitedString(uriMsg.getResourceETagHeaderValues(), ", ");
                httpHeaders.add("If-None-Match", ifNoneMatchHeaderValue);
            }
            DatasetResponseWithStatusCodeAndHeaders datasetWithHeaders = linkedDataSource.getDatasetWithHeadersForResource(URI.create(uriMsg.getUri()), httpHeaders);
            ds = datasetWithHeaders.getDataset();
            etags = datasetWithHeaders.getResponseHeaders().get("ETag");
            // if dataset was not modified (304) we can treat the current crawl uri as done
            if (ds == null && datasetWithHeaders.getStatusCode() == 304) {
                sendDoneUriMessage(uriMsg, uriMsg.getWonNodeUri(), etags);
                return;
            }
            // if there is paging activated and the won node tells us that there is more data (previous link)
            // to be downloaded, then we add this link to the crawling process too
            String prevLink = linkedDataSource.getPreviousLinkFromDatasetWithHeaders(datasetWithHeaders);
            if (prevLink != null) {
                CrawlUriMessage newUriMsg = new CrawlUriMessage(uriMsg.getBaseUri(), prevLink, uriMsg.getWonNodeUri(), CrawlUriMessage.STATUS.PROCESS, System.currentTimeMillis(), null);
                getSender().tell(newUriMsg, getSelf());
            }
        }
        lock = ds == null ? null : ds.getLock();
        lock.enterCriticalSection(true);
        // Save dataset to triple store
        sparqlService.updateNamedGraphsOfDataset(ds);
        String wonNodeUri = extractWonNodeUri(ds, uriMsg.getUri());
        if (wonNodeUri == null) {
            wonNodeUri = uriMsg.getWonNodeUri();
        }
        // do nothing more here if the STATUS of the message was SAVE
        if (uriMsg.getStatus().equals(CrawlUriMessage.STATUS.SAVE)) {
            log.debug("processed crawl uri event {} with status 'SAVE'", uriMsg);
            return;
        }
        // extract URIs from current resource and send extracted URI messages back to sender
        log.debug("Extract URIs from message {}", uriMsg);
        Set<CrawlUriMessage> newCrawlMessages = sparqlService.extractCrawlUriMessages(uriMsg.getBaseUri(), wonNodeUri);
        for (CrawlUriMessage newMsg : newCrawlMessages) {
            getSender().tell(newMsg, getSelf());
        }
        // signal sender that this URI is processed and save meta data about crawling the URI.
        // This needs to be done after all extracted URI messages have been sent to guarantee consistency
        // in case of failure
        sendDoneUriMessage(uriMsg, wonNodeUri, etags);
        // if this URI/dataset was a need then send an event to the distributed event bu
        if (NeedModelWrapper.isANeed(ds)) {
            NeedModelWrapper needModelWrapper = new NeedModelWrapper(ds, false);
            NeedState state = needModelWrapper.getNeedState();
            NeedEvent.TYPE type = state.equals(NeedState.ACTIVE) ? NeedEvent.TYPE.ACTIVE : NeedEvent.TYPE.INACTIVE;
            log.debug("Created need event for need uri {}", uriMsg.getUri());
            long crawlDate = System.currentTimeMillis();
            NeedEvent needEvent = new NeedEvent(uriMsg.getUri(), wonNodeUri, type, crawlDate, ds);
            pubSubMediator.tell(new DistributedPubSubMediator.Publish(needEvent.getClass().getName(), needEvent), getSelf());
        }
    } catch (RestClientException e1) {
        // usually happens if the fetch of the dataset fails e.g. HttpServerErrorException, HttpClientErrorException
        log.debug("Exception during crawling: " + e1);
        throw new CrawlWrapperException(e1, uriMsg);
    } catch (Exception e) {
        log.debug("Exception during crawling: " + e);
        throw new CrawlWrapperException(e, uriMsg);
    } finally {
        if (lock != null) {
            lock.leaveCriticalSection();
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CrawlUriMessage(won.matcher.service.crawler.msg.CrawlUriMessage) ResourceCrawlUriMessage(won.matcher.service.crawler.msg.ResourceCrawlUriMessage) DatasetResponseWithStatusCodeAndHeaders(won.protocol.rest.DatasetResponseWithStatusCodeAndHeaders) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) Dataset(org.apache.jena.query.Dataset) NeedState(won.protocol.model.NeedState) NeedModelWrapper(won.protocol.util.NeedModelWrapper) NeedEvent(won.matcher.service.common.event.NeedEvent) CrawlWrapperException(won.matcher.service.crawler.exception.CrawlWrapperException) CrawlWrapperException(won.matcher.service.crawler.exception.CrawlWrapperException) IncorrectPropertyCountException(won.protocol.exception.IncorrectPropertyCountException) RestClientException(org.springframework.web.client.RestClientException) Lock(org.apache.jena.shared.Lock) ResourceCrawlUriMessage(won.matcher.service.crawler.msg.ResourceCrawlUriMessage) RestClientException(org.springframework.web.client.RestClientException)

Example 4 with NeedModelWrapper

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

the class GoalInstantiationProducer method findInstantiationForGoal.

/**
 * Create a goal instantiation result from the attempt to instantiate one goal with data of two needs using all
 * the need data, the conversation data and the shapes data of the goal. The data is extracted and validated
 * against the shacl shape of the goal.
 *
 * @param goal resource referencing goal from need1 or need2
 * @return a goal instantiation result whose input model can either conform to its shacl shapes or not
 */
public GoalInstantiationResult findInstantiationForGoal(Resource goal) {
    NeedModelWrapper needWrapper1 = new NeedModelWrapper(need1);
    NeedModelWrapper needWrapper2 = new NeedModelWrapper(need2);
    Model goalShapesModel = null;
    Model goalDataModel = null;
    if (needWrapper1.getGoals().contains(goal) && !needWrapper2.getGoals().contains(goal)) {
        goalShapesModel = needWrapper1.getShapesGraph(goal);
        goalDataModel = needWrapper1.getDataGraph(goal);
    } else if (needWrapper2.getGoals().contains(goal) && !needWrapper1.getGoals().contains(goal)) {
        goalShapesModel = needWrapper2.getShapesGraph(goal);
        goalDataModel = needWrapper2.getDataGraph(goal);
    } else {
        throw new IllegalArgumentException("problem to identify goal resource in one of the two need models");
    }
    if (goalShapesModel == null) {
        throw new IllegalArgumentException("shapes model for goal not found");
    }
    Model combinedModelWithGoalData = ModelFactory.createDefaultModel();
    combinedModelWithGoalData.add(combinedModelWithoutGoals);
    if (goalDataModel != null) {
        combinedModelWithGoalData.add(goalDataModel);
    }
    Model extractedModel = GoalUtils.extractGoalData(combinedModelWithGoalData, goalShapesModel);
    return new GoalInstantiationResult(extractedModel, goalShapesModel);
}
Also used : Model(org.apache.jena.rdf.model.Model) NeedModelWrapper(won.protocol.util.NeedModelWrapper)

Example 5 with NeedModelWrapper

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

the class GoalInstantiationProducer method findInstantiationForGoalInDataset.

/**
 * Create a goal instantiation result from the attempt to instantiate one goal with data given in the dataset
 * The data is extracted and validated against the shacl shape of the goal.
 *
 * @param need Dataset of the need to retrieve the goalShapesModel from
 * @param goal resource referencing goal from need1 or need2
 * @param model Model that should be checked for goal validity
 * @return a goal instantiation result whose input model can either conform to its shacl shapes or not
 */
public static GoalInstantiationResult findInstantiationForGoalInDataset(Dataset need, Resource goal, Model model) {
    NeedModelWrapper needWrapper = new NeedModelWrapper(need);
    Model goalShapesModel;
    if (needWrapper.getGoals().contains(goal)) {
        goalShapesModel = needWrapper.getShapesGraph(goal);
    } else {
        throw new IllegalArgumentException("problem to identify goal resource in the need model");
    }
    if (goalShapesModel == null) {
        throw new IllegalArgumentException("shapes model for goal not found");
    }
    Model extractedModel = GoalUtils.extractGoalData(model, goalShapesModel);
    return new GoalInstantiationResult(extractedModel, goalShapesModel);
}
Also used : Model(org.apache.jena.rdf.model.Model) NeedModelWrapper(won.protocol.util.NeedModelWrapper)

Aggregations

NeedModelWrapper (won.protocol.util.NeedModelWrapper)27 Dataset (org.apache.jena.query.Dataset)14 Resource (org.apache.jena.rdf.model.Resource)12 Model (org.apache.jena.rdf.model.Model)9 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)6 URI (java.net.URI)5 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 GoalInstantiationProducer (won.utils.goals.GoalInstantiationProducer)4 GoalInstantiationResult (won.utils.goals.GoalInstantiationResult)4 WonMessage (won.protocol.message.WonMessage)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 Event (won.bot.framework.eventbot.event.Event)2 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)2 EventListener (won.bot.framework.eventbot.listener.EventListener)2 Coordinate (won.protocol.model.Coordinate)2 WonNodeInformationService (won.protocol.service.WonNodeInformationService)2 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 StringWriter (java.io.StringWriter)1