1 6 7 package com.hp.hpl.jena.graph.query; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.query.Expression; 11 import com.hp.hpl.jena.util.CollectionFactory; 12 import com.hp.hpl.jena.util.iterator.*; 13 14 import java.util.*; 15 16 22 23 public class PatternStage extends Stage 24 { 25 protected Graph graph; 26 protected Pattern [] compiled; 27 protected ValuatorSet [] guards; 28 protected Set [] boundVariables; 29 30 public PatternStage( Graph graph, Mapping map, ExpressionSet constraints, Triple [] triples ) 31 { 32 this.graph = graph; 33 this.compiled = compile( map, triples ); 34 this.boundVariables = makeBoundVariables( triples ); 35 this.guards = makeGuards( map, constraints, triples.length ); 36 } 37 38 43 protected Set [] makeBoundVariables( Triple [] triples ) 44 { 45 int length = triples.length; 46 Set [] result = new Set[length]; 47 Set prev = CollectionFactory.createHashedSet(); 48 for (int i = 0; i < length; i += 1) 49 prev = result[i] = Util.union( prev, Util.variablesOf( triples[i] ) ); 50 return result; 51 } 52 53 66 protected ValuatorSet [] makeGuards( Mapping map, ExpressionSet constraints, int length ) 67 { 68 ValuatorSet [] result = new ValuatorSet [length]; 69 for (int i = 0; i < length; i += 1) result[i] = new ValuatorSet(); 70 Iterator it = constraints.iterator(); 71 while (it.hasNext()) 72 plantWhereFullyBound( (Expression) it.next(), it, map, result ); 73 return result; 74 } 75 76 81 protected void plantWhereFullyBound( Expression e, Iterator it, Mapping map, ValuatorSet [] es ) 82 { 83 for (int i = 0; i < boundVariables.length; i += 1) 84 if (canEval( e, i )) 85 { 86 es[i].add( e.prepare( map ) ); 87 it.remove(); 88 return; 89 } 90 } 91 92 96 protected boolean canEval( Expression e, int index ) 97 { return Expression.Util.containsAllVariablesOf( boundVariables[index], e ); } 98 99 protected Pattern [] compile( Mapping map, Triple [] triples ) 100 { return compile( compiler, map, triples ); } 101 102 protected Pattern [] compile( PatternCompiler pc, Mapping map, Triple [] source ) 103 { return PatternStageCompiler.compile( pc, map, source ); } 104 105 private static final PatternCompiler compiler = new PatternStageCompiler(); 106 107 private static int count = 0; 108 109 public synchronized Pipe deliver( final Pipe result ) 110 { 111 final Pipe stream = previous.deliver( new BufferPipe() ); 112 new Thread ( "PatternStage-" + ++count ) { public void run() { PatternStage.this.run( stream, result ); } } .start(); 113 return result; 114 } 115 116 protected void run( Pipe source, Pipe sink ) 117 { 118 try { while (stillOpen && source.hasNext()) nest( sink, source.get(), 0 ); } 119 catch (Exception e) { sink.close( e ); return; } 120 sink.close(); 121 } 122 123 protected void nest( Pipe sink, Domain current, int index ) 124 { 125 if (index == compiled.length) 126 sink.put( current.copy() ); 127 else 128 { 129 Pattern p = compiled[index]; 130 ValuatorSet guard = guards[index]; 131 ClosableIterator it = graph.find( p.asTripleMatch( current ) ); 132 while (stillOpen && it.hasNext()) 133 if (p.match( current, (Triple) it.next()) && guard.evalBool( current )) 134 nest( sink, current, index + 1 ); 135 it.close(); 136 } 137 } 138 } 139 140 169 | Popular Tags |