首页 > 解决方案 > Jena Reasoner 无法推断

问题描述

我是本体的新手。最近,我正在尝试使用 Jena API 进行推断。我已经参考了这篇文章的答案。但是,问题是我的耶拿无法推断。我的代码写在下面。我的本体文件在这里,它包含 swrl 规则和一些实例。我试图在 protege 中进行推断,并且它有效。所以,这意味着我的规则很好。谁能告诉我我的代码有什么问题,或者如何纠正它?谢谢!

    String baseURI = "http://www.semanticweb.org/ncuis/ontologies/2021/2/People#";
    String ns = "http://www.semanticweb.org/ncuis/ontologies/2021/2/People#";
    OntModel ontModel = ModelFactory.createOntologyModel();
    String inputFileName = "src/main/java/org/isq/Onto/People.owl";

    // create ontology model using jena
    InputStream in = FileManager.get().open(inputFileName);
    try {
        File file = new File(inputFileName);
        FileReader reader = new FileReader(file);
        ontModel.read(reader, null);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Inferring using Jena: https://stackoverflow.com/questions/3024273/inferring-using-jena
    Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
    reasoner = reasoner.bindSchema(ontModel);
    // Obtain standard OWL-DL spec and attach the Pellet reasoner
    OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
    ontModelSpec.setReasoner(reasoner);
    // Create ontology model with reasoner support
    OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);

    OntClass marPerson = model.getOntClass(ns + "MarriedPerson"); // this is the uri for MarriedPerson class
    ExtendedIterator married = marPerson.listInstances();
    while(married.hasNext()) {
        OntResource mp = (OntResource) married.next();
        System.out.println(mp.getLocalName());
    } // it should infer two names

标签: javajenaontology

解决方案


推荐阅读