I am implementing an ontology in Eclipse with jena.I am trying to fetch the synonyms of an individual from whole ontology.
How i will get all the synonyms of a specific individual from whole ontology irrespective of the class.
static final String inputFileName = "http://word.owl";
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
InputStream in = FileManager.get().open(inputFileName);
inf.read(in, "");
ExtendedIterator classes = inf.listClasses();
while(classes.hasNext())
{
OntClass obj = (OntClass) classes.next();
String className = obj.getLocalName().toString();
System.out.println("Class Name : "+className);
}
ExtendedIterator instances = inf.listIndividuals();
while(instances.hasNext())
{
Individual ind = (Individual) instances.next();
String indName = ind.getLocalName().toString();
System.out.println("Individual Name : "+indName);
}
ExtendedIterator property = inf.listDatatypeProperties();
while(property.hasNext())
{
DatatypeProperty prop = (DatatypeProperty) property.next();
String propName = prop.getLocalName().toString();
System.out.println("Data Propties Name : "+propName);
}
ExtendedIterator property1 = inf.listObjectProperties();
while(property1.hasNext())
{
ObjectProperty prop = (ObjectProperty) property1.next();
String propName = prop.getLocalName().toString();
System.out.println("Object Propties Name : "+propName);
}
ObjectProperty isSynonymOf = inf.getObjectProperty("http://www.../Word#isSynonymOf");
System.out.println("Individuals having isSynonymOf Proterty:");
ExtendedIterator individuals1 = inf.listIndividuals();
while(individuals1.hasNext())
{
Individual ind = (Individual) individuals1.next();
if(ind.getProperty(isSynonymOf) != null)
{
String indName = ind.getLocalName().toString();
System.out.println(indName);
}
}
}
Problem :when i will enter an individual for example software
It should give me all the synonyms as
- Software
- Software Architecture
- Software design
- Program
This is my OWL file
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/ontologies/Word#"
xml:base="http://www.semanticweb.org/ontologies/Word"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.semanticweb.org/ontologies/Word"/>
The default OntModel
doesn't consider apply OWL reasoning ,thus, inference has to be enabled to explicitely to consider symmetric properties:
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
See the documentation for more details.
Just a side note, Jena has lots of convenience methods, e.g. your code could be shortened by using
model.listSubjectsWithProperty(isSynonymOf)