繁体   English   中英

显示来自sparql查询的结果“很好”

[英]Display results “nicely” from a sparql query

嘿,我尝试以“不错”的格式显示结果时遇到问题。

我有以下代码:

                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。 事实是,当我尝试显示该查询的结果时,我具有以下格式:

在此处输入图片说明

我只想打印那些收据,但没有(“?r = ...),我只希望收据的名称和配料的数量,而没有其他所有内容。在此示例中,我要打印:Prueba3

Prueba3 1

普鲁巴2

Prueba4 4

等等..

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

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

就这么简单!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM