Search in sources :

Example 1 with ModelBuilder

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

the class WriteRdfFileTest method write3.

@Test
public void write3() {
    ValueFactory vf = SimpleValueFactory.getInstance();
    BNode address = vf.createBNode();
    // First we do the same thing we did in example 02: create a new ModelBuilder, and add
    // two statements about Picasso.
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("ex", "http://example.org/").subject("ex:Picasso").add(RDF.TYPE, "ex:Artist").add(FOAF.FIRST_NAME, "Pablo").add("ex:homeAddress", // link the blank node
    address).subject(// switch the subject
    address).add("ex:street", "31 Art Gallery").add("ex:city", "Madrid").add("ex:country", "Spain");
    Model model = builder.build();
    // To see what's in our model, let's just print it to the screen
    for (Statement st : model) {
        System.out.println(st);
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) BNode(org.eclipse.rdf4j.model.BNode) Statement(org.eclipse.rdf4j.model.Statement) Model(org.eclipse.rdf4j.model.Model) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) Test(org.junit.Test)

Example 2 with ModelBuilder

use of org.eclipse.rdf4j.model.util.ModelBuilder 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 3 with ModelBuilder

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

the class ReadRdfFileTest method test1.

public void test1() {
    ModelBuilder builder = new ModelBuilder();
    Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add("ex:creationDate", new GregorianCalendar(1885, Calendar.APRIL, 1).getTime()).add("ex:peopleDepicted", 5).build();
    for (Statement st : model) {
        IRI property = st.getPredicate();
        Value value = st.getObject();
        if (value instanceof Literal) {
            Literal literal = (Literal) value;
            // 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() + "'");
        }
    }
}
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) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Value(org.eclipse.rdf4j.model.Value) Date(java.util.Date)

Example 4 with ModelBuilder

use of org.eclipse.rdf4j.model.util.ModelBuilder 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 5 with ModelBuilder

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

the class ReadRdfFileTest method test3.

public static void test3() {
    // To create a blank node for the address, we need a ValueFactory
    ValueFactory vf = SimpleValueFactory.getInstance();
    BNode address = vf.createBNode();
    // Identically to example 03, we create a model with some data
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("ex", "http://example.org/").subject("ex:Picasso").add(RDF.TYPE, "ex:Artist").add(FOAF.FIRST_NAME, "Pablo").add("ex:homeAddress", // link the blank node
    address).subject(// switch the subject
    address).add("ex:street", "31 Art Gallery").add("ex:city", "Madrid").add("ex:country", "Spain");
    Model model = builder.build();
    // Instead of simply printing the statements to the screen, we use a Rio writer to
    // write the model in RDF/XML syntax:
    Rio.write(model, System.out, RDFFormat.RDFXML);
    File file = new File("test.rdf");
    FileWriter writer = null;
    try {
        file.createNewFile();
        writer = new FileWriter(file);
    } catch (IOException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    Rio.write(model, writer, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) BNode(org.eclipse.rdf4j.model.BNode) FileWriter(java.io.FileWriter) Model(org.eclipse.rdf4j.model.Model) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) IOException(java.io.IOException) File(java.io.File)

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