KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2002, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: DBReifier.java,v 1.19 2005/02/21 12:02:50 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.impl;
8
9 import com.hp.hpl.jena.db.*;
10
11 /**
12  * Implementation of Reifier for graphs stored in a database.
13  *
14  * @author csayers based in part on SimpleReifier by kers.
15 */

16
17 import java.util.List JavaDoc;
18
19 import com.hp.hpl.jena.graph.*;
20 import com.hp.hpl.jena.util.iterator.*;
21 import com.hp.hpl.jena.shared.*;
22
23 public class DBReifier implements Reifier
24     {
25     protected GraphRDB m_parent = null;
26     protected Graph m_hiddenTriples = null;
27     protected List JavaDoc m_reifiers = null;
28     protected List JavaDoc m_hidden_reifiers = null;
29
30     // For now, we just deal with a single specializedGraphReifier,
31
// but in the future we could replace this with a list of
32
// those and operate much as the GraphRDB implementation
33
// does with it's list of SpecializedGraphs.
34
protected SpecializedGraphReifier m_reifier = null;
35     
36     protected ReificationStyle m_style;
37     
38     /**
39      * Construct a reifier for GraphRDB's.
40      *
41      * @param parent the Graph for which we will expose reified triples.
42      * @param allReifiers a List of SpecializedGraphReifiers which reifiy triples in that graph.
43      * @param hiddenReifiers the subset of allReifiers whose triples are hidden when querying the parent graph.
44      */

45     public DBReifier(GraphRDB parent, ReificationStyle style, List JavaDoc allReifiers, List JavaDoc hiddenReifiers ) {
46         m_parent = parent;
47         m_reifiers = allReifiers;
48         m_hidden_reifiers = hiddenReifiers;
49         m_style = style;
50         
51         // For now, just take the first specializedGraphReifier
52
if (m_reifiers.size() != 1)
53             throw new BrokenException("Internal error - DBReifier requires exactly one SpecializedGraphReifier");
54         m_reifier = (SpecializedGraphReifier) m_reifiers.get(0);
55     }
56             
57     /* (non-Javadoc)
58      * @see com.hp.hpl.jena.graph.Reifier#getParentGraph()
59      */

60     public Graph getParentGraph() {
61         return m_parent; }
62         
63     public ReificationStyle getStyle()
64         { return m_style; }
65
66     /* (non-Javadoc)
67      * @see com.hp.hpl.jena.graph.Reifier#getHiddenTriples()
68      */

69     private Graph getReificationTriples() {
70         if( m_hiddenTriples == null)
71             m_hiddenTriples = new DBReifierGraph(m_parent, m_hidden_reifiers);
72         return m_hiddenTriples;
73     }
74     
75     public ExtendedIterator find( TripleMatch m )
76         { return getReificationTriples().find( m ); }
77     
78     public ExtendedIterator findExposed( TripleMatch m )
79         { return getReificationTriples().find( m ); }
80     
81     public ExtendedIterator findEither( TripleMatch m, boolean showHidden )
82         { return showHidden == m_style.conceals() ? getReificationTriples().find( m ) : NullIterator.instance; }
83
84     public int size()
85         { return m_style.conceals() ? 0 : getReificationTriples().size(); }
86
87     /**
88         Utility method useful for its short name: answer a new CompletionFlag
89         initialised to false.
90     */

91     protected static SpecializedGraph.CompletionFlag newComplete()
92         { return new SpecializedGraph.CompletionFlag(); }
93         
94     /* (non-Javadoc)
95      * @see com.hp.hpl.jena.graph.Reifier#reifyAs(com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Triple)
96      */

97     public Node reifyAs( Node n, Triple t ) {
98         m_reifier.add( n, t, newComplete() );
99         return n;
100     }
101
102     /* (non-Javadoc)
103      * @see com.hp.hpl.jena.graph.Reifier#hasTriple(com.hp.hpl.jena.graph.Node)
104      */

105     public boolean hasTriple(Node n) {
106         return m_reifier.findReifiedTriple( n, newComplete() ) != null;
107     }
108
109     /* (non-Javadoc)
110      * @see com.hp.hpl.jena.graph.Reifier#hasTriple(com.hp.hpl.jena.graph.Triple)
111      */

112     public boolean hasTriple( Triple t ) {
113         return m_reifier.findReifiedNodes(t, newComplete() ).hasNext();
114     }
115
116     /* (non-Javadoc)
117      * @see com.hp.hpl.jena.graph.Reifier#allNodes()
118      */

119     public ExtendedIterator allNodes() {
120         return m_reifier.findReifiedNodes( null, newComplete() );
121     }
122     
123     /**
124         All the nodes reifying triple <code>t</code>, using the matching code
125         from SimpleReifier.
126     */

127     public ExtendedIterator allNodes( Triple t )
128         { return m_reifier.findReifiedNodes( t, newComplete() ); }
129         
130     /* (non-Javadoc)
131      * @see com.hp.hpl.jena.graph.Reifier#remove(com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Triple)
132      */

133     public void remove( Node n, Triple t ) {
134         m_reifier.delete( n, t, newComplete() );
135     }
136
137     /* (non-Javadoc)
138      * @see com.hp.hpl.jena.graph.Reifier#remove(com.hp.hpl.jena.graph.Triple)
139      */

140     public void remove( Triple t ) {
141         m_reifier.delete(null,t, newComplete() );
142     }
143
144     /* (non-Javadoc)
145      * @see com.hp.hpl.jena.graph.Reifier#handledAdd(com.hp.hpl.jena.graph.Triple)
146      */

147     public boolean handledAdd(Triple t) {
148         SpecializedGraph.CompletionFlag complete = newComplete();
149         m_reifier.add(t, complete);
150         return complete.isDone();
151     }
152
153     /* (non-Javadoc)
154      * @see com.hp.hpl.jena.graph.Reifier#handledRemove(com.hp.hpl.jena.graph.Triple)
155      */

156     public boolean handledRemove(Triple t) {
157         SpecializedGraph.CompletionFlag complete = newComplete();
158         m_reifier.delete(t, complete);
159         return complete.isDone();
160     }
161
162     /* (non-Javadoc)
163      * @see com.hp.hpl.jena.graph.GetTriple#getTriple(com.hp.hpl.jena.graph.Node)
164      */

165     public Triple getTriple(Node n) {
166         return m_reifier.findReifiedTriple(n, newComplete() );
167     }
168     
169     public void close() {
170         // TODO anything useful for a close operation
171
}
172         
173 }
174     
175 /*
176     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
177     All rights reserved.
178
179     Redistribution and use in source and binary forms, with or without
180     modification, are permitted provided that the following conditions
181     are met:
182
183     1. Redistributions of source code must retain the above copyright
184        notice, this list of conditions and the following disclaimer.
185
186     2. Redistributions in binary form must reproduce the above copyright
187        notice, this list of conditions and the following disclaimer in the
188        documentation and/or other materials provided with the distribution.
189
190     3. The name of the author may not be used to endorse or promote products
191        derived from this software without specific prior written permission.
192
193     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
198     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
201     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
202     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203 */

204
Popular Tags