Ontology 使用 Jenna 返回空值的简单 Sparql 查询
Ontology simple Sparql query returning null values with Jenna
基本上我在 Protégé
上创建了我的 Ontology,并且我已经验证了我的查询有效并返回了真实值,但是一旦我真的在我的 jsp 文件中将它们可视化,我的查询返回空值我试图调查和跟踪我的错误一周但找不到错误。
这是读取 ontology 文件的代码。
public static List<String> executeQueryOneColumn(ServletContext context, String queryString) {
List<String> values = new ArrayList<>();
Model model = FileManager.get().loadModel(context.getRealPath("/") + "/resources/LolOnto1.owl");
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
System.out.println(results);
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
Resource x = solution.getResource("x");
values.add(x.getLocalName());
}
} finally {
qexec.close();
}
return values;
}
这是我执行 SparQL 查询以检索已存储在我自己的文件中的信息的地方。
@WebServlet(name = "QueryChampions", urlPatterns = {"/QueryChampions"})
public class QueryChampions extends HttpServlet {
Logger logger = LoggerFactory.getLogger(QueryChampions.class);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String champion = request.getParameter("champion");
String ori = request.getParameter("origin");
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX lol: <http://www.LeagueOfLegends.ema/LolOntology#>"
+ " "
+ "SELECT * "
+ "WHERE "
+ "{ "
+ " ?Champion lol:comesFrom ?origins "
+ "}";
queryString = String.format(queryString, champion, ori);
List<List<String>> rows = com.lol.champions.OwlReaderUtil.executeQueryTwoColumn(getServletContext(), queryString);
System.out.println(rows);
request.setAttribute("results", rows);
request.setAttribute("champion", champion);
request.setAttribute("origin", ori);
request.getRequestDispatcher("/StandardSearch").forward(request, response);
}
}
我尝试将它们列在我的 Web 应用程序的 jsp 文件中。
<tr><th>Champion</th><th>Origin</th></tr>
<c:forEach items="${results}" var="row" >
<tr>
<c:forEach items="${row}" var="v" >
<td>
${v}
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
问题与我的 executeOneColumn
函数有关,因为我传递了 getResource("x")
并且在我的 SparQl 查询中我得到了 ?Champion
和 ?Origins
,所以如果我想要获得 x
我需要将所需元素之一重命名为 x
.
基本上我在 Protégé
上创建了我的 Ontology,并且我已经验证了我的查询有效并返回了真实值,但是一旦我真的在我的 jsp 文件中将它们可视化,我的查询返回空值我试图调查和跟踪我的错误一周但找不到错误。
这是读取 ontology 文件的代码。
public static List<String> executeQueryOneColumn(ServletContext context, String queryString) {
List<String> values = new ArrayList<>();
Model model = FileManager.get().loadModel(context.getRealPath("/") + "/resources/LolOnto1.owl");
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
System.out.println(results);
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
Resource x = solution.getResource("x");
values.add(x.getLocalName());
}
} finally {
qexec.close();
}
return values;
}
这是我执行 SparQL 查询以检索已存储在我自己的文件中的信息的地方。
@WebServlet(name = "QueryChampions", urlPatterns = {"/QueryChampions"})
public class QueryChampions extends HttpServlet {
Logger logger = LoggerFactory.getLogger(QueryChampions.class);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String champion = request.getParameter("champion");
String ori = request.getParameter("origin");
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX lol: <http://www.LeagueOfLegends.ema/LolOntology#>"
+ " "
+ "SELECT * "
+ "WHERE "
+ "{ "
+ " ?Champion lol:comesFrom ?origins "
+ "}";
queryString = String.format(queryString, champion, ori);
List<List<String>> rows = com.lol.champions.OwlReaderUtil.executeQueryTwoColumn(getServletContext(), queryString);
System.out.println(rows);
request.setAttribute("results", rows);
request.setAttribute("champion", champion);
request.setAttribute("origin", ori);
request.getRequestDispatcher("/StandardSearch").forward(request, response);
}
}
我尝试将它们列在我的 Web 应用程序的 jsp 文件中。
<tr><th>Champion</th><th>Origin</th></tr>
<c:forEach items="${results}" var="row" >
<tr>
<c:forEach items="${row}" var="v" >
<td>
${v}
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
问题与我的 executeOneColumn
函数有关,因为我传递了 getResource("x")
并且在我的 SparQl 查询中我得到了 ?Champion
和 ?Origins
,所以如果我想要获得 x
我需要将所需元素之一重命名为 x
.