我想以編程方式在一致的本體中獲得對推斷公理的解釋,就像在 Protégé UI 中可以做的那樣。我找不到任何直接的方法。我找到了owlexplanation repo,但我無法終生解決依賴問題來設定owlexplanation環境。我還瀏覽了有關解釋的 owlapi的javadoc(以完全避免其他 repo),但除了瀏覽 Java 源代碼已經看到的內容之外,我沒有看到任何有用的東西。
我曾想過簡單地否定推斷的公理,以通過不一致來獲得解釋,但我更喜歡更干凈的東西,而且我不確定這種方法是否正確。
其他(可能)有用的背景關系:
- 幾年前我使用過一些 Java,但我現在主要使用 Python(我嘗試將 OWL API 與 JPype 一起使用,而 OWL 通常與 Owlready2 一起使用)。
- 我正在使用 HermiT 推理器(再次通過 JPype)(根據 build.xml 檔案,
最新穩定版本 1.3.8)。 - 我已經設法從 HermiT 源代碼中獲得了對我的設定中的不可滿足性和不一致的解釋,而沒有
owlexplanation遵循這個示例。 - 我掉進了兔子洞,想要制作一個可用的
.jar檔案owlexplanation,以便將它添加到我的 JPype 類路徑中。當我一開始無法構建 Java 專案時,我的計劃就落空了。 - 我正在使用 Intellij IDE。
我將不勝感激任何見解或提示。
2022 年 1 月 6 日更新:
我決定owlexplanation用干凈的頭腦再次嘗試代碼,所以這就是我所在的位置:
- 從 github 下載源代碼并解壓縮 zip。
- 啟動 IntelliJ,而不是從“從現有源創建專案”,我單擊“打開”并選擇提取的目錄。
- 我建立了這個專案,它成功了。
- 從 Maven 工具中,我成功運行干凈、驗證、編譯和測驗。
- 如果我運行“包”Maven 操作,它會拋出“環境變數 JAVA_HOME 未正確設定”的錯誤。問題是,如果我去 File>Project Structure,我看到 SDK 設定為 11,它不是空的。
- 此外,從
pom.xml檔案中我得到這些問題:Plugin 'org.apache.maven.plugins:maven-gpg-plugin:1.5' not foundPlugin 'org.sonatype.plugins:nexus-staging-maven-plugin:1.6.6' not found
2022 年 1 月 8 日更新:(嘗試@Ignazio 的回答)
我創建了一個新的 IntelliJ 專案,并添加了 @Ignazio 提到的 Maven 依賴項(加上其他一些類似slf4j等),我得到了一個作業示例(我認為)。轉到我的主要專案(使用 JPype),我必須手動下載一些 .jar 以包含在類路徑中(因為這里不能使用 maven)。這些是目前下載的:
caffeine-3.0.5.jar hppcrt-0.7.5.jar org.semanticweb.hermit-1.4.5.519.jar slf4j-api-1.7.32.jar
commons-rdf-api-0.5.0.jar javax.inject-1.jar owlapi-distribution-5.1.9.jar slf4j-nop-1.7.32.jar
google-collect-1.0.jar owlexplanation-5.0.0.jar
接下來,NullPointerException在嘗試使用時會拋出a loadOntologyFromOntologyDocument()。我已嘗試按照此處的建議重新下載 jar ,但仍然存在例外。可能是缺少一些 .jar 嗎?我根據NoClassDefFoundError將發生的拋出下載了它們。
這發生在一個pizza.owl可以正常作業的通用檔案中。
編輯:我曾經mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR獲取依賴項并使用 OUTPUT_DIR 作為類路徑,但NullPointerException它已經消失了,所以看來我確實缺少一些 .jar 檔案。
作為記錄,然后我遇到了其他問題(gen.getExplanations()拋出NoSuchMethodError錯誤),但我沒有更多時間來除錯它。我將拋棄 JPype,不管它多么方便,只需從 Python 中呼叫 Java,使用subprocess. 這些是(我猜)Jpype 問題,所以我接受 Ignazio 的回答,因為它解決了我的 Java/OWL API/owlexplanation 方面的問題。
uj5u.com熱心網友回復:
您不僅在使用專案,而且實際上是從頭開始構建它們,這比使用已發布的工件需要更多的設定。
使用 Maven 可用 jar 的快捷方式(通過 Maven Central,盡管其他公共存盤庫也應該這樣做)
Java代碼:
import java.io.File;
import java.util.Set;
import java.util.function.Supplier;
import org.junit.Test;
import org.semanticweb.HermiT.ReasonerFactory;
import org.semanticweb.owl.explanation.api.Explanation;
import org.semanticweb.owl.explanation.api.ExplanationGenerator;
import org.semanticweb.owl.explanation.api.ExplanationGeneratorFactory;
import org.semanticweb.owl.explanation.api.ExplanationProgressMonitor;
import org.semanticweb.owl.explanation.impl.blackbox.Configuration;
import org.semanticweb.owl.explanation.impl.blackbox.DivideAndConquerContractionStrategy;
import org.semanticweb.owl.explanation.impl.blackbox.EntailmentCheckerFactory;
import org.semanticweb.owl.explanation.impl.blackbox.InitialEntailmentCheckStrategy;
import org.semanticweb.owl.explanation.impl.blackbox.StructuralTypePriorityExpansionStrategy;
import org.semanticweb.owl.explanation.impl.blackbox.checker.BlackBoxExplanationGeneratorFactory;
import org.semanticweb.owl.explanation.impl.blackbox.checker.SatisfiabilityEntailmentCheckerFactory;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
public class CheckOntology {
@Test
public void should() throws Exception {
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLOntology o = m.loadOntologyFromOntologyDocument(new File("pizza.owl"));
OWLReasonerFactory rf = new ReasonerFactory(); // Get hold of a reasoner factory
// Create the explanation generator factory which uses reasoners provided by the specified
// reasoner factory
ExplanationGeneratorFactory<OWLAxiom> genFac =
createExplanationGeneratorFactory(rf, null, OWLManager::createOWLOntologyManager);
// Now create the actual explanation generator for our ontology
ExplanationGenerator<OWLAxiom> gen = genFac.createExplanationGenerator(o);
// Ask for explanations for some entailment
// Get a reference to the axiom that represents the entailment that we want explanation for
// this will just run the explanations for all axioms
o.logicalAxioms().forEach(e -> explain(e, gen));
}
void explain(OWLAxiom entailment, ExplanationGenerator<OWLAxiom> gen) {
// Get our explanations. Ask for a maximum of 5.
try {
Set<Explanation<OWLAxiom>> expl = gen.getExplanations(entailment, 5);
System.out.println("CheckOntology.explain() " entailment);
expl.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
// this method replicates code existing in the owlexplanation project; it's needed because the factories in owlexplanation do not set InitialEntailmentCheckStrategy correctly
public static ExplanationGeneratorFactory<OWLAxiom> createExplanationGeneratorFactory(
OWLReasonerFactory reasonerFactory, ExplanationProgressMonitor<OWLAxiom> progressMonitor,
Supplier<OWLOntologyManager> m) {
EntailmentCheckerFactory<OWLAxiom> checker =
new SatisfiabilityEntailmentCheckerFactory(reasonerFactory, m);
Configuration<OWLAxiom> config = new Configuration<>(checker,
new StructuralTypePriorityExpansionStrategy<OWLAxiom>(
InitialEntailmentCheckStrategy.PERFORM, m),
new DivideAndConquerContractionStrategy<OWLAxiom>(), progressMonitor, m);
return new BlackBoxExplanationGeneratorFactory<>(config);
}
}
Maven配置:
4.0.0 等等等等 0.0.1-SNAPSHOT 等等 junit junit 4.12 net.sourceforge.owlapi org.semanticweb.hermit 1.4.5.519 net.sourceforge.owlapi owlexplanation 5.0.0
HermiT 可以通過我制作的發布版本在 Maven Central 上獲得 - 補丁版本是指構建它的 OWLAPI 版本。這里的 1.4.5.519 意味著 HermiT 是使用 OWLAPI 5.1.9 構建的。owlexplanation 5.0.0 是使用 OWLAPI 5 構建的,因此它與專案的主要 OWLAPI 版本相同。
這個代碼示例并沒有做很多事情,因為它只是尋找對斷言公理的解釋。通常,解釋只是公理本身,正如它所斷言的那樣。改變選擇的公理,或者使用本體中沒有斷言的公理,應該會給你你所追求的解釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/408606.html
標籤:
上一篇:HowtoresovleJavaRuntime(classfileversion55.0),這個版本的JavaRuntime只能識別到52.0的class檔案版本錯誤?[復制]
