当我尝试检索存储在jena模型中的数据时,我得到一个空结果集。
这是加载数据的代码(我为了简洁而删除了导入)
package basic;
//imports here
public class DataLoaderQn {
public static void main(String[] args) throws FileNotFoundException {
String resourceURI = "http://www.abc123.com/riskmodelling/static/risks";
String directory = "D:/mywork/dp/projs/static-risks/data/test";
Dataset dataset = TDBFactory.createDataset(directory);
Model model = null;
try {
dataset.begin(ReadWrite.WRITE);
model = dataset.getDefaultModel();
model.enterCriticalSection(Lock.WRITE);
model = model.removeAll();
Resource projectNatureRes = model.createResource(resourceURI+":PROJECT_NATURE");
Property projectNatureRiskProps = model.createProperty(resourceURI + ":COMPLEX_FUNCTIONALITY");
Bag projectNatureRisks = model.createBag();
projectNatureRisks.add("More defects");
projectNatureRisks.add("Effort estimation inaccurate");
projectNatureRes.addProperty(projectNatureRiskProps, projectNatureRisks);
Property migrationRiskProps = model.createProperty(resourceURI + ":MIGRATION");
Bag migrationRisks = model.createBag();
migrationRisks.add("Lack of knowledge of exsting application");
migrationRisks.add("Documentation not available");
projectNatureRes.addProperty(migrationRiskProps, migrationRisks);
model.write(System.out);
model.write(new FileOutputStream(new File(directory + "/Project_risk.ttl")), "N-TRIPLES");
model.commit();
dataset.commit();
TDB.sync(model);
} finally {
dataset.end();
model.leaveCriticalSection();
}
}
}
这就是我从java程序中读取数据的方式
public class ReadBasicQn {
public static void main(String[] args) {
String directory = "D:/mywork/dp/projs/static-risks/data/test";
Dataset dataset = TDBFactory.createDataset(directory);
Model model = null;
ResultSet rs;
QueryExecution qexeExecution = null;
try{
/*model = ModelFactory.createDefaultModel();
TDBLoader.loadModel(model, directory + "/Project_risk.ttl");*/
model = dataset.getDefaultModel();
String queryString = "PREFIX proj: <http://www.abc123.com/riskmodelling/static/risks#> ";
queryString += "select ?risks where ";
queryString += "{proj:PROJECT_NATURE proj:COMPLEX_FUNCTIONALITY ?risks}";
String queryString2 = "SELECT * WHERE { ?s ?p ?o }";
Query q = QueryFactory.create(queryString);
qexeExecution = QueryExecutionFactory.create(q, model);
rs = qexeExecution.execSelect();
ResultSetFormatter.out(System.out, rs);
qexeExecution.close();
q = QueryFactory.create(queryString2);
qexeExecution = QueryExecutionFactory.create(q, model);
rs = qexeExecution.execSelect();
ResultSetFormatter.out(System.out, rs);
/*while(rs.hasNext()){
QuerySolution qSol = rs.nextSolution();
RDFNode n = qSol.get("risks");
System.out.println(n);
}*/
}finally{
qexeExecution.close();
}
}
}
ResultSetFormatter中第二个查询(select *)的输出显示
------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
===================================================================================================================================================================================================
| <http://www.abc123.com/riskmodelling/static/risks:PROJECT_NATURE> | <http://www.abc123.com/riskmodelling/static/risks:COMPLEX_FUNCTIONALITY> | _:b0 |
| <http://www.abc123.com/riskmodelling/static/risks:PROJECT_NATURE> | <http://www.abc123.com/riskmodelling/static/risks:MIGRATION> | _:b1 |
| _:b0 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | <http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag> |
| _:b0 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> | "More defects" |
| _:b0 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2> | "Effort estimation inaccurate" |
| _:b1 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | <http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag> |
| _:b1 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> | "Lack of knowledge of exsting application" |
| _:b1 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2> | "Documentation not available" |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
表示数据可用并正确加载(正确吗?)。但是,自定义查询将返回以下输出。
---------
| risks |
=========
---------
感谢任何帮助。我刚刚和耶拿开始,所以也许我正在做一些非常愚蠢的事情。
答案 0 :(得分:3)
有一个错字。您的前缀以#
结尾,但在您的URI中,有一个:
。这是你的前缀声明和URI,排成一行:
prefix proj: http://www.abc123.com/riskmodelling/static/risks#>
http://www.abc123.com/riskmodelling/static/risks:PROJECT_NATURE
^
|
在你的URI中,冒险[COLON] PROJECT_NATURE ,而不是冒险[HASH] PROJECT_NATURE 。您需要将前缀更改为:
prefix proj: http://www.abc123.com/riskmodelling/static/risks:>
或将您的数据更改为
Resource projectNatureRes = model.createResource(resourceURI+"#PROJECT_NATURE");
// ...and in a few other places, too