01 package examples;
02
03 import com.hp.hpl.jena.ontology.OntModel;
04 import com.hp.hpl.jena.query.Query;
05 import com.hp.hpl.jena.query.QueryExecution;
06 import com.hp.hpl.jena.query.QueryExecutionFactory;
07 import com.hp.hpl.jena.query.QueryFactory;
08 import com.hp.hpl.jena.query.ResultSet;
09 import com.hp.hpl.jena.rdf.model.Resource;
10 import dlejena.DLEJenaParameters;
11 import dlejena.DLEJenaReasoner;
12 import java.io.File;
13 import java.net.URI;
14
15 /**
16 * This is a simple example of defining a SPARQL query using the
17 * Jena API over the ABox of DLEJena. The same approach can be
18 * used on the TBox.
19 *
20 * @author Georgios Meditskos <gmeditsk@csd.auth.gr>
21 */
22 public class SparqlQuery {
23
24 public static void main(String[] args) {
25
26
27 /*
28 * The physical URI (local path or Web address) of the ontology.
29 * This ontology has been obtained from the Web site of KAON2:
30 * http://kaon2.semanticweb.org/download/test_ontologies.zip
31 */
32 URI ontology = new File("src/examples/wine_0.owl").toURI();
33
34 /*
35 * Define an instance of the DLEJenaReasoner class.
36 * This class provides the necessary nethods for
37 * using the DLEJena library
38 */
39 DLEJenaReasoner dle = new DLEJenaReasoner();
40
41 /*
42 * Register the ontology to the reasoner.
43 */
44 dle.register(ontology);
45
46 /*
47 * Initiate the reasoner. This phase involves the
48 * separation of the TBox from ABox axioms (using the OWLAPI),
49 * the TBox reasoning procedure (using Pellet), the generation of the
50 * dynamic entailments and the ABox reasoning procedure (using
51 * the forwardRete rule engine of Jena).
52 */
53 dle.initialize();
54
55 /*
56 * Obtain a reference to the ABox inferred model.
57 */
58 OntModel abox = dle.getABox();
59
60 /*
61 * Define the SPARQL query, initialize the SPARQL engine and
62 * execute the query over the abox model.
63 */
64 String queryString = "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>" +
65 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
66 "SELECT ?i " +
67 "WHERE {" +
68 "?i rdf:type wine:Wine. " +
69 "?i wine:hasFlavor wine:Strong .}";
70
71 Query query = QueryFactory.create(queryString);
72 QueryExecution qexec = QueryExecutionFactory.create(query, abox);
73
74 System.out.println("");
75 System.out.println("Wines with strong flavor:");
76 ResultSet results = qexec.execSelect();
77 while (results.hasNext()) {
78 Resource s = (Resource) results.nextSolution().get("?i").as(Resource.class);
79 System.out.println(" - " + s.getLocalName());
80 }
81 }
82 }
|