簡體   English   中英

使用ARQ(SPARQL)在耶拿進行RDFS推理

[英]RDFS inference in Jena using ARQ (SPARQL)

此處的Jena文檔https://jena.apache.org/documentation/javadoc/jena/index.html指出createOntologyModel包含一個弱createOntologyModel用於在子類和子屬性層次結構上進行傳遞性關閉,這就是我要尋找的全部。 從一個簡單的模型開始:

x:A rdf:type rdfs:Class .
x:A x:prop1  x:whatever .

x:B rdf:type rdfs:Class .
x:B rdfs:subClassOf x:A .
x:B x:prop2 x:other .

x:myInstance  rdf:type  x:B  .

我試圖查詢rdf:type x:B並產生B和超類A所有屬性,例如

( ?all = <x:whatever> ) ( ?props = <x:prop1> ) -> ( ?type = <x:A> ) -> [Root]
( ?all = <x:A> ) ( ?props = <rdfs:subClassOf> ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <x:other> ) ( ?props = <x:prop2> ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <rdfs:Class> ) ( ?props = <rdf:type> ) -> ( ?type = <x:B> ) -> [Root]

我已經對該示例進行了一些試驗。 它編譯並運行,但僅產生B的屬性,並且不會遍歷subClassOf樹。 我相信我已經錯過了RDF架構的基本設置或使用功能,無法使推理機執行其工作。 有什么線索嗎?

public class jena2 {
    private static void addRaw(OntModel m, String s, String p, String o) {
    m.add(ResourceFactory.createStatement(
          new ResourceImpl(s),new PropertyImpl(p),new ResourceImpl(o))
          );
    }

    public static void main(String[] args) {
    OntModel model = ModelFactory.createOntologyModel(); 

    addRaw(model, "x:A", "rdf:type", "rdfs:Class");
    addRaw(model, "x:A", "x:prop1", "x:whatever");
    addRaw(model, "x:B", "rdf:type", "rdfs:Class");
    addRaw(model, "x:B", "x:prop2", "x:other");
    addRaw(model, "x:B", "rdfs:subClassOf", "x:A");
    addRaw(model, "x:widget", "rdf:type", "x:B");

    StringBuffer sb = new StringBuffer();

    sb.append("PREFIX x: <x:>");
    sb.append("PREFIX rdf: <rdf:>");
    sb.append("PREFIX rdfs: <rdfs:>");
    sb.append("SELECT *");
    sb.append("WHERE {");
    sb.append("  x:widget rdf:type ?type .");
    sb.append("  ?type ?props ?all .");
    sb.append("}");

    Query query = QueryFactory.create(sb.toString());
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        ResultSet results = qexec.execSelect() ;
        for ( ; results.hasNext() ; ) {
            QuerySolution soln = results.nextSolution() ;
            System.out.println(soln);
        }
        } catch(Exception e) {
        System.out.println("epic fail: " + e);
    }

}

(這不是完整的答案!)

根據上面的評論,一些工作代碼:

import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;

public class jena2 {

    private static void addRaw(OntModel m, Resource s, Property p, Resource o) {
        m.add(ResourceFactory.createStatement(s, p, o));
    }

    public static void main(String[] args) {
        OntModel model = ModelFactory.createOntologyModel();

        Resource A = ResourceFactory.createResource("x:A");
        Resource B = ResourceFactory.createResource("x:B");
        Property prop1 = ResourceFactory.createProperty("x:prop1");
        Property prop2 = ResourceFactory.createProperty("x:prop2");
        Resource whatever = ResourceFactory.createResource("x:whatever");
        Resource other = ResourceFactory.createResource("x:other");
        Resource widget = ResourceFactory.createResource("x:widget");

        addRaw(model, A, RDF.type, RDFS.Class);
        addRaw(model, A, prop1, whatever);
        addRaw(model, B, RDF.type, RDFS.Class);
        addRaw(model, B, prop2, other);
        addRaw(model, B, RDFS.subClassOf, A);
        addRaw(model, widget, RDF.type, B);

        StringBuffer sb = new StringBuffer();

        sb.append("PREFIX x: <x:>");
        sb.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
        sb.append("SELECT * {");
        sb.append("  x:widget rdf:type ?type .");
        sb.append("  ?type ?props ?all .");
        sb.append("}");

        Query query = QueryFactory.create(sb.toString());
        try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            ResultSet results = qexec.execSelect();
            for (; results.hasNext(); ) {
                QuerySolution soln = results.nextSolution();
                System.out.println(soln);
            }
        } catch (Exception e) {
            System.out.println("epic fail: " + e);
        }

    }
}

輸出:

( ?all = <x:A> ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <x:other> ) ( ?props = <x:prop2> ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Class ) ( ?props = rdf:type ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <x:B> ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdf:type ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Class ) ( ?props = rdf:type ) -> ( ?type = rdfs:Resource ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdfs:subClassOf ) -> ( ?type = rdfs:Resource ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdf:type ) -> ( ?type = rdfs:Resource ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:A> ) -> [Root]
( ?all = <x:whatever> ) ( ?props = <x:prop1> ) -> ( ?type = <x:A> ) -> [Root]
( ?all = rdfs:Class ) ( ?props = rdf:type ) -> ( ?type = <x:A> ) -> [Root]
( ?all = <x:A> ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:A> ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdf:type ) -> ( ?type = <x:A> ) -> [Root]

它還返回RDFS類A的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM