繁体   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