org.apache.jena.sparql.algebra.Op Java Examples
The following examples show how to use
org.apache.jena.sparql.algebra.Op.
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: AlgebraEx.java From xcurator with Apache License 2.0 | 6 votes |
public static void main(String []args) { String s = "SELECT DISTINCT ?s { ?s ?p ?o }"; // Parse Query query = QueryFactory.create(s) ; System.out.println(query) ; // Generate algebra Op op = Algebra.compile(query) ; op = Algebra.optimize(op) ; System.out.println(op) ; // Execute it. QueryIterator qIter = Algebra.exec(op, ExQuerySelect1.createModel()) ; // Results for ( ; qIter.hasNext() ; ) { Binding b = qIter.nextBinding() ; System.out.println(b) ; } qIter.close() ; }
Example #2
Source File: RuleforJena.java From quetzal with Eclipse Public License 2.0 | 6 votes |
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 #3
Source File: Drivers.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public static void runRepair(URL datasetURL, String queryFile, SparqlSelectResult result) throws URISyntaxException, MalformedURLException, ParserConfigurationException, SAXException, IOException { Query q = JenaUtil.parse(queryFile); List<Var> vars = q.getProjectVars(); UniverseFactory uf = new ComparisonUniverse(datasetURL); SolutionRelation r = null; if (result != null) { uf.addSolution(r = new SolutionRelation(result, vars, Collections.<String,Object>emptyMap())); } Op query = JenaUtil.compile(q); JenaTranslator jt = r==null? JenaTranslator.make(vars, query, uf): JenaTranslator.make(vars, query, uf, r); Pair<Formula, Pair<Formula, Formula>> answer = jt.translateSingle(Collections.<String,Object>emptyMap(), false).iterator().next(); Formula minDiff = QuadTableRelations.quads.union(QuadTableRelations.desiredQuads).count().minus( QuadTableRelations.quads.intersection(QuadTableRelations.desiredQuads).count()).lte(IntConstant.constant(1)); answer = Pair.make(answer.fst.and(minDiff), Pair.make(answer.snd.fst.and(minDiff), answer.snd.snd.and(minDiff))); check(uf, answer, "solution"); }
Example #4
Source File: Drivers.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public static void runFabricate(String queryFile, SparqlSelectResult result) throws URISyntaxException, MalformedURLException, ParserConfigurationException, SAXException, IOException { Query q = JenaUtil.parse(queryFile); List<Var> vars = q.getProjectVars(); UniverseFactory uf = new BoundedUniverse(); SolutionRelation r = null; if (result != null) { uf.addSolution(r = new SolutionRelation(result, vars, Collections.<String,Object>emptyMap())); } Op query = JenaUtil.compile(q); JenaTranslator jt = r==null? JenaTranslator.make(vars, query, uf): JenaTranslator.make(vars, query, uf, r); Pair<Formula, Pair<Formula, Formula>> answer = jt.translateSingle(Collections.<String,Object>emptyMap(), false).iterator().next(); check(uf, answer, "solution"); }
Example #5
Source File: Drivers.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public static void run(String queryFile1, String queryFile2, boolean leftNonEmpty, boolean rightNonEmpty, int bound) throws URISyntaxException, MalformedURLException, IOException { UniverseFactory uf = new BoundedUniverse(); Query q1 = JenaUtil.parse(queryFile1); Op query1 = JenaUtil.compile(q1); Query q2 = JenaUtil.parse(queryFile2); Op query2 = JenaUtil.compile(q2); JenaTranslator jt = JenaTranslator.make(q1.getProjectVars(), Arrays.asList(query1, query2), uf, null); Pair<Formula, Pair<Formula, Formula>> answer = jt.translateMulti(leftNonEmpty, rightNonEmpty); if (bound > 0) { answer = Pair.make(answer.fst.and(QuadTableRelations.quads.count().lte(IntConstant.constant(bound))), answer.snd); } if (DEBUG) System.err.println(answer.fst); check(uf, answer, "solution"); }
Example #6
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private Pair<Relation,List<Pair<Variable,Domain>>> reifyOpAsRelation(Op rhs, TranslatorContext context) { List<Variable> rvs = getVariables(rhs, context); if (DEBUG) System.err.println(rhs + " -- " + context.top()); Set<String> privateNames = privateVariableNames(rhs, context.top()); for(Iterator<Variable> vs = rvs.iterator(); vs.hasNext(); ) { if (privateNames.contains(vs.next().name())) { vs.remove(); } } Set<String> constants = context.constants().keySet(); for(Iterator<Variable> vs = rvs.iterator(); vs.hasNext(); ) { if (constants.contains(vs.next().name())) { vs.remove(); } } rvs = sortVars(rvs); Pair<Relation, List<Pair<Variable, Domain>>> ret = reifyOpAsRelation(rhs, rvs); assert ret != null; return ret; }
Example #7
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private void visit(Op op, Continuation next) { if (context.getCurrentContinuation() == null) { context.setCurrentContinuation(next); } else { Continuation c = context.getCurrentContinuation(); context.setCurrentContinuation((TranslatorContext cc, Formula ff) -> { context.setCurrentContinuation(c); next.next(cc, ff); }); } if (op != context.top()) { Set<String> privateVars = privateVariableNames(op, context.top()); if (DEBUG && !privateVars.isEmpty()) { System.err.println("private vars " + privateVars + " for " + op); } } op.visit(this); }
Example #8
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private Set<String> privateVariableNames(Op inner, Op outer) { List<String> outerNames = getVariableNames(outer); List<String> innerNames = getVariableNames(inner); Set<String> result = HashSetFactory.make(innerNames); for(Iterator<String> vars = result.iterator(); vars.hasNext(); ) { String var = vars.next(); if (count(innerNames, var) < count(outerNames, var)) { vars.remove(); } } for (Variable v : projectedVars) { if (result.contains(v.name())) { result.remove(v.name()); } } return result; }
Example #9
Source File: ResolutionEngineForJena.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private Op createJoinOfOps(List<Op> alternatives) { Op union = null; if (alternatives.size() == 1) { return alternatives.get(0); } while (!alternatives.isEmpty()) { Op newunion = null; if (union == null) { newunion = OpJoin.create(alternatives.remove(0), alternatives.remove(0)); } else { newunion = OpJoin.create(alternatives.remove(0), union); } union = newunion; } return union; }
Example #10
Source File: ResolutionEngineForJena.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private Op createUnionOfOps(List<Op> alternatives) { Op union = null; if (alternatives.size() == 1) { return alternatives.get(0); } while (!alternatives.isEmpty()) { OpUnion newunion = null; if (union == null) { newunion = new OpUnion(alternatives.remove(0), alternatives.remove(0)); } else { newunion = new OpUnion(alternatives.remove(0), union); } union = newunion; } return union; }
Example #11
Source File: labelSearch.java From xcurator with Apache License 2.0 | 6 votes |
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt) { Var var2 = createNewVar() ; // Triple patterns for ?x rdfs:label ?hiddenVar ElementTriplesBlock elementBGP = new ElementTriplesBlock(); Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2) ; elementBGP.addTriple(t) ; // Regular expression for regex(?hiddenVar, "pattern", "i") Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i") ; ElementGroup elementGroup = new ElementGroup() ; elementGroup.addElement(elementBGP) ; elementGroup.addElement(new ElementFilter(regex)) ; // Compile it. // The better design is to build the Op structure programmatically, Op op = Algebra.compile(elementGroup) ; op = Algebra.optimize(op, execCxt.getContext()) ; return QC.execute(op, input, execCxt) ; }
Example #12
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private static Set<String> gatherVariableNames(Collection<Op> queries) { Set<String> result = HashSetFactory.make(); for (Op query : queries) { result.addAll(getVariableNames(query)); } return result; }
Example #13
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public BaseTranslatorContext(Map<String, Variable> vars, Map<String, Object> bindings, Expression activeGraph, Op top, boolean choices) { this.vars = vars; this.activeGraph = activeGraph; this.top = top; this.constants = bindings; this.choices = choices; }
Example #14
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private static List<String> getVariableNames(final Op query) { return new OpVariableVistor<String>(query, true) { @Override protected String processVar(Node v) { return v.getName(); } }.getResult(); }
Example #15
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private List<Variable> getVariables(Op query, TranslatorContext context) { final Map<String,Variable> vars = context.getVars(); return new OpVariableVistor<Variable>(query, false) { @Override protected Variable processVar(Node v) { return vars.get(v.getName()); } }.getResult(); }
Example #16
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private JenaTranslator(Map<String,Variable> allVars, List<Variable> projectedVars, Collection<Op> queries, UniverseFactory universe, SolutionRelation solution) { this.queries = queries; this.universe = universe; this.allVars = allVars; this.projectedVars = projectedVars; this.solution = solution; }
Example #17
Source File: RuleforJena.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public RuleforJena getFreshRule() throws Exception { RenameAllVars r = new RenameAllVars(); Op newAnt = NodeTransformLib.transform(r, antecedent); OpTriple newCons = (OpTriple) NodeTransformLib.transform(r, consequent); RuleforJena result = new RuleforJena(newAnt, newCons, this.id); return result; }
Example #18
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private static Map<String,Variable> makeAllVars(Collection<Op> queries) { Map<String,Variable> allVars = HashMapFactory.make(); List<String> varNames = new ArrayList<String>(gatherVariableNames(queries)); Collections.sort(varNames); for(String name : varNames) { allVars.put(name, Variable.unary(name)); } return allVars; }
Example #19
Source File: MyQueryEngine.java From xcurator with Apache License 2.0 | 5 votes |
@Override public QueryIterator eval(Op op, DatasetGraph dsg, Binding initial, Context context) { // Extension point: access possible to all the parameters for execution. // Be careful to deal with initial bindings. Transform transform = new MyTransform() ; op = Transformer.transform(transform, op) ; return super.eval(op, dsg, initial, context) ; }
Example #20
Source File: MyQueryEngine.java From xcurator with Apache License 2.0 | 5 votes |
@Override protected Op modifyOp(Op op) { // Extension point: possible place to alter the algebra expression. // Alternative to eval(). op = super.modifyOp(op) ; // op = Algebra.toQuadForm(op) ; return op ; }
Example #21
Source File: SparqlToGremlinCompiler.java From sparql-gremlin with Apache License 2.0 | 5 votes |
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 #22
Source File: JenaTranslator.java From quetzal with Eclipse Public License 2.0 | 4 votes |
private Pair<Relation,List<Pair<Variable,Domain>>> reifyOpAsRelation(Op rhs, Collection<Variable> neededVars) { if (! relations.containsKey(rhs)) { Set<String> rvs = gatherVariableNames(Collections.singleton(rhs)); if (context.constants() != null) { rvs.removeAll(context.constants().keySet()); } final Map<String,Variable> newVars = HashMapFactory.make(); for(Map.Entry<String,Variable> var : context.getVars().entrySet()) { if (rvs.contains(var.getKey())) { newVars.put(var.getKey(), var.getValue()); } } TranslatorContext save = context; context = new BaseTranslatorContext(newVars, context.constants(), context.getActiveGraph(), context.top(), context.explicitChoices()); visit(rhs, (TranslatorContext context1, Formula nr) -> { Set<Variable> rhsVars = ASTUtils.gatherVariables(nr); neededVars.retainAll(rhsVars); if (rhsVars.isEmpty() || neededVars.isEmpty()) { context = save; return; } Relation opRelation = Relation.nary("rel" + relationBindings.size(), neededVars.size()); Expression rel = scope(nr, context.getDynamicBinding(), neededVars); if (DEBUG) System.err.println(rel); relationBindings.add(opRelation.eq(rel)); universe.nodesRelation(opRelation); List<Pair<Variable,Domain>> vars = new LinkedList<Pair<Variable,Domain>>(); for(Variable v : neededVars) { vars.add(Pair.make(v, context.getDomain(v))); } relations.put(rhs, Pair.make(opRelation, vars)); if (DEBUG) System.err.println(relations.get(rhs)); context = save; }); } assert relations.containsKey(rhs); return relations.get(rhs); }
Example #23
Source File: AlgebraExec.java From xcurator with Apache License 2.0 | 4 votes |
public static void main (String[] argv) { String BASE = "http://example/" ; BasicPattern bp = new BasicPattern() ; Var var_x = Var.alloc("x") ; Var var_z = Var.alloc("z") ; // ---- Build expression bp.add(new Triple(var_x, NodeFactory.createURI(BASE+"p"), var_z)) ; Op op = new OpBGP(bp) ; //Expr expr = ExprUtils.parse("?z < 2 ") ; Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2)) ; op = OpFilter.filter(expr, op) ; // ---- Example setup Model m = makeModel() ; m.write(System.out, "TTL") ; System.out.println("--------------") ; System.out.print(op) ; System.out.println("--------------") ; // ---- Execute expression QueryIterator qIter = Algebra.exec(op, m.getGraph()) ; // -------- Either read the query iterator directly ... if ( false ) { for ( ; qIter.hasNext() ; ) { Binding b = qIter.nextBinding() ; Node n = b.get(var_x) ; System.out.println(NodeFmtLib.displayStr(n)) ; System.out.println(b) ; } qIter.close() ; } else { // -------- Or make ResultSet from it (but not both - reading an // iterator consumes the current solution) List<String> varNames = new ArrayList<String>() ; varNames.add("x") ; varNames.add("z") ; ResultSet rs = new ResultSetStream(varNames, m, qIter); ResultSetFormatter.out(rs) ; qIter.close() ; } System.exit(0) ; }
Example #24
Source File: RuleforJena.java From quetzal with Eclipse Public License 2.0 | 4 votes |
private RuleforJena(Op antecedent, OpTriple consequent, int id) { this.antecedent = antecedent; this.consequent = consequent; this.id = id; }
Example #25
Source File: RuleforJena.java From quetzal with Eclipse Public License 2.0 | 4 votes |
public Op getAntecedent() { return antecedent; }
Example #26
Source File: MyQueryEngine.java From xcurator with Apache License 2.0 | 4 votes |
@Override public Plan create(Op op, DatasetGraph dataset, Binding inputBinding, Context context) { // Shodul notbe called because acceept/Op is false throw new ARQInternalErrorException("MyQueryEngine: factory calleddirectly with an algebra expression") ; }
Example #27
Source File: MyQueryEngine.java From xcurator with Apache License 2.0 | 4 votes |
@Override public boolean accept(Op op, DatasetGraph dataset, Context context) { // Refuse to accept algebra expressions directly. return false ; }
Example #28
Source File: MyQueryEngine.java From xcurator with Apache License 2.0 | 4 votes |
@Override public Op transform(OpBGP opBGP) { return opBGP ; }
Example #29
Source File: ResolutionEngineForJena.java From quetzal with Eclipse Public License 2.0 | 4 votes |
/*** * 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 #30
Source File: ResolutionEngineForJena.java From quetzal with Eclipse Public License 2.0 | 4 votes |
public ResolutionVisitor(List<RuleforJena> rules) { this.rules = rules; this.visited = new HashSet<Op>(); }