I want to implement an ontology of Word
that includes three sub classes:
I tried to load the ontology from a file, but I can not fetch the subclasses of a Word
and all the instances of a specified subclass. My code looks like this:
import java.util.Iterator;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.util.AutoIRIMapper;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
public class Main {
public static OWLOntologyManager create() {
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
// PriorityCollection<OWLOntologyIRIMapper> iriMappers = m.getIRIMappers();
// iriMappers.add(new AutoIRIMapper(new File("materializedOntologies"), true));
return m;
}
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
// Get hold of an ontology manager
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// Load an ontology from the Web
IRI iri = IRI.create("http://anywhere");
OWLOntology pizzaOntology = `manager.loadOntologyFromOntologyDocument(iri);`
System.out.println("Loaded ontology: " `enter code here`+ WordOntology);
manager.removeOntology(WordOntology);
File file = new File("word.owl");
OWLDataFactory dataFactory = manager.getOWLDataFactory();
PrefixManager pm = new DefaultPrefixManager(base);
OWLClass Word = dataFactory.getOWLClass(":Word", pm);
OWLNamedIndividual Software = `dataFactory.getOWLNamedIndividual(":Software", pm);`
OWLClassAssertionAxiom classAssertion =
dataFactory.getOWLClassAssertionAxiom(Word, Software);
OWLOntology ontology = manager.createOntology(IRI.create(base));
manager.addAxiom(ontology, classAssertion);
manager.saveOntology(ontology, new SystemOutDocumentTarget());
OWLOntologyManager m = create();
OWLOntology o = m.loadOntologyFromOntologyDocument(iri);
assertNotNull(o);
OWLDataFactory factory = manager.getOWLDataFactory();
for (OWLClass cls : o.getClassesInSignature())
System.out.println(cls);
}
}
OWLOntology::getSubClassAxiomsForSuperClass(OWLClass)
will return the subclass axioms where the specified class is the superclass - the subclass in the axiom will be a subclass of Word
in your example.
OWLOntology::getClassAssertionAxioms(OWLClass)
returns class assertions with the specified class - the individuals in these axioms will be the instances of Word
.
Note that these methods only return data that is explicit in the ontology; for finding data that can be inferred, you need to use an OWLReasoner
. Proper implementations of OWLReasoner
exist outside the OWLAPI, see for example Pellet, HermiT, FaCT++, Konclude, JFact (Open Source) and StarDog (commercial licence).