org.apache.jena.sparql.core.Var Java Examples
The following examples show how to use
org.apache.jena.sparql.core.Var.
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: NodeExprNormalizer.java From sparql-generate with Apache License 2.0 | 6 votes |
/** * {@inheritDoc */ @Override public Object visit(Node_List node) { if (cache.containsKey(node)) { result = cache.get(node); return null; } if (node.getExpr().isVariable()) { result = node; return null; } Var var = Var.alloc(node.getLabel()); final Expr expr = nzer.normalize(node.getExpr()); bindings.add(new ElementBind(var, expr)); Node_List nzedList = new Node_List(new ExprVar(var)); cache.put(node, nzedList); this.result = nzedList; return null; }
Example #2
Source File: SelectPlan.java From sparql-generate with Apache License 2.0 | 6 votes |
private Query createQuery(final SPARQLExtQuery select, final List<Var> variables, final List<Binding> values, final Context context) { // SPARQLExtQuery q = select.cloneQuery(); Binding binding = !values.isEmpty() ? values.get(0) : null; SelectQueryPartialCopyVisitor cloner = new SelectQueryPartialCopyVisitor(binding, context); select.visit(cloner); Query q = cloner.getOutput(); if (!isSelectType && !q.hasGroupBy() && !q.hasAggregators()) { variables.forEach(v -> { if (!q.getProjectVars().contains(v)) { q.getProject().add(v); } }); } return q; }
Example #3
Source File: TemplatePlan.java From sparql-generate with Apache License 2.0 | 6 votes |
public void exec(List<Var> variables, List<Binding> values, Context context) { final IndentedWriter writer = ContextUtils.getTemplateOutput(context); boolean first = true; final FunctionEnv env = new FunctionEnvBase(context); String result; for(Iterator<Binding> it=values.iterator(); it.hasNext();) { Binding binding = it.next(); if (first && before != null) { result = getExprEval(before, binding, context, env); writer.print(result); } if (!first && separator != null) { result = getExprEval(separator, binding, context, env); writer.print(result); } result = getExprEval(expr, binding, context, env); writer.print(result); first = false; if (!it.hasNext() && after != null) { result = getExprEval(after, binding, context, env); writer.print(result); } writer.flush(); } }
Example #4
Source File: SelectExtractionVisitor.java From sparql-generate with Apache License 2.0 | 6 votes |
@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 #5
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 #6
Source File: Drivers.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private static Map<String, Object> tupleBindings(List<Var> vars, List<Object> t) { assert vars.size() == t.size(); Map<String, Object> result = HashMapFactory.make(); vars = JenaTranslator.sortVars(vars, new Function<Var,String>() { @Override public String apply(Var arg0) { return arg0.getVarName(); } }); for(int i = 0; i < vars.size(); i++) { result.put(vars.get(i).getVarName(), t.get(i)); } if (DEBUG) System.err.println(result); return result; }
Example #7
Source File: ElementTransformSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 6 votes |
public boolean canBeIsomorphic(Node n1, Node n2) { if ( (n1.isBlank() || Var.isBlankNodeVar(n1)) && (n2.isBlank() || Var.isBlankNodeVar(n2)) ) { final Node other = map.get(n1); if ( other == null ) { if ( valueSet.contains(n2) ) return false; map.put(n1, n2); valueSet.add(n2); return true; } return other.equals(n2); } return n1.equals(n2); }
Example #8
Source File: QueryExecutor.java From sparql-generate with Apache License 2.0 | 6 votes |
private List<Binding> getNewValues(String queryName, SPARQLExtQuery query, List<Var> signature, List<List<Node>> callParameters) { final int size = signature.size(); final List<Binding> bindings = new ArrayList<>(); for (List<Node> callParams : callParameters) { if (callParams.size() != size) { throw new SPARQLExtException("Query " + queryName + " called with " + callParams.size() + " parameters but accepts only " + size); } final BindingHashMap b = new BindingHashMap(); for (int i = 0; i < size; i++) { if(callParams.get(i) != null) { b.add(signature.get(i), callParams.get(i)); } } bindings.add(b); } return bindings; }
Example #9
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
/** * Turns a QuerySolution into a Binding. * @param map the input QuerySolution * @return a Binding or null if the input is null */ public static Binding asBinding(final QuerySolution map) { if(map != null) { BindingHashMap result = new BindingHashMap(); Iterator<String> varNames = map.varNames(); while(varNames.hasNext()) { String varName = varNames.next(); RDFNode node = map.get(varName); if(node != null) { result.add(Var.alloc(varName), node.asNode()); } } return result; } else { return null; } }
Example #10
Source File: SPARQLStarParser11.java From RDFstarTools with Apache License 2.0 | 6 votes |
final public Var Var() throws ParseException { Token t ; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case VAR1: t = jj_consume_token(VAR1); break; case VAR2: t = jj_consume_token(VAR2); break; default: jj_la1[125] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return createVariable(t.image, t.beginLine, t.beginColumn) ;} throw new Error("Missing return statement in function"); }
Example #11
Source File: CSVParser.java From tarql with BSD 2-Clause "Simplified" License | 6 votes |
private Var toVar(String s) { if (s == null) return null; /* SPARQL 1.1 VAR Gramar ? VARNAME ::= ( PN_CHARS_U | [0-9] ) ( PN_CHARS_U | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040] )* PN_CHARS_U ::= PN_CHARS_BASE | '_' PN_CHARS_BASE ::= [A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] I've omitted UTF-16 character range #x10000-#xEFFFF. */ String PN_CHARS_BASE = "A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD"; String pattern = PN_CHARS_BASE + "0-9\u00B7\u0300-\u036F\u203F-\u2040"; s = s.trim().replaceAll("[^" + pattern + "]", "_").replace(":", ""); if ("".equals(s)) return null; return Var.alloc(s); }
Example #12
Source File: RootPlan.java From sparql-generate with Apache License 2.0 | 6 votes |
private void execIteratorAndSourcePlans(final List<Var> variables, final List<Binding> values, final Context context, final int i) { if (i < iteratorAndSourcePlans.size()) { final BindingsClausePlan plan = iteratorAndSourcePlans.get(i); if (plan instanceof BindOrSourcePlan) { final BindOrSourcePlan bindOrSourcePlan = (BindOrSourcePlan) plan; variables.add(bindOrSourcePlan.getVar()); final List<Binding> newValues = bindOrSourcePlan.exec(values, context); execIteratorAndSourcePlans(variables, newValues, context, i + 1); LOG.debug("Finished plan " + bindOrSourcePlan); } else { IteratorPlan iteratorPlan = (IteratorPlan) plan; iteratorPlan.exec(variables, values, context, (newValues) -> { final List<Var> newVariables = new ArrayList<>(variables); newVariables.addAll(iteratorPlan.getVars()); execIteratorAndSourcePlans(newVariables, newValues, context, i + 1); LOG.debug("Finished batch for " + iteratorPlan); }); LOG.debug("Finished plan " + iteratorPlan); } } else { execSelectPlan(variables, values, context); } }
Example #13
Source File: localname.java From xcurator with Apache License 2.0 | 6 votes |
private QueryIterator execFixedSubject(Node nodeURI, Node nodeLocalname, Binding binding, ExecutionContext execCxt) { if ( ! nodeURI.isURI() ) // Subject bound but not a URI return QueryIterNullIterator.create(execCxt) ; // Subject is bound and a URI - get the localname as a Node Node localname = NodeFactory.createLiteral(nodeURI.getLocalName()) ; // Object - unbound variable or a value? if ( ! nodeLocalname.isVariable() ) { // Object bound or a query constant. Is it the same as the calculated value? if ( nodeLocalname.equals(localname) ) // Same return QueryIterSingleton.create(binding, execCxt) ; // No - different - no match. return QueryIterNullIterator.create(execCxt) ; } // Object unbound variable - assign the localname to it. return QueryIterSingleton.create(binding, Var.alloc(nodeLocalname), localname, execCxt) ; }
Example #14
Source File: localname.java From xcurator with Apache License 2.0 | 6 votes |
private QueryIterator execAllNodes(Var subjVar, Node nodeLocalname, Binding input, ExecutionContext execCxt) { if ( ! nodeLocalname.isVariable() ) { if ( ! nodeLocalname.isLiteral() ) // Not a variable, not a literal=> can't match return QueryIterNullIterator.create(execCxt) ; if( ! NodeUtils.isSimpleString(nodeLocalname) ) return QueryIterNullIterator.create(execCxt) ; } //Set bindings = new HashSet() ; // Use a Set if you want unique results. List<Binding> bindings = new ArrayList<Binding>() ; // Use a list if you want counting results. Graph graph = execCxt.getActiveGraph() ; ExtendedIterator<Triple>iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ; for ( ; iter.hasNext() ; ) { Triple t = iter.next() ; slot(bindings, input, t.getSubject(), subjVar, nodeLocalname) ; slot(bindings, input, t.getPredicate(), subjVar, nodeLocalname) ; slot(bindings, input, t.getObject(), subjVar, nodeLocalname) ; } return new QueryIterPlainWrapper(bindings.iterator(), execCxt) ; }
Example #15
Source File: PlanFactory.java From sparql-generate with Apache License 2.0 | 6 votes |
/** * Makes the plan for a SPARQL SOURCE clause. * * @param elementSource the SPARQL SOURCE * @return - */ private static BindOrSourcePlan makeSourcePlan( final ElementSource elementSource) throws SPARQLExtException { Objects.requireNonNull(elementSource, "The Source must not be null"); Node node = elementSource.getSource(); Node accept = elementSource.getAccept(); Var var = elementSource.getVar(); Objects.requireNonNull(node, "The source must not be null"); checkIsTrue(node.isURI() || node.isVariable(), "The source must be a" + " URI or a variable. Got " + node); // accept may be null checkIsTrue(accept == null || accept.isVariable() || accept.isURI(), "The accept must be null, a variable or a URI. Got " + accept); Objects.requireNonNull(var, "The variable must not be null."); return new SourcePlan(node, accept, var); }
Example #16
Source File: PlanFactory.java From sparql-generate with Apache License 2.0 | 6 votes |
/** * Makes the plan for a SPARQL ITERATOR clause. * * @param elementIterator the SPARQL ITERATOR * @return - */ static IteratorPlan makeIteratorPlan( final ElementIterator elementIterator) throws SPARQLExtException { Objects.requireNonNull(elementIterator, "The Iterator must not be null"); List<Var> vars = elementIterator.getVars(); Expr expr = elementIterator.getExpr(); Objects.requireNonNull(vars, "The variables of the Iterator must not be null"); Objects.requireNonNull(expr, "The Expr in the iterator must not be null"); checkIsTrue(expr.isFunction(), "Iterator should be a function:" + " <iri>(...) AS ?var1 ?var2 ..."); ExprFunction function = expr.getFunction(); String iri = function.getFunctionIRI(); ExprList exprList = new ExprList(function.getArgs()); return new IteratorPlan(iri, exprList, vars); }
Example #17
Source File: QueryBNodeNormalizer.java From sparql-generate with Apache License 2.0 | 6 votes |
private static Node replace(Node node, Set<String> assignNeeded, Map<String, Var> assignSuper, Map<String, Var> assign) { if (!node.isBlank()) { return node; } String label = node.getBlankNodeLabel(); if (!assignNeeded.contains(label)) { return node; } if (assignSuper.containsKey(label)) { return assignSuper.get(label); } if (assign.containsKey(label)) { return assign.get(label); } return node; }
Example #18
Source File: Query.java From quetzal with Eclipse Public License 2.0 | 6 votes |
@Override public List<SortCondition> getOrderBy() { List<SortCondition> conditions = new ArrayList<SortCondition>(); List<OrderCondition> orderConditions = selectQuery .getSolutionModifier().getOrderClause(); for (OrderCondition condition : orderConditions) { Var expr = null; int dir = ORDER_UNKNOW; if (condition.getType() == OrderCondition.EOrderType.ASC) { dir = ORDER_ASCENDING; } else if (condition.getType() == OrderCondition.EOrderType.DESC) { dir = ORDER_DESCENDING; } SortCondition sortCondition = new SortCondition(expr, dir); conditions.add(sortCondition); } return conditions.size() > 0 ? conditions : null; }
Example #19
Source File: LogUtils.java From sparql-generate with Apache License 2.0 | 6 votes |
private static ElementData compress(List<Var> variables, List<Binding> input) { ElementData el = new ElementData(); variables.forEach(el::add); if (input.size() < 10) { input.forEach((b) -> el.add(compress(variables, b))); return el; } for (int i = 0; i < 5; i++) { el.add(compress(variables, input.get(i))); } BindingMap binding = BindingFactory.create(); Node n = NodeFactory.createLiteral("[ " + (input.size() - 10) + " more ]"); variables.forEach((v) -> binding.add(v, n)); el.add(binding); for (int i = input.size() - 5; i < input.size(); i++) { el.add(compress(variables, input.get(i))); } return el; }
Example #20
Source File: SPARQLStarParser11.java From RDFstarTools with Apache License 2.0 | 6 votes |
final public Node VarOrIri() throws ParseException { Node n = null ; String iri ; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case VAR1: case VAR2: n = Var(); break; case IRIref: case PNAME_NS: case PNAME_LN: iri = iri(); n = createNode(iri) ; break; default: jj_la1[123] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return n ;} throw new Error("Missing return statement in function"); }
Example #21
Source File: PlanFactory.java From sparql-generate with Apache License 2.0 | 5 votes |
/** * Makes the plan for a SPARQL BIND clause. * * @param elementBind the SPARQL BIND * @return - */ private static BindOrSourcePlan makeBindPlan( final ElementBind elementBind) throws SPARQLExtException { Objects.requireNonNull(elementBind, "The Bind element must not be null"); Var var = elementBind.getVar(); Expr expr = elementBind.getExpr(); Objects.requireNonNull(var, "The source must not be null"); Objects.requireNonNull(expr, "The expression must not be null."); return new BindPlan(expr, var); }
Example #22
Source File: DB2ResultSetImpl.java From quetzal with Eclipse Public License 2.0 | 5 votes |
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 #23
Source File: SelectQueryPartialCopyVisitor.java From sparql-generate with Apache License 2.0 | 5 votes |
@Override public void visitFunctionExpression(SPARQLExtQuery query) { Var var = Var.alloc("out"); ElementBind bind = new ElementBind(var, query.getFunctionExpression()); output.setQueryPattern(bind); output.addResultVar(var); }
Example #24
Source File: SelectExtractionVisitor.java From sparql-generate with Apache License 2.0 | 5 votes |
@Override public void visitFunctionExpression(SPARQLExtQuery query) { Var var = Var.alloc("out"); ElementBind bind = new ElementBind(var, query.getFunctionExpression()); output.setQueryPattern(bind); output.addResultVar(var); isDummyQuery = false; }
Example #25
Source File: QueryExecutor.java From sparql-generate with Apache License 2.0 | 5 votes |
public void execSelectFromQuery( final SPARQLExtQuery query, final List<List<Node>> callParameters, final Context context) { Objects.nonNull(query); Objects.nonNull(callParameters); Objects.nonNull(context); final String queryName = LogUtils.compress(query.toString()); final RootPlan plan = getPlan(query); final List<Var> signature = getSignature(query); final List<Binding> newValues = getNewValues(queryName, query, signature, callParameters); execSelectPlan(plan, newValues, context); }
Example #26
Source File: RootPlan.java From sparql-generate with Apache License 2.0 | 5 votes |
/** * Executes a SELECT query and emits generated results to the stream. * * @param values * the values for the query signature, or null. * @param context * the execution context, created using {@link ContextUtils} * @param output * where to emit {@link ResultSet} objects. */ public void execSelectStream(final List<Binding> values, final Context context) { Objects.requireNonNull(context); Objects.requireNonNull(ContextUtils.getSelectOutput(context)); if (!query.isSelectType()) { throw new SPARQLExtException("Query is not a SELECT query."); } final List<Var> variables = getVariables(values); final List<Binding> newValues = getValues(variables, values); exec(variables, newValues, context); }
Example #27
Source File: SPARQLExtSyntaxVarScope.java From sparql-generate with Apache License 2.0 | 5 votes |
private static void checkTemplateClauseExpressions(SPARQLExtQuery query) { final List<Var> signature; if (query.getSignature() != null) { signature = query.getSignature(); } else { signature = new ArrayList<>(); } if (query.isTemplateType() && !query.isSubQuery()) { checkVarsInExpr(query.getTemplateClauseBefore(), signature, "Before expression should only have vars from the signature"); checkVarsInExpr(query.getTemplateClauseSeparator(), signature, "Separator expression should only have vars from the signature"); checkVarsInExpr(query.getTemplateClauseAfter(), signature, "After expression should only have vars from the signature"); // << except! if the query has no signature and is a sub-query. Then the vars should be in-scope of the super-query. >> } if(query.isSelectType() && !query.hasName()) { return; } if(!query.isSubQuery()) { for (FromClause fromClause : query.getFromClauses()) { if (fromClause.getName() != null) { checkVarsInExpr(fromClause.getName(), signature, "FROM expression should only have vars from the signature"); } if (fromClause.getGenerate() != null) { SPARQLExtQuery generate = fromClause.getGenerate(); if (generate.hasSignature()) { checkVarsInList(generate.getSignature(), signature, "Signature of FROM GENERATE should be a subset of the signature"); } if (generate.hasName()) { checkVarsInExpr(generate.getName(), signature, "Call parameters of FROM GENERATE should only have vars from the signature"); } if (generate.hasCallParameters()) { generate.getCallParameters().forEach((callParameter) -> { checkVarsInExpr(callParameter, signature, "Call parameters of FROM GENERATE should only have vars from the signature"); }); } } } } }
Example #28
Source File: QueryExecutor.java From sparql-generate with Apache License 2.0 | 5 votes |
/** * * @param queryName * @param callParameters never null * @param context */ public void execGenerateFromName( final String queryName, final List<List<Node>> callParameters, final Context context) { Objects.nonNull(queryName); Objects.nonNull(callParameters); Objects.nonNull(context); final RootPlan plan = getPlanFromName(queryName, context); final SPARQLExtQuery query = plan.getQuery(); final List<Var> signature = getSignature(query); final List<Binding> newValues = getNewValues(queryName, query, signature, callParameters); execGeneratePlan(plan, newValues, context); }
Example #29
Source File: RootPlan.java From sparql-generate with Apache License 2.0 | 5 votes |
/** * Executes a GENERATE query and emits generated triples to the stream. * * @param values * the values for the query signature, or null. * @param context * the execution context, created using {@link ContextUtils} * @param output * the RDF Stream object. */ public void execGenerateStream(final List<Binding> values, final Context context) { Objects.requireNonNull(context); Objects.requireNonNull(ContextUtils.getGenerateOutput(context)); if (!query.isGenerateType()) { throw new SPARQLExtException("Query is not a GENERATE query."); } final List<Var> variables = getVariables(values); final List<Binding> newValues = getValues(variables, values); exec(variables, newValues, context); }
Example #30
Source File: RuleSystemToUnionQuery.java From quetzal with Eclipse Public License 2.0 | 5 votes |
protected Node replace(Node old) { if (old!=null && old.isVariable()) { Var var = Var.alloc(old); Node newN = oldVar2NewValue.get(var); if (newN!=null) { return newN; } } return old; }