从查询中选择多个结果 [英] Selecting multiple results from query

查看:56
本文介绍了从查询中选择多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有此查询,它返回四个结果.我在用此代码执行的操作是通过整数选择这些结果之一,以便稍后可以对其进行另一个查询.我一直想做但还没有做的就是选择多个结果,以便我可以分别重用它们.例如,此查询将返回:

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:

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

例如,我可以在控制台中键入1并以字符串形式获取该值并重新使用它.例如,键入1,2,3并将这些值添加到String数组的便捷方法是什么?

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;

编辑,更新代码:

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

推荐答案

获得ResultSet时,只能使用其解决方案一次,然后使用它们.因此,为了多次遍历解决方案,您需要使用例如的输入字符串,并使用

When you get a ResultSet, you can only use its solutions once, and then they're consumed. So, in order to iterate over the solutions multiple times, you need to copy the results using, e.g., ResultSetFactory.copyResults. Then you can access the query solutions multiple times. You can take an input string like "1,2,3" and get a string array ["1", "2", "3"] using something like the answer to

然后,您可以遍历索引,仅选择所需的查询解决方案并将其添加到列表中.例如:

Then you can iterate through the indices and select just the query solutions that you want and add them to a list. For instance:

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

这篇关于从查询中选择多个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆