Java Code Examples for org.apache.jena.sparql.engine.binding.Binding#vars()

The following examples show how to use org.apache.jena.sparql.engine.binding.Binding#vars() . 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: RootPlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private List<Var> getVariables(List<Binding> values) {
	Set<Var> variables = new HashSet<>();
	if (values != null) {
		for (Binding binding : values) {
			for (Iterator<Var> it = binding.vars(); it.hasNext();) {
				variables.add(it.next());
			}
		}
	}
	List<Var> signature = query.getSignature();
	if (signature == null) {
		return new ArrayList<>(variables);
	}
	if (!signature.containsAll(variables)) {
		Set<Var> superfluousVars = variables.stream().filter((v) -> !signature.contains(v))
				.collect(Collectors.toSet());
		throw new SPARQLExtException(String.format(
				"The given input bindings use variables that are not in the signature: %s are not in the signature %s",
				superfluousVars, signature));

	}
	return new ArrayList<>(signature);
}
 
Example 2
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private static void addCompressedToElementData(ElementData el, Binding b) {
    final Binding compressed = compress(b);
    final Iterator<Var> varsIterator = compressed.vars();
    while (varsIterator.hasNext()) {
        el.add(varsIterator.next());
    }
    el.add(compressed);
}
 
Example 3
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Binding compress(Binding input) {
    final List<Var> vars = new ArrayList<>();
    final Iterator<Var> varsIterator = input.vars();
    while (varsIterator.hasNext()) {
        vars.add(varsIterator.next());
    }
    return compress(vars, input);
}
 
Example 4
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Turns a Binding into a QuerySolutionMap.
 * @param binding  the Binding to convert
 * @return a QuerySolutionMap
 */
public static QuerySolutionMap asQuerySolutionMap(Binding binding) {
	QuerySolutionMap map = new QuerySolutionMap();
	Iterator<Var> vars = binding.vars();
	while(vars.hasNext()) {
		Var var = vars.next();
		Node node = binding.get(var);
		if(node != null) {
			map.add(var.getName(), dummyModel.asRDFNode(node));
		}
	}
	return map;
}
 
Example 5
Source File: Helpers.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static Binding removePseudoVars(Binding binding) {
	BindingHashMap result = new BindingHashMap();
	Iterator<Var> it = binding.vars();
	while (it.hasNext()) {
		Var var = it.next();
		if (var.equals(TarqlQuery.ROWNUM)) continue;
		result.add(var, binding.get(var));
	}
	return result;
}
 
Example 6
Source File: JenaGraphSearcher.java    From Stargraph with MIT License 4 votes vote down vote up
private Map<String, List<LabeledEntity>> doSparqlQuery(String sparqlQuery) {
    logger.debug(marker, "Executing: {}", sparqlQuery);

    long startTime = System.currentTimeMillis();

    Map<String, List<LabeledEntity>> result = new LinkedHashMap<>();

    try (QueryExecution qexec = QueryExecutionFactory.create(sparqlQuery, graphModel)) {
        ResultSet results = qexec.execSelect();

        while (results.hasNext()) {
            Binding jBinding = results.nextBinding();
            Iterator<Var> vars = jBinding.vars();
            while (vars.hasNext()) {
                Var jVar = vars.next();

                if (!jBinding.get(jVar).isLiteral()) {
                    String id = jBinding.get(jVar).getURI();
                    List<LabeledEntity> entities = result.computeIfAbsent(jVar.getVarName(), (v) -> new ArrayList<>());
                    LabeledEntity labeledEntity = ns.isFromMainNS(id) ? entitySearcher.getEntity(dbId, id) : ModelUtils.createInstance(id);
                    entities.add(labeledEntity);
                } else {
                    LiteralLabel lit = jBinding.get(jVar).getLiteral();
                    ValueEntity valueEntity = new ValueEntity(lit.getLexicalForm(), lit.getDatatype().getURI(), lit.language());
                    result.computeIfAbsent(jVar.getVarName(), (v) -> new ArrayList<>()).add(valueEntity);
                }
            }
        }

    }
    long millis = System.currentTimeMillis() - startTime;

    if (!result.isEmpty()) {
        logger.info(marker, "SPARQL {} query took {}s", sparqlQuery, millis / 1000.0);
    }
    else {
        logger.warn(marker, "No matches for {}", sparqlQuery);
    }

    return result;
}