2014-04-03 29 views
0

我試着執行下面的java代碼來創建使用SPARQL構造功能的新三元組。使用sparql查詢創建新的三元組

package jenasemweb; 


import com.hp.hpl.jena.query.QueryExecution; 
import com.hp.hpl.jena.query.QueryExecutionFactory; 
import com.hp.hpl.jena.query.QueryFactory; 
import com.hp.hpl.jena.query.QuerySolution; 
import com.hp.hpl.jena.query.ResultSet; 
import com.hp.hpl.jena.rdf.model.InfModel; 
import com.hp.hpl.jena.rdf.model.Model; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 
import com.hp.hpl.jena.rdf.model.Property; 
import com.hp.hpl.jena.rdf.model.Resource; 
import com.hp.hpl.jena.rdf.model.Statement; 
import com.hp.hpl.jena.rdf.model.StmtIterator; 
import com.hp.hpl.jena.util.FileManager; 
import com.hp.hpl.jena.util.PrintUtil; 

    public class SparqlQuery03 { 
public static void main(String [] args) { 

// LOAD Raw Model from URL 
Model myRawModel = 
FileManager.get().loadModel(
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/Houghland.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/cruz.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/abraham.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/akundi.v2.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/bennett.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/hanna.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/liew_hw2.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/ramani.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/rawal.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/sison.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/tara.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/turney.n3", "N3"); 

// READ another N3 into the Model 
FileManager.get().readModel(myRawModel, 
"http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/wilson.n3", "N3"); 

// Create an RDFS inference model from the Raw Model 
InfModel infmodel = ModelFactory.createRDFSModel(myRawModel); 

// Create a new SPARQL query 
String queryString = 
"PREFIX drc: <http://www.codesupreme.com/onto/cse7392/#> " + 
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + 
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + 
"SELECT DISTINCT ?lname ?fname " + // space after last ?var 
"WHERE {" + 
" ?who foaf:lastName ?lname." + 
" ?who foaf:firstName ?fname." + 
" }" + 
"ORDER BY ?lname"; 

// create a Jena query from the queryString 
com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString); 

// create a Jena QueryExecution object that knows the query 
// and the N3 over which the query will be run 
QueryExecution qe = QueryExecutionFactory.create(query, infmodel); 


// execute the query - get back a ResultSet 
ResultSet results = qe.execSelect(); 

// iterate over the result set 
while(results.hasNext()) { 
QuerySolution sol = results.next(); 
System.out.println("Solution:" + sol.toString()); 
}}} 

但執行返回下列錯誤:

Exception in thread "main" org.apache.jena.riot.RiotException: [line: 119, col: 27] Unrecognized: [DOT] 
    at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136) 
    at org.apache.jena.riot.lang.LangEngine.raiseException(LangEngine.java:163) 
    at org.apache.jena.riot.lang.LangEngine.exceptionDirect(LangEngine.java:156) 
    at org.apache.jena.riot.lang.LangEngine.exception(LangEngine.java:149) 
    at org.apache.jena.riot.lang.LangTurtleBase.triplesNodeCompound(LangTurtleBase.java:408) 
    at org.apache.jena.riot.lang.LangTurtleBase.triplesNode(LangTurtleBase.java:388) 
    at org.apache.jena.riot.lang.LangTurtleBase.objectList(LangTurtleBase.java:350) 
    at org.apache.jena.riot.lang.LangTurtleBase.predicateObjectItem(LangTurtleBase.java:288) 
    at org.apache.jena.riot.lang.LangTurtleBase.predicateObjectList(LangTurtleBase.java:269) 
    at org.apache.jena.riot.lang.LangTurtleBase.triples(LangTurtleBase.java:250) 
    at org.apache.jena.riot.lang.LangTurtleBase.triplesSameSubject(LangTurtleBase.java:191) 
    at org.apache.jena.riot.lang.LangTurtle.oneTopLevelElement(LangTurtle.java:44) 
    at org.apache.jena.riot.lang.LangTurtleBase.runParser(LangTurtleBase.java:90) 
    at org.apache.jena.riot.lang.LangBase.parse(LangBase.java:42) 
    at org.apache.jena.riot.RDFParserRegistry$ReaderRIOTFactoryImpl$1.read(RDFParserRegistry.java:142) 
    at org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:859) 
    at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:687) 
    at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:208) 
    at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:141) 
    at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:130) 
    at org.apache.jena.riot.adapters.AdapterFileManager.readModelWorker(AdapterFileManager.java:291) 
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:369) 
    at jenasemweb.SparqlQuery03.main(SparqlQuery03.java:44) 

我是新來這個,非常新。所以所有的幫助表示讚賞。

+1

因爲@jkbkot指出錯誤與解析你的一個數據文件的問題有關,而沒有在錯誤中給出的位置('[line:119,col:27]')的人發佈相關文件的片段不能再幫助你 – RobV

+0

關於你的數據的一個註釋:我在http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/rawal.n3中看到許多形式爲foaf:person的三元組一個rdfs:class.'應該使用['rdfs:Class'](http://www.w3.org/TR/rdf-schema/#ch_class)(注意大寫'C')。 –

+0

抱歉,我在複製粘貼提供的代碼時犯了一個錯誤,它抱怨的文件是http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/hanna.n3 –

回答

1

該錯誤與您的SPARQL查詢無關。這是關於解析文件http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/hanna.n3(至少在你提供的代碼段中的44行)的問題。
我試圖運行該文件,我得到一個異常上線36(不44),即http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/akundi.v2.n3

這也許可以是一個URI以點結尾的問題,可能在的對象三倍。是否有可能使用Jena 2.11.0或更高版本?

請參閱https://issues.apache.org/jira/browse/JENA-584 - >您可以嘗試升級到耶拿2.11.1。

+0

感謝您的回答;但我正在使用jena的最後一個版本。 – Nanis

+0

@ user3347859因此它可能是文件或其他錯誤中的語法問題。查看該文件中第119行的內容很有用。 –

+0

@ user3347859這將很難診斷,因爲我們在這裏沒有任何文件副本,我們可以依賴保持不變。我剛剛拉下了http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/rawal.n3,耶拿的'rdfcat'讀取它沒有問題。你沒有告訴我們改變了嗎? :) –