getLiteral returns null when reading RDF file usin

2019-08-23 08:20发布

I have been reading about OWL ontology and RDF files all these days. I still can't understand this.

Let's say I created a simple ontology using Protege. It has single class called Review with two data properties which are comment and rating.

Now I want to create a separate RDF file written in xml which has some comments. The file I created look like

<?xml version="1.0"?>
<rdf:RDF
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
      xmlns:c="http://review-analyzer.local/ontologies/reviews_2.owl#">

<rdf:Description rdf:ID="me">
    <c:review>Display looks amazing!</c:review>
    <c:raitng>5</c:raitng>
</rdf:Description>
<rdf:Description rdf:ID="me2">
    <c:review>Display is great!</c:review>
    <c:raitng>5</c:raitng>
</rdf:Description>
</rdf:RDF>

Now I want to read this file into jena model, read this reviews and create individuals in my ontology. Creating individual part I already have figured out. But I can't get rating and comment values from these reviews.

The code I have tried is

  Model model = ModelFactory.createOntologyModel();
        model.read("./rdf/119.rdf", "RDF/XML");
  String queryString = 
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n" +
                "PREFIX ns: <http://review-analyzer.local/ontologies/reviews_2.owl#>"+
                "select *\r\n" + 
                "where {\r\n" + 
                "  ?Comment ns:review ?review .\r\n" + 
                "  ?Comment ns:raitng ?raitng .\r\n" + 
                "}";
        Query query = QueryFactory.create(queryString);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        try {
            ResultSet results = qexec.execSelect();
            List<String> varNames = results.getResultVars();
            while (results.hasNext()) { 

                QuerySolution soln = results.nextSolution();
                Literal name = soln.getLiteral("review");
                System.out.println(soln);
            }
        } finally {
            qexec.close();
        }

Following line returns null.

Literal name = soln.getLiteral("review");

What's the problem in this?

1条回答
再贱就再见
2楼-- · 2019-08-23 08:51

rdf:ID needs to be resolved to an absolute IRI but there is no xmlns:base in your file. Either add one or use rdf:about with absolute IRIs, e.g., "c:me".

查看更多
登录 后发表回答