Search in sources :

Example 1 with TerminationException

use of org.apache.jena.cmd.TerminationException in project jena by apache.

the class sse_query method exec.

@Override
protected void exec() {
    Op op = modAlgebra.getOp();
    if (op == null) {
        System.err.println("No query expression to execute");
        throw new TerminationException(9);
    }
    Dataset dataset = modDataset.getDataset();
    // Check there is a dataset.
    if (dataset == null)
        dataset = DatasetFactory.createGeneral();
    modTime.startTimer();
    DatasetGraph dsg = dataset.asDatasetGraph();
    if (printOp || printPlan) {
        if (printOp) {
            divider();
            IndentedWriter out = new IndentedWriter(System.out, true);
            op.output(out);
            out.flush();
        }
        if (printPlan) {
            QueryIterator qIter = Algebra.exec(op, dsg);
            Plan plan = new PlanOp(op, null, qIter);
            divider();
            IndentedWriter out = new IndentedWriter(System.out, false);
            plan.output(out);
            out.flush();
        }
    // return ;
    }
    // Do not optimize.  Execute as-is.
    QueryExecUtils.execute(op, dsg, modResults.getResultsFormat());
    long time = modTime.endTimer();
    if (modTime.timingEnabled())
        System.out.println("Time: " + modTime.timeStr(time));
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) PlanOp(org.apache.jena.sparql.engine.PlanOp) Op(org.apache.jena.sparql.algebra.Op) TerminationException(org.apache.jena.cmd.TerminationException) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ModDataset(arq.cmdline.ModDataset) Dataset(org.apache.jena.query.Dataset) PlanOp(org.apache.jena.sparql.engine.PlanOp) Plan(org.apache.jena.sparql.engine.Plan) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 2 with TerminationException

use of org.apache.jena.cmd.TerminationException in project jena by apache.

the class FusekiMain method exec.

@Override
protected void exec() {
    try {
        FusekiServer server = buildServer(serverConfig);
        info(server);
        try {
            server.start();
        } catch (FusekiException ex) {
            if (ex.getCause() instanceof BindException) {
                if (serverConfig.jettyConfigFile == null)
                    Fuseki.serverLog.error("Failed to start server: " + ex.getCause().getMessage() + ": port=" + serverConfig.port);
                else
                    Fuseki.serverLog.error("Failed to start server: " + ex.getCause().getMessage() + ": port in use");
                System.exit(1);
            }
            throw ex;
        } catch (Exception ex) {
            throw new FusekiException("Failed to start server: " + ex.getMessage(), ex);
        }
        server.join();
        System.exit(0);
    } catch (AssemblerException | FusekiException ex) {
        if (ex.getCause() != null)
            System.err.println(ex.getCause().getMessage());
        else
            System.err.println(ex.getMessage());
        throw new TerminationException(1);
    }
}
Also used : TerminationException(org.apache.jena.cmd.TerminationException) AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) FusekiException(org.apache.jena.fuseki.FusekiException) BindException(java.net.BindException) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) RiotException(org.apache.jena.riot.RiotException) BindException(java.net.BindException) AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) FusekiException(org.apache.jena.fuseki.FusekiException) CmdException(org.apache.jena.cmd.CmdException) TerminationException(org.apache.jena.cmd.TerminationException)

Example 3 with TerminationException

use of org.apache.jena.cmd.TerminationException 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#");
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (actionPrint) {
                IndentedWriter iOut = IndentedWriter.stdout;
                if (actionPrintSPARQL) {
                    ExprUtils.fmtSPARQL(iOut, expr);
                    iOut.ensureStartOfLine();
                }
                if (actionPrintPrefix) {
                    WriterSSE.out(iOut, expr, new Prologue(pmap));
                    iOut.ensureStartOfLine();
                }
                iOut.flush();
                continue;
            }
            if (verbose)
                System.out.print(expr.toString() + " => ");
            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(org.apache.jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(org.apache.jena.cmd.ArgDecl) CmdLineArgs(org.apache.jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(org.apache.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 4 with TerminationException

use of org.apache.jena.cmd.TerminationException in project jena by apache.

the class query method queryExec.

protected void queryExec(boolean timed, ResultsFormat fmt, PrintStream resultsDest) {
    try {
        Query query = getQuery();
        if (isVerbose()) {
            IndentedWriter out = new IndentedWriter(resultsDest, true);
            query.serialize(out);
            out.setLineNumbers(false);
            out.println();
            out.flush();
        }
        if (isQuiet())
            LogCtl.setError(SysRIOT.riotLoggerName);
        Dataset dataset = getDataset(query);
        // The default policy is to create an empty one - convenience for VALUES and BIND providing the data.
        if (dataset == null && !query.hasDatasetDescription()) {
            System.err.println("Dataset not specified in query nor provided on command line.");
            throw new TerminationException(1);
        }
        Transactional transactional = (dataset != null && dataset.supportsTransactions()) ? dataset : new TransactionalNull();
        Txn.executeRead(transactional, () -> {
            modTime.startTimer();
            try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
                try {
                    QueryExecUtils.executeQuery(query, qe, fmt, resultsDest);
                } catch (QueryCancelledException ex) {
                    IO.flush(resultsDest);
                    System.err.println("Query timed out");
                }
                long time = modTime.endTimer();
                if (timed) {
                    totalTime += time;
                    System.err.println("Time: " + modTime.timeStr(time) + " sec");
                }
            } catch (ResultSetException ex) {
                System.err.println(ex.getMessage());
                ex.printStackTrace(System.err);
            } catch (QueryException qEx) {
                // System.err.println(qEx.getMessage()) ;
                throw new CmdException("Query Exeception", qEx);
            }
        });
    } catch (ARQInternalErrorException intEx) {
        System.err.println(intEx.getMessage());
        if (intEx.getCause() != null) {
            System.err.println("Cause:");
            intEx.getCause().printStackTrace(System.err);
            System.err.println();
        }
        intEx.printStackTrace(System.err);
    } catch (JenaException | CmdException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new CmdException("Exception", ex);
    }
}
Also used : CmdException(org.apache.jena.cmd.CmdException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) RiotException(org.apache.jena.riot.RiotException) CmdException(org.apache.jena.cmd.CmdException) ResultSetException(org.apache.jena.sparql.resultset.ResultSetException) NotFoundException(org.apache.jena.shared.NotFoundException) JenaException(org.apache.jena.shared.JenaException) TerminationException(org.apache.jena.cmd.TerminationException) RiotNotFoundException(org.apache.jena.riot.RiotNotFoundException) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) JenaException(org.apache.jena.shared.JenaException) TerminationException(org.apache.jena.cmd.TerminationException) ResultSetException(org.apache.jena.sparql.resultset.ResultSetException) TransactionalNull(org.apache.jena.sparql.core.TransactionalNull) Transactional(org.apache.jena.sparql.core.Transactional)

Example 5 with TerminationException

use of org.apache.jena.cmd.TerminationException in project jena by apache.

the class sse method exec.

@Override
protected void exec(Item item) {
    if (!print)
        return;
    if (item == null) {
        System.err.println("No expression");
        throw new TerminationException(9);
    }
    divider();
    IndentedWriter out = new IndentedWriter(System.out, lineNumbers);
    // Need to check if used.
    // PrefixMapping pmap = SSE.getDefaultPrefixMapWrite() ;
    PrefixMapping pmap = null;
    SerializationContext sCxt = new SerializationContext(pmap);
    ItemWriter.write(out, item, sCxt);
    // item.output(out) ;
    out.ensureStartOfLine();
    out.flush();
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(org.apache.jena.cmd.TerminationException)

Aggregations

TerminationException (org.apache.jena.cmd.TerminationException)5 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)4 CmdException (org.apache.jena.cmd.CmdException)3 RiotException (org.apache.jena.riot.RiotException)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 ModDataset (arq.cmdline.ModDataset)1 BindException (java.net.BindException)1 AssemblerException (org.apache.jena.assembler.exceptions.AssemblerException)1 ArgDecl (org.apache.jena.cmd.ArgDecl)1 CmdLineArgs (org.apache.jena.cmd.CmdLineArgs)1 FusekiException (org.apache.jena.fuseki.FusekiException)1 FusekiServer (org.apache.jena.fuseki.main.FusekiServer)1 Node (org.apache.jena.graph.Node)1 Dataset (org.apache.jena.query.Dataset)1 QueryParseException (org.apache.jena.query.QueryParseException)1 RiotNotFoundException (org.apache.jena.riot.RiotNotFoundException)1 JenaException (org.apache.jena.shared.JenaException)1 NotFoundException (org.apache.jena.shared.NotFoundException)1 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)1 Op (org.apache.jena.sparql.algebra.Op)1