2017-09-29 4 views
0

ここではcreateOntologyModelには、私が探しているすべてのサブクラスおよびサブプロパティ階層の推移的閉包の弱い推論が含まれています。単純なモデルで始まる:ARQ(SPARQL)を使用したJenaのRDFS推論

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スキーマの基本的な設定や使い方を見逃していると思っています。すべての手がかりは?上記のコメントを1として

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); 
    } 

} 
+0

ステートメントを追加することはできません。プレフィックス付きのURIを単に使用することはできません。解決する完全なURIをAPIがどのように知っていますか?たとえば、 '' x:A "、" rdf:type "、" rdfs:Class "' - > "rdf:type"を決して正しいURIに解決しないでください。http://www.w3.org/ 1999/02/22-rdf-syntax-ns#type'である。他のすべての場合も同様です。指定されたプロトコルのないものはすべて、ローカルURIに解決されます。 – AKSW

+0

SPARQLクエリでも同じことが言えます: 'PREFIX rdfs:'はRDFS名前空間とは関係がありません。完全なURIが必要です – AKSW

+0

@AKSWそうです。私はRDF.typeとRDFS.subClassOfなどに切り替えることを実験していました。私は推論エンジンのために完全なURIを必要としている理由を知ることができます(それを探しているので)単純なx:接頭辞で取得しますか? –

答えて

2

(これはないいっぱいの答えです!)

、いくつかの作業コード:

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

+0

あなたの推薦が働くことを確認します。迷惑URIで不正行為しようとしたことに対して、多くのおかげで恥ずかしいです。ところで、私は静的な文字列xは使用されていないと思う。 –

関連する問題