I have this query for example which returns four results. What I am doing with this code is selecting one of those results through an integer, so that I can do another query for it later. What I have been trying to do, and haven't been able to yet, is select more than just one result, so that I can reuse them separately. For example this query would return:
- RESULT A
- RESULT B
- RESULT C
- RESULT D
I can type, for example, 1
in the console and get that value in a string and reuse it. What is a convenient way, for example, to type 1,2,3
and get these values added to a String array?
public static String[] path = new String[30];
String queryString =
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX bio: <http://www.semanticweb.org/vassilis/ontologies/2013/5/Onto#> " +
" SELECT DISTINCT ?Animal " +
" WHERE { ?Animal rdf:type bio:Animal } " ;
Query query = QueryFactory.create(queryString);
QueryExecution qe= QueryExecutionFactory.create(query, model);
ResultSet resultset = qe.execSelect();
ResultSet results = ResultSetFactory.copyResults(resultset);
ResultSet results2 = ResultSetFactory.copyResults(results);
ResultSetFormatter.out(System.out, results, query);
List<QuerySolution> e = ResultSetFormatter.toList(results2);
String next;
System.out.println("Select Animal: ");
next = user_input.next( );
int i = Integer.parseInt(next);
QuerySolution e1 = e.get(i);
RDFNode rd = e1.get("");
String rds = rd.toString();
String phrase = rds;
String delims = "[#]";
String[] tokens = phrase.split(delims);
newStr = tokens[1].replaceAll("_","");
path[1] = newStr;
Edit, updated code:
final Scanner input = new Scanner( System.in );
String selec2;
selec2 = input.next();
final String[] indices = selec2.split("\\s*,\\s*");
final List<QuerySolution> selectedSolutions = new ArrayList<QuerySolution>( indices.length ) {{
final List<QuerySolution> solutions = ResultSetFormatter.toList( results2 );
for ( final String index : indices ) {
add( solutions.get( Integer.valueOf( index )));
}
}};
System.out.println( "== Selected Solutions ==" );
System.out.println(selectedSolutions);
int k = 0;
while (input.hasNext()) {
int i = Integer.parseInt(selec2);
QuerySolution e1 = selectedSolutions.get(i);
RDFNode rd = e1.get("Ani");
String rds = rd.toString();
String phrase = rds;
String delims = "[#]";
String[] tokens = phrase.split(delims);
newStr = tokens[1].replaceAll("_", "");
path[k]= newStr;
k++;
}
System.out.println(path);