Search in sources :

Example 11 with PrefixMap

use of org.apache.jena.riot.system.PrefixMap in project jena by apache.

the class TestDatasetPrefixes method dsg_prefixes_basic_2.

@Test
public void dsg_prefixes_basic_2() {
    DatasetGraph dsg = create();
    Txn.executeWrite(dsg, () -> {
        PrefixMap pmap = dsg.prefixes();
        pmap.add("ex", "http://example/");
        String x = pmap.get("ex");
        assertEquals("http://example/", x);
        assertEquals(1, pmap.size());
        assertFalse(pmap.isEmpty());
    });
}
Also used : PrefixMap(org.apache.jena.riot.system.PrefixMap) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 12 with PrefixMap

use of org.apache.jena.riot.system.PrefixMap in project jena by apache.

the class TestDatasetPrefixes method dsg_prefixes_txn_1.

@Test
public void dsg_prefixes_txn_1() {
    DatasetGraph dsg = create();
    Txn.executeWrite(dsg, () -> {
        PrefixMap pmap = dsg.prefixes();
        pmap.add("ex", "http://example/");
    });
    Txn.executeRead(dsg, () -> {
        PrefixMap pmap = dsg.prefixes();
        String x = pmap.get("ex");
        assertEquals("http://example/", x);
    });
}
Also used : PrefixMap(org.apache.jena.riot.system.PrefixMap) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 13 with PrefixMap

use of org.apache.jena.riot.system.PrefixMap in project jena by apache.

the class TestDatasetPrefixes method dsg_prefixes_basic_1.

@Test
public void dsg_prefixes_basic_1() {
    DatasetGraph dsg = create();
    PrefixMap pmap = dsg.prefixes();
    Txn.executeRead(dsg, () -> {
        assertEquals(0, pmap.size());
        assertTrue(pmap.isEmpty());
    });
}
Also used : PrefixMap(org.apache.jena.riot.system.PrefixMap) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 14 with PrefixMap

use of org.apache.jena.riot.system.PrefixMap in project jena by apache.

the class TestDatasetPrefixes method dsg_prefixes_txn_4.

@Test
public void dsg_prefixes_txn_4() {
    Assume.assumeTrue(txnIsolation);
    DatasetGraph dsg = create();
    Txn.executeWrite(dsg, () -> {
        PrefixMap pmap = dsg.prefixes();
        pmap.add("ex", "http://example/2");
        dsg.abort();
    });
    Txn.executeRead(dsg, () -> {
        PrefixMap pmap = dsg.prefixes();
        String x = pmap.get("ex");
        assertNull(x);
    });
}
Also used : PrefixMap(org.apache.jena.riot.system.PrefixMap) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 15 with PrefixMap

use of org.apache.jena.riot.system.PrefixMap 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

PrefixMap (org.apache.jena.riot.system.PrefixMap)33 Test (org.junit.Test)16 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)10 Map (java.util.Map)3 Node (org.apache.jena.graph.Node)2 QueryCancelledException (org.apache.jena.query.QueryCancelledException)2 QueryExecException (org.apache.jena.query.QueryExecException)2 NodeFormatter (org.apache.jena.riot.out.NodeFormatter)2 NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)2 JsonLdApi (com.github.jsonldjava.core.JsonLdApi)1 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 RDFDataset (com.github.jsonldjava.core.RDFDataset)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 Iter (org.apache.jena.atlas.iterator.Iter)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1 JsonString (org.apache.jena.atlas.json.JsonString)1 StoragePrefixes (org.apache.jena.dboe.storage.StoragePrefixes)1