如何从List <querysolution> </querysolution>构造ResultSetFormat

时间:2013-09-17 17:21:01

标签: java jquery parsing format jena

此代码打印:[(?Random = http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello),(?Random = http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye)]

我需要一个resultsetformat,以便只有#和&gt;之间的部分打印出来。例如它应该打印:你好,再见我怎么能这样做? 提前致谢

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

       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( results );
            for ( final String index : indices ) {
                add( solutions.get( Integer.valueOf( index )));
            }
        }};

        System.out.println( "== Selected Solutions ==" );
        System.out.println( selectedSolutions );
    }
}

1 个答案:

答案 0 :(得分:0)

通过在代码末尾添加它来修复它:

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



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