KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > Query


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: Query.java,v 1.36 2005/02/21 11:52:25 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.util.iterator.*;
11 import com.hp.hpl.jena.shared.*;
12
13 import java.util.*;
14
15 /**
16     The class of graph queries, plus some machinery (which should move) for
17     implementing them.
18
19     @author hedgehog
20 */

21
22 public class Query
23     {
24     /**
25         A convenient synonym for Node.ANY, used in a match to match anything.
26     */

27     public static final Node ANY = Node.ANY;
28     
29     /**
30         A query variable called "S".
31     */

32     public static final Node S = Node.createVariable( "S" );
33     /**
34         A query variable called "P".
35     */

36     public static final Node P = Node.createVariable( "P" );
37     /**
38         A query variable called "O".
39     */

40     public static final Node O = Node.createVariable( "O" );
41     /**
42         A query variable called "X".
43     */

44     public static final Node X = Node.createVariable( "X" );
45     /**
46         A query variable called "Y".
47     */

48     public static final Node Y = Node.createVariable( "Y" );
49     /**
50         A query variable called "Z".
51     */

52     public static final Node Z = Node.createVariable( "Z" );
53         
54     /**
55         Initialiser for Query; makes an empty Query [no matches, no constraints]
56     */

57     public Query()
58         { }
59         
60     /**
61         Initialiser for Query; makes a Query with its matches taken from
62         <code>pattern</code>.
63         @param pattern a Graph whose triples are used as match elements
64     */

65     public Query( Graph pattern )
66         { addMatches( pattern ); }
67
68     /**
69         Exception thrown when a query variable is discovered to be unbound.
70     */

71     public static class UnboundVariableException extends JenaException
72         { public UnboundVariableException( Node n ) { super( n.toString() ); } }
73                         
74     /**
75         Add an (S, P, O) match to the query's collection of match triples. Return
76         this query for cascading.
77         @param s the node to match the subject
78         @param p the node to match the predicate
79         @param o the node to match the object
80         @return this Query, for cascading
81     */

82     public Query addMatch( Node s, Node p, Node o )
83         { return addNamedMatch( NamedTripleBunches.anon, s, p, o ); }
84         
85     /**
86         Add a triple to the query's collection of match triples. Return this query
87         for cascading.
88         @param t an (S, P, O) triple to add to the collection of matches
89         @return this Query, for cascading
90     */

91     public Query addMatch( Triple t )
92         { triples.add( NamedTripleBunches.anon, t );
93         return this; }
94         
95     /**
96         Add an (S, P, O) match triple to this query to match against the graph labelled
97         with <code>name</code>. Return this query for cascading.
98         @param name the name that will identify the graph in the matching
99         @param s the node to match the subject
100         @param p the node to match the predicate
101         @param o the node to match the object
102         @return this Query, for cascading.
103     */

104     public Query addMatch( String JavaDoc name, Node s, Node p, Node o )
105         { return addNamedMatch( name, s, p, o ); }
106         
107     private ExpressionSet constraint = new ExpressionSet();
108     
109     public ExpressionSet getConstraints()
110         { return constraint; }
111         
112     public Query addConstraint( Expression e )
113         {
114         if (e.isApply() && e.getFun().equals( ExpressionFunctionURIs.AND ))
115            for (int i = 0; i < e.argCount(); i += 1) addConstraint( e.getArg( i ) );
116         else if (e.isApply() && e.argCount() == 2 && e.getFun().equals( ExpressionFunctionURIs.Q_StringMatch))
117             constraint.add( Rewrite.rewriteStringMatch( e ) );
118         else
119             constraint.add( e );
120         return this;
121         }
122     
123     /**
124         Add all the (S, P, O) triples of <code>p</code> to this Query as matches.
125     */

126     private void addMatches( Graph p )
127         {
128         ClosableIterator it = GraphUtil.findAll( p );
129         while (it.hasNext()) addMatch( (Triple) it.next() );
130         }
131
132     public ExtendedIterator executeBindings( Graph g, Node [] results )
133         { return executeBindings( args().put( NamedTripleBunches.anon, g ), results ); }
134                 
135     public ExtendedIterator executeBindings( Graph g, List stages, Node [] results )
136         { return executeBindings( stages, args().put( NamedTripleBunches.anon, g ), results ); }
137     
138     public ExtendedIterator executeBindings( NamedGraphMap args, Node [] nodes )
139         { return executeBindings( new ArrayList(), args, nodes ); }
140         
141     /**
142         the standard "default" implementation of executeBindings.
143     */

144     public ExtendedIterator executeBindings( List outStages, NamedGraphMap args, Node [] nodes )
145         {
146         SimpleQueryEngine e = new SimpleQueryEngine( triples, sortMethod, constraint );
147         ExtendedIterator result = e.executeBindings( outStages, args, nodes );
148         lastQueryEngine = e;
149         return result;
150         }
151     
152     private SimpleQueryEngine lastQueryEngine = null;
153     
154     /** The named bunches of triples for graph matching */
155     
156     private NamedTripleBunches triples = new NamedTripleBunches();
157     
158     public NamedTripleBunches getTriples()
159         { return triples; }
160         
161     /** mapping of graph name -> graph */
162     private NamedGraphMap argMap = new NamedGraphMap();
163             
164     public NamedGraphMap args()
165         { return argMap; }
166
167     private Query addNamedMatch( String JavaDoc name, Node s, Node p, Node o )
168         { triples.add( name, Triple.create( s, p, o ) );
169         return this; }
170
171     public TripleSorter getSorter()
172         { return sortMethod; }
173         
174     public void setTripleSorter( TripleSorter ts )
175         { sortMethod = ts == null ? TripleSorter.dontSort : ts; }
176         
177     /**
178         @deprecated - use TripleSorter.dontSort instead.
179     */

180     public static final TripleSorter dontSort = TripleSorter.dontSort;
181         
182     private TripleSorter sortMethod = TripleSorter.dontSort;
183     
184     public int getVariableCount()
185         { return lastQueryEngine.getVariableCount(); }
186     }
187
188 /*
189     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
190     All rights reserved.
191
192     Redistribution and use in source and binary forms, with or without
193     modification, are permitted provided that the following conditions
194     are met:
195
196     1. Redistributions of source code must retain the above copyright
197        notice, this list of conditions and the following disclaimer.
198
199     2. Redistributions in binary form must reproduce the above copyright
200        notice, this list of conditions and the following disclaimer in the
201        documentation and/or other materials provided with the distribution.
202
203     3. The name of the author may not be used to endorse or promote products
204        derived from this software without specific prior written permission.
205
206     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
207     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
214     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
215     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216 */

217
Popular Tags