Search in sources :

Example 51 with Var

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

the class ExprTransformExpand method transform.

@Override
public Expr transform(ExprFunctionN func, ExprList args) {
    ExprFunction f = func.getFunction();
    if (this.shouldExpand(f)) {
        UserDefinedFunctionDefinition def = this.definitions.get(f.getFunction().getFunctionIRI());
        UserDefinedFunction uFunc = (UserDefinedFunction) def.newFunctionInstance();
        //Need to watch out for the case where the arguments supplied to the invoked
        //function are in a different order to the arguments supplied to the defined
        //function
        //Thus we will build the list of arguments used to expand the inner function
        //manually
        List<Var> defArgs = def.getArgList();
        ExprList subArgs = new ExprList();
        for (int i = 0; i < args.size(); i++) {
            Expr arg = args.get(i);
            String var = arg.getVarName();
            if (var == null) {
                //Non-variable args may be passed as-is
                subArgs.add(arg);
            } else {
                //We then use the arg as-is to substitute
                if (i > defArgs.size())
                    throw new ExprBuildException("Unable to expand function dependency, the function <" + def.getUri() + "> is called but uses an argument ?" + var + " which is not an argument to the outer function");
                subArgs.add(arg);
            }
        }
        //Expand the function
        uFunc.build(def.getUri(), subArgs);
        return uFunc.getActualExpr();
    } else {
        return super.transform(func, args);
    }
}
Also used : ExprBuildException(org.apache.jena.sparql.sse.builders.ExprBuildException) Var(org.apache.jena.sparql.core.Var)

Example 52 with Var

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

the class SyntaxVarScope method fmtExprList.

private static String fmtExprList(VarExprList exprList) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (Var v : exprList.getVars()) {
        Expr e = exprList.getExpr(v);
        if (!first) {
            sb.append(" ");
        }
        first = false;
        sb.append("(").append(e).append(" AS ").append(v).append(")");
    }
    return sb.toString();
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var)

Example 53 with Var

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

the class QueryTransformOps method transformVarExprList.

// ** Mutates the VarExprList
private static void transformVarExprList(VarExprList varExprList, ExprTransform exprTransform) {
    Map<Var, Expr> map = varExprList.getExprs();
    for (Var v : varExprList.getVars()) {
        Expr e = varExprList.getExpr(v);
        ExprVar ev = new ExprVar(v);
        Expr ev2 = exprTransform.transform(ev);
        if (ev != ev2) {
            if (e != null)
                throw new ARQException("Can't substitute " + v + " because it's used as an AS variable");
            if (ev2.isConstant() || ev2.isVariable()) {
                // Convert to (substitute value AS ?var)
                map.put(v, ev2);
                continue;
            } else
                throw new ARQException("Can't substitute " + v + " because it's not a simple value: " + ev2);
        }
        if (e == null)
            continue;
        // Didn't change the variable.
        Expr e2 = ExprTransformer.transform(exprTransform, e);
        if (e != e2)
            // replace
            map.put(v, e2);
    }
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) ARQException(org.apache.jena.sparql.ARQException) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var)

Example 54 with Var

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

the class TransformElementLib method apply.

public static Node apply(Node n, ExprTransform exprTransform) {
    if (exprTransform == null)
        return n;
    Expr e = null;
    if (Var.isVar(n)) {
        Var v = Var.alloc(n);
        ExprVar expr = new ExprVar(v);
        e = exprTransform.transform(expr);
    } else {
        NodeValue nv = NodeValue.makeNode(n);
        e = exprTransform.transform(nv);
    }
    if (e instanceof ExprVar)
        return ((ExprVar) e).asVar();
    if (e instanceof NodeValue)
        return ((NodeValue) e).asNode();
    throw new InternalErrorException("Managed to turn a node " + n + " into " + e);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) NodeValue(org.apache.jena.sparql.expr.NodeValue) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Example 55 with Var

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

the class TestSemanticEquivalence method testAsAlgebra.

/**
     * Tests whether an algebra expression gives the same results when run both
     * with and without a given optimizer
     * 
     * @param algStr
     *            Algebra
     * @param ds
     *            Dataset
     * @param opt
     *            Optimizer
     * @param expected
     *            Expected number of results
     */
public static void testAsAlgebra(String algStr, Dataset ds, Symbol opt, int expected) {
    Op op = SSE.parseOp(algStr);
    List<String> vars = new ArrayList<>();
    for (Var v : OpVars.visibleVars(op)) {
        vars.add(v.getName());
    }
    // Track current state
    boolean isEnabled = ARQ.isTrue(opt);
    boolean isDisabled = ARQ.isFalse(opt);
    try {
        // Run first without optimization
        ARQ.set(opt, false);
        QueryEngineMain engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
        QueryIterator iter = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
        ResultSetRewindable rs = ResultSetFactory.makeRewindable(new ResultSetStream(vars, ModelFactory.createDefaultModel(), iter));
        if (expected != rs.size()) {
            System.err.println("Non-optimized results not as expected");
            TextOutput output = new TextOutput((SerializationContext) null);
            output.format(System.out, rs);
            rs.reset();
        }
        Assert.assertEquals(expected, rs.size());
        iter.close();
        // Run with optimization
        ARQ.set(opt, true);
        engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
        QueryIterator iterOpt = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext());
        ResultSetRewindable rsOpt = ResultSetFactory.makeRewindable(new ResultSetStream(vars, ModelFactory.createDefaultModel(), iterOpt));
        if (expected != rsOpt.size()) {
            System.err.println("Optimized results not as expected");
            TextOutput output = new TextOutput((SerializationContext) null);
            output.format(System.out, rsOpt);
            rsOpt.reset();
        }
        Assert.assertEquals(expected, rsOpt.size());
        iterOpt.close();
        Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
    } finally {
        // Restore previous state
        if (isEnabled) {
            ARQ.set(opt, true);
        } else if (isDisabled) {
            ARQ.set(opt, false);
        } else {
            ARQ.unset(opt);
        }
    }
}
Also used : TextOutput(org.apache.jena.sparql.resultset.TextOutput) Op(org.apache.jena.sparql.algebra.Op) QueryEngineMain(org.apache.jena.sparql.engine.main.QueryEngineMain) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Aggregations

Var (org.apache.jena.sparql.core.Var)264 Node (org.apache.jena.graph.Node)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)47 Binding (org.apache.jena.sparql.engine.binding.Binding)33 VarExprList (org.apache.jena.sparql.core.VarExprList)30 Op (org.apache.jena.sparql.algebra.Op)29 Expr (org.apache.jena.sparql.expr.Expr)28 Triple (org.apache.jena.graph.Triple)17 HashMap (java.util.HashMap)15 ContractTest (org.xenei.junit.contract.ContractTest)13 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)12 Query (org.apache.jena.query.Query)11 ExprList (org.apache.jena.sparql.expr.ExprList)11 SortCondition (org.apache.jena.query.SortCondition)10 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 HashSet (java.util.HashSet)9 Pair (org.apache.jena.atlas.lib.Pair)9 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9