001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: SimpleQueryEngine.java,v 1.10 2008/01/02 12:07:58 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.util.iterator.*;
013:
014: /**
015: SimpleQueryEngine
016:
017: @author kers
018: */
019: public class SimpleQueryEngine {
020: private ExpressionSet constraint;
021: private NamedTripleBunches triples;
022: private TripleSorter sortMethod;
023: private int variableCount;
024:
025: public SimpleQueryEngine(NamedTripleBunches triples,
026: TripleSorter ts, ExpressionSet constraint) {
027: this .constraint = constraint;
028: this .triples = triples;
029: this .sortMethod = ts;
030: }
031:
032: int getVariableCount() {
033: return variableCount;
034: }
035:
036: public ExtendedIterator executeBindings(List outStages,
037: NamedGraphMap args, Node[] nodes) {
038: Mapping map = new Mapping(nodes);
039: ArrayList stages = new ArrayList();
040: addStages(stages, args, map);
041: if (constraint.isComplex())
042: stages.add(new ConstraintStage(map, constraint));
043: outStages.addAll(stages);
044: variableCount = map.size();
045: return filter(connectStages(stages, variableCount));
046: }
047:
048: private ExtendedIterator filter(final Stage allStages) {
049: // final Pipe complete = allStages.deliver( new BufferPipe() );
050: return new NiceIterator() {
051: private Pipe complete;
052:
053: private void ensurePipe() {
054: if (complete == null)
055: complete = allStages.deliver(new BufferPipe());
056: }
057:
058: public void close() {
059: allStages.close();
060: clearPipe();
061: }
062:
063: public Object next() {
064: ensurePipe();
065: return complete.get();
066: }
067:
068: public boolean hasNext() {
069: ensurePipe();
070: return complete.hasNext();
071: }
072:
073: private void clearPipe() {
074: int count = 0;
075: while (hasNext()) {
076: count += 1;
077: next();
078: }
079: }
080: };
081: }
082:
083: public static Cons cons(Triple pattern, Object cons) {
084: return new Cons(pattern, (Cons) cons);
085: }
086:
087: public static class Cons {
088: Triple head;
089: Cons tail;
090:
091: Cons(Triple head, Cons tail) {
092: this .head = head;
093: this .tail = tail;
094: }
095:
096: static int size(Cons L) {
097: int n = 0;
098: while (L != null) {
099: n += 1;
100: L = L.tail;
101: }
102: return n;
103: }
104: }
105:
106: private void addStages(ArrayList stages, NamedGraphMap arguments,
107: Mapping map) {
108: Iterator it2 = triples.entrySetIterator();
109: while (it2.hasNext()) {
110: Map.Entry e = (Map.Entry) it2.next();
111: String name = (String) e.getKey();
112: Cons nodeTriples = (Cons) e.getValue();
113: Graph g = arguments.get(name);
114: int nBlocks = Cons.size(nodeTriples), i = nBlocks;
115: Triple[] nodes = new Triple[nBlocks];
116: while (nodeTriples != null) {
117: nodes[--i] = nodeTriples.head;
118: nodeTriples = nodeTriples.tail;
119: }
120: nodes = sortTriples(nodes);
121: Stage next = g.queryHandler().patternStage(map, constraint,
122: nodes);
123: stages.add(next);
124: }
125: }
126:
127: private Triple[] sortTriples(Triple[] ts) {
128: return sortMethod.sort(ts);
129: }
130:
131: private Stage connectStages(ArrayList stages, int count) {
132: Stage current = Stage.initial(count);
133: for (int i = 0; i < stages.size(); i += 1)
134: current = ((Stage) stages.get(i)).connectFrom(current);
135: return current;
136: }
137: }
138:
139: /*
140: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
141: All rights reserved.
142:
143: Redistribution and use in source and binary forms, with or without
144: modification, are permitted provided that the following conditions
145: are met:
146:
147: 1. Redistributions of source code must retain the above copyright
148: notice, this list of conditions and the following disclaimer.
149:
150: 2. Redistributions in binary form must reproduce the above copyright
151: notice, this list of conditions and the following disclaimer in the
152: documentation and/or other materials provided with the distribution.
153:
154: 3. The name of the author may not be used to endorse or promote products
155: derived from this software without specific prior written permission.
156:
157: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
158: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
159: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
161: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
162: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
163: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
164: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
165: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
166: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
167: */
|