001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: GraphExtract.java,v 1.9 2008/01/02 12:06:55 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph;
008:
009: import java.util.Iterator;
010: import java.util.Set;
011:
012: import com.hp.hpl.jena.util.CollectionFactory;
013:
014: /**
015: GraphExtract offers a very simple recursive extraction of a subgraph with a
016: specified root in some supergraph. The recursion is terminated by triples
017: that satisfy some supplied boundary condition.
018:
019: @author hedgehog
020: */
021: public class GraphExtract {
022: protected final TripleBoundary b;
023:
024: public GraphExtract(TripleBoundary b) {
025: this .b = b;
026: }
027:
028: /**
029: Answer a new graph which is the reachable subgraph from <code>node</code>
030: in <code>graph</code> with the terminating condition given by the
031: TripleBoundary passed to the constructor.
032: */
033: public Graph extract(Node node, Graph graph) {
034: return extractInto(Factory.createGraphMem(), node, graph);
035: }
036:
037: /**
038: Answer the graph <code>toUpdate</code> augmented with the sub-graph of
039: <code>extractFrom</code> reachable from <code>root</code> bounded
040: by this instance's TripleBoundary.
041: */
042: public Graph extractInto(Graph toUpdate, Node root,
043: Graph extractFrom) {
044: new Extraction(b, toUpdate, extractFrom).extractInto(root);
045: return toUpdate;
046: }
047:
048: /**
049: This is the class that does all the work, in the established context of the
050: source and destination graphs, the TripleBoundary that determines the
051: limits of the extraction, and a local set <code>active</code> of nodes
052: already seen and hence not to be re-processed.
053: @author kers
054: */
055: protected static class Extraction {
056: protected Graph toUpdate;
057: protected Graph extractFrom;
058: protected Set active;
059: protected TripleBoundary b;
060:
061: Extraction(TripleBoundary b, Graph toUpdate, Graph extractFrom) {
062: this .toUpdate = toUpdate;
063: this .extractFrom = extractFrom;
064: this .active = CollectionFactory.createHashedSet();
065: this .b = b;
066: }
067:
068: public void extractInto(Node root) {
069: active.add(root);
070: Iterator it = extractFrom.find(root, Node.ANY, Node.ANY);
071: while (it.hasNext()) {
072: Triple t = (Triple) it.next();
073: Node subRoot = t.getObject();
074: toUpdate.add(t);
075: if (!(active.contains(subRoot) || b.stopAt(t)))
076: extractInto(subRoot);
077: }
078: }
079: }
080:
081: }
082:
083: /*
084: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
085: All rights reserved.
086:
087: Redistribution and use in source and binary forms, with or without
088: modification, are permitted provided that the following conditions
089: are met:
090:
091: 1. Redistributions of source code must retain the above copyright
092: notice, this list of conditions and the following disclaimer.
093:
094: 2. Redistributions in binary form must reproduce the above copyright
095: notice, this list of conditions and the following disclaimer in the
096: documentation and/or other materials provided with the distribution.
097:
098: 3. The name of the author may not be used to endorse or promote products
099: derived from this software without specific prior written permission.
100:
101: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
102: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
103: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
104: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
105: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
106: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
107: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
108: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
109: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
110: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
111: */
|