Search in sources :

Example 86 with Resource

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

the class TurtleWriter method handleList.

private void handleList(Statement st) throws IOException {
    Resource subj = st.getSubject();
    boolean first = RDF.FIRST.equals(st.getPredicate());
    boolean rest = RDF.REST.equals(st.getPredicate()) && !RDF.NIL.equals(st.getObject());
    boolean nil = RDF.REST.equals(st.getPredicate()) && RDF.NIL.equals(st.getObject());
    if (first && REST != lastWrittenPredicate && isHanging()) {
        // new collection
        writer.write("(");
        writer.increaseIndentation();
        wrapLine(false);
        lastWrittenSubject = subj;
        path.addLast(FIRST);
        lastWrittenPredicate = FIRST;
        writeValue(st.getObject(), inlineBNodes);
    } else if (first && REST == lastWrittenPredicate) {
        // item in existing collection
        lastWrittenSubject = subj;
        path.addLast(FIRST);
        lastWrittenPredicate = FIRST;
        writeValue(st.getObject(), inlineBNodes);
    } else {
        closeNestedResources(subj);
        if (rest && FIRST == lastWrittenPredicate) {
            // next item
            wrapLine(true);
            // RDF.FIRST
            path.removeLast();
            path.addLast(REST);
            lastWrittenPredicate = REST;
            writeValue(st.getObject(), inlineBNodes);
        } else if (nil && FIRST == lastWrittenPredicate) {
            writer.decreaseIndentation();
            writer.write(")");
            // RDF.FIRST
            path.removeLast();
            path.addLast(REST);
            while (REST == path.peekLast()) {
                stack.pollLast();
                path.pollLast();
                lastWrittenSubject = stack.peekLast();
                lastWrittenPredicate = path.peekLast();
            }
        } else {
            handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
        }
    }
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Example 87 with Resource

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

the class TurtleWriter method handleInlineNode.

private void handleInlineNode(Statement st) throws IOException {
    Resource subj = st.getSubject();
    IRI pred = st.getPredicate();
    if (isHanging() && subj.equals(stack.peekLast())) {
        // blank subject
        lastWrittenSubject = subj;
        writer.write("[");
        if (prettyPrint && !RDF.TYPE.equals(pred)) {
            writer.writeEOL();
        } else {
            wrapLine(prettyPrint);
        }
        writer.increaseIndentation();
        // Write new predicate
        writePredicate(pred);
        writer.increaseIndentation();
        wrapLine(true);
        path.addLast(pred);
        lastWrittenPredicate = pred;
        writeValue(st.getObject(), inlineBNodes);
    } else if (!subj.equals(lastWrittenSubject) && stack.contains(subj)) {
        closeNestedResources(subj);
        handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
    } else {
        assert false;
    }
}
Also used : SimpleIRI(org.eclipse.rdf4j.model.impl.SimpleIRI) ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource)

Example 88 with Resource

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

the class TurtleWriter method handleStatement.

@Override
public void handleStatement(Statement st) throws RDFHandlerException {
    if (!writingStarted) {
        throw new RuntimeException("Document writing has not yet been started");
    }
    try {
        Resource subj = st.getSubject();
        IRI pred = st.getPredicate();
        if (inlineBNodes && (pred.equals(RDF.FIRST) || pred.equals(RDF.REST))) {
            handleList(st);
        } else if (inlineBNodes && !subj.equals(lastWrittenSubject) && stack.contains(subj)) {
            handleInlineNode(st);
        } else {
            closeHangingResource();
            handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
        }
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : SimpleIRI(org.eclipse.rdf4j.model.impl.SimpleIRI) ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Resource(org.eclipse.rdf4j.model.Resource) IOException(java.io.IOException)

Example 89 with Resource

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

the class TurtleWriter method handleStatementInternal.

/**
 * Internal method that differentiates between the pretty-print and streaming writer cases.
 *
 * @param st
 *        The next statement to write
 * @param endRDFCalled
 *        True if endRDF has been called before this method is called. This is used to buffer statements
 *        for pretty-printing before dumping them when all statements have been delivered to us.
 * @param canShortenSubjectBNode
 *        True if, in the current context, we may be able to shorten the subject of this statement iff it
 *        is an instance of {@link BNode}.
 * @param canShortenObjectBNode
 *        True if, in the current context, we may be able to shorten the object of this statement iff it
 *        is an instance of {@link BNode}.
 */
protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubjectBNode, boolean canShortenObjectBNode) {
    Resource subj = st.getSubject();
    IRI pred = st.getPredicate();
    Value obj = st.getObject();
    try {
        if (subj.equals(lastWrittenSubject)) {
            if (pred.equals(lastWrittenPredicate)) {
                // Identical subject and predicate
                writer.write(",");
                wrapLine(prettyPrint);
            } else {
                // Identical subject, new predicate
                writer.write(";");
                writer.writeEOL();
                // Write new predicate
                writer.decreaseIndentation();
                writePredicate(pred);
                writer.increaseIndentation();
                wrapLine(true);
                path.removeLast();
                path.addLast(pred);
                lastWrittenPredicate = pred;
            }
        } else {
            // New subject
            closePreviousStatement();
            stack.addLast(subj);
            // Write new subject:
            if (prettyPrint) {
                writer.writeEOL();
            }
            writeResource(subj, canShortenSubjectBNode);
            wrapLine(true);
            writer.increaseIndentation();
            lastWrittenSubject = subj;
            // Write new predicate
            writePredicate(pred);
            wrapLine(true);
            path.addLast(pred);
            lastWrittenPredicate = pred;
            statementClosed = false;
            writer.increaseIndentation();
        }
        writeValue(obj, canShortenObjectBNode);
    // Don't close the line just yet. Maybe the next
    // statement has the same subject and/or predicate.
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : SimpleIRI(org.eclipse.rdf4j.model.impl.SimpleIRI) ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Example 90 with Resource

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

the class QueryBuilderFactory method describe.

/**
 * Create a QueryBuilder for creating a describe query
 *
 * @param theVars
 *        the variables to be described
 * @param theValues
 *        the specific bound URI values to be described
 * @return a describe query builder
 */
public static QueryBuilder<ParsedGraphQuery> describe(String[] theVars, Resource... theValues) {
    QueryBuilder<ParsedGraphQuery> aBuilder = new AbstractQueryBuilder<ParsedGraphQuery>(new ParsedDescribeQuery());
    aBuilder.reduced();
    aBuilder.addProjectionVar("descr_subj", "descr_pred", "descr_obj");
    GroupBuilder<?, ?> aGroup = aBuilder.group();
    if (theVars != null) {
        for (String aVar : theVars) {
            Var aVarObj = new Var(aVar);
            aVarObj.setAnonymous(true);
            aGroup.filter().or(new SameTerm(aVarObj, new Var("descr_subj")), new SameTerm(aVarObj, new Var("descr_obj")));
        }
    }
    if (theValues != null) {
        for (Resource aVar : theValues) {
            Var aSubjVar = new Var("descr_subj");
            aSubjVar.setAnonymous(true);
            Var aObjVar = new Var("descr_obj");
            aObjVar.setAnonymous(true);
            aGroup.filter().or(new SameTerm(new ValueConstant(aVar), aSubjVar), new SameTerm(new ValueConstant(aVar), aObjVar));
        }
    }
    aGroup.atom("descr_subj", "descr_pred", "descr_obj");
    return aBuilder;
}
Also used : ParsedDescribeQuery(org.eclipse.rdf4j.query.parser.ParsedDescribeQuery) Var(org.eclipse.rdf4j.query.algebra.Var) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4