2016-04-20 57 views
0

在Python中創建使用RDFLib申請的傳感器本體(I用於該傳感器本體,用過也命名空間和B節點,其是表示用於其中的資源的空節點RDF圖後沒有給出URI或文字)。我試圖查詢中使用SPARQL Java中的數據因此我不得不存儲第一使用Jena的TDB然後我執行一個非常簡單的查詢其是圖:SPARQL查詢返回從一個特定的RDF圖沒什麼

String qs1 = "SELECT * {?s ?p ?o} LIMIT 10" ; 

和我用

String source = "/path/graph.rdf"; 
     FileManager.get().readModel(tdb, source); 
     dataset.begin(ReadWrite.READ) ; 
     String qs1 = "SELECT * {?s ?o ?p } " ; 

    try(QueryExecution qExec = QueryExecutionFactory.create(qs1, dataset)) { 
      ResultSet rs = qExec.execSelect() ; 
      ResultSetFormatter.outputAsJSON(rs) ; 
    }` 

執行查詢並觀察json格式的數據。 我面臨的問題是它沒有任何回報! 這是輸出:

{ 
    "head": { 
    "vars": [ "s" , "o" , "p" ] 
    } , 
    "results": { 
    "bindings": [ 

    ] 
    } 
} 

我做了一個簡單的代碼來驗證是否被存儲的數據:

StmtIterator iter = tdb.listStatements(); 
     // print out the predicate, subject and object of each statement 
     while (iter.hasNext()) { 
      Statement stmt  = iter.nextStatement(); // get next statement 
      Resource subject = stmt.getSubject();  // get the subject 
      Property predicate = stmt.getPredicate(); // get the predicate 
      RDFNode object = stmt.getObject();  // get the object 

      System.out.print(subject.toString()); 
      System.out.print("  " + predicate.toString() + "    "); 
      if (object instanceof Resource) { 
       System.out.print(object.toString()); 
      } else { 
       // object is a literal 
       System.out.print(" \"" + object.toString() + "\""); 
      } 

      System.out.println(" ."); 
     } 

,事實上它們被存儲在TDB數據庫。這是一些輸出,其中包括Bnode的奇怪表示,根據一些文章,TDB與Bnode的反應方式使其看起來像這樣。

6f98bd70:1543430b66e:-7fc3  http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue    "37^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" . 
-6f98bd70:1543430b66e:-7fc2  http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit    http://purl.oclc.org/NET/ssnx/qu/unit#hPa . 
-6f98bd70:1543430b66e:-7fc2  http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue    "996.94^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" . 
-6f98bd70:1543430b66e:-7fc1  http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit    http://purl.oclc.org/NET/ssnx/qu/unit# . 
-6f98bd70:1543430b66e:-7fc1  http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue    "OK^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" . 
-6f98bd70:1543430b66e:-7fc0  http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit    http://purl.oclc.org/NET/ssnx/qu/unit#C . 
-6f98bd70:1543430b66e:-7fc0  http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue    "24.2^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" . 

我也試過它採用了朋友本體的朋友另一個圖形並能正常工作,正確。 Bnode是否有可能導致此問題?

+0

有兩個問題:(i)您確定查詢是針對正確的圖執行的嗎? (ii)您向我們展示了結果集「rs」的一個用法,但在此之前您是否在做任何事情? ResultSets默認情況下只允許您訪問一次結果。 –

+0

@Joshua我用我使用ResultSet的代碼編輯了我的問題。我居然在TDB一個與FOAF和我創建了一個挽救了兩個不同的圖形,每次我執行它讓我回的數據從FOAF圖的查詢時間,因此我改變了數據集目錄到其他路徑,因此將只存儲一個圖形。 – raeX

+0

我不[T在我面前的文檔,但我認爲'FileManager.get()readModel(TDB,源);'讀取內容到模型'tdb',但你使用'dataset'爲您的查詢。 'tdb'實際上是'dataset'中的模型嗎? –

回答

2

嘗試:SELECT * { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }

您的意見建議中的數據是在一個名爲圖形,但你問只是無名/默認的圖形的查詢。建議的查詢會查找數據集中任何地方的所有內容。

+0

謝謝!它現在有效 – raeX

+0

是否有任何方式來指定圖形的方式,我不必使用聯合部分? – raeX

+0

一旦你知道那裏的圖,你可以使用:'GRAPH {... ...格局}'。但要注意是一個合法的完整URI,而不是一些簡短的名字(短名解析解析成爲完整的URI)。 – AndyS

0

由於@AndyS montionned查詢建議工作正常。如果您不想使用union部分,只需按照Andy的建議添加所需圖形的名稱即可。它應該是這樣的:

QueryExecution qExec = QueryExecutionFactory.create(qs1, YourGraphNameHERE)); 
      ResultSet rs = qExec.execSelect() ; 
      ResultSetFormatter.outputAsJSON(rs) ;