org.apache.jena.sparql.core.VarExprList Java Examples

The following examples show how to use org.apache.jena.sparql.core.VarExprList. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryXExprNormalizer.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visitGroupBy(Query query) {
    if (query.hasGroupBy() && !query.getGroupBy().isEmpty()) {
        // Can have an empty GROUP BY list if the grouping is implicit
        // by use of an aggregate in the SELECT clause.
        final VarExprList namedExprs = query.getGroupBy();
        final VarExprList newNamedExprs = new VarExprList();
        for (Var var : namedExprs.getVars()) {
            if (namedExprs.hasExpr(var)) {
                final Expr nzed = enzer.normalize(namedExprs.getExpr(var));
                newNamedExprs.add(var, nzed);
            } else {
                newNamedExprs.add(var);
            }
        }
        namedExprs.clear();
        namedExprs.addAll(newNamedExprs);
    }
}
 
Example #2
Source File: SelectQueryPartialCopyVisitor.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visitGroupBy(final Query query) {
    if (query.hasGroupBy()) {
        if (!query.getGroupBy().isEmpty()) {
            VarExprList namedExprs = query.getGroupBy();
            for (Var var : namedExprs.getVars()) {
                Expr expr = namedExprs.getExpr(var);
                if (expr != null) {
                    output.addGroupBy(var, expr);
                } else {
                    output.addGroupBy(var.getVarName());
                }
            }
        }
    }
}
 
Example #3
Source File: SelectExtractionVisitor.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visitGroupBy(final Query query) {
    if (query.hasGroupBy()) {
    	isDummyQuery = false;
        if (!query.getGroupBy().isEmpty()) {
            VarExprList namedExprs = query.getGroupBy();
            for (Var var : namedExprs.getVars()) {
                Expr expr = namedExprs.getExpr(var);
                if (expr != null) {
                    output.addGroupBy(var, expr);
                    if (!query.isSelectType()) {
                        output.addResultVar(var);
                    }
                } else {
                    output.addGroupBy(var.getVarName());
                    if (!query.isSelectType()) {
                        output.addResultVar(var);
                    }
                }
            }
        }
    }
}
 
Example #4
Source File: RuleforJena.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public RuleforJena(Query constructQuery, int id) {
	this.id = id;
	antecedent = Algebra.compile(constructQuery);
	// KAVITHA: the consequent should be a single triple
	List<Triple> list = constructQuery.getConstructTemplate().getBGP()
			.getList();
	consequent = new OpTriple(list.get(0));

	if (SPARQLRewriterForJena.GENERATE_TRACE == true) {

		Op bind = null;
		VarExprList expr = new VarExprList();
		expr.add(
				Var.alloc("RULEID"),
				new NodeValueNode(NodeFactory.createLiteral(String
						.valueOf(id))));
		bind = OpExtend.extend(antecedent, expr);
		antecedent = bind;
	}

}
 
Example #5
Source File: QueryXExprNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visitSelectResultForm(Query query) {
    final VarExprList project = query.getProject();
    final VarExprList newProject = new VarExprList();
    project.getVars().forEach((var) -> {
        if (project.hasExpr(var)) {
            final Expr nzed = enzer.normalize(project.getExpr(var));
            newProject.add(var, nzed);
        } else {
            newProject.add(var);
        }
    });
    project.clear();
    project.addAll(newProject);
}
 
Example #6
Source File: QueryXExprNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visitPostSelect(SPARQLExtQuery query) {
    final VarExprList postSelect = query.getPostSelect();
    final VarExprList newProject = new VarExprList();
    postSelect.getVars().forEach((var) -> {
        if (postSelect.hasExpr(var)) {
            final Expr nzed = enzer.normalize(postSelect.getExpr(var));
            newProject.add(var, nzed);
        } else {
            newProject.add(var);
        }
    });
    postSelect.clear();
    postSelect.addAll(newProject);
}
 
Example #7
Source File: SelectQueryPartialCopyVisitor.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visitSelectResultForm(final Query q) {
    SPARQLExtQuery query = asSPARQLExtQuery(q);
    output.setDistinct(query.isDistinct());
    output.setReduced(query.isReduced());
    output.setQueryResultStar(query.isQueryResultStar());
    VarExprList project = query.getProject();
    project.forEachVar((v) -> {
        output.addResultVar(v, project.getExpr(v));
    });
}
 
Example #8
Source File: SelectExtractionVisitor.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visitSelectResultForm(final Query q) {
    SPARQLExtQuery query = asSPARQLExtQuery(q);
    output.setName(query.getName());
    output.setSignature(query.getSignature());
    output.setCallParameters(query.getCallParameters());
    output.setDistinct(query.isDistinct());
    output.setReduced(query.isReduced());
    output.setQueryResultStar(query.isQueryResultStar());
    VarExprList project = query.getProject();
    project.forEachVar((v) -> {
        output.addResultVar(v, project.getExpr(v));
    });
}
 
Example #9
Source File: Query.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public VarExprList getGroupBy() {
	VarExprList list = new VarExprList();
	GroupCondition condition = selectQuery.getSolutionModifier()
			.getGroupClause();
	if (condition != null) {
		List<Expression> expressions = condition.getConditions();
		for (Expression expression : expressions) {
			// Set<Variable> variables = expression.gatherVariables();
			// System.out.println(expression.getType());
		}
	}
	return list;
}
 
Example #10
Source File: Query.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public VarExprList getProject() {
	List<ProjectedVariable> variables = selectQuery.getSelectClause()
			.getProjectedVariables();
	VarExprList list = new VarExprList();
	for (ProjectedVariable variable : variables) {
		Var var = Var.alloc(variable.getVariable().getName());
		list.add(var);
	}

	return list;
}
 
Example #11
Source File: DB2ResultSetImpl.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public DB2ResultSetImpl(LiteralInfoResultSet rs, Store store, Connection c, List<String> list, VarExprList varExprList)
{
liRs = rs;
set = rs.getResultSet();
try {
 ResultSetMetaData rsMetaData = set.getMetaData();
 int numberOfColumns = rsMetaData.getColumnCount();
	
 // get the column names; column indexes start from 1
 for (int i = 1; i <= numberOfColumns; i++) {
     columnNames.add(rsMetaData.getColumnName(i));
    
 }
} catch (SQLException e) {
 e.printStackTrace();
 throw new RuntimeException("Error getting result metadata");
}
this.store = store;
connection = c;

if (varExprList.isEmpty())
   {
   varList = list;
   }
else
   {
   List<Var> vars = varExprList.getVars();
   varList = new ArrayList<String>();
   for (int i = 0; i < vars.size(); i++)
      {
      varList.add(vars.get(i).getName());
      }
   }
}
 
Example #12
Source File: SPARQLExtQuery.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public VarExprList getPostSelect() {
	return postSelect;
}
 
Example #13
Source File: SPARQLExtQuerySerializer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
void appendNamedExprList(Query query, IndentedWriter sb, VarExprList namedExprs) {
    boolean first = true;
    for (Var var : namedExprs.getVars()) {
        Expr expr = namedExprs.getExpr(var);
        if (!first) {
            sb.print(" ");
        }

        if (expr != null) {
            // The following are safe to write without () 
            // Compare/merge with fmtExpr.format
            boolean needParens = true;

            if (expr.isFunction()) {
                needParens = false;
            } //                else if ( expr instanceof E_Aggregator )
            //                    // Aggregators are variables (the function maps to an internal variable 
            //                    // that is accesses by the E_Aggregator
            //                    needParens = false ;
            else if (expr.isVariable()) {
                needParens = false;
            }

            if (!Var.isAllocVar(var)) // AS ==> need parens
            {
                needParens = true;
            }

            if (needParens) {
                out.print("(");
            }
            fmtExpr.format(expr);
            if (!Var.isAllocVar(var)) {
                sb.print(" AS ");
                sb.print(var.toString());
            }
            if (needParens) {
                out.print(")");
            }
        } else {
            sb.print(var.toString());
        }
        first = false;
    }
}
 
Example #14
Source File: ResolutionEngineForJena.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/***
 * Takes a SELECT query and unroll it into a more complex SPARQL query under
 * rules
 * 
 * @param query
 */
public Query unfold(Query query) {

	ResolutionVisitor visitor = new ResolutionVisitor(rules);
	Op body = Algebra.compile(query);

	OpVariableVistor<String> collector = new OpVariableVistor<String>(body,
			true) {
		@Override
		protected String processVar(Node v) {
			return v.getName();
		}
	};
	OpWalker.walk(body, collector);

	Set<String> bindingNames = new HashSet<String>(collector.getResult());

	visitor.setBindingNames(bindingNames);

	Op newQuery = body;
	newQuery = Transformer.transform(visitor, body);

	
	VarExprList expr = new VarExprList();	
//	E_StrConcat trace = getRuleTracingConcat(newQuery);
	E_StrConcat trace = null;
	
	if (trace!=null) {
		expr.add(Var.alloc("trace"),trace);		
		newQuery = OpExtend.extend(newQuery, expr);
		newQuery = OpJoin.create(newQuery, OpTable.unit());
		
		if (SPARQLRewriterForJena.GENERATE_TRACE_SUMMARY) {
			
			VarExprList groupVars = new VarExprList();
			groupVars.add(Var.alloc("trace"));
			AggCount count = new AggCount();
			ExprAggregator aggregators = new ExprAggregator(Var.alloc("trace.sum"), count);
			List<ExprAggregator> list = new LinkedList<ExprAggregator>();
			list.add(aggregators);
			
			Op groupBy = new OpGroup(newQuery, groupVars, list);
			
			VarExprList expr2 = new VarExprList();	
			expr2.add(Var.alloc("sum"), new ExprVar("trace.sum"));			
			groupBy = OpExtend.extend(groupBy, expr2);
			List<Var> vars = new LinkedList<Var>();
			vars.add(Var.alloc("sum"));
			vars.add(Var.alloc("trace"));
			OpProject project = new OpProject(groupBy, vars);
			newQuery = project;
			
			
		}

	}
	
	
	
	
	
	Query q = OpAsQuery.asQuery(newQuery);
	q.setDistinct(true);	// KAVITHA: Not setting distinct on the consequent leaves duplicates
	return q;

}
 
Example #15
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public QWRRV_VarExprList(VarExprList other) {
	super(other);
}