Search in sources :

Example 6 with Var

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

the class QueryTest method convertToStrings.

private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) {
    List<Binding> bindings = new ArrayList<>();
    while (resultsActual.hasNext()) {
        Binding b = resultsActual.nextBinding();
        BindingMap b2 = BindingFactory.create();
        for (String vn : resultsActual.getResultVars()) {
            Var v = Var.alloc(vn);
            Node n = b.get(v);
            String s;
            if (n == null)
                s = "";
            else if (n.isBlank())
                s = "_:" + n.getBlankNodeLabel();
            else
                s = NodeFunctions.str(n);
            b2.add(v, NodeFactory.createLiteral(s));
        }
        bindings.add(b2);
    }
    ResultSet rs = new ResultSetStream(resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator()));
    return ResultSetFactory.makeRewindable(rs);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 7 with Var

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

the class TestSolverTDB method exec.

private static ResultSet exec(String pattern, Graph graph) {
    Op op = SSE.parseOp(pattern, pmap);
    List<Var> vars = new ArrayList<>();
    vars.addAll(OpVars.visibleVars(op));
    QueryIterator qIter = Algebra.exec(op, graph);
    return ResultSetFactory.create(qIter, Var.varNames(vars));
}
Also used : Op(org.apache.jena.sparql.algebra.Op) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList)

Example 8 with Var

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

the class TableWriter method output.

private static void output(Binding binding, IndentedWriter out, SerializationContext sCxt) {
    out.print("(row");
    for (Iterator<Var> iter = binding.vars(); iter.hasNext(); ) {
        Var v = iter.next();
        Node n = binding.get(v);
        out.print(" ");
        out.print(Plan.startMarker2);
        out.print(FmtUtils.stringForNode(v));
        out.print(" ");
        out.print(FmtUtils.stringForNode(n));
        out.print(Plan.finishMarker2);
    }
    out.print(")");
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 9 with Var

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

the class GraphSPARQLService method graphBaseFind.

//    @Override
//    public Capabilities getCapabilities()
//    { 
//    	if (capabilities == null)
//            capabilities = new AllCapabilities()
//        	  { @Override public boolean handlesLiteralTyping() { return false; } };
//        return capabilities;
//    }
@Override
protected ExtendedIterator<Triple> graphBaseFind(Triple m) {
    Node s = m.getMatchSubject();
    Var sVar = null;
    if (s == null) {
        sVar = Var.alloc("s");
        s = sVar;
    }
    Node p = m.getMatchPredicate();
    Var pVar = null;
    if (p == null) {
        pVar = Var.alloc("p");
        p = pVar;
    }
    Node o = m.getMatchObject();
    Var oVar = null;
    if (o == null) {
        oVar = Var.alloc("o");
        o = oVar;
    }
    Triple triple = new Triple(s, p, o);
    // Evaluate as an algebra expression
    BasicPattern pattern = new BasicPattern();
    pattern.add(triple);
    Op op = new OpBGP(pattern);
    //        return WrappedIterator.createNoRemove(triples.iterator()) ;
    return null;
}
Also used : Triple(org.apache.jena.graph.Triple) Op(org.apache.jena.sparql.algebra.Op) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 10 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)

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