2

below is my .rdfs file. i need to get YYYY if my input is XXXX. How do i do it.

<rdfs:Class rdf:about="&kb;XXXX"
     rdfs:label="XXXX">
    <rdfs:subClassOf rdf:resource="&kb;YYYY"/>
</rdfs:Class>

my code:

StmtIterator iter = model.listStatements(
            new
                SimpleSelector(null, RDFS.label, (RDFNode) null) {
                    @Override
                    public boolean selects(Statement s) {
                            return s.getString().endsWith("XXXX");
                    }

            });
    if (iter.hasNext()) {
        System.out.println("The database contains:");
        while (iter.hasNext()) {
            System.out.println("  " + iter.nextStatement()
                                          .getString());
        }
Ian Dickinson
  • 12,875
  • 11
  • 40
  • 67
Raj
  • 440
  • 2
  • 6
  • 22
  • 2
    That seems like your average XML file, so don't reinvent the wheel but just use a XML parser? – Voo Oct 10 '11 at 09:16
  • Voo: RDF and RDFS have slightly unpredictable XML serializations, so regular XML tools don't always fit comfortably. However, the OP is actually talking about traversing the data structure after it has been parsed by Jena (which is where `StmtIterator` etc comes from). – Ian Dickinson Oct 18 '11 at 11:40

1 Answers1

2

There's absolutely no need to search the entire Model every time you need to access a triple. Try:

String kb = "..."; // whatever &kb; expands to
Resource subj = model.getResource( kb + "XXXX" );
Resource super = subj.getProperty( RDFS.subClassOf )
                     .getResource();

It's even easier if you use the ontology API:

OntModel m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
m.read( ... your file: or http: URL here );
OntClass x = m.getOntClass( NS + "XXX" );
OntClass y = x.getSuperClass();
Ian Dickinson
  • 12,875
  • 11
  • 40
  • 67