001: /*
002: (c) Copyright 2002, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: DBReifier.java,v 1.22 2008/01/02 12:08:25 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import com.hp.hpl.jena.db.*;
010:
011: /**
012: * Implementation of Reifier for graphs stored in a database.
013: *
014: * @author csayers based in part on SimpleReifier by kers.
015: */
016:
017: import java.util.List;
018:
019: import com.hp.hpl.jena.graph.*;
020: import com.hp.hpl.jena.util.iterator.*;
021: import com.hp.hpl.jena.shared.*;
022:
023: public class DBReifier implements Reifier {
024: protected GraphRDB m_parent = null;
025: protected Graph m_hiddenTriples = null;
026: protected List m_reifiers = null;
027: protected List m_hidden_reifiers = null;
028:
029: // For now, we just deal with a single specializedGraphReifier,
030: // but in the future we could replace this with a list of
031: // those and operate much as the GraphRDB implementation
032: // does with it's list of SpecializedGraphs.
033: protected SpecializedGraphReifier m_reifier = null;
034:
035: protected ReificationStyle m_style;
036:
037: /**
038: * Construct a reifier for GraphRDB's.
039: *
040: * @param parent the Graph for which we will expose reified triples.
041: * @param allReifiers a List of SpecializedGraphReifiers which reifiy triples in that graph.
042: * @param hiddenReifiers the subset of allReifiers whose triples are hidden when querying the parent graph.
043: */
044: public DBReifier(GraphRDB parent, ReificationStyle style,
045: List allReifiers, List hiddenReifiers) {
046: m_parent = parent;
047: m_reifiers = allReifiers;
048: m_hidden_reifiers = hiddenReifiers;
049: m_style = style;
050:
051: // For now, just take the first specializedGraphReifier
052: if (m_reifiers.size() != 1)
053: throw new BrokenException(
054: "Internal error - DBReifier requires exactly one SpecializedGraphReifier");
055: m_reifier = (SpecializedGraphReifier) m_reifiers.get(0);
056: }
057:
058: /* (non-Javadoc)
059: * @see com.hp.hpl.jena.graph.Reifier#getParentGraph()
060: */
061: public Graph getParentGraph() {
062: return m_parent;
063: }
064:
065: public ReificationStyle getStyle() {
066: return m_style;
067: }
068:
069: /* (non-Javadoc)
070: * @see com.hp.hpl.jena.graph.Reifier#getHiddenTriples()
071: */
072: private Graph getReificationTriples() {
073: if (m_hiddenTriples == null)
074: m_hiddenTriples = new DBReifierGraph(m_parent,
075: m_hidden_reifiers);
076: return m_hiddenTriples;
077: }
078:
079: public ExtendedIterator find(TripleMatch m) {
080: return getReificationTriples().find(m);
081: }
082:
083: public ExtendedIterator findExposed(TripleMatch m) {
084: return getReificationTriples().find(m);
085: }
086:
087: public ExtendedIterator findEither(TripleMatch m, boolean showHidden) {
088: return showHidden == m_style.conceals() ? getReificationTriples()
089: .find(m)
090: : NullIterator.instance;
091: }
092:
093: public int size() {
094: return m_style.conceals() ? 0 : getReificationTriples().size();
095: }
096:
097: /**
098: Utility method useful for its short name: answer a new CompletionFlag
099: initialised to false.
100: */
101: protected static SpecializedGraph.CompletionFlag newComplete() {
102: return new SpecializedGraph.CompletionFlag();
103: }
104:
105: /* (non-Javadoc)
106: * @see com.hp.hpl.jena.graph.Reifier#reifyAs(com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Triple)
107: */
108: public Node reifyAs(Node n, Triple t) {
109: m_reifier.add(n, t, newComplete());
110: return n;
111: }
112:
113: /* (non-Javadoc)
114: * @see com.hp.hpl.jena.graph.Reifier#hasTriple(com.hp.hpl.jena.graph.Node)
115: */
116: public boolean hasTriple(Node n) {
117: return m_reifier.findReifiedTriple(n, newComplete()) != null;
118: }
119:
120: /* (non-Javadoc)
121: * @see com.hp.hpl.jena.graph.Reifier#hasTriple(com.hp.hpl.jena.graph.Triple)
122: */
123: public boolean hasTriple(Triple t) {
124: return m_reifier.findReifiedNodes(t, newComplete()).hasNext();
125: }
126:
127: /* (non-Javadoc)
128: * @see com.hp.hpl.jena.graph.Reifier#allNodes()
129: */
130: public ExtendedIterator allNodes() {
131: return m_reifier.findReifiedNodes(null, newComplete());
132: }
133:
134: /**
135: All the nodes reifying triple <code>t</code>, using the matching code
136: from SimpleReifier.
137: */
138: public ExtendedIterator allNodes(Triple t) {
139: return m_reifier.findReifiedNodes(t, newComplete());
140: }
141:
142: /* (non-Javadoc)
143: * @see com.hp.hpl.jena.graph.Reifier#remove(com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Triple)
144: */
145: public void remove(Node n, Triple t) {
146: m_reifier.delete(n, t, newComplete());
147: }
148:
149: /* (non-Javadoc)
150: * @see com.hp.hpl.jena.graph.Reifier#remove(com.hp.hpl.jena.graph.Triple)
151: */
152: public void remove(Triple t) {
153: m_reifier.delete(null, t, newComplete());
154: }
155:
156: /* (non-Javadoc)
157: * @see com.hp.hpl.jena.graph.Reifier#handledAdd(com.hp.hpl.jena.graph.Triple)
158: */
159: public boolean handledAdd(Triple t) {
160: SpecializedGraph.CompletionFlag complete = newComplete();
161: m_reifier.add(t, complete);
162: return complete.isDone();
163: }
164:
165: /* (non-Javadoc)
166: * @see com.hp.hpl.jena.graph.Reifier#handledRemove(com.hp.hpl.jena.graph.Triple)
167: */
168: public boolean handledRemove(Triple t) {
169: SpecializedGraph.CompletionFlag complete = newComplete();
170: m_reifier.delete(t, complete);
171: return complete.isDone();
172: }
173:
174: /* (non-Javadoc)
175: * @see com.hp.hpl.jena.graph.GetTriple#getTriple(com.hp.hpl.jena.graph.Node)
176: */
177: public Triple getTriple(Node n) {
178: return m_reifier.findReifiedTriple(n, newComplete());
179: }
180:
181: public void close() {
182: // TODO anything useful for a close operation
183: }
184:
185: }
186:
187: /*
188: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
189: All rights reserved.
190:
191: Redistribution and use in source and binary forms, with or without
192: modification, are permitted provided that the following conditions
193: are met:
194:
195: 1. Redistributions of source code must retain the above copyright
196: notice, this list of conditions and the following disclaimer.
197:
198: 2. Redistributions in binary form must reproduce the above copyright
199: notice, this list of conditions and the following disclaimer in the
200: documentation and/or other materials provided with the distribution.
201:
202: 3. The name of the author may not be used to endorse or promote products
203: derived from this software without specific prior written permission.
204:
205: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
206: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
207: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
209: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
210: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
212: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
213: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
214: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
215: */
|