Search in sources :

Example 1 with Model

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

the class ReadRdfFile_rdf4j method readFile.

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
// 
@Test
public void readFile() {
    File file = new File("/Users/Miled/Desktop/Bureau2/test_unesco.rdf");
    RDFParser rdfParser = Rio.createParser(RDFFormat.RDFXML);
    Model model = new LinkedHashModel();
    rdfParser.setRDFHandler(new StatementCollector(model));
    Rio.write(model, System.out, RDFFormat.TURTLE);
    try {
        rdfParser.parse(new FileReader(file), "http://example.org");
        /*       for (Statement statement : model) {
                
        //        writeLine(statement.getObject().stringValue(), statement.getSubject().stringValue());

                System.out.println("LocalName = " + statement.getPredicate().getLocalName());

                System.out.println("objet = " + statement.getObject().stringValue());
                System.out.println("predicat = " + statement.getPredicate());
                System.out.println("URI = " + statement.getSubject());
                System.out.println("");
               
           //     model.getNamespace(statement.getClass().getgetObject().stringValue());

            } */
        for (Statement statement2 : model) {
            Literal literal = (SimpleLiteral) statement2;
            System.out.println(literal.getLabel());
            // System.out.println(literal.getLanguage());
            System.out.println(literal.getDatatype());
        }
    } catch (IOException e) {
    // handle IO problems (e.g. the file could not be read)
    } catch (RDFParseException e) {
    // handle unrecoverable parse error
    } catch (RDFHandlerException e) {
    // handle a problem encountered by the RDFHandler
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) FileReader(java.io.FileReader) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) File(java.io.File) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 2 with Model

use of org.eclipse.rdf4j.model.Model 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)

Example 3 with Model

use of org.eclipse.rdf4j.model.Model 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 Model

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

use of org.eclipse.rdf4j.model.Model 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)

Aggregations

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