Semantic Web Technologies: XML, RDF, OWL
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package edu.sc.cse; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import com.hp.hpl.jena.rdf.model.*; import com.hp.hpl.jena.vocabulary.*; import com.hp.hpl.jena.query.*; import javax.xml.ws.WebServiceRef; /** * * @author jmvidal */ public class Test extends HttpServlet { @WebServiceRef(wsdlLocation = "http://api.opencalais.com/enlighten/?wsdl") private Calais service; static String apiKey = "jp35z8payqnh5zcpasnpvxmq"; //this is mine, please get your own! static String paramsXML = "<c:params xmlns:c=\"http://s.opencalais.com/1/pred/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" + "<c:processingDirectives c:contentType=\"text/txt\" c:outputFormat=\"xml/rdf\">" + "</c:processingDirectives>" + "<c:userDirectives c:allowDistribution=\"true\" c:allowSearch=\"true\" c:externalID=\"17cabs901\" c:submitter=\"jmvidal\">" + "</c:userDirectives>" + "<c:externalMetadata></c:externalMetadata></c:params>"; /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { //create RDF model Model model = ModelFactory.createDefaultModel(); java.lang.String content = request.getParameter("text"); out.println("<html><head></head><title>OpenCalais Test</title>"); out.println("<body>"); out.println("<h1>OpenCalaisTest</h1>" + "<form action=\"Test\" method=\"POST\">" + "<textarea name=\"text\" rows=\"20\" cols=\"40\">"); if (content != null) { //fill in content from previous quey. out.println(content); } out.println("</textarea><br/>" + "<input type=\"submit\" value=\"Send\"/> <input type=\"reset\"/></form>"); try { // Call Web Service Operation //first, create the port. edu.sc.cse.CalaisSoap port = service.getCalaisSoap(); if (content != null) {//the user submitted some content to process //call calais, get the result, which will be in RDF java.lang.String result = port.enlighten(apiKey, content, paramsXML); StringReader resultIn = new StringReader(result); model.read(resultIn, ""); //Various ways to view the raw response out.println("<!--"); model.write(out,"RDF/XML-ABBREV"); //output in a comment cse its xml. out.println("-->"); // model.write(out,"N3"); // model.write(out); //List all triples by getting rid of all the tags, just the SVO out.println("<h2>The Triples</h2>"); out.println("<table border=\"1\"><tr><th>Subject</th><th>Verb/Predicate</th><th>Object</th></tr>"); StmtIterator iter = model.listStatements(); while (iter.hasNext()) { Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object out.println("<tr><td>" + subject + "</td><td>" + predicate.toString() + "</td><td>" + object + "</td></tr>\n"); } out.println("</table>"); //Some highly ineffecient functions for finding the People's names out.println("<h2>The People</h2><ol>"); //Find all statements with a RDF.type property whose oject ends in Person. //That is, find all things of type Person. StmtIterator iter2 = model.listStatements( new SimpleSelector(null, RDF.type, (RDFNode) null) { public boolean selects(Statement s) { String object = s.getResource().toString(); return object.endsWith("Person"); } }); // ResIterator iter2 = model.listResourcesWithProperty(RDF.type); if (iter2.hasNext()) { while (iter2.hasNext()) { Statement stmt = iter2.nextStatement(); Resource subject = stmt.getSubject(); //find this subject in the list of all triples iter = model.listStatements(); while (iter.hasNext()){ Statement stmt2 = iter.nextStatement(); Resource subject2 = stmt2.getSubject(); String predicate = stmt2.getPredicate().toString(); if (subject2.equals(subject) && predicate.endsWith("name")){ //we found him out.println("<li>" + stmt2.getObject() + "</li>"); } } } } else { out.println("No People found."); } out.println("</ol>"); //Find the Persons again but this time using SPARQL out.println("<h2>The People (SPARQL query)</h2><ol>"); // Create a new query String queryString = "PREFIX calaispred: <http://s.opencalais.com/1/pred/>" + "PREFIX calaistype: <http://s.opencalais.com/1/type/em/e/>" + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "SELECT ?name " + "WHERE {" + " ?person rdf:type calaistype:Person . " + " ?person calaispred:name ?name . " + " }"; Query query = QueryFactory.create(queryString); // Execute the query and obtain results QueryExecution qe = QueryExecutionFactory.create(query, model); com.hp.hpl.jena.query.ResultSet results = qe.execSelect(); while (results.hasNext()){ out.println("<li>" + results.next() + "</li>"); } // Important - free up resources used running the query qe.close(); out.println("</ol>"); } } catch (Exception ex) { out.println("ERROR: " + ex); } out.println("</body></html>"); } finally { out.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold> }
27 of 41