Search in sources :

Example 1 with Literal

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

the class WriteRdf4j method writeGPS.

private void writeGPS(SKOSResource resource) {
    SKOSGPSCoordinates gps = resource.getGPSCoordinates();
    String lat = gps.getLat();
    String lon = gps.getLon();
    Literal literal;
    if (lat != null && lon != null) {
        literal = vf.createLiteral(lat, XMLSchema.DOUBLE);
        builder.add("geo:lat", literal);
        literal = vf.createLiteral(lon, XMLSchema.DOUBLE);
        builder.add("geo:long", literal);
    }
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) SKOSGPSCoordinates(mom.trd.opentheso.skosapi.SKOSGPSCoordinates)

Example 2 with Literal

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

the class WriteRdf4j method writeLabel.

private void writeLabel(SKOSResource resource) {
    int prop;
    for (SKOSLabel label : resource.getLabelsList()) {
        prop = label.getProperty();
        Literal literal = vf.createLiteral(label.getLabel(), label.getLanguage());
        if (prop == SKOSProperty.prefLabel) {
            builder.add(SKOS.PREF_LABEL, literal);
        } else if (prop == SKOSProperty.altLabel) {
            builder.add(SKOS.ALT_LABEL, literal);
        }
    }
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) SKOSLabel(mom.trd.opentheso.skosapi.SKOSLabel)

Example 3 with Literal

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

the class ReadRdf4j method readModel.

/**
 * Permet de lire un fichier RDF précédament charger avec la fonction
 * laodModel() les données sont stoqué dans la variable thesaurus
 */
private void readModel(rdf4jFileBean fileBean) {
    ReadStruct readStruct = new ReadStruct();
    readStruct.resource = null;
    // pour le debug :
    ArrayList<String> nonReco = new ArrayList();
    for (Statement st : model) {
        readStruct.value = st.getObject();
        readStruct.property = st.getPredicate();
        if (readStruct.value instanceof Literal) {
            readStruct.literal = (Literal) readStruct.value;
        }
        // Concept or ConceptScheme or Collection or ConceptGroup
        if (readStruct.property.getLocalName().equals("type")) {
            int prop = -1;
            String type = readStruct.value.toString();
            type = type.toUpperCase();
            if (type.contains("ConceptScheme".toUpperCase())) {
                prop = SKOSProperty.ConceptScheme;
                sKOSXmlDocument.setTitle(st.getSubject().stringValue());
            } else if (type.contains("ConceptGroup".toUpperCase())) {
                prop = SKOSProperty.ConceptGroup;
            } else if (type.contains("Theme".toUpperCase())) {
                prop = SKOSProperty.Theme;
            } else if (type.contains("MicroThesaurus".toUpperCase())) {
                prop = SKOSProperty.MicroThesaurus;
            } else if (type.contains("Collection".toUpperCase())) {
                prop = SKOSProperty.Collection;
            } else if (type.contains("Concept".toUpperCase())) {
                prop = SKOSProperty.Concept;
            }
            String uri = st.getSubject().stringValue();
            readStruct.resource = new SKOSResource(uri, prop);
            if (prop == SKOSProperty.ConceptScheme) {
                sKOSXmlDocument.setConceptScheme(readStruct.resource);
            } else if (prop == SKOSProperty.ConceptGroup || prop == SKOSProperty.Collection || prop == SKOSProperty.Theme || prop == SKOSProperty.MicroThesaurus) {
                sKOSXmlDocument.addGroup(readStruct.resource);
            } else if (prop == SKOSProperty.Concept) {
                sKOSXmlDocument.addconcept(readStruct.resource);
            }
        } else // Labelling Properties
        if (readLabellingProperties(readStruct)) {
            // Dates
            if (readDate(readStruct)) {
                // Semantic Relationships
                if (readRelationships(readStruct)) {
                    // Documentation Properties
                    if (readDocumentation(readStruct)) {
                        if (readCreator(readStruct)) {
                            if (readGPSCoordinates(readStruct)) {
                                if (readNotation(readStruct)) {
                                    if (readIdentifier(readStruct)) {
                                        if (readMatch(readStruct)) {
                                            // debug
                                            if (!nonReco.contains(readStruct.property.getLocalName())) {
                                                // System.out.println("non reconue : " + readStruct.property.getLocalName());
                                                nonReco.add(readStruct.property.getLocalName());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (!nonReco.isEmpty()) {
        String balises = "";
        for (String b : nonReco) {
            balises += "    " + b + "\n";
        }
        fileBean.setWarning("Not readed RDF tag\n" + balises);
    }
}
Also used : SKOSResource(mom.trd.opentheso.skosapi.SKOSResource) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) ArrayList(java.util.ArrayList)

Example 4 with Literal

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

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

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)96 Test (org.junit.Test)52 IRI (org.eclipse.rdf4j.model.IRI)34 Value (org.eclipse.rdf4j.model.Value)17 BNode (org.eclipse.rdf4j.model.BNode)14 Model (org.eclipse.rdf4j.model.Model)13 Statement (org.eclipse.rdf4j.model.Statement)13 Resource (org.eclipse.rdf4j.model.Resource)9 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)7 InputStream (java.io.InputStream)6 Date (java.util.Date)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 IOException (java.io.IOException)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4 ArrayList (java.util.ArrayList)3