1 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 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 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 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 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 name = (String ) 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 | Popular Tags |