KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > examples > rdql > rdql_sc


1 /*
2  * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

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 JavaDoc[] argv)
15     {
16         try {
17             Model model = ModelFactory.createDefaultModel() ;
18             model.read("file:hierarchy.nt", "N-TRIPLE") ;
19
20             String JavaDoc 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 JavaDoc x = res.get("x").toString() ;
38                 String JavaDoc 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             // Always close result iterator - frees work space.
46
results.close() ;
47             
48         } catch (Exception JavaDoc ex)
49         {
50             System.err.println("Exception: "+ex) ;
51             ex.printStackTrace(System.err) ;
52         }
53     }
54 }
55
56 /*
57  * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, LP
58  * All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  * 1. Redistributions of source code must retain the above copyright
64  * notice, this list of conditions and the following disclaimer.
65  * 2. Redistributions in binary form must reproduce the above copyright
66  * notice, this list of conditions and the following disclaimer in the
67  * documentation and/or other materials provided with the distribution.
68  * 3. The name of the author may not be used to endorse or promote products
69  * derived from this software without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
72  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
74  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
75  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
76  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
77  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
78  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
79  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
80  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81  */

82
Popular Tags