Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package winequery;
- import com.hp.hpl.jena.query.Query;
- import com.hp.hpl.jena.query.QueryExecution;
- import com.hp.hpl.jena.query.QueryExecutionFactory;
- import com.hp.hpl.jena.query.QueryFactory;
- import com.hp.hpl.jena.query.QuerySolution;
- import com.hp.hpl.jena.rdf.model.Model;
- import com.hp.hpl.jena.rdf.model.ModelFactory;
- import com.hp.hpl.jena.util.FileManager;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.allwines.Wine;
- import prologjava.QueryWineDatabase;
- /**
- *
- * @author andreas
- */
- public class ontologyquery {
- public String wineName, winery, region, body, flavor, sugar, color;
- Model model;
- Wine allWine;
- Wine loopWines[];
- String prologPredicate = "";
- // Pointer to RDF file to read.
- String inputFileName = "wine.rdf";
- public ontologyquery() throws Exception {
- try {
- // Model for reading wine.rdf
- model = ModelFactory.createDefaultModel();
- //Read the wine file and throw an error if it fails
- InputStream in = FileManager.get().open(inputFileName);
- if (in == null) {
- throw new IllegalArgumentException("File: " + inputFileName + " not found");
- }
- // read the RDF/XML file
- model.read(in, "");
- predicatesToFile();
- } catch (IOException ex) {
- Logger.getLogger(ontologyquery.class.getName()).log(Level.SEVERE, null, ex);
- }
- Wine w = new Wine();
- String prologPredicatsFile = "PrologPredicates.pro";
- QueryWineDatabase qwd = new QueryWineDatabase(prologPredicatsFile);
- String winesFromRegion = ("wine(Output,_,_,_,_,_,_)");
- ArrayList<String> qp = qwd.queryProlog(winesFromRegion);
- // Picks a random from the arraylist
- Random r = new Random();
- int i = r.nextInt(qp.size());
- String s = qp.get(i);
- System.out.println(s);
- w.setWineName(s);
- Writer writer = new StringWriter();
- javax.xml.bind.JAXBContext jaxbCtx;
- javax.xml.bind.Marshaller marshaller;
- try {
- jaxbCtx = javax.xml.bind.JAXBContext.newInstance(w.getClass().getPackage().getName());
- marshaller = jaxbCtx.createMarshaller();
- marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
- marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
- marshaller.marshal(w, writer);
- } catch (javax.xml.bind.JAXBException ex) {
- // XXXTODO Handle exception
- java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
- }
- String xmlResult = writer.toString();
- System.out.println(xmlResult);
- }
- /**
- *
- * Run the Query
- *
- * @param args
- */
- public static void main(String[] args) {
- try {
- ontologyquery oq = new ontologyquery();
- } catch (Exception ex) {
- Logger.getLogger(ontologyquery.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- *
- * This method queries the ontology for all wines and gives their
- * different resources
- *
- * @param search The search term from the web service
- * @return the queryresult in XML form
- */
- public String querySparql() {
- String queryString
- = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
- + "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
- + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n"
- + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
- + "PREFIX wine:<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n"
- + "select distinct ?wine ?winery ?region ?flavor ?body ?sugar ?color where {\n"
- + "?a ?p ?winename.\n"
- + "?wine ?p ?winery .\n"
- + "?a wine:locatedIn ?region .\n"
- + " { ?a wine:hasMaker ?winery . }\n"
- + "OPTIONAL { ?a wine:hasFlavor ?flavor .}\n"
- + "OPTIONAL { ?a wine:hasBody ?body . }\n"
- + "OPTIONAL { ?a wine:hasSugar ?sugar }\n"
- + "OPTIONAL {?a wine:hasColor ?color}}";
- Query query = QueryFactory.create(
- queryString);
- allWine = new Wine();
- //Run query
- try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
- // Results from query
- Iterator<QuerySolution> results = qexec.execSelect();
- while (results.hasNext()) {
- // Parse through the result and add the winery to arraylist
- QuerySolution row = results.next();
- wineName = row.getResource("wine").getLocalName();
- winery = row.getResource("winery").getLocalName();
- region = row.getResource("region").getLocalName();
- if (row.getResource("body") != null) {
- body = row.getResource("body").getLocalName();
- } else {
- body = "no_bootay";
- }
- if (row.getResource("sugar") != null) {
- sugar = row.getResource("sugar").getLocalName();
- } else {
- sugar = "no_sugar";
- }
- if (row.getResource("flavor") != null) {
- flavor = row.getResource("flavor").getLocalName();
- } else {
- flavor = "no_flavor";
- }
- if (row.getResource("color") != null) {
- color = row.getResource("color").getLocalName();
- } else {
- color = "no_color";
- }
- prologPredicate += "wine(" + wineName.toLowerCase() + "," + winery.toLowerCase() + "," + region.toLowerCase() + "," + body.toLowerCase() + "," + flavor.toLowerCase() + "," + sugar.toLowerCase() + "," + color.toLowerCase() + ").\n";
- }
- qexec.close();
- }
- return prologPredicate;
- }
- void predicatesToFile() throws IOException {
- File file = new File("PrologPredicates.pro");
- file.createNewFile();
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
- writer.write(querySparql());
- writer.flush();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement