org.apache.jena.sparql.algebra.OpWalker Java Examples

The following examples show how to use org.apache.jena.sparql.algebra.OpWalker. 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: SparqlToGremlinCompiler.java    From sparql-gremlin with Apache License 2.0 5 votes vote down vote up
GraphTraversal<Vertex, ?> convertToGremlinTraversal(final Query query) {
    final Op op = Algebra.compile(query);
    OpWalker.walk(op, this);
    if (!query.isQueryResultStar()) {
        final List<String> vars = query.getResultVars();
        switch (vars.size()) {
            case 0:
                throw new IllegalStateException();
            case 1:
                if (query.isDistinct()) {
                    traversal = traversal.dedup(vars.get(0));
                }
                traversal = traversal.select(vars.get(0));
                break;
            case 2:
                if (query.isDistinct()) {
                    traversal = traversal.dedup(vars.get(0), vars.get(1));
                }
                traversal = traversal.select(vars.get(0), vars.get(1));
                break;
            default:
                final String[] all = new String[vars.size()];
                vars.toArray(all);
                if (query.isDistinct()) {
                    traversal = traversal.dedup(all);
                }
                final String[] others = Arrays.copyOfRange(all, 2, vars.size());
                traversal = traversal.select(vars.get(0), vars.get(1), others);
                break;
        }
    } else {
        if (query.isDistinct()) {
            traversal = traversal.dedup();
        }
    }
    return traversal;
}
 
Example #2
Source File: SparqlToGremlinCompiler.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * Walking OP for Optional in SPARQL algebra.
 */
private void optionalVisit(final Op op) {

    OpWalker.walk(op, this);
}
 
Example #3
Source File: OpVariableVistor.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public OpVariableVistor(Op query2, boolean duplicates) {
	this.query = query2;
	this.duplicates = duplicates;
	OpWalker.walk(query2, this);
}
 
Example #4
Source File: OpVariableVistor.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public OpVariableVistor(Op query2, boolean duplicates) {
	this.query = query2;
	this.duplicates = duplicates;
	OpWalker.walk(query2, this);
}
 
Example #5
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;

}