Search in sources :

Example 96 with Model

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;
}
Also used : Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader)

Example 97 with Model

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();
}
Also used : Model(org.apache.jena.rdf.model.Model) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 98 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class ExRIOT_1 method main.

public static void main(String... argv) {
    Model m = ModelFactory.createDefaultModel();
    // read into the model.
    m.read("data.ttl");
    // Alternatively, use the RDFDataMgr, which reads from the web,
    // with content negotiation.  Plain names are assumed to be 
    // local files where file extension indicates the syntax.  
    Model m2 = RDFDataMgr.loadModel("data.ttl");
    // read in more data, the remote server serves up the data
    // with the right MIME type.
    RDFDataMgr.read(m2, "http://host/some-published-data");
    // Read some data but also give a hint for the synatx if it is not
    // discovered by inspectying the file or by HTTP content negotiation.  
    RDFDataMgr.read(m2, "some-more-data.out", RDFLanguages.TURTLE);
}
Also used : Model(org.apache.jena.rdf.model.Model)

Example 99 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class ExRIOT_out1 method main.

public static void main(String[] args) {
    Model model = RDFDataMgr.loadModel("D.ttl");
    System.out.println();
    System.out.println("#### ---- Write as Turtle");
    System.out.println();
    RDFDataMgr.write(System.out, model, Lang.TURTLE);
    System.out.println();
    System.out.println("#### ---- Write as Turtle (streaming)");
    System.out.println();
    RDFDataMgr.write(System.out, model, RDFFormat.TURTLE_BLOCKS);
    System.out.println();
    System.out.println("#### ---- Write as Turtle via model.write");
    System.out.println();
    model.write(System.out, "TTL");
}
Also used : Model(org.apache.jena.rdf.model.Model)

Example 100 with Model

use of org.apache.jena.rdf.model.Model in project jena by apache.

the class ExRIOT_out2 method main.

public static void main(String[] args) {
    Model model = RDFDataMgr.loadModel("D.ttl");
    System.out.println();
    System.out.println("#### ---- Write as TriG");
    System.out.println();
    // This wil be the default graph of the dataset written.
    RDFDataMgr.write(System.out, model, Lang.TRIG);
    // Loading Turtle as Trig reads into the default graph.
    Dataset dataset = RDFDataMgr.loadDataset("D.ttl");
    System.out.println();
    System.out.println("#### ---- Write as NQuads");
    System.out.println();
    RDFDataMgr.write(System.out, dataset, Lang.NQUADS);
}
Also used : Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model)

Aggregations

Model (org.apache.jena.rdf.model.Model)443 Test (org.junit.Test)177 BaseTest (org.apache.jena.atlas.junit.BaseTest)96 Resource (org.apache.jena.rdf.model.Resource)86 StringReader (java.io.StringReader)43 Dataset (org.apache.jena.query.Dataset)31 RDFReader (org.apache.jena.rdf.model.RDFReader)30 Property (org.apache.jena.rdf.model.Property)23 DatasetAccessor (org.apache.jena.query.DatasetAccessor)20 InfModel (org.apache.jena.rdf.model.InfModel)20 Node (org.apache.jena.graph.Node)19 Statement (org.apache.jena.rdf.model.Statement)17 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)15 Reader (java.io.Reader)14 JsonString (org.apache.jena.atlas.json.JsonString)14 AbstractFusekiTest (org.apache.jena.fuseki.AbstractFusekiTest)14 FusekiTest (org.apache.jena.fuseki.FusekiTest)14 Graph (org.apache.jena.graph.Graph)14 JenaException (org.apache.jena.shared.JenaException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12