Java Code Examples for org.apache.jena.query.ResultSet#getResultVars()

The following examples show how to use org.apache.jena.query.ResultSet#getResultVars() . 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: SPARQLDataReader.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private void parseResultSet(DataStore dataStore, MetaData dataStoreMeta, ResultSet resultSet) {
	List<String> columnNames = resultSet.getResultVars();
	for (; resultSet.hasNext();) {
		QuerySolution row = resultSet.nextSolution();
		IRecord record = new Record(dataStore);
		for (int i = 0; i < columnNames.size(); i++) {
			IFieldMetaData fieldMeta = dataStoreMeta.getFieldMeta(i);

			String columnName = columnNames.get(i);
			RDFNode rdfNode = row.get(columnName);
			getValue(rdfNode, record);
			getMetaData(rdfNode, fieldMeta);
		}
		dataStore.appendRecord(record);
	}
}
 
Example 2
Source File: SPARQLExtIteratorFunction.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private List<List<NodeValue>> getListNodeValues(ResultSet result) {
	List<String> resultVars = result.getResultVars();
	List<List<NodeValue>> listNodeValues = new ArrayList<>();
	while (result.hasNext()) {
		List<NodeValue> nodeValues = new ArrayList<>();
		QuerySolution sol = result.next();
		for (String var : resultVars) {
			RDFNode rdfNode = sol.get(var);
			if (rdfNode != null) {
				NodeValue n = new NodeValueNode(rdfNode.asNode());
				nodeValues.add(n);
			} else {
				nodeValues.add(null);
			}
		}
		listNodeValues.add(nodeValues);
	}
	return listNodeValues;
}
 
Example 3
Source File: ITER_Call_Select.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private List<List<NodeValue>> getListNodeValues(ResultSet result) {
    List<String> resultVars = result.getResultVars();
    List<List<NodeValue>> listNodeValues = new ArrayList<>();
    while (result.hasNext()) {
        List<NodeValue> nodeValues = new ArrayList<>();
        QuerySolution sol = result.next();
        for (String var : resultVars) {
            RDFNode rdfNode = sol.get(var);
            if (rdfNode != null) {
                NodeValue n = new NodeValueNode(rdfNode.asNode());
                nodeValues.add(n);
            } else {
                nodeValues.add(null);
            }
        }
        listNodeValues.add(nodeValues);
    }
    return listNodeValues;
}
 
Example 4
Source File: SHACLSPARQLARQFunction.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
   public NodeValue executeBody(Dataset dataset, Model defaultModel, QuerySolution bindings) {
    try( QueryExecution qexec = createQueryExecution(dataset, defaultModel, bindings) ) {
        if(arqQuery.isAskType()) {
            boolean result = qexec.execAsk();
            return NodeValue.makeBoolean(result);
        }
        else {
            ResultSet rs = qexec.execSelect();
            if(rs.hasNext()) {
                QuerySolution s = rs.nextSolution();
                List<String> resultVars = rs.getResultVars();
                String varName = resultVars.get(0);
                RDFNode resultNode = s.get(varName);
                if(resultNode != null) {
                    return NodeValue.makeNode(resultNode.asNode());
                }
            }
            throw new ExprEvalException("Empty result set for SHACL function");
        }
    }
}
 
Example 5
Source File: CorrectnessTask.java    From IGUANA with GNU Affero General Public License v3.0 4 votes vote down vote up
private double[] compare(JSONObject expectedResults, ResultSet systemResults) {
	double[] rawDouble = new double[] { 0, 0, 0 };
	JSONArray bindings = (JSONArray) ((JSONObject) expectedResults.get("results")).get("bindings");
	if (systemResults == null) {
		rawDouble[2] = bindings.size();
		return rawDouble;
	}
	Set<Integer> alreadySet = new HashSet<Integer>();

	while (systemResults.hasNext()) {
		QuerySolution solution = systemResults.next();
		boolean equals = true;
		//check for each bindings if the solution exists
		for (int i = 0; i < bindings.size(); i++) {
			//if the binding is already set continue
			if (alreadySet.contains(i)) {
				continue;
			}
			JSONObject binding = (JSONObject) bindings.get(i);
			equals = true;
			//check for each var if the solution is the same as the expected
			for (String varName : systemResults.getResultVars()) {
				RDFNode solutionNode = solution.get(varName);
				JSONObject varBinding = (JSONObject) binding.get(varName);

				equals &= compareNodes(solutionNode, varBinding);
				
			}
			if (equals) {
				// found solution
				alreadySet.add(i);
				break;
			}
		}
		if (equals) {
			rawDouble[0]++;
		} else {
			rawDouble[1]++;
		}
	}
	rawDouble[2] = bindings.size() - alreadySet.size();
	return rawDouble;
}
 
Example 6
Source File: PatternQueryHandler.java    From IGUANA with GNU Affero General Public License v3.0 4 votes vote down vote up
protected Set<String> getInstances(String queryStr) {
	Set<String> instances = new HashSet<String>();

	//check if query is already an instance
	try{
		QueryFactory.create(queryStr);
		//query is instance
		LOGGER.debug("[QueryHandler: {{}}] Query is already an instance: {{}}", this.getClass().getName(), queryStr);
		instances.add(queryStr);
		return instances;
	}catch(Exception e) {
		//query is pattern, nothing to do
	}
	
	//get vars from queryStr
	Set<String> varNames = new HashSet<String>();
	String command = replaceVars(queryStr, varNames);
	
	//generate parameterized sparql query
	ParameterizedSparqlString pss = new ParameterizedSparqlString();
	pss.setCommandText(command);
	ResultSet exchange = getInstanceVars(pss, varNames);		
	System.out.println("instaces retrieved");
	// exchange vars in PSS
	if(!exchange.hasNext()) {
		//no solution
		System.out.println("No solution");
		LOGGER.warn("[QueryHandler: {{}}] Query has no solution, will use variables instead of var instances: {{}}", this.getClass().getName(), queryStr);
		instances.add(command);
	}
	while(exchange.hasNext()) {
		QuerySolution solution = exchange.next();
		for(String var : exchange.getResultVars()) {
			//exchange variable with 
			pss.clearParam(var);
			pss.setParam(var, solution.get(var));
		}
		instances.add(pss.toString());
	}
	System.out.println("Found instances "+instances);

	return instances;
}