1 5 package jena.examples.rdql ; 6 7 import com.hp.hpl.jena.rdf.model.* ; 8 import com.hp.hpl.jena.rdql.* ; 9 10 import java.util.* ; 11 12 public class rdql_sc 13 { 14 static public void main(String [] argv) 15 { 16 try { 17 Model model = ModelFactory.createDefaultModel() ; 18 model.read("file:hierarchy.nt", "N-TRIPLE") ; 19 20 String queryString = 21 "SELECT * " + 22 "WHERE (?x, <rdfs:subClassOf>, ?y) "+ 23 "USING "+ 24 " ex FOR <http://hpl.hp.com/semweb/JenaTutorial/RDQL/schema#> ,"+ 25 " rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ," + 26 " rdfs FOR <http://www.w3.org/2000/01/rdf-schema#>" 27 ; 28 29 Query query = new Query(queryString) ; 30 query.setSource(model); 31 QueryExecution qe = new QueryEngine(query) ; 32 33 QueryResults results = qe.exec() ; 34 for ( Iterator iter = results ; iter.hasNext() ; ) 35 { 36 ResultBinding res = (ResultBinding)iter.next() ; 37 String x = res.get("x").toString() ; 38 String y = res.get("y").toString() ; 39 40 x = x.substring(x.indexOf('#')+1) ; 41 y = y.substring(y.indexOf('#')+1) ; 42 43 System.out.println(x+" subClassOf "+y) ; 44 } 45 results.close() ; 47 48 } catch (Exception ex) 49 { 50 System.err.println("Exception: "+ex) ; 51 ex.printStackTrace(System.err) ; 52 } 53 } 54 } 55 56 82 | Popular Tags |