Search in sources :

Example 1 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project timbuctoo by HuygensING.

the class JsonProvenanceToRdfPatch method sendQuads.

@Override
public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatus, DataSet dataSet) throws LogStorageFailedException {
    for (JsonNode revision : activity.get(PROV_GENERATES)) {
        final String entityUri = revision.get(PROV_SPECIALIZATION_OF).get(0).get("@id").asText();
        final String revisionUri = revision.get("@id").asText();
        String wasRevisionOf = null;
        if (revision.get(PROV_REVISION_OF) != null) {
            wasRevisionOf = revision.get(PROV_REVISION_OF).get(0).get("@id").asText();
        }
        generateRevisionInfo(saver, revisionUri, entityUri, wasRevisionOf);
        generatePatch(saver, revision.get(TIM_ADDITIONS), entityUri, true);
        for (CursorQuad quad : toReplace.getOrDefault(revisionUri, new ArrayList<>())) {
            saver.delQuad(quad.getSubject(), quad.getPredicate(), quad.getObject(), quad.getValuetype().orElse(null), quad.getLanguage().orElse(null), null);
        }
        generatePatch(saver, revision.get(TIM_REPLACEMENTS), entityUri, true);
        generatePatch(saver, revision.get(TIM_DELETIONS), entityUri, false);
    }
    try {
        final HashMap map = OBJECT_MAPPER.treeToValue(activity, HashMap.class);
        final RDFDataset dataset = (RDFDataset) JsonLdProcessor.toRDF(map);
        for (String graphName : dataset.graphNames()) {
            for (RDFDataset.Quad quad : dataset.getQuads(graphName)) {
                saver.onQuad(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad.getObject().getValue(), quad.getObject().isLiteral() ? quad.getObject().getDatatype() : null, quad.getObject().getLanguage(), quad.getGraph() == null ? null : quad.getGraph().getValue());
            }
        }
    } catch (JsonProcessingException | JsonLdError e) {
        throw new LogStorageFailedException(e);
    }
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) HashMap(java.util.HashMap) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonLdError(com.github.jsonldjava.core.JsonLdError)

Example 2 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project timbuctoo by HuygensING.

the class JsonProvenanceToRdfPatch method fromCurrentState.

public static JsonProvenanceToRdfPatch fromCurrentState(DocumentLoader documentLoader, String jsonLd, QuadStore quadStore, String editorUri, String randomness, Clock clock) throws IOException, ConcurrentUpdateException {
    JsonLdOptions opts = new JsonLdOptions();
    opts.setDocumentLoader(documentLoader);
    // used also for side-effect of checking the json-ld for wellformedness
    // turns the json object into an array of objects that have type prov:activity
    // The json is normalized:
    // - all properties are arrays
    // - all values are wrapped in an object
    // - all non-value objects have an @id property that contains a string (with either a blank node id or a URI)
    Map<String, String> frame = new HashMap<>();
    frame.put("@type", "http://www.w3.org/ns/prov#Activity");
    try {
        final JsonNode activities = OBJECT_MAPPER.valueToTree(JsonLdProcessor.expand(JsonLdProcessor.frame(JsonUtils.fromString(jsonLd), frame, opts)));
        final List<String> errors = integrityCheck(activities);
        if (errors.size() > 0) {
            throw new IOException(String.join("\n", errors));
        }
        final ObjectNode activity = (ObjectNode) activities.get(0);
        if (!skolemize(activity, randomness, "")) {
            throw new IOException("Not all nodes were blank nodes");
        }
        if (!checkTypes(activity, "")) {
            throw new IOException("Not all nodes had a type");
        }
        checkIfUpdatedInTheMeanTime(quadStore, activity);
        addEditor(editorUri, activity, randomness);
        return new JsonProvenanceToRdfPatch(activity, collectReplacements(activity, quadStore), clock.instant().toString());
    } catch (JsonLdError e) {
        throw new IOException(e);
    }
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonLdError(com.github.jsonldjava.core.JsonLdError)

Example 3 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project rdf4j by eclipse.

the class JSONLDParser method parse.

@Override
public void parse(final InputStream in, final String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        final JSONLDInternalTripleCallback callback = new JSONLDInternalTripleCallback(getRDFHandler(), valueFactory, getParserConfig(), getParseErrorListener(), nodeID -> createNode(nodeID), () -> createNode());
        final JsonLdOptions options = new JsonLdOptions(baseURI);
        options.useNamespaces = true;
        JsonLdProcessor.toRDF(JsonUtils.fromInputStream(in), callback, options);
    } catch (final JsonLdError e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final JsonParseException e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final RuntimeException e) {
        if (e.getCause() != null && e.getCause() instanceof RDFParseException) {
            throw (RDFParseException) e.getCause();
        }
        throw e;
    } finally {
        clear();
    }
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonLdError(com.github.jsonldjava.core.JsonLdError) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 4 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project rdf4j by eclipse.

the class JSONLDParser method parse.

@Override
public void parse(final Reader reader, final String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        final JSONLDInternalTripleCallback callback = new JSONLDInternalTripleCallback(getRDFHandler(), valueFactory, getParserConfig(), getParseErrorListener(), nodeID -> createNode(nodeID), () -> createNode());
        final JsonLdOptions options = new JsonLdOptions(baseURI);
        options.useNamespaces = true;
        JsonLdProcessor.toRDF(JsonUtils.fromReader(reader), callback, options);
    } catch (final JsonLdError e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final JsonParseException e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final RuntimeException e) {
        if (e.getCause() != null && e.getCause() instanceof RDFParseException) {
            throw (RDFParseException) e.getCause();
        }
        throw e;
    } finally {
        clear();
    }
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonLdError(com.github.jsonldjava.core.JsonLdError) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 5 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project rdf4j by eclipse.

the class JSONLDWriter method endRDF.

@Override
public void endRDF() throws RDFHandlerException {
    final JSONLDInternalRDFParser serialiser = new JSONLDInternalRDFParser();
    try {
        Object output = JsonLdProcessor.fromRDF(model, serialiser);
        final JSONLDMode mode = getWriterConfig().get(JSONLDSettings.JSONLD_MODE);
        final JsonLdOptions opts = new JsonLdOptions();
        // opts.addBlankNodeIDs =
        // getWriterConfig().get(BasicParserSettings.PRESERVE_BNODE_IDS);
        opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE));
        opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES));
        if (baseURI != null && getWriterConfig().get(BasicWriterSettings.BASE_DIRECTIVE)) {
            opts.setBase(baseURI);
        }
        if (mode == JSONLDMode.EXPAND) {
            output = JsonLdProcessor.expand(output, opts);
        }
        // TODO: Implement inframe in JSONLDSettings
        final Object inframe = null;
        if (mode == JSONLDMode.FLATTEN) {
            output = JsonLdProcessor.flatten(output, inframe, opts);
        }
        if (mode == JSONLDMode.COMPACT) {
            final Map<String, Object> ctx = new LinkedHashMap<String, Object>();
            addPrefixes(ctx, model.getNamespaces());
            final Map<String, Object> localCtx = new HashMap<String, Object>();
            localCtx.put("@context", ctx);
            output = JsonLdProcessor.compact(output, localCtx, opts);
        }
        if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
            JsonUtils.writePrettyPrint(writer, output);
        } else {
            JsonUtils.write(writer, output);
        }
    } catch (final JsonLdError e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonGenerationException e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonMappingException e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final IOException e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    }
}
Also used : JSONLDMode(org.eclipse.rdf4j.rio.helpers.JSONLDMode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IOException(java.io.IOException) JsonLdError(com.github.jsonldjava.core.JsonLdError) LinkedHashMap(java.util.LinkedHashMap) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Aggregations

JsonLdError (com.github.jsonldjava.core.JsonLdError)9 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)6 HashMap (java.util.HashMap)5 RDFDataset (com.github.jsonldjava.core.RDFDataset)3 IOException (java.io.IOException)3 Map (java.util.Map)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonLdTripleCallback (com.github.jsonldjava.core.JsonLdTripleCallback)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Node (org.apache.jena.graph.Node)2 Triple (org.apache.jena.graph.Triple)2 Quad (org.apache.jena.sparql.core.Quad)2 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 LinkedDataObject (com.sri.ai.praise.other.integration.linkeddata.api.LinkedDataObject)1