Question

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());
        }
Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top