KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > GraphExtract


1 /*
2       (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3       [See end of file]
4       $Id: GraphExtract.java,v 1.5 2005/02/21 11:51:56 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph;
8
9 import java.util.Iterator JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import com.hp.hpl.jena.mem.GraphMem;
13 import com.hp.hpl.jena.util.CollectionFactory;
14
15 /**
16      GraphExtract offers a very simple recursive extraction of a subgraph with a
17      specified root in some supergraph. The recursion is terminated by triples
18      that satisfy some supplied boundary condition.
19      
20     @author hedgehog
21 */

22 public class GraphExtract
23     {
24     protected final TripleBoundary b;
25     
26     public GraphExtract( TripleBoundary b )
27         { this.b = b; }
28     
29     /**
30          Answer a new graph which is the reachable subgraph from <code>node</code>
31          in <code>graph</code> with the terminating condition given by the
32          TripleBoundary passed to the constructor.
33     */

34     public Graph extract( Node node, Graph graph )
35         { return extractInto( new GraphMem(), node, graph ); }
36     
37     /**
38          Answer the graph <code>toUpdate</code> augmented with the sub-graph of
39          <code>extractFrom</code> reachable from <code>root</code> bounded
40          by this instance's TripleBoundary.
41     */

42     public Graph extractInto( Graph toUpdate, Node root, Graph extractFrom )
43         { new Extraction( b, toUpdate, extractFrom ).extractInto( root );
44         return toUpdate; }
45     
46     /**
47          This is the class that does all the work, in the established context of the
48          source and destination graphs, the TripleBoundary that determines the
49          limits of the extraction, and a local set <code>active</code> of nodes
50          already seen and hence not to be re-processed.
51         @author kers
52      */

53     protected static class Extraction
54         {
55         protected Graph toUpdate;
56         protected Graph extractFrom;
57         protected Set JavaDoc active;
58         protected TripleBoundary b;
59         
60         Extraction( TripleBoundary b, Graph toUpdate, Graph extractFrom )
61             {
62             this.toUpdate = toUpdate;
63             this.extractFrom = extractFrom;
64             this.active = CollectionFactory.createHashedSet();
65             this.b = b;
66             }
67         
68         public void extractInto( Node root )
69             {
70             active.add( root );
71             Iterator JavaDoc it = extractFrom.find( root, Node.ANY, Node.ANY );
72             while (it.hasNext())
73                 {
74                 Triple t = (Triple) it.next();
75                 Node subRoot = t.getObject();
76                 toUpdate.add( t );
77                 if (! (active.contains( subRoot ) || b.stopAt( t ))) extractInto( subRoot );
78                 }
79             }
80         }
81     
82
83     }
84
85 /*
86     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
87     All rights reserved.
88     
89     Redistribution and use in source and binary forms, with or without
90     modification, are permitted provided that the following conditions
91     are met:
92     
93     1. Redistributions of source code must retain the above copyright
94        notice, this list of conditions and the following disclaimer.
95     
96     2. Redistributions in binary form must reproduce the above copyright
97        notice, this list of conditions and the following disclaimer in the
98        documentation and/or other materials provided with the distribution.
99     
100     3. The name of the author may not be used to endorse or promote products
101        derived from this software without specific prior written permission.
102     
103     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
104     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
105     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
106     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
107     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
108     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
109     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
110     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
111     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
112     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113 */
Popular Tags