1

例如,我有这个查询,它返回四个结果。我对这段代码所做的是通过一个整数选择其中一个结果,以便稍后我可以对其进行另一个查询。我一直在尝试做的,但还没有做的是选择多个结果,以便我可以单独重用它们。例如,此查询将返回:

  1. 结果 A
  2. 结果 B
  3. 结果 C
  4. 结果 D

例如,我可以1在控制台中键入并在字符串中获取该值并重用它。有什么方便的方法,例如,输入1,2,3这些值并将这些值添加到字符串数组中?

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;

编辑,更新代码:

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);
4

1 回答 1

2

当你得到一个 ResultSet 时,你只能使用它的解决方案一次,然后它们就被消耗掉了。因此,为了多次迭代解决方案,您需要使用例如ResultSetFactory.copyResults来复制结果。然后您可以多次访问查询解决方案。您可以使用类似答案的输入字符串"1,2,3"并获取字符串数组["1", "2", "3"]

然后您可以遍历索引并仅选择您想要的查询解决方案并将它们添加到列表中。例如:

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class QuerySolutionsFromIndicesExample {

    final static String modelText = "" +
            "@prefix bio: <http://www.semanticweb.org/vassilis/ontologies/2013/5/Onto#>.\n" +
            "@prefix ex: <http://example.org/>.\n" +
            "\n" +
            "ex:Giraffe a bio:Animal .\n" +
            "ex:Dog a bio:Animal .\n" +
            "ex:Cat a bio:Animal . \n" +
            "ex:WoollyMammoth a bio:Animal.\n" +
            "";

    final static String sparqlQuery = "" +
            "prefix bio: <http://www.semanticweb.org/vassilis/ontologies/2013/5/Onto#>\n" +
            "\n" +
            "select ?animal where {\n" +
            "  ?animal a bio:Animal\n" +
            "}\n" +
            "";

    public static void main(String[] args) {
        final Model model = ModelFactory.createDefaultModel();
        model.read( new ByteArrayInputStream( modelText.getBytes()), null, "TTL" );

        final ResultSet results = ResultSetFactory.copyResults( QueryExecutionFactory.create( sparqlQuery, model ).execSelect() );

        System.out.println( "== All Solutions ==" );
        ResultSetFormatter.out( results );

        // based on https://stackoverflow.com/q/10565335/1281433
        final String input = "0,3"; 
        final String[] indices = input.split("\\s*,\\s*");

        final List<QuerySolution> selectedSolutions = new ArrayList<QuerySolution>( indices.length ) {{
            final List<QuerySolution> solutions = ResultSetFormatter.toList( results );
            for ( final String index : indices ) {
                add( solutions.get( Integer.valueOf( index )));
            }
        }};

        System.out.println( "== Selected Solutions ==" );
        System.out.println( selectedSolutions );
    }
}
于 2013-09-17T00:33:12.593 回答