1
Individual ind = model.createIndividual("http://www.semanticweb.org/ontologies/Word#Human", isSynonymOf);

    System.out.println( "Synonyms of given instance are:" );

   StmtIterator it =ind.listProperties(isSynonymOf);
    while( it.hasNext() ) {
      Statement stmt = ((StmtIterator) it).nextStatement();
      System.out.println( " * "+stmt.getObject());
    }

Output

Synonyms of given instance are:

  http://www.semanticweb.org/ontologies/Word#Human
  http://www.semanticweb.org//ontologies/Word#Mortal
  http://www.semanticweb.org/ontologies/Word#Person

Problem 1: My output shows whole URI but I need output as under

 Synonyms of given instance are:
 Human
 Mortal
 Person

Problem 2: I have 26 instances and every time I have to mention its URI to show its synonyms. How will I show synonyms of any instance from whole ontology model instead of mentioning URIs again and again. I am using eclipse Mars 2.0 and Jena API

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ALee
  • 53
  • 1
  • 8

1 Answers1

2
  1. You can use REGEX or simply Java string operations to extract the substring after #. Note, best practice is to provide human readable representations of URIs and not to encode it in the URI. For instance, rdfs:label is a common property for doing that.

  2. It is simply iterating over all individuals of the ontology which are returned by

    model.listIndividuals()

Some comments:

  • You're using the method createIndividual not as expected. The second argument denotes a class and you're giving it a property. Please use Javadoc for the future.
  • I don't understand why you're casting it to StmtIterator - that doesn't make sense
  • Using listPropertiesValues is more convenient since you're only interested in the values.
  • Use Java 8 to make the code more compact
model.listIndividuals().forEachRemaining(ind -> {
    System.out.println("Synonyms of instance " + ind + " are:");
    ind.listPropertyValues(isSynonymOf).forEachRemaining(val -> {
        System.out.println(" * " + val);
    });
});

Java 6 compatible version:

ExtendedIterator<Individual> indIter = model.listIndividuals();
while(indIter.hasNext()) {
    Individual ind = indIter.next();
    System.out.println("Synonyms of instance " + ind + " are:");
    NodeIterator valueIter = ind.listPropertyValues(isSynonymOf);
    while(valueIter.hasNext()) {
        RDFNode val = valueIter.next();
        System.out.println(" * " + val);
    }
}
Community
  • 1
  • 1
UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • when i use java 8 the above code works well. But i have constraint of java 6. the Lamda expression do not work in java 6. I have to use java 6. How to make above code for java 6 ?? – ALee Mar 14 '17 at 07:55
  • `model.listIndividuals()` returns an iterator. Just use it as usual. `ind.listPropertyValues(isSynonymOf)` returns an iterator as well. – UninformedUser Mar 14 '17 at 08:57
  • Thank you. I did not uderstand it correctly. Please reflect it on above code. – ALee Mar 14 '17 at 09:38
  • Shouldn't you know how to use an iterator in Java? I mean it's pretty basic. `ExtendedIterator it = model.listIndividuals(); while(it.hasNext()) {...` – UninformedUser Mar 14 '17 at 10:18
  • I don't understand what is now so difficult to replace both usages of `forEachRemaining` with an iterator. I told you that `model.listIndividuals()` returns an iterator of individuals. And for each such individual `ind` you call the method `ind.listPropertyValues(isSynonymOf)` which also returns an iterator, this time of nodes. – UninformedUser Mar 14 '17 at 21:10
  • Sorry, but I really don't understand the problem. It is so basic Java stuff...I updated my answer. Please learn Java programming and read the Jena documentation next time. And using Java 6 nowadays is really strange to be honest. – UninformedUser Mar 15 '17 at 09:11