1 6 7 package com.hp.hpl.jena.graph; 8 9 import java.util.Iterator ; 10 import java.util.Set ; 11 12 import com.hp.hpl.jena.mem.GraphMem; 13 import com.hp.hpl.jena.util.CollectionFactory; 14 15 22 public class GraphExtract 23 { 24 protected final TripleBoundary b; 25 26 public GraphExtract( TripleBoundary b ) 27 { this.b = b; } 28 29 34 public Graph extract( Node node, Graph graph ) 35 { return extractInto( new GraphMem(), node, graph ); } 36 37 42 public Graph extractInto( Graph toUpdate, Node root, Graph extractFrom ) 43 { new Extraction( b, toUpdate, extractFrom ).extractInto( root ); 44 return toUpdate; } 45 46 53 protected static class Extraction 54 { 55 protected Graph toUpdate; 56 protected Graph extractFrom; 57 protected Set 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 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 | Popular Tags |