我使用OWL API 4.1.3来加载我的本体,它并不大。由于我需要使用推断信息,我还使用 Hermit 1.3.8.413 库进行推理。下面的代码展示了我是如何做到的。
public class ReasonRDF {
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
readRDF("C:/Users/workspace/Ontology_matching/NVDB_Matching_v18_H_4_1_CONVERTYING/results/NewInstantiated/owl/OSM1.owl");
}
public static void readRDF(String address) throws OWLOntologyCreationException, OWLOntologyStorageException{
OWLOntologyManager manager =OWLManager.createOWLOntologyManager();
File file = new File (address);
OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create(file));
System.out.println("Ontology Loaded...");
System.out.println("Logical IRI : " + ont.getOntologyID());
System.out.println("Format : " + manager.getOntologyFormat(ont));
System.out.println("Runtime memory: " + Runtime.getRuntime().totalMemory());
ReasonerFactory reasonerFactory = new ReasonerFactory();
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
Configuration config = new Configuration();
config.ignoreUnsupportedDatatypes=true;
config.reasonerProgressMonitor= progressMonitor;
OWLReasoner reasoner = reasonerFactory.createReasoner(ont, config);
long t0 = System.nanoTime();
System.out.println("Starting to add axiom generators");
OWLDataFactory datafactory = manager.getOWLDataFactory();
List<InferredAxiomGenerator<? extends OWLAxiom>> inferredAxioms = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
//inferredAxioms.add(new InferredSubClassAxiomGenerator());
inferredAxioms.add(new InferredClassAssertionAxiomGenerator());
//inferredAxioms.add(new InferredDataPropertyCharacteristicAxiomGenerator());
//inferredAxioms.add(new InferredObjectPropertyCharacteristicAxiomGenerator());
//inferredAxioms.add(new InferredEquivalentClassAxiomGenerator());
//inferredAxioms.add(new InferredPropertyAssertionGenerator());
//inferredAxioms.add(new InferredInverseObjectPropertiesAxiomGenerator());
inferredAxioms.add(new InferredSubDataPropertyAxiomGenerator());
inferredAxioms.add(new InferredSubObjectPropertyAxiomGenerator());
System.out.println("finished adding axiom generators");
// List<InferredIndividualAxiomGenerator<? extends OWLIndividualAxiom>> individualAxioms= new ArrayList<InferredIndividualAxiomGenerator<? extends OWLIndividualAxiom>>();
// inferredAxioms.addAll(individualAxioms);
// for writing inferred axioms to the new ontology
OWLOntology infOnt = manager.createOntology(IRI.create(ont.getOntologyID().getOntologyIRI().get()+"_inferred"));
// use generator and reasoner to infer some axioms
System.out.println("Starting to infer");
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, inferredAxioms);
//InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
System.out.println("Inferrence is over");
System.out.println("Storing the results");
iog.fillOntology(datafactory,infOnt);
System.out.println("Results are stored");
long elapsed_time = System.nanoTime()-t0;
System.out.println(elapsed_time);
// save the ontology
manager.saveOntology(infOnt, IRI.create("file:///C:/Users/ontologies/NVDB4_test.rdf"));
}
}
它不会抛出任何错误,但需要永远将推断的本体存储在新文件中。事实上,即使 2 天后它也没有完成工作。我的 IDE 是 eclipse EE,我提供了 6 到 12 GB 内存来运行该应用程序。 我找不到我的代码或本体有任何问题。
有人可以建议一种优化,甚至更好的实现方式或其他 api 吗?
here是我的本体,以防有人想测试它。
最佳答案
本体的大小与其推理的复杂性只有松散的关系——对于推理者来说,一些小的本体比其他非常大的本体更难。 (当然也有可能出现错误)。
可以分享本体内容吗?
编辑:尝试过本体后,看起来大小并不那么重要;事实证明,本体论很难推理。
我尝试过禁用 SWRL 规则并跳过类断言生成,但仍然遇到了障碍。对象属性的数量和拓扑结构足以让 HermiT 感到压力。
我已经尝试过版本 1.3.8.500,以防 OWLAPI 中出现任何可能已在更新版本中修复的问题;我得到的唯一重要结果是代码没有运行内存限制。分配给虚拟机的 3 GB RAM 似乎绰绰有余。
与不相交相关的推理似乎需要花费大量时间 - 这并不意外。考虑一下您是否可以从本体中删除不相交的公理并仍然满足您的要求。
还要考虑通过划分 ABox 来分离个体是否有意义 - 如果您确定存在不相关的个体,那么将多个本体中的断言分开可能会更好。大量不相关的个体可能会导致推理器尝试推理路径,而这些路径永远不会提供有用的推论。
关于java - 使用 OWL API 推理本体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41554063/