Search in sources :

Example 26 with Var

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

the class SPARQLParser10 method Project.

public final void Project() throws ParseException {
    Var v;
    Expr expr;
    Node n;
    jj_consume_token(SELECT);
    getQuery().setQuerySelectType();
    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case DISTINCT:
        case REDUCED:
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case DISTINCT:
                    jj_consume_token(DISTINCT);
                    getQuery().setDistinct(true);
                    break;
                case REDUCED:
                    jj_consume_token(REDUCED);
                    getQuery().setReduced(true);
                    break;
                default:
                    jj_la1[4] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
            break;
        default:
            jj_la1[5] = jj_gen;
            ;
    }
    allowAggregatesInExpressions = true;
    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case VAR1:
        case VAR2:
            label_3: while (true) {
                v = Var();
                getQuery().addResultVar(v);
                getQuery().setQueryResultStar(false);
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case VAR1:
                    case VAR2:
                        ;
                        break;
                    default:
                        jj_la1[6] = jj_gen;
                        break label_3;
                }
            }
            break;
        case STAR:
            jj_consume_token(STAR);
            getQuery().setQueryResultStar(true);
            break;
        default:
            jj_la1[7] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
    }
    allowAggregatesInExpressions = false;
}
Also used : Var(org.apache.jena.sparql.core.Var)

Example 27 with Var

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

the class SingleQuadHolder method mapValue.

// convert variable if in map
private Node mapValue(Node n, Map<Var, Node> values) {
    Node retval = null;
    if (n.isVariable()) {
        Var v = Var.alloc(n);
        retval = values.get(v);
    }
    return retval == null ? n : retval;
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 28 with Var

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

the class ValuesHandler method addValueRow.

/**
	 * Add the values for the variables. There must be one value for each value
	 * var.
	 * 
	 * @param values
	 *            the collection of values to add.
	 * @return The builder for chaining.
	 */
public void addValueRow(Collection<Node> values) {
    if (values.size() != valuesTable.size()) {
        throw new IllegalArgumentException(String.format("Number of values (%s) must match number of columns %s", values.size(), valuesTable.size()));
    }
    Iterator<Node> iter = values.iterator();
    for (Var v : valuesTable.keySet()) {
        List<Node> lst = getNodesList(v);
        lst.add(iter.next());
    }
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 29 with Var

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

the class ValuesHandler method addAll.

/**
	 * Add the ValuesHandler values to this values Handler.
	 * 
	 * @param handler
	 *            the handler that has the values to add.
	 */
public void addAll(ValuesHandler handler) {
    if (handler.valuesTable.size() == 0) {
        return;
    }
    // assume our table is square.
    int count = 0;
    if (valuesTable.size() > 0) {
        count = valuesTable.values().iterator().next().size();
    }
    for (Var var : handler.valuesTable.keySet()) {
        List<Node> lst = valuesTable.get(var);
        if (lst == null) {
            lst = new ArrayList<Node>();
            lst.addAll(Arrays.asList(new Node[count]));
            valuesTable.put(var, lst);
        }
        lst.addAll(handler.valuesTable.get(var));
    }
    // keep table square by adding nulls to the vars that are not in the
    // other table.
    List<Var> lst = new ArrayList<Var>(valuesTable.keySet());
    lst.removeAll(handler.valuesTable.keySet());
    if (!lst.isEmpty()) {
        count = handler.valuesTable.values().iterator().next().size();
        for (Var var : lst) {
            List<Node> lst2 = valuesTable.get(var);
            lst2.addAll(Arrays.asList(new Node[count]));
        }
    }
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList)

Example 30 with Var

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

the class DatasetHandler method setVars.

/**
	 * Set the variables for field names that contain lists of strings.
	 * 
	 * Strings that start with "?" are assumed to be variable names and will be replaced
	 * with matching node.toString() values. 
	 * 
	 * @param values The values to set.
	 * @param fieldName The field name in Query that contain a list of strings.
	 */
private void setVars(Map<Var, Node> values, List<String> lst) {
    if (values.isEmpty() || lst == null || lst.isEmpty()) {
        return;
    }
    for (int i = 0; i < lst.size(); i++) {
        String s = lst.get(i);
        Node n = null;
        if (s.startsWith("?")) {
            Var v = Var.alloc(s.substring(1));
            n = values.get(v);
            lst.set(i, n == null ? s : n.toString());
        }
    }
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

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