Search code examples
rdfjena

eclipse jena parsing statement


I am trying to parse an RDF file into statements and display values in dropdowns as subject object and predicate. Textfields will be localNames whereas the values behind will be URI's or literals accordingly. For subjects and predicates I think I can handle this but for objects, RDFNode class doesn't have getLocalName property which left me in a difficult situation. Any help will be highly appreciated

public void TestMethod() {
    Map<String, Map<String, Object>> patternMap = new HashMap<String, Map<String, Object>>();
    Map<String, Object> subjectMap = new HashMap<String, Object>();
    Map<String, Object> predicateMap = new HashMap<String, Object>();
    Map<String, Object> objectMap = new HashMap<String, Object>();
    Model model = ModelFactory.createDefaultModel();
    model.read("dbpedia.owl");
    StmtIterator iter = model.listStatements();
    while (iter.hasNext()) {
        Statement stmt = iter.nextStatement();
        subjectMap.put(stmt.getSubject().getLocalName(),stmt.getSubject().getURI() );
        predicateMap.put(stmt.getPredicate().getLocalName(),(stmt.getPredicate().isLiteral() ? stmt.getPredicate().getLocalName() : stmt.getPredicate().getURI()));
        objectMap.put(stmt.getObject().toString(), stmt.getObject().toString());

    }
    patternMap.put("Subjects", subjectMap);
    patternMap.put("Predicates", predicateMap);
    patternMap.put("Objects",objectMap);
}

Solution

  • The RDFNode object will be either a Literal or a Resource. You can use the same isLiteral() method that you've used on the Predicate to determine which type an object is, and then use the asLiteral() and asResource() methods to perform the necessary conversion.

    In the case of a Literal, try literal.getValue().toString() to return its name. A Resource will of course be handled the same as a Subject.