Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package winequery;
  7.  
  8. import com.hp.hpl.jena.query.Query;
  9. import com.hp.hpl.jena.query.QueryExecution;
  10. import com.hp.hpl.jena.query.QueryExecutionFactory;
  11. import com.hp.hpl.jena.query.QueryFactory;
  12. import com.hp.hpl.jena.query.QuerySolution;
  13. import com.hp.hpl.jena.rdf.model.Model;
  14. import com.hp.hpl.jena.rdf.model.ModelFactory;
  15. import com.hp.hpl.jena.util.FileManager;
  16. import java.io.BufferedWriter;
  17. import java.io.File;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.StringWriter;
  22. import java.io.Writer;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.Random;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import org.allwines.Wine;
  29. import prologjava.QueryWineDatabase;
  30.  
  31. /**
  32. *
  33. * @author andreas
  34. */
  35. public class ontologyquery {
  36.  
  37. public String wineName, winery, region, body, flavor, sugar, color;
  38. Model model;
  39. Wine allWine;
  40. Wine loopWines[];
  41. String prologPredicate = "";
  42.  
  43. // Pointer to RDF file to read.
  44. String inputFileName = "wine.rdf";
  45.  
  46. public ontologyquery() throws Exception {
  47.  
  48. try {
  49. // Model for reading wine.rdf
  50. model = ModelFactory.createDefaultModel();
  51.  
  52. //Read the wine file and throw an error if it fails
  53. InputStream in = FileManager.get().open(inputFileName);
  54. if (in == null) {
  55. throw new IllegalArgumentException("File: " + inputFileName + " not found");
  56. }
  57.  
  58. // read the RDF/XML file
  59. model.read(in, "");
  60.  
  61. predicatesToFile();
  62. } catch (IOException ex) {
  63. Logger.getLogger(ontologyquery.class.getName()).log(Level.SEVERE, null, ex);
  64. }
  65.  
  66. Wine w = new Wine();
  67. String prologPredicatsFile = "PrologPredicates.pro";
  68.  
  69. QueryWineDatabase qwd = new QueryWineDatabase(prologPredicatsFile);
  70. String winesFromRegion = ("wine(Output,_,_,_,_,_,_)");
  71. ArrayList<String> qp = qwd.queryProlog(winesFromRegion);
  72.  
  73. // Picks a random from the arraylist
  74. Random r = new Random();
  75. int i = r.nextInt(qp.size());
  76. String s = qp.get(i);
  77. System.out.println(s);
  78. w.setWineName(s);
  79.  
  80. Writer writer = new StringWriter();
  81. javax.xml.bind.JAXBContext jaxbCtx;
  82. javax.xml.bind.Marshaller marshaller;
  83. try {
  84. jaxbCtx = javax.xml.bind.JAXBContext.newInstance(w.getClass().getPackage().getName());
  85. marshaller = jaxbCtx.createMarshaller();
  86. marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
  87. marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  88. marshaller.marshal(w, writer);
  89. } catch (javax.xml.bind.JAXBException ex) {
  90. // XXXTODO Handle exception
  91. java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
  92. }
  93.  
  94.  
  95. String xmlResult = writer.toString();
  96. System.out.println(xmlResult);
  97.  
  98. }
  99.  
  100. /**
  101. *
  102. * Run the Query
  103. *
  104. * @param args
  105. */
  106. public static void main(String[] args) {
  107. try {
  108. ontologyquery oq = new ontologyquery();
  109. } catch (Exception ex) {
  110. Logger.getLogger(ontologyquery.class.getName()).log(Level.SEVERE, null, ex);
  111. }
  112. }
  113.  
  114. /**
  115. *
  116. * This method queries the ontology for all wines and gives their
  117. * different resources
  118. *
  119. * @param search The search term from the web service
  120. * @return the queryresult in XML form
  121. */
  122. public String querySparql() {
  123.  
  124. String queryString
  125. = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
  126. + "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
  127. + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n"
  128. + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
  129. + "PREFIX wine:<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n"
  130. + "select distinct ?wine ?winery ?region ?flavor ?body ?sugar ?color where {\n"
  131. + "?a ?p ?winename.\n"
  132. + "?wine ?p ?winery .\n"
  133. + "?a wine:locatedIn ?region .\n"
  134. + " { ?a wine:hasMaker ?winery . }\n"
  135. + "OPTIONAL { ?a wine:hasFlavor ?flavor .}\n"
  136. + "OPTIONAL { ?a wine:hasBody ?body . }\n"
  137. + "OPTIONAL { ?a wine:hasSugar ?sugar }\n"
  138. + "OPTIONAL {?a wine:hasColor ?color}}";
  139.  
  140. Query query = QueryFactory.create(
  141. queryString);
  142. allWine = new Wine();
  143.  
  144. //Run query
  145. try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
  146.  
  147. // Results from query
  148. Iterator<QuerySolution> results = qexec.execSelect();
  149.  
  150. while (results.hasNext()) {
  151. // Parse through the result and add the winery to arraylist
  152. QuerySolution row = results.next();
  153. wineName = row.getResource("wine").getLocalName();
  154. winery = row.getResource("winery").getLocalName();
  155. region = row.getResource("region").getLocalName();
  156.  
  157. if (row.getResource("body") != null) {
  158. body = row.getResource("body").getLocalName();
  159.  
  160. } else {
  161. body = "no_bootay";
  162.  
  163. }
  164. if (row.getResource("sugar") != null) {
  165. sugar = row.getResource("sugar").getLocalName();
  166.  
  167. } else {
  168. sugar = "no_sugar";
  169. }
  170. if (row.getResource("flavor") != null) {
  171. flavor = row.getResource("flavor").getLocalName();
  172.  
  173. } else {
  174. flavor = "no_flavor";
  175. }
  176. if (row.getResource("color") != null) {
  177. color = row.getResource("color").getLocalName();
  178. } else {
  179. color = "no_color";
  180. }
  181.  
  182. prologPredicate += "wine(" + wineName.toLowerCase() + "," + winery.toLowerCase() + "," + region.toLowerCase() + "," + body.toLowerCase() + "," + flavor.toLowerCase() + "," + sugar.toLowerCase() + "," + color.toLowerCase() + ").\n";
  183. }
  184. qexec.close();
  185.  
  186. }
  187.  
  188. return prologPredicate;
  189. }
  190.  
  191. void predicatesToFile() throws IOException {
  192.  
  193. File file = new File("PrologPredicates.pro");
  194. file.createNewFile();
  195. try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
  196. writer.write(querySparql());
  197. writer.flush();
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement