Java Code Examples for org.apache.jena.query.QuerySolution#varNames()

The following examples show how to use org.apache.jena.query.QuerySolution#varNames() . 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: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source File: NashornScriptEngine.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public Object invokeFunction(String functionName, QuerySolution bindings) throws javax.script.ScriptException, java.lang.NoSuchMethodException {
	List<String> functionParams = getFunctionParameters(functionName);
	Object[] params = new Object[functionParams.size()];
	Iterator<String> varNames = bindings.varNames();
	while(varNames.hasNext()) {
		String varName = varNames.next();
		int index = functionParams.indexOf(varName);
		if(index < 0) {
			index = functionParams.indexOf("$" + varName);
		}
		if(index >= 0) {
			RDFNode value = bindings.get(varName);
			if(value != null) {
				params[index] = JSFactory.asJSTerm(value.asNode());
			}
		}
	}
	return invokeFunctionOrdered(functionName, params);
}
 
Example 3
Source File: EvalUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Binding createBinding(QuerySolution sol) {
	final BindingMap binding = BindingFactory.create();
	for (Iterator<String> it = sol.varNames(); it.hasNext();) {
		final String varName = it.next();
		binding.add(VarUtils.allocVar(varName), sol.get(varName).asNode());
	}
	return binding;
}
 
Example 4
Source File: VarUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static List<Var> getVariables(
        final QuerySolution sol) {
    final List<Var> variables = new ArrayList<>();
    for (Iterator<String> it = sol.varNames(); it.hasNext();) {
        variables.add(allocVar(it.next()));
    }
    return variables;
}
 
Example 5
Source File: VarUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Binding getBinding(
        final QuerySolution sol) {
    final BindingMap binding = BindingFactory.create();
    for (Iterator<String> it = sol.varNames(); it.hasNext();) {
        String varName = it.next();
        binding.add(allocVar(varName), sol.get(varName).asNode());
    }
    return binding;
}
 
Example 6
Source File: SPARQLSubstitutions.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static QueryExecution createQueryExecution(Query query, Dataset dataset, QuerySolution bindings) {
	if(USE_TRANSFORM && bindings != null) {
		Map<Var,Node> substitutions = new HashMap<Var,Node>();
		Iterator<String> varNames = bindings.varNames();
		while(varNames.hasNext()) {
			String varName = varNames.next();
			substitutions.put(Var.alloc(varName), bindings.get(varName).asNode());
		}
		Query newQuery = JenaUtil.queryWithSubstitutions(query, substitutions);
		return ARQFactory.get().createQueryExecution(newQuery, dataset);
	}
	else {
		return ARQFactory.get().createQueryExecution(query, dataset, bindings);
	}
}
 
Example 7
Source File: QUEPY.java    From NLIWOD with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Overriding Original search method to implement Quepy's two step requests for
 * QA
 */
@Override
public void search(IQuestion question, String language) throws Exception {
	String questionString;
	if (!question.getLanguageToQuestion().containsKey(language)) {
		return;
	}
	questionString = question.getLanguageToQuestion().get(language);
	log.debug(this.getClass().getSimpleName() + ": " + questionString);
	this.getParamMap().put(this.getQueryKey(), questionString);
	if (this.setLangPar) {
		this.getParamMap().put(this.getLangKey(), language);
	}
	HttpResponse response = this.getIsPostReq() ? fetchPostResponse(getUrl(), getParamMap())
			: fetchGetResponse(getUrl(), getParamMap());
	// Test if error occured
	if (response.getStatusLine().getStatusCode() >= 400) {
		throw new Exception("QUEPY Server could not answer due to: " + response.getStatusLine());
	}
	// Fetch the SPARQL
	String sparqlStr = null;
	JSONParser parser = new JSONParser();

	JSONObject responsejson = (JSONObject) parser.parse(responseparser.responseToString(response));
	JSONArray queriesArr = (JSONArray) responsejson.get("queries");
	for (int i = 0; i < queriesArr.size(); i++) {
		JSONObject queryObj = (JSONObject) queriesArr.get(i);
		if (queryObj.get("language").toString().equalsIgnoreCase("sparql") && queryObj.get("query") != null) {
			sparqlStr = queryObj.get("query").toString();
			break;
		}
	}
	if (sparqlStr != null) {
		HashSet<String> result = new HashSet<String>();
		question.setSparqlQuery(sparqlStr);
		// Fetch results using sparql
		Query query = QueryFactory.create(sparqlStr);
		// Remote execution.
		QueryExecution qexec = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, query);
		// Set the DBpedia specific timeout.
		((QueryEngineHTTP) qexec).addParam("timeout", "10000");
		// Execute.
		ResultSet rs = qexec.execSelect();
		// Get the values and push them to the question
		while (rs.hasNext()) {
			QuerySolution qs = rs.next();
			Iterator<String> varIt = qs.varNames();
			while (varIt.hasNext()) {
				RDFNode node = qs.get(varIt.next());
				result.add(node.asLiteral().getString());
			}
		}
		question.setGoldenAnswers(result);
	}
}