Search in sources :

Example 6 with Literal

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

the class ReadRdfFileTest method test4.

@Test
public void test4() {
    // read the file 'example-data-artists.ttl' as an InputStream.
    InputStream is = null;
    try {
        is = new FileInputStream("test_unesco - Copie.rdf");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    Model model = null;
    try {
        model = Rio.parse(is, "", RDFFormat.RDFXML);
    } catch (IOException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (RDFParseException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedRDFormatException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (Statement statement : model) {
        System.out.println(statement);
    }
    System.out.println("\n----------------");
    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("type"))
            System.out.println("\n......\n");
        if (property.getLocalName().equals("notation")) {
            Literal title = (Literal) value;
            System.out.println("notation: " + title.getLabel());
        } else if (property.getLocalName().equals("lat")) {
            Literal title = (Literal) value;
            System.out.println("latitude : " + title.getLabel());
        } else if (property.getLocalName().equals("long")) {
            Literal title = (Literal) value;
            System.out.println("longitude : " + title.getLabel());
        } else if (property.getLocalName().equals("created")) {
            Literal title = (Literal) value;
            System.out.println("created : " + title.getLabel());
        } else if (property.getLocalName().equals("modified")) {
            Literal title = (Literal) value;
            System.out.println("modified : " + title.getLabel());
        } else if (value instanceof Literal) {
            String pref = "";
            if (property.getLocalName().equals("prefLabel"))
                pref = " // pref label";
            Literal title = (Literal) value;
            System.out.println("language: " + title.getLanguage().orElse("unknown"));
            System.out.println(" title: " + title.getLabel() + pref);
        } else {
            System.out.println("   ****  " + value + " //// " + property.getLocalName());
            if (property.getLocalName().equals("type")) {
                System.out.println("        URL: " + st.getSubject());
            }
        }
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 7 with Literal

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

the class ReadRdfFileTest method test2.

public static void test2() {
    ValueFactory vf = SimpleValueFactory.getInstance();
    ModelBuilder builder = new ModelBuilder();
    Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add(DC.TITLE, vf.createLiteral("The Potato Eaters", "en")).add(DC.TITLE, vf.createLiteral("De Aardappeleters", "nl")).build();
    // To see what's in our model, let's just print it to the screen
    for (Statement st : model) {
        // we want to see the object values of each statement
        Value value = st.getObject();
        if (value instanceof Literal) {
            Literal title = (Literal) value;
            System.out.println("language: " + title.getLanguage().orElse("unknown"));
            System.out.println(" title: " + title.getLabel());
        }
    }
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory)

Example 8 with Literal

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

the class WriteRdfFileTest method write2.

@Test
public void write2() {
    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();
    // To see what's in our model, let's just print stuff to the screen
    for (Statement st : model) {
        // we want to see the object values of each property
        IRI property = st.getPredicate();
        Value value = st.getObject();
        if (value instanceof Literal) {
            Literal literal = (Literal) value;
            System.out.println("datatype: " + literal.getDatatype());
            // get the value of the literal directly as a Java primitive.
            if (property.getLocalName().equals("peopleDepicted")) {
                int peopleDepicted = literal.intValue();
                System.out.println(peopleDepicted + " people are depicted in this painting");
            } else if (property.getLocalName().equals("creationDate")) {
                XMLGregorianCalendar calendar = literal.calendarValue();
                Date date = calendar.toGregorianCalendar().getTime();
                System.out.println("The painting was created on " + date);
            }
            // you can also just get the lexical value (a string) without worrying about the datatype
            System.out.println("Lexical value: '" + literal.getLabel() + "'");
        }
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) IRI(org.eclipse.rdf4j.model.IRI) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) Value(org.eclipse.rdf4j.model.Value) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) Date(java.util.Date) Test(org.junit.Test)

Example 9 with Literal

use of org.eclipse.rdf4j.model.Literal 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 10 with Literal

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

the class WriteRdf4j method writeDate.

private void writeDate(SKOSResource resource) {
    int prop;
    Literal literal;
    for (SKOSDate date : resource.getDateList()) {
        literal = vf.createLiteral(date.getDate(), XMLSchema.DATE);
        prop = date.getProperty();
        switch(prop) {
            case SKOSProperty.created:
                builder.add(DCTERMS.CREATED, literal);
                break;
            case SKOSProperty.modified:
                builder.add(DCTERMS.MODIFIED, literal);
                break;
            case SKOSProperty.date:
                builder.add(DCTERMS.DATE, literal);
                break;
            default:
                break;
        }
    }
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) SKOSDate(mom.trd.opentheso.skosapi.SKOSDate)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)11 Statement (org.eclipse.rdf4j.model.Statement)7 Model (org.eclipse.rdf4j.model.Model)6 Value (org.eclipse.rdf4j.model.Value)5 IRI (org.eclipse.rdf4j.model.IRI)4 ModelBuilder (org.eclipse.rdf4j.model.util.ModelBuilder)4 Test (org.junit.Test)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)3 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)2 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1