显示结果"很好地"来自sparql查询

时间:2015-03-02 19:07:42

标签: java jsp rdf sparql

嘿,我在尝试将结果显示在" nice"上时遇到了问题。格式。

我有以下代码:

                try {
        //opening owl file
        Model model = ModelFactory.createDefaultModel();
        model.read(new FileInputStream("C:/Users/Karen/Desktop/Proyecto/bbdd.owl"), null, "TTL");
        //System.out.println(model);

        //create a new query
        String queryString
                 ="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                +"PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                +"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                +"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"         
                +"PREFIX rec:<http://www.receta.org#>"
                +"SELECT reduced ?r WHERE { "
                +"  ?x rdf:type rec:Receta . "
                +"  ?x rdfs:label ?r."
                +"  filter not exists {"
                +"      ?x rec:Ingrediente ?i "
                + "filter(?i not in " + convertToSPARQLListQuery1(ing) + ")"
                +"}"
                +"}";
        System.out.println(queryString);
        com.hp.hpl.jena.query.Query q = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(q, model);
        ResultSet results = qe.execSelect();

        //print query results
         while (results.hasNext()) {
            //System.out.println(results.getResourceModel());
            //ResultSetFormatter.out(System.out,results, q);
            list.add(results.next());
        }

    } catch (java.lang.NullPointerException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println("Query Failed !" + e);
    }

一个非常简单的JSP。事实是,当我尝试显示该查询的结果时,我有以下格式:

enter image description here

我想准确打印那些食谱但没有(&#34;?r = ...)我只想要食谱的名称和成分的数量而不需要其他所有食谱。 在这个例子中我想打印: Prueba3

Prueba3 1

Prueba 2

Prueba4 4

等。

1 个答案:

答案 0 :(得分:1)

感谢您的提示。我有解决方案:

   public static List getAllReceipes(){
    List list = new ArrayList();

    String log4jConfPath = "C:/Users/Karen/workspace/Jena/src/Tutorial/log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);
    try {
        //opening owl file
        Model model = ModelFactory.createDefaultModel();
        model.read(new FileInputStream("C:/Users/Karen/Desktop/Proyecto/bbdd.owl"), null, "TTL");
        //System.out.println(model);

        //create a new query
        String queryString
                = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                + "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
                + "PREFIX rec:<http://www.receta.org#>"
                + "SELECT ?r ?i WHERE { "
                + "  ?x rdf:type rec:Receta ."
                + "  ?x rdfs:label ?r."
                + " ?x rec:Ingrediente ?i."
                + "}";

        com.hp.hpl.jena.query.Query q = QueryFactory.create(queryString);

        //execute the query and obtain results
        QueryExecution qe = QueryExecutionFactory.create(q, model);
        ResultSet results = qe.execSelect();

        //print query results
        while (results.hasNext()) {

            //System.out.println(results.getResourceModel());
            //ResultSetFormatter.out(System.out,results, q);
            QuerySolution qs = results.next();
            list.add(qs.getLiteral("r")); 
        }
    } catch (java.lang.NullPointerException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println("Query Failed !");
    }
    System.out.println("size: " + list.size());
        return list;      
}

就像那个一样简单!