Search in sources :

Example 6 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class qexpr method main2.

public static void main2(String... argv) {
    CmdLineArgs cl = new CmdLineArgs(argv);
    ArgDecl helpDecl = new ArgDecl(ArgDecl.NoValue, "h", "help");
    cl.add(helpDecl);
    ArgDecl verboseDecl = new ArgDecl(ArgDecl.NoValue, "v", "verbose");
    cl.add(verboseDecl);
    ArgDecl versionDecl = new ArgDecl(ArgDecl.NoValue, "ver", "version", "V");
    cl.add(versionDecl);
    ArgDecl quietDecl = new ArgDecl(ArgDecl.NoValue, "q", "quiet");
    cl.add(quietDecl);
    ArgDecl reduceDecl = new ArgDecl(ArgDecl.NoValue, "reduce", "fold", "simplify");
    cl.add(reduceDecl);
    ArgDecl strictDecl = new ArgDecl(ArgDecl.NoValue, "strict");
    cl.add(strictDecl);
    ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print");
    cl.add(printDecl);
    try {
        cl.process();
    } catch (IllegalArgumentException ex) {
        System.err.println(ex.getMessage());
        usage(System.err);
        throw new CmdException();
    }
    if (cl.contains(helpDecl)) {
        usage();
        throw new TerminationException(0);
    }
    if (cl.contains(versionDecl)) {
        System.out.println("ARQ Version: " + ARQ.VERSION + " (Jena: " + Jena.VERSION + ")");
        throw new TerminationException(0);
    }
    // ==== General things
    boolean verbose = cl.contains(verboseDecl);
    boolean quiet = cl.contains(quietDecl);
    if (cl.contains(strictDecl))
        ARQ.setStrictMode();
    boolean actionCopySubstitute = cl.contains(reduceDecl);
    boolean actionPrintPrefix = false;
    boolean actionPrintSPARQL = false;
    boolean actionPrint = cl.contains(printDecl);
    for (String v : cl.getValues(printDecl)) {
        if (v.equalsIgnoreCase("prefix") || v.equalsIgnoreCase("op")) {
            actionPrintPrefix = true;
        } else if (v.equalsIgnoreCase("expr")) {
            actionPrintSPARQL = true;
        } else {
            System.err.println("Unknown print form: " + v);
            throw new TerminationException(0);
        }
    }
    for (int i = 0; i < cl.getNumPositional(); i++) {
        String exprStr = cl.getPositionalArg(i);
        exprStr = cl.indirect(exprStr);
        try {
            PrefixMapping pmap = PrefixMapping.Factory.create();
            pmap.setNsPrefixes(ARQConstants.getGlobalPrefixMap());
            pmap.setNsPrefix("", "http://example/");
            pmap.setNsPrefix("ex", "http://example/ns#");
            //              Node n = asNode() ;
            //              return makeNode(n) ;
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (verbose)
                System.out.print(expr.toString() + " => ");
            if (actionPrint) {
                if (actionPrintSPARQL)
                    System.out.println(ExprUtils.fmtSPARQL(expr));
                if (actionPrintPrefix)
                    WriterSSE.out(IndentedWriter.stdout, expr, new Prologue(pmap));
                continue;
            }
            try {
                if (actionCopySubstitute) {
                    Expr e = ExprLib.foldConstants(expr);
                    System.out.println(e);
                } else {
                    // Default action
                    ARQ.getContext().set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
                    FunctionEnv env = new ExecutionContext(ARQ.getContext(), null, null, null);
                    NodeValue r = expr.eval(null, env);
                    //System.out.println(r.asQuotedString()) ;
                    Node n = r.asNode();
                    String s = NodeFmtLib.displayStr(n);
                    System.out.println(s);
                }
            } catch (ExprEvalException ex) {
                System.out.println("Exception: " + ex.getMessage());
                throw new TerminationException(2);
            }
        } catch (QueryParseException ex) {
            System.err.println("Parse error: " + ex.getMessage());
            throw new TerminationException(2);
        }
    }
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) CmdException(jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(jena.cmd.ArgDecl) CmdLineArgs(jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(jena.cmd.TerminationException) Prologue(org.apache.jena.sparql.core.Prologue) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Expr(org.apache.jena.sparql.expr.Expr) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 7 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class TestPath method parse.

private static void parse(String path1, String path2) {
    Prologue prologue = new Prologue(pmap);
    Path p1 = PathParser.parse(path1, prologue);
    Path p2 = PathParser.parse(path2, prologue);
    assertEquals(p1, p2);
}
Also used : WriterPath(org.apache.jena.sparql.sse.writers.WriterPath) BuilderPath(org.apache.jena.sparql.sse.builders.BuilderPath) Prologue(org.apache.jena.sparql.core.Prologue)

Example 8 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class TestPath method parse.

private void parse(String string, boolean expectLegal) {
    Prologue prologue = new Prologue(pmap);
    Path p = null;
    try {
        p = PathParser.parse(string, prologue);
        //            System.out.println(PathWriterSSE.asString(p, new Prologue(pmap))) ;
        if (!expectLegal)
            fail("Expected error; " + string);
    } catch (QueryParseException ex) {
        if (expectLegal)
            fail("Expected success: " + string + ": " + ex.getMessage());
        return;
    }
    String x = p.toString(prologue);
    Path p2 = PathParser.parse(x, prologue);
    assertEquals(p, p2);
    String sse = WriterPath.asString(p, prologue);
    Item item = SSE.parseItem(sse, pmap);
    p2 = BuilderPath.buildPath(item);
    assertEquals(p, p2);
}
Also used : WriterPath(org.apache.jena.sparql.sse.writers.WriterPath) BuilderPath(org.apache.jena.sparql.sse.builders.BuilderPath) Item(org.apache.jena.sparql.sse.Item) Prologue(org.apache.jena.sparql.core.Prologue) QueryParseException(org.apache.jena.query.QueryParseException)

Aggregations

Prologue (org.apache.jena.sparql.core.Prologue)8 QueryParseException (org.apache.jena.query.QueryParseException)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 BuilderPath (org.apache.jena.sparql.sse.builders.BuilderPath)2 WriterPath (org.apache.jena.sparql.sse.writers.WriterPath)2 NodeToLabelMapBNode (org.apache.jena.sparql.util.NodeToLabelMapBNode)2 ArgDecl (jena.cmd.ArgDecl)1 CmdException (jena.cmd.CmdException)1 CmdLineArgs (jena.cmd.CmdLineArgs)1 TerminationException (jena.cmd.TerminationException)1 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)1 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 DatasetRef (org.apache.jena.fuseki.server.DatasetRef)1 Node (org.apache.jena.graph.Node)1 Syntax (org.apache.jena.query.Syntax)1 Model (org.apache.jena.rdf.model.Model)1 Lang (org.apache.jena.riot.Lang)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)1 Expr (org.apache.jena.sparql.expr.Expr)1