2015-09-20 29 views
0

我正在尋找如何通過屬性和對象類從我的模型中獲取所有語句的方式。Jena - 按屬性和對象類查找語句

例如我有屬性:driverOf和個人巴士或卡車類型。然後我想獲得屬性爲的所有語句:driverOf,對象是instanceOf Bus。謝謝。

更新1

其實我需要的結果是一組語句(RESP。StmtIterator),因爲在我的應用我使用Statement對象已的。我認爲最乾淨的解決方案是具有以下屬性:driverOf屬性,如:driverOfBus和:driverOfTruck。但它會讓我的應用程序更復雜,所以我想找出一些簡單的解決方法。

回答

1

您可以使用sparql查詢。你必須用全名稱空間替換標籤。

String queryString = 
    "SELECT ?x WHERE { ?x driverOflabel ?y . {?y a Buslabel} UNION { ?y a Trucklabel} . }"; 

Query query = QueryFactory.create(queryString); 
QueryExecution qexec = QueryExecutionFactory.create(query, YOURMODEL); 
try { 
    ResultSet results = qexec.execSelect(); 
    while(results.hasNext()) { 
     QuerySolution soln = results.nextSolution(); 
     System.out.println(soln.toString()); 
    } 
} finally { 
    qexec.close(); 
} 
+0

您的回覆非常感謝!但是,我需要結果是陳述...請參閱我的更新。 – user3024710

+0

您可以從QuerySolution爲您提供的結果創建語句。 'Statement stmt = ResourceFactory.createStatement(soln.getResource(「x」),yourresource,soln.getResource(「y」));' – dziewxc

0

我希望我正確地理解這一點:

說你有model mnamespace NAMESPACE

// Get the property and the subject 
Property driverOf = m.getProperty(NAMESPACE + "driverOf"); 
Resource bus = m.getResource(NAMESPACE + "bus"); 

// Get all statements/triples of the form (****, driverOf, bus) 
StmtIterator stmtIterator = m.listStatements(null, driverOf, bus); 
while (stmtIterator.hasNext()){ 
    Statement s = stmtIterator.nextStatement(); 
    Resource busDriver = s.getObject(); 
    // do something to the busdriver (only nice things, everybody likes busdrivers) 
}