Search code examples
javajspjenaontology

Ontology simple Sparql query returning null values with Jenna


Basically I created my Ontology on Protégé, and I already verified my queries that are working and returning real values but once I tru to visualize them on my jsp file, my queries are returning null values I tried to investigate and track my error for a week but couldn't find the error.

Here's my code to read my ontology file.

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;

    }

And this is where I do my SparQL query to retrieve my informations already stored on my own file.


@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);
   
    }

}

And I try to list them on my web application on a jsp file.

        <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>

Solution

  • The problem was related to my executeOneColumn function since I was passing getResource("x") and in my SparQl query I was getting ?Champion and ?Origins, so if I wanted to get the x I need to rename one of the desired elements to x.