KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > ResultBindingImpl


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdql;
7
8 import java.util.* ;
9
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.rdf.model.* ;
12
13
14 /** A mapping from variable name to a value.
15  *
16  * @author Andy Seaborne
17  * @version $Id: ResultBindingImpl.java,v 1.4 2005/02/21 12:15:26 andy_seaborne Exp $
18  */

19
20
21 public class ResultBindingImpl implements ResultBinding
22 {
23     // We store name -> the value, not the variable itself. ResultBindings will
24
// keep the association of name-value.
25

26     ResultBindingImpl parent = null ;
27
28     List varNames = new ArrayList() ;
29     List values = new ArrayList() ; // Values or usually RDFNodes (Resource or Properties or Literals)
30
List causalTriples = null ;
31     Query query = null ;
32
33     public ResultBindingImpl(ResultBindingImpl parent) { this.parent = parent ; }
34     public ResultBindingImpl() { this(null) ; }
35     
36     public int add(String JavaDoc varName, Value value)
37     {
38         varNames.add(varName) ;
39         values.add(value) ;
40         check() ;
41         return varNames.size()-1 ;
42     }
43
44     public int add(String JavaDoc varName, RDFNode node)
45     {
46         varNames.add(varName) ;
47         values.add(node) ;
48         check() ;
49         return varNames.size()-1 ;
50     }
51
52     public void setQuery(Query q)
53     {
54         query = q ;
55     }
56     
57
58     /** Add a triple to the ResultBindingImpl. Assumed to be related to
59      * the some binding of this result binding.
60      */

61     // Stop being public when/if remote QueryEngineRemote moves into rdf.query
62
public void addTriple(Statement s)
63     {
64         if ( causalTriples == null )
65             causalTriples = new ArrayList() ;
66         causalTriples.add(s) ;
67     }
68
69     /** Get the set of statements that caused this ResultBindingImpl.
70      * Note: returns a set so there may be less statements in some result bindings
71      * than in others and there may be less than the number of triple patterns in
72      * the query.
73      */

74     public Set getTriples()
75     {
76         Set set = new HashSet() ;
77         getTriples(set) ;
78         return set ;
79     }
80     
81     // Worker function
82
private void getTriples(Collection acc)
83     {
84         if ( causalTriples == null )
85         {
86             causalTriples = new ArrayList() ;
87             
88             if ( query != null )
89             {
90                 Model model = query.getSource() ;
91                 try {
92                     for ( Iterator iter = query.getTriplePatterns().iterator() ; iter.hasNext() ; )
93                     {
94                         Triple t1 = (Triple)iter.next() ;
95                         Triple t2 = QueryEngine.substituteIntoTriple(t1, this) ;
96                         RDFNode s = QueryEngine.convertGraphNodeToRDFNode(t2.getSubject(), model) ;
97                         RDFNode p = QueryEngine.convertGraphNodeToRDFNode(t2.getPredicate(), model) ;
98                         if ( p instanceof Resource )
99                             p = model.createProperty(((Resource)p).getURI()) ;
100                         RDFNode o = QueryEngine.convertGraphNodeToRDFNode(t2.getObject(), model) ;
101                         Statement stmt = model.createStatement((Resource)s, (Property)p, o) ;
102                         causalTriples.add(stmt) ;
103                     }
104                 } catch (Exception JavaDoc ex)
105                 {
106                     System.err.println("ResultBindingImpl.getTriples: Substitution error: "+ex) ;
107                 }
108             }
109         }
110         
111         if ( causalTriples == null )
112             return ;
113             
114         acc.addAll(causalTriples) ;
115     }
116     
117     /** Merge the triples that caused this result binding into a model.
118      * @return The model passed in
119      */

120     public Model mergeTriples(Model model)
121     {
122         Set s = getTriples() ;
123         for ( Iterator iter = s.iterator() ; iter.hasNext() ; )
124         {
125             model.add((Statement)iter.next()) ;
126         }
127         return model ;
128     }
129     
130     public Iterator names() { return new ResultBindingIterator(this) ; }
131     
132     public ResultBindingIterator iterator()
133     {
134         return new ResultBindingIterator(this) ;
135     }
136
137     public Object JavaDoc get(String JavaDoc varName)
138     {
139         return lookup(varName, 0) ;
140     }
141
142     private Object JavaDoc lookup(String JavaDoc varName, int localOffset)
143     {
144         for ( int i = localOffset ; i < varNames.size() ; i++ )
145         {
146             if ( varName.equals((String JavaDoc)varNames.get(i)) )
147             {
148                 // Turn graph Nodes into Model onjects
149
Object JavaDoc obj = values.get(i) ;
150                 if ( obj instanceof RDFNode )
151                     return obj ;
152                 if ( obj instanceof Node )
153                     return QueryEngine.convertGraphNodeToRDFNode((Node)obj, query.getSource()) ;
154             }
155         }
156
157         if ( parent == null )
158             return null ;
159
160         // Tail recursion
161
return parent.lookup(varName,0) ;
162     }
163
164
165 // public Value getValue(String varName)
166
// {
167
// Object tmp = get(varName) ;
168
// if ( ! ( tmp instanceof Node ) )
169
// return null ;
170
// return convert(tmp) ;
171
// }
172
//
173
// static private Value convert(Object arg)
174
// {
175
// if ( arg == null ) return null ;
176
//
177
// if ( arg instanceof Value ) return (Value)arg ;
178
//
179
// // Try to turn an RDFNode into a Value.
180
// // RDFNodes are RDF Literals, Resources, Properties or a container.
181
// // But containers are Resources.
182
//
183
// // Properties and Resources
184
// if ( arg instanceof Resource )
185
// {
186
// WorkingVar w = new WorkingVar() ;
187
// w.setRDFResource((Resource)arg) ;
188
// return w ;
189
// }
190
//
191
// if ( arg instanceof Literal )
192
// {
193
// WorkingVar w = new WorkingVar() ;
194
// w.setRDFLiteral((Literal)arg) ;
195
// return w ;
196
// }
197
//
198
// // Opps!
199
// throw new RDQL_InternalErrorException("ResultBinding: unexpected object class: "+arg.getClass().getName()) ;
200
// //return null ;
201
// }
202

203
204     /** Set the parent ResultBindingImpl. This is only needed for testing of parts of the query engine */
205     
206     void setParent(ResultBindingImpl p)
207     {
208         //QSys.assertTrue( parent == null, "Attempt to change parent", "ResultBindingImpl", "setParent") ;
209
parent = p ;
210     }
211
212     public int size()
213     {
214         int size = varNames.size() ;
215
216         if ( parent != null )
217             size += parent.size() ;
218
219         return size ;
220     }
221
222     // Check for multiple bindings of the same variable.
223
public void check()
224     {
225         for (int i = 0 ; i < varNames.size() ; i++ )
226         {
227             String JavaDoc varName = (String JavaDoc)varNames.get(i) ;
228             Object JavaDoc tmp = lookup(varName, i+1) ;
229             //QSys.assertTrue(tmp == null , "Duplicate binding: "+this, "ResultBindingImpl", "check") ;
230
}
231     }
232
233
234     public String JavaDoc toString()
235     {
236         StringBuffer JavaDoc sbuff = new StringBuffer JavaDoc("") ;
237         for (int i = 0 ; i < varNames.size() ; i++ )
238         {
239             if ( i != 0 )
240                 sbuff.append(" ") ;
241             sbuff.append("("+varNames.get(i)+", "+values.get(i)+")") ;
242         }
243
244         if ( parent != null )
245         {
246             String JavaDoc tmp = parent.toString() ;
247             if ( tmp != null && (tmp.length() != 0 ) )
248             {
249                 sbuff.append(" ") ;
250                 sbuff.append(tmp) ;
251             }
252         }
253         return sbuff.toString() ;
254     }
255 }
256
257 /*
258  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
259  * All rights reserved.
260  *
261  * Redistribution and use in source and binary forms, with or without
262  * modification, are permitted provided that the following conditions
263  * are met:
264  * 1. Redistributions of source code must retain the above copyright
265  * notice, this list of conditions and the following disclaimer.
266  * 2. Redistributions in binary form must reproduce the above copyright
267  * notice, this list of conditions and the following disclaimer in the
268  * documentation and/or other materials provided with the distribution.
269  * 3. The name of the author may not be used to endorse or promote products
270  * derived from this software without specific prior written permission.
271  *
272  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
273  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
274  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
275  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
276  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
277  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
278  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
279  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
280  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
281  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282  */

283
Popular Tags