Search in sources :

Example 51 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class ExQuerySelect2 method main.

public static void main(String[] args) {
    // Create the data.
    // This wil be the background (unnamed) graph in the dataset.
    Model model = createModel();
    // First part or the query string
    String prolog = "PREFIX dc: <" + DC.getURI() + ">";
    // Query string.
    String queryString = prolog + NL + "SELECT ?title WHERE {?x dc:title ?title}";
    Query query = QueryFactory.create(queryString);
    // Print with line numbers
    query.serialize(new IndentedWriter(System.out, true));
    System.out.println();
    // Or QueryExecutionFactory.create(queryString, model) ;
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        // A ResultSet is an iterator - any query solutions returned by .next()
        // are not accessible again.
        // Create a ResultSetRewindable that can be reset to the beginning.
        // Do before first use.
        ResultSetRewindable rewindable = qexec.execSelect().rewindable();
        ResultSetFormatter.out(rewindable);
        rewindable.reset();
        ResultSetFormatter.out(rewindable);
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Model(org.apache.jena.rdf.model.Model)

Example 52 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class ExProg1 method main.

public static void main(String[] args) {
    Model model = createModel();
    Query query = QueryFactory.make();
    query.setQuerySelectType();
    // Build pattern
    ElementGroup elg = new ElementGroup();
    Var varTitle = Var.alloc("title");
    Var varX = Var.alloc("x");
    Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
    elg.addTriplePattern(t1);
    // Don't use bNodes for anon variables.  The conversion is done in parsing.
    // BNodes here are assumed to be values from the target graph.
    Triple t2 = new Triple(varX, DC.description.asNode(), Var.alloc("desc"));
    elg.addTriplePattern(t2);
    // Attach the group to query.
    query.setQueryPattern(elg);
    // Choose what we want - SELECT *
    // query.setQueryResultStar(true) ;
    query.addResultVar(varTitle);
    // Print query with line numbers
    // Prefix mapping just helps serialization
    query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
    query.serialize(new IndentedWriter(System.out, true));
    System.out.println();
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        // Assumption: it's a SELECT query.
        ResultSet rs = qexec.execSelect();
        // The order of results is undefined.
        System.out.println("Titles: ");
        for (; rs.hasNext(); ) {
            QuerySolution rb = rs.nextSolution();
            // Get title - variable names do not include the '?' (or '$')
            RDFNode x = rb.get("title");
            // Check the type of the result value
            if (x instanceof Literal) {
                Literal titleStr = (Literal) x;
                System.out.println("    " + titleStr);
            } else
                System.out.println("Strange - not a literal: " + x);
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Var(org.apache.jena.sparql.core.Var) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 53 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class TriGWriterBase method write.

@Override
public void write(Writer out, DatasetGraph dsg, PrefixMap prefixMap, String baseURI, Context context) {
    IndentedWriter iOut = RiotLib.create(out);
    output$(iOut, dsg, prefixMap, baseURI, context);
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 54 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class ItemWriter method write.

public static void write(OutputStream out, Item item) {
    IndentedWriter iw = new IndentedWriter(out);
    write(iw, item, null);
    iw.ensureStartOfLine();
    iw.flush();
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 55 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class SSE method write.

public static void write(OutputStream out, Triple triple) {
    IndentedWriter iOut = new IndentedWriter(out);
    write(iOut, triple);
    iOut.flush();
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Aggregations

IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)57 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)5 TerminationException (org.apache.jena.cmd.TerminationException)4 Node (org.apache.jena.graph.Node)4 PrefixMapping (org.apache.jena.shared.PrefixMapping)4 CmdException (org.apache.jena.cmd.CmdException)3 Triple (org.apache.jena.graph.Triple)3 Syntax (org.apache.jena.query.Syntax)3 IOException (java.io.IOException)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 Pair (org.apache.jena.atlas.lib.Pair)2 RiotException (org.apache.jena.riot.RiotException)2 RiotNotFoundException (org.apache.jena.riot.RiotNotFoundException)2 NodeFormatter (org.apache.jena.riot.out.NodeFormatter)2 NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)2 JenaException (org.apache.jena.shared.JenaException)2 ARQException (org.apache.jena.sparql.ARQException)2 Var (org.apache.jena.sparql.core.Var)2 Plan (org.apache.jena.sparql.engine.Plan)2 Expr (org.apache.jena.sparql.expr.Expr)2