KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > ResultSetReifIterator


1 /*
2  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  *
6  */

7
8 //=======================================================================
9
// Package
10
package com.hp.hpl.jena.db.impl;
11
12 //=======================================================================
13
// Imports
14
import java.sql.*;
15
16 import com.hp.hpl.jena.db.RDFRDBException;
17 import com.hp.hpl.jena.graph.*;
18 import com.hp.hpl.jena.shared.JenaException;
19 import com.hp.hpl.jena.vocabulary.RDF;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 //=======================================================================
25
/**
26 * Version of ResultSetIterator that extracts database rows as Triples from a reified statement table.
27 *
28 * @author hkuno. Based on ResultSetResource Iterator, by Dave Reynolds, HPLabs, Bristol <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
29 * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:03:10 $
30 */

31 public class ResultSetReifIterator extends ResultSetIterator {
32
33     /** The rdf model in which to instantiate any resources */
34     protected IDBID m_graphID;
35
36     /** The database driver, used to access namespace and resource caches */
37     protected IPSet m_pset;
38         
39     /** Holds subject, predicate and object of current row */
40     protected Node m_subjNode;
41     protected Node m_predNode;
42     protected Node m_objNode;
43         
44     /** Statement URI of current row */
45     protected Node m_stmtURI;
46     
47     /** HasType flag is true if reified statement has type rdf:Statement */
48     protected boolean m_hasType;
49     
50     /** getTriples is true if this iterator should return all triples for the reified statement
51      * otherwise, reified statements are returned. */

52     protected boolean m_getTriples;
53     
54     /** total number of fragments to generate for this row (ranges 1-4) */
55     protected int m_fragCount;
56     
57     /** number of remaining fragments to generate for this row (ranges 1-4) */
58     protected int m_fragRem;
59     
60     /** number of next fragment to generate (0-4 for subj, pred, obj, type, resp). */
61     protected int m_nextFrag;
62
63
64     static protected Log logger = LogFactory.getLog( ResultSetReifIterator.class );
65     
66     // Constructor
67
public ResultSetReifIterator(IPSet p, boolean getTriples, IDBID graphID) {
68         m_pset = p;
69         setGraphID(graphID);
70         m_getTriples = getTriples;
71     }
72
73     /**
74      * Set m_graphID.
75      * @param gid is the id of the graph associated with this iterator.
76      */

77     public void setGraphID(IDBID gid) {
78         m_graphID = gid;
79     }
80     
81     /**
82      * Reset an existing iterator to scan a new result set.
83      * @param resultSet the result set being iterated over
84      * @param sourceStatement The source Statement to be cleaned up when the iterator finishes - return it to cache or close it if no cache
85      * @param cache The originating SQLcache to return the statement to, can be null
86      * @param opname The name of the original operation that lead to this statement, can be null if SQLCache is null
87      */

88     public void reset(ResultSet resultSet, PreparedStatement sourceStatement, SQLCache cache, String JavaDoc opname) {
89         super.reset(resultSet, sourceStatement, cache, opname);
90     }
91
92     /**
93      * Extract the current row into a triple.
94      * Requires the row to be of the form:
95      * subject URI (String)
96      * predicate URI (String)
97      * object URI (String)
98      * object value (String)
99      * Object literal id (Object)
100      *
101      * The object of the triple can be either a URI, a simple literal (in
102      * which case it will just have an object value, or a complex literal
103      * (in which case both the object value and the object literal id
104      * columns may be populated.
105      */

106     protected void extractRow() throws SQLException {
107         int rx = 1;
108         ResultSet rs = m_resultSet;
109         String JavaDoc subj = rs.getString(1);
110         String JavaDoc pred = rs.getString(2);
111         String JavaDoc obj = rs.getString(3);
112         
113         m_stmtURI = m_pset.driver().RDBStringToNode(rs.getString(4));
114         m_hasType = rs.getString(5).equals("T");
115         
116         m_fragRem = m_hasType ? 1 : 0;
117         if ( subj == null ) {
118             m_subjNode = Node.NULL;
119         } else {
120             m_subjNode = m_pset.driver().RDBStringToNode(subj);
121             m_fragRem++;
122         }
123         if ( pred == null ) {
124             m_predNode = Node.NULL;
125         } else {
126             m_predNode = m_pset.driver().RDBStringToNode(pred);
127             m_fragRem++;
128         }
129         if ( obj == null ) {
130             m_objNode = Node.NULL;
131         } else {
132             m_objNode = m_pset.driver().RDBStringToNode(obj);
133             m_fragRem++;
134         }
135         m_nextFrag = 0;
136         m_fragCount = m_fragRem;
137     }
138     
139         /**
140          * Return triples for the current row, which should have already been extracted.
141          */

142         protected Object JavaDoc getRow() {
143             Triple t = null;
144             
145             if ( m_getTriples == true ) {
146                 if ( m_nextFrag == 0) {
147                     if ( !m_subjNode.equals(Node.NULL) ) {
148                         t = Triple.create(m_stmtURI,RDF.Nodes.subject,m_subjNode);
149                         m_fragRem--;
150                     } else
151                         m_nextFrag++;
152                 }
153                 if ( m_nextFrag == 1) {
154                     if ( !m_predNode.equals(Node.NULL) ) {
155                         t = Triple.create(m_stmtURI,RDF.Nodes.predicate,m_predNode);
156                         m_fragRem--;
157                     } else
158                         m_nextFrag++;
159                 }
160                 if ( m_nextFrag == 2) {
161                     if ( !m_objNode.equals(Node.NULL) ) {
162                         t = Triple.create(m_stmtURI,RDF.Nodes.object,m_objNode);
163                         m_fragRem--;
164                     } else
165                         m_nextFrag++;
166                 }
167                 if ( m_nextFrag >= 3) {
168                     if ( m_hasType ) {
169                         t = Triple.create(m_stmtURI,RDF.Nodes.type,RDF.Nodes.Statement);
170                         m_fragRem--;
171                     } else
172                         throw new JenaException("Reified triple not found");
173                 }
174                 m_nextFrag++;
175                 if ( m_fragRem > 0 )
176                     m_prefetched = true;
177
178             } else {
179                 t = Triple.create(m_subjNode, m_predNode, m_objNode);
180             }
181         
182             return t;
183         }
184         
185     /**
186     * Return the true if the current row has a non-null subject.
187     */

188     protected boolean hasSubj() {
189         return m_subjNode != Node.NULL;
190     }
191
192     /**
193     * Return the true if the current row has a non-null predicate.
194     */

195     protected boolean hasPred() {
196         return m_predNode != Node.NULL;
197     }
198
199     /**
200     * Return the true if the current row has a non-null object.
201     */

202     protected boolean hasObj() {
203         return m_objNode != Node.NULL;
204     }
205
206     /**
207     * Return the true if the current row has T (true) for hasType.
208     */

209     protected boolean hasType() {
210         return m_hasType;
211     }
212
213     /**
214     * Return the number of (reification statement) fragments for the current row.
215     */

216     protected int getFragCount() {
217         return m_fragCount;
218     }
219
220         /**
221         * Return the reifying URI for current row, which should have already been extracted.
222         */

223         protected Node getStmtURI() {
224             return m_stmtURI;
225         }
226         
227         /**
228         * Return the hasType value of current row, which should have already been extracted.
229         */

230         protected boolean getHasType() {
231             return m_hasType;
232         }
233         
234         /**
235         * Delete the current row, which should have already been extracted.
236         * Should only be used (carefully and) internally by db layer.
237         */

238         protected void deleteRow() {
239             try {
240                 m_resultSet.deleteRow();
241             } catch (SQLException e) {
242                 throw new RDFRDBException("Internal sql error", e);
243             }
244         }
245         
246
247 } // End class
248

249 /*
250  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
251  * All rights reserved.
252  *
253  * Redistribution and use in source and binary forms, with or without
254  * modification, are permitted provided that the following conditions
255  * are met:
256  * 1. Redistributions of source code must retain the above copyright
257  * notice, this list of conditions and the following disclaimer.
258  * 2. Redistributions in binary form must reproduce the above copyright
259  * notice, this list of conditions and the following disclaimer in the
260  * documentation and/or other materials provided with the distribution.
261  * 3. The name of the author may not be used to endorse or promote products
262  * derived from this software without specific prior written permission.
263
264  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
265  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
266  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
267  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
268  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
269  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
270  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
271  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
273  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274  */

275
276
Popular Tags