001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: Query.java,v 1.41 2008/01/02 12:07:58 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.util.iterator.*;
011: import com.hp.hpl.jena.shared.*;
012:
013: import java.util.*;
014:
015: /**
016: The class of graph queries, plus some machinery (which should move) for
017: implementing them.
018:
019: @author hedgehog
020: */
021:
022: public class Query {
023: /**
024: A convenient synonym for Node.ANY, used in a match to match anything.
025: */
026: public static final Node ANY = Node.ANY;
027:
028: /**
029: A query variable called "S".
030: */
031: public static final Node S = Node.createVariable("S");
032: /**
033: A query variable called "P".
034: */
035: public static final Node P = Node.createVariable("P");
036: /**
037: A query variable called "O".
038: */
039: public static final Node O = Node.createVariable("O");
040: /**
041: A query variable called "X".
042: */
043: public static final Node X = Node.createVariable("X");
044: /**
045: A query variable called "Y".
046: */
047: public static final Node Y = Node.createVariable("Y");
048: /**
049: A query variable called "Z".
050: */
051: public static final Node Z = Node.createVariable("Z");
052:
053: /**
054: Initialiser for Query; makes an empty Query [no matches, no constraints]
055: */
056: public Query() {
057: }
058:
059: /**
060: Initialiser for Query; makes a Query with its matches taken from
061: <code>pattern</code>.
062: @param pattern a Graph whose triples are used as match elements
063: */
064: public Query(Graph pattern) {
065: addMatches(pattern);
066: }
067:
068: /**
069: Exception thrown when a query variable is discovered to be unbound.
070: */
071: public static class UnboundVariableException extends JenaException {
072: public UnboundVariableException(Node n) {
073: super (n.toString());
074: }
075: }
076:
077: /**
078: Add an (S, P, O) match to the query's collection of match triples. Return
079: this query for cascading.
080: @param s the node to match the subject
081: @param p the node to match the predicate
082: @param o the node to match the object
083: @return this Query, for cascading
084: */
085: public Query addMatch(Node s, Node p, Node o) {
086: return addNamedMatch(NamedTripleBunches.anon, s, p, o);
087: }
088:
089: /**
090: Add an (S, P, O) match triple to this query to match against the graph labelled
091: with <code>name</code>. Return this query for cascading.
092:
093: @deprecated named triple patterns are not supported
094:
095: @param name the name that will identify the graph in the matching
096: @param s the node to match the subject
097: @param p the node to match the predicate
098: @param o the node to match the object
099: @return this Query, for cascading.
100: */
101: public Query addMatch(String name, Node s, Node p, Node o) {
102: return addNamedMatch(name, s, p, o);
103: }
104:
105: /**
106: Add a triple to the query's collection of match triples. Return this query
107: for cascading.
108: @param t an (S, P, O) triple to add to the collection of matches
109: @return this Query, for cascading
110: */
111: public Query addMatch(Triple t) {
112: triplePattern.add(t);
113: triples.add(NamedTripleBunches.anon, t);
114: return this ;
115: }
116:
117: private Query addNamedMatch(String name, Node s, Node p, Node o) {
118: triplePattern.add(Triple.create(s, p, o));
119: triples.add(name, Triple.create(s, p, o));
120: return this ;
121: }
122:
123: /**
124: The named bunches of triples for graph matching
125: */
126: private NamedTripleBunches triples = new NamedTripleBunches();
127:
128: private List triplePattern = new ArrayList();
129:
130: /**
131: Answer a list of the triples that have been added to this query.
132: (Note: ignores "named triples").
133:
134: @return List
135: */
136: public List getPattern() {
137: return new ArrayList(triplePattern);
138: }
139:
140: private ExpressionSet constraint = new ExpressionSet();
141:
142: public ExpressionSet getConstraints() {
143: return constraint;
144: }
145:
146: public Query addConstraint(Expression e) {
147: if (e.isApply()
148: && e.getFun().equals(ExpressionFunctionURIs.AND))
149: for (int i = 0; i < e.argCount(); i += 1)
150: addConstraint(e.getArg(i));
151: else if (e.isApply()
152: && e.argCount() == 2
153: && e.getFun().equals(
154: ExpressionFunctionURIs.Q_StringMatch))
155: constraint.add(Rewrite.rewriteStringMatch(e));
156: else
157: constraint.add(e);
158: return this ;
159: }
160:
161: /**
162: Add all the (S, P, O) triples of <code>p</code> to this Query as matches.
163: */
164: private void addMatches(Graph p) {
165: ClosableIterator it = GraphUtil.findAll(p);
166: while (it.hasNext())
167: addMatch((Triple) it.next());
168: }
169:
170: public ExtendedIterator executeBindings(Graph g, Node[] results) {
171: return executeBindings(args().put(NamedTripleBunches.anon, g),
172: results);
173: }
174:
175: public ExtendedIterator executeBindings(Graph g, List stages,
176: Node[] results) {
177: return executeBindings(stages, args().put(
178: NamedTripleBunches.anon, g), results);
179: }
180:
181: public ExtendedIterator executeBindings(NamedGraphMap args,
182: Node[] nodes) {
183: return executeBindings(new ArrayList(), args, nodes);
184: }
185:
186: /**
187: the standard "default" implementation of executeBindings.
188: */
189: public ExtendedIterator executeBindings(List outStages,
190: NamedGraphMap args, Node[] nodes) {
191: SimpleQueryEngine e = new SimpleQueryEngine(triples,
192: sortMethod, constraint);
193: ExtendedIterator result = e.executeBindings(outStages, args,
194: nodes);
195: lastQueryEngine = e;
196: return result;
197: }
198:
199: private SimpleQueryEngine lastQueryEngine = null;
200:
201: /**
202: @deprecated use getPattern for the raw triples
203: */
204: public NamedTripleBunches getTriples() {
205: return triples;
206: }
207:
208: /** mapping of graph name -> graph */
209: private NamedGraphMap argMap = new NamedGraphMap();
210:
211: public NamedGraphMap args() {
212: return argMap;
213: }
214:
215: public TripleSorter getSorter() {
216: return sortMethod;
217: }
218:
219: public void setTripleSorter(TripleSorter ts) {
220: sortMethod = ts == null ? TripleSorter.dontSort : ts;
221: }
222:
223: /**
224: @deprecated - use TripleSorter.dontSort instead.
225: */
226: public static final TripleSorter dontSort = TripleSorter.dontSort;
227:
228: private TripleSorter sortMethod = TripleSorter.dontSort;
229:
230: public int getVariableCount() {
231: return lastQueryEngine.getVariableCount();
232: }
233: }
234:
235: /*
236: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
237: All rights reserved.
238:
239: Redistribution and use in source and binary forms, with or without
240: modification, are permitted provided that the following conditions
241: are met:
242:
243: 1. Redistributions of source code must retain the above copyright
244: notice, this list of conditions and the following disclaimer.
245:
246: 2. Redistributions in binary form must reproduce the above copyright
247: notice, this list of conditions and the following disclaimer in the
248: documentation and/or other materials provided with the distribution.
249:
250: 3. The name of the author may not be used to endorse or promote products
251: derived from this software without specific prior written permission.
252:
253: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
254: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
255: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
256: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
258: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
259: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
260: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263: */
|