Search in sources :

Example 1 with Statement

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

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class JSONLDWriterBackgroundTest method testRoundTripNamespaces.

@Test
public void testRoundTripNamespaces() throws Exception {
    String exNs = "http://example.org/";
    IRI uri1 = vf.createIRI(exNs, "uri1");
    IRI uri2 = vf.createIRI(exNs, "uri2");
    Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING);
    Statement st1 = vf.createStatement(uri1, uri2, plainLit);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
    rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
    rdfWriter.handleNamespace("ex", exNs);
    rdfWriter.startRDF();
    rdfWriter.handleStatement(st1);
    rdfWriter.endRDF();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    RDFParser rdfParser = rdfParserFactory.getParser();
    ParserConfig config = new ParserConfig();
    config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
    config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
    rdfParser.setParserConfig(config);
    rdfParser.setValueFactory(vf);
    Model model = new LinkedHashModel();
    rdfParser.setRDFHandler(new StatementCollector(model));
    rdfParser.parse(in, "foo:bar");
    assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size());
    assertTrue("missing namespaced statement", model.contains(st1));
    if (rdfParser.getRDFFormat().supportsNamespaces()) {
        assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(), model.getNamespaces().size() >= 1);
        assertEquals(exNs, model.getNamespace("ex").get().getName());
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFParser(org.eclipse.rdf4j.rio.RDFParser) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFWriterTest(org.eclipse.rdf4j.rio.RDFWriterTest) Test(org.junit.Test)

Example 3 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class NQuadsHandlingTest method writeNQuads.

/**
 * Helper method to write the given model to N-Triples and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeNQuads(Model statements) throws RDFHandlerException {
    StringWriter writer = new StringWriter();
    RDFWriter nQuadsWriter = new NQuadsWriter(writer);
    nQuadsWriter.startRDF();
    for (Statement nextStatement : statements) {
        nQuadsWriter.handleStatement(nextStatement);
    }
    nQuadsWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) NQuadsWriter(org.eclipse.rdf4j.rio.nquads.NQuadsWriter)

Example 4 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class NTriplesHandlingTest method writeNTriples.

/**
 * Helper method to write the given model to N-Triples and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeNTriples(Model statements) throws RDFHandlerException {
    StringWriter writer = new StringWriter();
    RDFWriter nTriplesWriter = new NTriplesWriter(writer);
    nTriplesWriter.startRDF();
    for (Statement nextStatement : statements) {
        nTriplesWriter.handleStatement(nextStatement);
    }
    nTriplesWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : NTriplesWriter(org.eclipse.rdf4j.rio.ntriples.NTriplesWriter) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter)

Example 5 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class TestTurtleParser method testParseDots.

@Test
public void testParseDots() throws Exception {
    String data = prefixes + " ex:foo.bar ex:\\~foo.bar ex:foobar. ";
    parser.parse(new StringReader(data), baseURI);
    assertTrue(errorCollector.getWarnings().isEmpty());
    assertTrue(errorCollector.getErrors().isEmpty());
    assertTrue(errorCollector.getFatalErrors().isEmpty());
    assertFalse(statementCollector.getStatements().isEmpty());
    assertEquals(1, statementCollector.getStatements().size());
    for (Statement st : statementCollector.getStatements()) {
        System.out.println(st);
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5