KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.graph.query;
8
9 import java.util.*;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.util.iterator.*;
13
14 /**
15     SimpleQueryEngine
16
17     @author kers
18 */

19 public class SimpleQueryEngine
20     {
21     private ExpressionSet constraint;
22     private NamedTripleBunches triples;
23     private TripleSorter sortMethod;
24     private int variableCount;
25     
26     public SimpleQueryEngine( NamedTripleBunches triples, TripleSorter ts, ExpressionSet constraint )
27         { this.constraint = constraint;
28         this.triples = triples;
29         this.sortMethod = ts; }
30         
31     int getVariableCount()
32         { return variableCount; }
33         
34     public ExtendedIterator executeBindings( List outStages, NamedGraphMap args, Node [] nodes )
35         {
36         Mapping map = new Mapping( nodes );
37         ArrayList stages = new ArrayList();
38         addStages( stages, args, map );
39         if (constraint.isComplex()) stages.add( new ConstraintStage( map, constraint ) );
40         outStages.addAll( stages );
41         variableCount = map.size();
42         return filter( connectStages( stages, variableCount ) );
43         }
44                                   
45     private ExtendedIterator filter( final Stage allStages )
46         {
47         // final Pipe complete = allStages.deliver( new BufferPipe() );
48
return new NiceIterator()
49             {
50             private Pipe complete;
51             
52             private void ensurePipe()
53                 { if (complete == null) complete = allStages.deliver( new BufferPipe() ); }
54             
55             public void close() { allStages.close(); clearPipe(); }
56             
57             public Object JavaDoc next() { ensurePipe(); return complete.get(); }
58             
59             public boolean hasNext() { ensurePipe(); return complete.hasNext(); }
60             
61             private void clearPipe()
62                 {
63                 int count = 0;
64                 while (hasNext()) { count += 1; next(); }
65                 }
66             };
67         }
68         
69     public static Cons cons( Triple pattern, Object JavaDoc cons )
70         { return new Cons( pattern, (Cons) cons ); }
71         
72     private static class Cons
73         {
74         Triple head;
75         Cons tail;
76         Cons( Triple head, Cons tail ) { this.head = head; this.tail = tail; }
77         static int size( Cons L ) { int n = 0; while (L != null) { n += 1; L = L.tail; } return n; }
78         }
79                 
80     private void addStages( ArrayList stages, NamedGraphMap arguments, Mapping map )
81         {
82         Iterator it2 = triples.entrySetIterator();
83         while (it2.hasNext())
84             {
85             Map.Entry e = (Map.Entry) it2.next();
86             String JavaDoc name = (String JavaDoc) e.getKey();
87             Cons nodeTriples = (Cons) e.getValue();
88             Graph g = arguments.get( name );
89             int nBlocks = Cons.size( nodeTriples ), i = nBlocks;
90             Triple [] nodes = new Triple[nBlocks];
91             while (nodeTriples != null)
92                 {
93                 nodes[--i] = nodeTriples.head;
94                 nodeTriples = nodeTriples.tail;
95                 }
96             nodes = sortTriples( nodes );
97             Stage next = g.queryHandler().patternStage( map, constraint, nodes );
98             stages.add( next );
99             }
100         }
101
102     private Triple [] sortTriples( Triple [] ts )
103         { return sortMethod.sort( ts ); }
104                 
105     private Stage connectStages( ArrayList stages, int count )
106         {
107         Stage current = Stage.initial( count );
108         for (int i = 0; i < stages.size(); i += 1)
109             current = ((Stage) stages.get( i )).connectFrom( current );
110         return current;
111         }
112     }
113
114 /*
115     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
116     All rights reserved.
117
118     Redistribution and use in source and binary forms, with or without
119     modification, are permitted provided that the following conditions
120     are met:
121
122     1. Redistributions of source code must retain the above copyright
123        notice, this list of conditions and the following disclaimer.
124
125     2. Redistributions in binary form must reproduce the above copyright
126        notice, this list of conditions and the following disclaimer in the
127        documentation and/or other materials provided with the distribution.
128
129     3. The name of the author may not be used to endorse or promote products
130        derived from this software without specific prior written permission.
131
132     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
133     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
134     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
135     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
136     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
137     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
138     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
139     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
140     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
141     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
142 */
Popular Tags