KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: PatternStage.java,v 1.24 2005/02/21 11:52:15 andy_seaborne Exp $
5 */

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 /**
17     A PatternStage is a Stage that handles some bunch of related patterns; those patterns
18     are encoded as Triples.
19     
20     @author hedgehog
21 */

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     /**
39         Answer an array of sets exactly as long as the argument array of Triples.
40         The i'th element of the answer is the set of all variables that have been
41         matched when the i'th triple has been matched.
42     */

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     /**
54         Answer an array of ExpressionSets exactly as long as the supplied length.
55         The i'th ExpressionSet contains the prepared [against <code>map</code>]
56         expressions that can be evaluated as soon as the i'th triple has been matched.
57         By "can be evaluated as soon as" we mean that all its variables are bound.
58         The original ExpressionSet is updated by removing those elements that can
59         be so evaluated.
60         
61         @param map the Mapping to prepare Expressions against
62         @param constraints the set of constraint expressions to plant
63         @param length the number of evaluation slots available
64         @return the array of prepared ExpressionSets
65     */

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     /**
77         Find the earliest triple index where this expression can be evaluated, add it
78         to the appropriate expression set, and remove it from the original via the
79         iterator.
80     */

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     /**
93         Answer true iff this Expression can be evaluated after the index'th triple
94         has been matched, ie, all the variables of the expression have been bound.
95     */

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 JavaDoc( "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 JavaDoc 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 /*
141     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
142     All rights reserved.
143
144     Redistribution and use in source and binary forms, with or without
145     modification, are permitted provided that the following conditions
146     are met:
147
148     1. Redistributions of source code must retain the above copyright
149        notice, this list of conditions and the following disclaimer.
150
151     2. Redistributions in binary form must reproduce the above copyright
152        notice, this list of conditions and the following disclaimer in the
153        documentation and/or other materials provided with the distribution.
154
155     3. The name of the author may not be used to endorse or promote products
156        derived from this software without specific prior written permission.
157
158     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168 */

169
Popular Tags