001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [see end of file]
004: $Id: ModelReifier.java,v 1.23 2008/01/02 12:05:05 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.impl;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.shared.*;
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.graph.compose.*;
013: import com.hp.hpl.jena.graph.impl.GraphBase;
014: import com.hp.hpl.jena.util.iterator.*;
015:
016: /**
017: This class impedance-matches the reification requests of Model[Com] to the operations
018: supplied by it's Graph's Reifier.
019:
020: @author kers
021: */
022: public class ModelReifier {
023: private ModelCom model;
024: public Reifier reifier;
025:
026: /**
027: DEVEL. setting this _true_ means that nodes that reify statements
028: will drag their reification quads into other nodes when they are
029: added to them inside statements.
030: */
031: private static boolean copyingReifications = false;
032:
033: /**
034: establish the internal state of this ModelReifier: the associated
035: Model[Com] and its graph's Reifier.
036: */
037: public ModelReifier(ModelCom model) {
038: this .model = model;
039: this .reifier = model.asGraph().getReifier();
040: }
041:
042: public ReificationStyle getReificationStyle() {
043: return reifier.getStyle();
044: }
045:
046: /**
047: Answer a version of the model, but with all its reifiying statements
048: added.
049: @param m a model that may have reified statements
050: @return a new model, the union of m and the reification statements of m
051: */
052: public static Model withHiddenStatements(Model m) {
053: Graph mGraph = m.getGraph();
054: Graph hiddenTriples = getHiddenTriples(m);
055: return new ModelCom(new DisjointUnion(mGraph, hiddenTriples));
056: }
057:
058: /**
059: @param mGraph
060: @return
061: */
062: protected static Graph getHiddenTriples(Model m) {
063: Graph mGraph = m.getGraph();
064: final Reifier r = mGraph.getReifier();
065: return new GraphBase() {
066: public ExtendedIterator graphBaseFind(TripleMatch m) {
067: return r.findEither(m, true);
068: }
069: };
070: }
071:
072: /**
073: Answer a model that consists of the hidden reification statements of this model.
074: @return a new model containing the hidden statements of this model
075: */
076: public Model getHiddenStatements() {
077: return new ModelCom(getHiddenTriples(model));
078: }
079:
080: /**
081: Answer a fresh reification of a statement associated with a fresh bnode.
082: @param s a Statement to reifiy
083: @return a reified statement object who's name is a new bnode
084: */
085: public ReifiedStatement createReifiedStatement(Statement s) {
086: return createReifiedStatement(null, s);
087: }
088:
089: /**
090: Answer a reification of a statement with a given uri. If that uri
091: already reifies a distinct Statement, throw an AlreadyReifiedException.
092: @param uri the URI of the resource which will reify <code>s</code>
093: @param s the Statement to reify
094: @return a reified statement object associating <code>uri</code> with <code>s</code>.
095: @throws AlreadyReifiedException if uri already reifies something else.
096: */
097: public ReifiedStatement createReifiedStatement(String uri,
098: Statement s) {
099: return ReifiedStatementImpl.create(model, uri, s);
100: }
101:
102: /**
103: Find any existing reified statement that reifies a givem statement. If there isn't one,
104: create one.
105: @param s a Statement for which to find [or create] a reification
106: @return a reification for s, re-using an existing one if possible
107: */
108: public Resource getAnyReifiedStatement(Statement s) {
109: RSIterator it = listReifiedStatements(s);
110: if (it.hasNext())
111: try {
112: return it.nextRS();
113: } finally {
114: it.close();
115: }
116: else
117: return createReifiedStatement(s);
118: }
119:
120: /**
121: Answer true iff a given statement is reified in this model
122: @param s the statement for which a reification is sought
123: @return true iff s has a reification in this model
124: */
125: public boolean isReified(FrontsTriple s) {
126: return reifier.hasTriple(s.asTriple());
127: }
128:
129: /**
130: Remove all the reifications of a given statement in this model, whatever
131: their associated resources.
132: @param s the statement whose reifications are to be removed
133: */
134: public void removeAllReifications(FrontsTriple s) {
135: reifier.remove(s.asTriple());
136: }
137:
138: /**
139: Remove a given reification from this model. Other reifications of the same statement
140: are untouched.
141: @param rs the reified statement to be removed
142: */
143: public void removeReification(ReifiedStatement rs) {
144: reifier.remove(rs.asNode(), rs.getStatement().asTriple());
145: }
146:
147: /**
148: Answer an iterator that iterates over all the reified statements
149: in this model.
150: @return an iterator over all the reifications of the model.
151: */
152: public RSIterator listReifiedStatements() {
153: return new RSIteratorImpl(findReifiedStatements());
154: }
155:
156: /**
157: Answer an iterator that iterates over all the reified statements in
158: this model that reify a given statement.
159: @param s the statement whose reifications are sought.
160: @return an iterator over the reifications of s.
161: */
162: public RSIterator listReifiedStatements(FrontsTriple s) {
163: return new RSIteratorImpl(findReifiedStatements(s.asTriple()));
164: }
165:
166: /**
167: the triple (s, p, o) has been asserted into the model. Any reified statements
168: among them need to be added to this model.
169: */
170: public void noteIfReified(RDFNode s, RDFNode p, RDFNode o) {
171: if (copyingReifications) {
172: noteIfReified(s);
173: noteIfReified(p);
174: noteIfReified(o);
175: }
176: }
177:
178: /**
179: If _n_ is a ReifiedStatement, create a local copy of it, which
180: will force the underlying reifier to take note of the mapping.
181: */
182: private void noteIfReified(RDFNode n) {
183: if (n.canAs(ReifiedStatement.class)) {
184: ReifiedStatement rs = (ReifiedStatement) n
185: .as(ReifiedStatement.class);
186: createReifiedStatement(rs.getURI(), rs.getStatement());
187: }
188: }
189:
190: /**
191: A mapper that maps modes to their corresponding ReifiedStatement objects. This
192: cannot be static: getRS cannot be static, because the mapping is model-specific.
193: */
194: protected final Map1 mapToRS = new Map1() {
195: public Object map1(Object node) {
196: return getRS((Node) node);
197: }
198: };
199:
200: private ExtendedIterator findReifiedStatements() {
201: return reifier.allNodes().mapWith(mapToRS);
202: }
203:
204: private ExtendedIterator findReifiedStatements(Triple t) {
205: return reifier.allNodes(t).mapWith(mapToRS);
206: }
207:
208: /**
209: Answer a ReifiedStatement that is based on the given node.
210: @param n the node which represents the reification (and is bound to some triple t)
211: @return a ReifiedStatement associating the resource of n with the statement of t.
212: */
213: private ReifiedStatement getRS(Node n) {
214: return ReifiedStatementImpl.createExistingReifiedStatement(
215: model, n);
216: }
217: }
218:
219: /*
220: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
221: All rights reserved.
222:
223: Redistribution and use in source and binary forms, with or without
224: modification, are permitted provided that the following conditions
225: are met:
226:
227: 1. Redistributions of source code must retain the above copyright
228: notice, this list of conditions and the following disclaimer.
229:
230: 2. Redistributions in binary form must reproduce the above copyright
231: notice, this list of conditions and the following disclaimer in the
232: documentation and/or other materials provided with the distribution.
233:
234: 3. The name of the author may not be used to endorse or promote products
235: derived from this software without specific prior written permission.
236:
237: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
238: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
240: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
242: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
246: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247: */
|