Search in sources :

Example 6 with ModelBuilder

use of org.eclipse.rdf4j.model.util.ModelBuilder in project opentheso by miledrousset.

the class WriteRdf4j method loadModel.

private void loadModel() {
    builder = new ModelBuilder();
    builder.setNamespace("skos", "http://www.w3.org/2004/02/skos/core#");
    builder.setNamespace("dc", "http://purl.org/dc/elements/1.1/");
    builder.setNamespace("dcterms", "http://purl.org/dc/terms/");
    builder.setNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
    builder.setNamespace("iso-thes", "http://purl.org/iso25964/skos-thes#");
    builder.setNamespace("opentheso", "http://purl.org/umu/uneskos#");
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder)

Example 7 with ModelBuilder

use of org.eclipse.rdf4j.model.util.ModelBuilder in project opentheso by miledrousset.

the class json_ld_first_test method start_test.

@Test
public void start_test() throws FileNotFoundException, IOException, JsonLdError {
    System.out.println("");
    System.out.println("");
    System.out.println("*******************************************************");
    System.out.println("*                     JSON LD                         *");
    System.out.println("*******************************************************");
    InputStream inputStream = new FileInputStream("input_test.json");
    // Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
    // Number or null depending on the root object in the file).
    Object jsonObject = JsonUtils.fromInputStream(inputStream);
    // Create a context JSON map containing prefixes and definitions
    Map context = new HashMap();
    // Customise context...
    // Create an instance of JsonLdOptions with the standard JSON-LD options
    JsonLdOptions options = new JsonLdOptions();
    // Customise options...
    // Call whichever JSONLD function you want! (e.g. compact)
    Object compact = JsonLdProcessor.compact(jsonObject, context, options);
    // Print out the result (or don't, it's your call!)
    System.out.println(JsonUtils.toPrettyString(compact));
    System.out.println("*******************************************************");
    System.out.println("*                     org.JSON                        *");
    System.out.println("*******************************************************");
    inputStream = new FileInputStream("input_test.json");
    JSONObject obj = new JSONObject(IOUtils.toString(inputStream));
    System.out.println(obj.getString("name"));
    obj.put("test", "writeIsWorking");
    System.out.println(obj.getString("test"));
    System.out.println("");
    System.out.println(obj.toString());
    System.out.println("");
    System.out.println("*******************************************************");
    System.out.println("*                     RDJF WRITE                      *");
    System.out.println("*******************************************************");
    ValueFactory vf = SimpleValueFactory.getInstance();
    // Create a new RDF model containing information about the painting "The Potato Eaters"
    ModelBuilder builder = new ModelBuilder();
    Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add("ex:creationDate", vf.createLiteral("1885-04-01T00:00:00Z", XMLSchema.DATETIME)).add("ex:peopleDepicted", 5).build();
    Rio.write(model, System.out, RDFFormat.JSONLD);
    System.out.println("");
    System.out.println("");
    System.out.println("*******************************************************");
    System.out.println("*                     RDJF READ                       *");
    System.out.println("*******************************************************");
    inputStream = new FileInputStream("input_test.json");
    model = Rio.parse(inputStream, "", RDFFormat.JSONLD);
    Rio.write(model, System.out, RDFFormat.JSONLD);
    System.out.println("");
    for (Statement st : model) {
        // we want to see the object values of each statement
        Value value = st.getObject();
        IRI property = st.getPredicate();
        if (property.getLocalName().equals("name")) {
            Literal title = (Literal) value;
            System.out.println("name: " + title.getLabel());
        }
    }
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) FileInputStream(java.io.FileInputStream) ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JSONObject(org.primefaces.json.JSONObject) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) JSONObject(org.primefaces.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with ModelBuilder

use of org.eclipse.rdf4j.model.util.ModelBuilder in project opentheso by miledrousset.

the class WriteRdfFileTest method write4.

@Test
public void write4() {
    // We'll use a ModelBuilder to create two named graphs, one containing data about
    // picasso, the other about Van Gogh.
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("ex", "http://example.org/");
    // in named graph 1, we add info about Picasso
    builder.namedGraph("ex:namedGraph1").subject("ex:Picasso").add(RDF.TYPE, "type1").add(FOAF.FIRST_NAME, "Pablo");
    // in named graph2, we add info about Van Gogh.
    builder.namedGraph("ex:namedGraph2").subject("ex:VanGogh").add(RDF.TYPE, "type2").add(FOAF.FIRST_NAME, "Vincent");
    // We're done building, create our Model
    Model model = builder.build();
    // each named graph is stored as a separate context in our Model
    for (Resource context : model.contexts()) {
        System.out.println("Named graph " + context + " contains: ");
        // write _only_ the statemements in the current named graph to the console, in N-Triples format
        Rio.write(model.filter(null, null, null, context), System.out, RDFFormat.NTRIPLES);
        System.out.println();
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) Model(org.eclipse.rdf4j.model.Model) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) Resource(org.eclipse.rdf4j.model.Resource) Test(org.junit.Test)

Aggregations

ModelBuilder (org.eclipse.rdf4j.model.util.ModelBuilder)8 Model (org.eclipse.rdf4j.model.Model)7 Statement (org.eclipse.rdf4j.model.Statement)5 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)5 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)5 Literal (org.eclipse.rdf4j.model.Literal)4 Value (org.eclipse.rdf4j.model.Value)4 Test (org.junit.Test)4 IRI (org.eclipse.rdf4j.model.IRI)3 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)3 Date (java.util.Date)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 BNode (org.eclipse.rdf4j.model.BNode)2 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GregorianCalendar (java.util.GregorianCalendar)1