use of org.apache.jena.rdf.model.Model 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);
}
use of org.apache.jena.rdf.model.Model 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
}
use of org.apache.jena.rdf.model.Model 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);
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExJsonLD method parse.
/** Parse a jsonld string into a Model */
private Model parse(String jsonld) {
Model m = ModelFactory.createDefaultModel();
StringReader reader = new StringReader(jsonld);
m.read(reader, null, "JSON-LD");
return m;
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExJsonLD method moreControl.
/**
* To get more control about the output,
* we have to use a mechanism provided by jena to pass information
* to the writing process
*
* This requires a few lines of code, see {@link #write(DatasetGraph, RDFFormat, Context)}
*
* Here we use this write method to see what can be customized.
*/
public void moreControl() {
Model m = aSimpleModel();
m.setNsPrefix("ex", "http://www.ex.com/");
m.setNsPrefix("sh", "http://schema.org/");
// the write method takes a DatasetGraph as input to represent the data that we want to output
// Let's create one from our model:
DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
// and let's use the write method to output the data in json-ld compact format,
// passing a null Context for the moment
// (remember, "Context" here is not to be confused with "@context" in JSON-LD,
// see {@link #write(DatasetGraph, RDFFormat, Context)})
System.out.println("\n--- COMPACT with a null Context: same result as default ---");
write(g, RDFFormat.JSONLD_COMPACT_PRETTY, null);
// A Context is just a way to pass implementation-specific parameters as named values
// to a given general interface (the WriterDatasetRIOT, in this case).
// In order to make it easier to define the named values relevant here,
// there is a subclass of Context that defines setters for the values:
// the JsonLDWriteContext class
// so, the way to proceed is:
// JsonLDWriteContext ctx = new JsonLDWriteContext();
// ctx.setSomething(...)
// write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
// let's see now what can be customized with the JsonLDWriteContext object
controllingAtContext();
settingAtContextToURI();
frame();
controllingJsonLDApiOptions();
}
Aggregations