Search in sources :

Example 1 with RDFDataset

use of com.github.jsonldjava.core.RDFDataset 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 RDFDataset

use of com.github.jsonldjava.core.RDFDataset in project stanbol by apache.

the class ClerezzaRDFParser method parse.

@Override
public RDFDataset parse(Object input) throws JsonLdError {
    count = 0;
    Map<BlankNode, String> bNodeMap = new HashMap<BlankNode, String>(1024);
    final RDFDataset result = new RDFDataset();
    if (input instanceof Graph) {
        for (Triple t : ((Graph) input)) {
            handleStatement(result, t, bNodeMap);
        }
    }
    // help gc
    bNodeMap.clear();
    return result;
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) HashMap(java.util.HashMap) BlankNode(org.apache.clerezza.commons.rdf.BlankNode)

Example 3 with RDFDataset

use of com.github.jsonldjava.core.RDFDataset in project jena by apache.

the class JsonLDReader method read$.

private void read$(Object jsonObject, JsonLdOptions options, final StreamRDF output) {
    output.start();
    try {
        JsonLdTripleCallback callback = new JsonLdTripleCallback() {

            @Override
            public Object call(RDFDataset dataset) {
                // Copy across namespaces
                for (Entry<String, String> namespace : dataset.getNamespaces().entrySet()) {
                    output.prefix(namespace.getKey(), namespace.getValue());
                }
                // Copy triples and quads
                for (String gn : dataset.keySet()) {
                    Object x = dataset.get(gn);
                    if ("@default".equals(gn)) {
                        @SuppressWarnings("unchecked") List<Map<String, Object>> triples = (List<Map<String, Object>>) x;
                        for (Map<String, Object> t : triples) {
                            Node s = createNode(t, "subject");
                            Node p = createNode(t, "predicate");
                            Node o = createNode(t, "object");
                            Triple triple = profile.createTriple(s, p, o, -1, -1);
                            output.triple(triple);
                        }
                    } else {
                        @SuppressWarnings("unchecked") List<Map<String, Object>> quads = (List<Map<String, Object>>) x;
                        Node g = createURI(gn);
                        for (Map<String, Object> q : quads) {
                            Node s = createNode(q, "subject");
                            Node p = createNode(q, "predicate");
                            Node o = createNode(q, "object");
                            Quad quad = profile.createQuad(g, s, p, o, -1, -1);
                            output.quad(quad);
                        }
                    }
                }
                return null;
            }
        };
        JsonLdProcessor.toRDF(jsonObject, callback, options);
    } catch (JsonLdError e) {
        errorHandler.error(e.getMessage(), -1, -1);
        throw new RiotException(e);
    }
    output.finish();
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Quad(org.apache.jena.sparql.core.Quad) Node(org.apache.jena.graph.Node) JsonLdTripleCallback(com.github.jsonldjava.core.JsonLdTripleCallback) JsonLdError(com.github.jsonldjava.core.JsonLdError) Triple(org.apache.jena.graph.Triple) List(java.util.List) Map(java.util.Map)

Example 4 with RDFDataset

use of com.github.jsonldjava.core.RDFDataset in project jena by apache.

the class JsonLDReader method read$.

private void read$(Object jsonObject, String baseURI, ContentType ct, final StreamRDF output, Context context) {
    output.start();
    try {
        JsonLdTripleCallback callback = new JsonLdTripleCallback() {

            @Override
            public Object call(RDFDataset dataset) {
                // Copy across namespaces
                for (Entry<String, String> namespace : dataset.getNamespaces().entrySet()) {
                    output.prefix(namespace.getKey(), namespace.getValue());
                }
                // Copy triples and quads
                for (String gn : dataset.keySet()) {
                    Object x = dataset.get(gn);
                    if ("@default".equals(gn)) {
                        @SuppressWarnings("unchecked") List<Map<String, Object>> triples = (List<Map<String, Object>>) x;
                        for (Map<String, Object> t : triples) {
                            Node s = createNode(t, "subject");
                            Node p = createNode(t, "predicate");
                            Node o = createNode(t, "object");
                            Triple triple = profile.createTriple(s, p, o, -1, -1);
                            output.triple(triple);
                        }
                    } else {
                        @SuppressWarnings("unchecked") List<Map<String, Object>> quads = (List<Map<String, Object>>) x;
                        Node g = createURI(gn);
                        for (Map<String, Object> q : quads) {
                            Node s = createNode(q, "subject");
                            Node p = createNode(q, "predicate");
                            Node o = createNode(q, "object");
                            Quad quad = profile.createQuad(g, s, p, o, -1, -1);
                            output.quad(quad);
                        }
                    }
                }
                return null;
            }
        };
        JsonLdOptions options = new JsonLdOptions(baseURI);
        options.useNamespaces = true;
        JsonLdProcessor.toRDF(jsonObject, callback, options);
    } catch (JsonLdError e) {
        errorHandler.error(e.getMessage(), -1, -1);
        throw new RiotException(e);
    }
    output.finish();
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Quad(org.apache.jena.sparql.core.Quad) Node(org.apache.jena.graph.Node) JsonLdTripleCallback(com.github.jsonldjava.core.JsonLdTripleCallback) JsonLdError(com.github.jsonldjava.core.JsonLdError) Triple(org.apache.jena.graph.Triple) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) List(java.util.List) Map(java.util.Map)

Example 5 with RDFDataset

use of com.github.jsonldjava.core.RDFDataset in project jena by apache.

the class JsonLDWriter method toJsonLDJavaAPI.

/**
     * the JsonLD-java API object corresponding to a dataset and a JsonLD format.
     */
public static Object toJsonLDJavaAPI(RDFFormat.JSONLDVariant variant, DatasetGraph dataset, PrefixMap prefixMap, String baseURI, Context jenaContext) throws JsonLdError, JsonParseException, IOException {
    JsonLdOptions opts = getJsonLdOptions(baseURI, jenaContext);
    // we can benefit from the fact we know that there are no duplicates in the jsonld RDFDataset that we create
    // (optimization in jsonld-java 0.8.3)
    // see https://github.com/jsonld-java/jsonld-java/pull/173
    // with this, we cannot call the json-ld fromRDF method that assumes no duplicates in RDFDataset
    // Object obj = JsonLdProcessor.fromRDF(dataset, opts, new JenaRDF2JSONLD()) ;
    final RDFDataset jsonldDataset = (new JenaRDF2JSONLD()).parse(dataset);
    // JsonLdApi.fromRDF(RDFDataset, boolean) is "experimental" rather than "deprecated"
    @SuppressWarnings("deprecation") Object // true because we know that we don't have any duplicate in jsonldDataset
    obj = (new JsonLdApi(opts)).fromRDF(jsonldDataset, true);
    if (variant.isExpand()) {
    // nothing more to do
    } else if (variant.isFrame()) {
        Object frame = null;
        if (jenaContext != null) {
            frame = jenaContext.get(JSONLD_FRAME);
        }
        if (frame == null) {
            throw new IllegalArgumentException("No frame object found in jena Context");
        }
        if (frame instanceof String) {
            frame = JsonUtils.fromString((String) frame);
        }
        obj = JsonLdProcessor.frame(obj, frame, opts);
    } else {
        // compact or flatten
        // we need a (jsonld) context. Get it from jenaContext, or create one:
        Object ctx = getJsonldContext(dataset, prefixMap, jenaContext);
        if (variant.isCompact()) {
            obj = JsonLdProcessor.compact(obj, ctx, opts);
        } else if (variant.isFlatten()) {
            obj = JsonLdProcessor.flatten(obj, ctx, opts);
        } else {
            throw new IllegalArgumentException("Unexpected " + RDFFormat.JSONLDVariant.class.getName() + ": " + variant);
        }
        // replace @context in output?
        if (jenaContext != null) {
            Object ctxReplacement = jenaContext.get(JSONLD_CONTEXT_SUBSTITUTION);
            if (ctxReplacement != null) {
                if (obj instanceof Map) {
                    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) obj;
                    if (map.containsKey("@context")) {
                        map.put("@context", JsonUtils.fromString(ctxReplacement.toString()));
                    }
                }
            }
        }
    }
    return obj;
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) Util.isLangString(org.apache.jena.rdf.model.impl.Util.isLangString) Util.isSimpleString(org.apache.jena.rdf.model.impl.Util.isSimpleString) LinkedHashMap(java.util.LinkedHashMap) PrefixMap(org.apache.jena.riot.system.PrefixMap) Map(java.util.Map) JsonLdApi(com.github.jsonldjava.core.JsonLdApi) RDFFormat(org.apache.jena.riot.RDFFormat)

Aggregations

RDFDataset (com.github.jsonldjava.core.RDFDataset)6 JsonLdError (com.github.jsonldjava.core.JsonLdError)3 Map (java.util.Map)3 Node (org.apache.jena.graph.Node)3 Quad (org.apache.jena.sparql.core.Quad)3 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)2 JsonLdTripleCallback (com.github.jsonldjava.core.JsonLdTripleCallback)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Triple (org.apache.jena.graph.Triple)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonLdApi (com.github.jsonldjava.core.JsonLdApi)1 LinkedHashMap (java.util.LinkedHashMap)1 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)1 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)1 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 Util.isLangString (org.apache.jena.rdf.model.impl.Util.isLangString)1