Search in sources :

Example 16 with ExprEvalException

use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.

the class BindingComparator method compare.

// Compare bindings by iterating.
// Node comparsion is:
//  Compare by 
@Override
public int compare(Binding bind1, Binding bind2) {
    for (SortCondition sc : conditions) {
        if (sc.expression == null) {
            throw new QueryExecException("Broken sort condition");
        }
        NodeValue nv1 = null;
        NodeValue nv2 = null;
        try {
            nv1 = sc.expression.eval(bind1, env);
        } catch (VariableNotBoundException ex) {
        } catch (ExprEvalException ex) {
            Log.warn(this, ex.getMessage());
        }
        try {
            nv2 = sc.expression.eval(bind2, env);
        } catch (VariableNotBoundException ex) {
        } catch (ExprEvalException ex) {
            Log.warn(this, ex.getMessage());
        }
        Node n1 = NodeValue.toNode(nv1);
        Node n2 = NodeValue.toNode(nv2);
        int x = compareNodes(nv1, nv2, sc.direction);
        if (x != Expr.CMP_EQUAL) {
            return x;
        }
    }
    // Same by the SortConditions - now do any extra tests to make sure they are unique.
    return compareBindingsSyntactic(bind1, bind2);
//return 0 ;
}
Also used : SortCondition(org.apache.jena.query.SortCondition) NodeValue(org.apache.jena.sparql.expr.NodeValue) VariableNotBoundException(org.apache.jena.sparql.expr.VariableNotBoundException) Node(org.apache.jena.graph.Node) QueryExecException(org.apache.jena.query.QueryExecException) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 17 with ExprEvalException

use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.

the class date method exec.

@Override
public NodeValue exec(NodeValue v) {
    if (!v.isString()) {
        Log.warn(this, "date: argument not a string: " + v);
        throw new ExprEvalException("date: argument not a string: " + v);
    }
    String lexicalForm = v.getString();
    // Quite picky about format
    if (!lexicalForm.matches("\\d{4}-\\d{2}-\\d{2}")) {
        Log.warn(this, "date: argument not in date format: " + v);
        throw new ExprEvalException("date: argument not in date format: " + v);
    }
    lexicalForm = lexicalForm + "T00:00:00Z";
    NodeValue nv = NodeValue.makeNode(lexicalForm, XSDDatatype.XSDdateTime);
    return nv;
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 18 with ExprEvalException

use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.

the class evenInteger method exec.

@Override
public NodeValue exec(NodeValue x) {
    if (!x.isInteger())
        throw new ExprEvalException("evenInteger: Not an intger: " + x);
    int i = x.getInteger().getLowestSetBit();
    boolean b = (i == -1) || (i != 0);
    return NodeValue.makeNodeBoolean(b);
}
Also used : ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 19 with ExprEvalException

use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.

the class factorial method exec.

@Override
public NodeValue exec(NodeValue v) {
    BigInteger i = v.getInteger();
    switch(i.compareTo(BigInteger.ZERO)) {
        case 0:
            // 0! is 1
            return NodeValue.makeInteger(BigInteger.ONE);
        case -1:
            // Negative factorial is error
            throw new ExprEvalException("Cannot evaluate a negative factorial");
        case 1:
            BigInteger res = i.add(BigInteger.ZERO);
            i = i.subtract(BigInteger.ONE);
            while (i.compareTo(BigInteger.ZERO) != 0) {
                res = res.multiply(i);
                i = i.subtract(BigInteger.ONE);
            }
            return NodeValue.makeInteger(res);
        default:
            throw new ExprEvalException("Unexpecte comparison result");
    }
}
Also used : BigInteger(java.math.BigInteger) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 20 with ExprEvalException

use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.

the class sprintf method exec.

@Override
public NodeValue exec(List<NodeValue> args) {
    if (args.size() < 2)
        throw new ExprEvalException(Lib.className(this) + ": Wrong number of arguments: " + args.size() + " : [wanted at least 2]");
    NodeValue v1 = args.get(0);
    List<NodeValue> allArgs = new ArrayList<>();
    for (int i = 1; i < args.size(); i++) allArgs.add(args.get(i));
    return XSDFuncOp.javaSprintf(v1, allArgs);
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) ArrayList(java.util.ArrayList) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Aggregations

ExprEvalException (org.apache.jena.sparql.expr.ExprEvalException)32 Node (org.apache.jena.graph.Node)14 NodeValue (org.apache.jena.sparql.expr.NodeValue)13 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)3 BigInteger (java.math.BigInteger)2 GNode (org.apache.jena.sparql.util.graph.GNode)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Duration (javax.xml.datatype.Duration)1 ArgDecl (jena.cmd.ArgDecl)1 CmdException (jena.cmd.CmdException)1 CmdLineArgs (jena.cmd.CmdLineArgs)1 TerminationException (jena.cmd.TerminationException)1 Graph (org.apache.jena.graph.Graph)1 IRI (org.apache.jena.iri.IRI)1 Violation (org.apache.jena.iri.Violation)1 QueryExecException (org.apache.jena.query.QueryExecException)1 QueryParseException (org.apache.jena.query.QueryParseException)1 SortCondition (org.apache.jena.query.SortCondition)1 PrefixMapping (org.apache.jena.shared.PrefixMapping)1