我想实现Word
的本体,包括三个子类:
我试图从文件加载本体,但我无法获取Word
的子类和指定子类的所有实例。我的代码如下所示:
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);
}
}
答案 0 :(得分:0)
OWLOntology::getSubClassAxiomsForSuperClass(OWLClass)
将返回子类公理,其中指定的类是超类 - 公理中的子类将是示例中Word
的子类。
OWLOntology::getClassAssertionAxioms(OWLClass)
返回具有指定类的类断言 - 这些公理中的个体将是Word
的实例。
请注意,这些方法只返回本体中明确的数据;要查找可以推断的数据,您需要使用OWLReasoner
。在OWLAPI之外存在OWLReasoner
的正确实现,例如参见Pellet,HermiT,FaCT ++,Konclude,JFact(开源)和StarDog(商业许可)。