-1

所以我有这个 List strarray 作为输入,它有 [Aa, Bb, Cc,..] 形式的字符串我需要对列表的每个元素运行相同的查询。让用户选择要保留的结果,然后将这些结果添加到字符串中。字符串的输出应如下所示:“Aa.(Aaqueryresult1,Aaqueryresult2,...) \n Bb.(Bbqueryresult1,Bbqueryresult2,...) \n Cc.Ccqueryresult1,Ccqueryresult2,...)"

使用我的代码,我没有得到任何结果,因为可能我正在处理循环问题。有什么建议可以让我完成这项工作吗?

public static List<String> strarray = new ArrayList<String>() ;

for(int i =0;i<strarray.size(); i++){

        String selec1 = strarray.get(i);


        String queryString2 =
                "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 bi: <#> " +

    "SELECT ?X " +
    " WHERE {?X bi:has_input <"+selec1+">} " ;

        Query query2 = QueryFactory.create(queryString2);

        QueryExecution qe2 = QueryExecutionFactory.create(query2, model);
        com.hp.hpl.jena.query.ResultSet results3 =  qe2.execSelect();
        ResultSet results4 = ResultSetFactory.copyResults(results3);
        final ResultSet results5 = ResultSetFactory.copyResults(results4);

        System.out.println("==== Available Options ====");
        ResultSetFormatter.out(System.out, results4, query2);


        System.out.println( "== Select Option ==" );
        System.out.println( "== Type 0,1,2,3.. to choose ==" );

        Scanner input = new Scanner( System.in );
        final String inputs ;
        inputs = input.next();


   final String[] indices = inputs.split("\\s*,\\s*");


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


        String s = selectedSolutions.toString();


         String newstr;
          newstr = s.replaceAll("_", "");

          List<String> temp1 = new ArrayList<String>() ;

          Pattern p = Pattern.compile("#([^>]+)>");
           Matcher m = p.matcher(newstr);


           while (m.find()) {
               temp1.add(m.group(1));
            }         

           String selecfin = null;

           for(int j =0;j<temp1.size(); j++){
           String selecP = temp1.get(j);

           selecfin = selec1+".("+selecP+")";
           System.out.println(selecfin);
           }
           System.out.println(selecfin);
        }
4

1 回答 1

0

查询中的输入有问题。忘了我之前在代码中替换了 _ 。所以查询不会识别个人。问题解决了。谢谢

于 2013-09-19T15:36:04.720 回答