Search in sources :

Example 1 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class ExJsonLD method frame.

/**
     * Shows how to apply a frame to the output RDF data
     */
void frame() {
    // a "frame" is a specific graph layout that is applied to output data
    // It can be used to filter the output data.
    // In this example, we show how to output only the resources of a givn rdf:type
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource person = m.createResource(ns + "Person");
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
    m.add(s, RDF.type, person);
    s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Gado Salamatou");
    m.add(s, m.createProperty(ns + "url"), "http://www.salamatou.com");
    m.add(s, RDF.type, person);
    s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Not a person");
    m.add(s, RDF.type, m.createResource(ns + "Event"));
    DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    // only output the persons using a frame
    String frame = "{\"@type\" : \"http://schema.org/Person\"}";
    ctx.setFrame(frame);
    System.out.println("\n--- Using frame to select resources to be output: only output persons ---");
    write(g, RDFFormat.JSONLD_FRAME_PRETTY, ctx);
}
Also used : JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 2 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class ExJsonLD method settingAtContextToURI.

/**
     * Shows how to set "@context" to a URI.
     */
void settingAtContextToURI() {
    // One thing you'll probably want to do is to set the "@context" to the URL of a file
    // containing the actual JSON-LD context.
    // Let's take one Model that only uses schema.org terms,
    // and let's try to set the "@Context" to the URL of schema.org 
    // "@context" : "http://schema.org/"
    Model m = aModelThatOnlyUsesSchemaDotOrg();
    DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    // The following should work, but unfortunately it doesn't (with JSONLD-java 0.8.3):
    ctx.setJsonLDContext("\"http://schema.org/\"");
    System.out.println("\n--- Setting the context to a URI, WRONG WAY: it's slow, and the output is not JSON-LD. Sorry about that. ---");
    write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
    // But don't worry (be happy): 
    // - there is a solution
    // - and what we tried is not what we would want to do, anyway.
    // The output process needs to have the content of the "@context" at hand
    // in order to compute the output. So, if passing the URL of the vocab,
    // the output process must download the vocab before anything.
    // (that's why the previous attempt was slow)
    // -> that would not be an very efficient way to output your data.
    // -> it doesn't work, (with JSONLD-java 0.8.3), but no regret.
    // To achieve the expected result,
    // you have to do 2 things:
    // 1)
    // you have to pass the dereferenced content of http://schema.org/
    // - or the relevant subset of it (as we only use very few terms).
    // Here it is:
    String atContextAsJson = "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
    ctx.setJsonLDContext(atContextAsJson);
    // Alternatively, we could just pass "null":
    // ctx.setJsonLDContext(null);
    // and let jena compute the context (as the model only uses schema.org vocab)
    // 2)
    // and then you pass the schema.org url using:
    ctx.setJsonLDContextSubstitution("\"http://schema.org/\"");
    // To summarize:
    // - ctx.setJsonLDContext allows to define the @context used to compute the output
    // - ctx.setJsonLDContextSubstitution allows to change the value of the "@context" in the output
    System.out.println("\n--- COMPACT with @context replaced by schema.org URI ---");
    write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
// Final note: BEWARE when replacing the context:
// if you let some things undefined, the output will be json, not jsonld
}
Also used : JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 3 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class ExJsonLD method controllingJsonLDApiOptions.

/**
     *  the JSON-LD java API (that jena uses for JSON-LD I/O) defines a set of options
     *  that can be customized
     */
void controllingJsonLDApiOptions() {
    Model m = aSimpleModel();
    m.setNsPrefix("ex", "http://www.ex.com/");
    m.setNsPrefix("sh", "http://schema.org/");
    DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    JsonLdOptions opts = new JsonLdOptions();
    ctx.setOptions(opts);
    // default is true
    opts.setCompactArrays(false);
    System.out.println("\n--- COMPACT with CompactArrays false: there is an @graph node");
    write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 4 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class TestJsonLDWriter method testContextByUri.

/**
 * Checks that one can pass a context defined by its URI
 */
@Test
public final void testContextByUri() {
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, RDF.type, "Person");
    // we can pass an uri in the context, as a quoted string (it is a JSON string)
    JsonLDWriteContext jenaContext = new JsonLDWriteContext();
    jenaContext.setJsonLDContext("{\"@context\" : \"http://schema.org/\"}");
    String jsonld = toString(m, RDFFormat.JSONLD, jenaContext);
    // check it parses ok
    Model m2 = parse(jsonld);
    assertTrue(m2.isIsomorphicWith(m));
}
Also used : JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) JsonString(org.apache.jena.atlas.json.JsonString) Test(org.junit.Test)

Example 5 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class Ex_WriteJsonLD method frame.

/**
 * Shows how to apply a frame to the output RDF data
 */
void frame() {
    // a "frame" is a specific graph layout that is applied to output data
    // It can be used to filter the output data.
    // In this example, we show how to output only the resources of a givn rdf:type
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource person = m.createResource(ns + "Person");
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
    m.add(s, RDF.type, person);
    s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Gado Salamatou");
    m.add(s, m.createProperty(ns + "url"), "http://www.salamatou.com");
    m.add(s, RDF.type, person);
    s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Not a person");
    m.add(s, RDF.type, m.createResource(ns + "Event"));
    DatasetGraph g = DatasetFactory.wrap(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    // only output the persons using a frame
    String frame = "{\"@type\" : \"http://schema.org/Person\"}";
    ctx.setFrame(frame);
    System.out.println("\n--- Using frame to select resources to be output: only output persons ---");
    write(g, RDFFormat.JSONLD_FRAME_PRETTY, ctx);
}
Also used : JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)12 Model (org.apache.jena.rdf.model.Model)8 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)8 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)4 JsonString (org.apache.jena.atlas.json.JsonString)4 Test (org.junit.Test)4 Resource (org.apache.jena.rdf.model.Resource)2 DocumentLoader (com.github.jsonldjava.core.DocumentLoader)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1