Search in sources :

Example 31 with Statement

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

the class NQuadsParser method parseQuad.

private int parseQuad(int c) throws IOException, RDFParseException, RDFHandlerException {
    boolean ignoredAnError = false;
    try {
        c = parseSubject(c);
        c = skipWhitespace(c);
        c = parsePredicate(c);
        c = skipWhitespace(c);
        c = parseObject(c);
        c = skipWhitespace(c);
        // Context is not required
        if (c != '.') {
            c = parseContext(c);
            c = skipWhitespace(c);
        }
        if (c == -1) {
            throwEOFException();
        } else if (c != '.') {
            reportFatalError("Expected '.', found: " + new String(Character.toChars(c)));
        }
        c = assertLineTerminates(c);
    } catch (RDFParseException rdfpe) {
        if (getParserConfig().isNonFatalError(NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES)) {
            reportError(rdfpe, NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
            ignoredAnError = true;
        } else {
            throw rdfpe;
        }
    }
    c = skipLine(c);
    if (!ignoredAnError) {
        Statement st = createStatement(subject, predicate, object, context);
        if (rdfHandler != null) {
            rdfHandler.handleStatement(st);
        }
    }
    subject = null;
    predicate = null;
    object = null;
    context = null;
    return c;
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 32 with Statement

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

the class NTriplesParser method parseTriple.

private int parseTriple(int c) throws IOException, RDFParseException, RDFHandlerException {
    boolean ignoredAnError = false;
    try {
        c = parseSubject(c);
        c = skipWhitespace(c);
        c = parsePredicate(c);
        c = skipWhitespace(c);
        c = parseObject(c);
        c = skipWhitespace(c);
        if (c == -1) {
            throwEOFException();
        } else if (c != '.') {
            reportError("Expected '.', found: " + new String(Character.toChars(c)), NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
        }
        c = assertLineTerminates(c);
    } catch (RDFParseException rdfpe) {
        if (getParserConfig().isNonFatalError(NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES)) {
            reportError(rdfpe, NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
            ignoredAnError = true;
        } else {
            throw rdfpe;
        }
    }
    c = skipLine(c);
    if (!ignoredAnError) {
        Statement st = createStatement(subject, predicate, object);
        if (rdfHandler != null) {
            rdfHandler.handleStatement(st);
        }
    }
    subject = null;
    predicate = null;
    object = null;
    return c;
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 33 with Statement

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

the class BufferedGroupingRDFHandler method processBuffer.

/*
	 * not synchronized, assumes calling method has obtained a lock on bufferLock
	 */
private void processBuffer() throws RDFHandlerException {
    // primary grouping per context.
    for (Resource context : contexts) {
        Set<Resource> subjects = GraphUtil.getSubjects(bufferedStatements, null, null, context);
        for (Resource subject : subjects) {
            Set<IRI> processedPredicates = new HashSet<IRI>();
            // give rdf:type preference over other predicates.
            Iterator<Statement> typeStatements = bufferedStatements.match(subject, RDF.TYPE, null, context);
            while (typeStatements.hasNext()) {
                Statement typeStatement = typeStatements.next();
                super.handleStatement(typeStatement);
            }
            processedPredicates.add(RDF.TYPE);
            // retrieve other statement from this context with the same
            // subject, and output them grouped by predicate
            Iterator<Statement> subjectStatements = bufferedStatements.match(subject, null, null, context);
            while (subjectStatements.hasNext()) {
                Statement subjectStatement = subjectStatements.next();
                IRI predicate = subjectStatement.getPredicate();
                if (!processedPredicates.contains(predicate)) {
                    Iterator<Statement> toWrite = bufferedStatements.match(subject, predicate, null, context);
                    while (toWrite.hasNext()) {
                        Statement toWriteSt = toWrite.next();
                        super.handleStatement(toWriteSt);
                    }
                    processedPredicates.add(predicate);
                }
            }
        }
    }
    bufferedStatements.clear();
    contexts.clear();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) HashSet(java.util.HashSet)

Example 34 with Statement

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

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

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