1 6 package com.hp.hpl.jena.graph.impl; 7 8 import java.util.HashSet ; 9 import java.util.*; 10 11 import com.hp.hpl.jena.graph.*; 12 import com.hp.hpl.jena.util.CollectionFactory; 13 import com.hp.hpl.jena.util.iterator.*; 14 15 20 public class SimpleReifierTripleMap implements ReifierTripleMap 21 { 22 protected Map inverseMap = CollectionFactory.createHashedMap(); 23 24 protected Map forwardMap = CollectionFactory.createHashedMap(); 25 26 public Triple getTriple( Node tag ) 27 { return (Triple) forwardMap.get( tag ); } 28 29 32 public boolean hasTriple( Triple t ) 33 { return inverseMap.containsKey( t ); } 34 35 public Triple putTriple( Node key, Triple value ) 36 { 37 forwardMap.put( key, value ); 38 inversePut( value, key ); 39 return value; 40 } 41 42 public void removeTriple( Node key ) 43 { 44 Object t = forwardMap.get( key ); 45 forwardMap.remove( key ); 46 if (t instanceof Triple) inverseRemove( (Triple) t, key ); 47 } 48 49 public void removeTriple( Node key, Triple value ) 50 { 51 forwardMap.remove( key ); 52 inverseRemove( value, key ); 53 } 54 55 public void removeTriple( Triple t ) 56 { 57 ExtendedIterator it = tagIterator( t ); 58 Set nodes = CollectionFactory.createHashedSet(); 59 while (it.hasNext()) nodes.add( it.next() ); 60 Iterator them = nodes.iterator(); 61 while (them.hasNext()) removeTriple( (Node) them.next() ); 62 } 63 64 protected void inverseRemove( Triple value, Node key ) 65 { 66 Set s = (Set ) inverseMap.get( value ); 67 if (s != null) 68 { 69 s.remove( key ); 70 if (s.isEmpty()) inverseMap.remove( value ); 71 } 72 } 73 74 protected void inversePut( Triple value, Node key ) 75 { 76 Set s = (Set ) inverseMap.get( value ); 77 if (s == null) inverseMap.put( value, s = new HashSet () ); 78 s.add( key ); 79 } 80 81 public ExtendedIterator tagIterator( Triple t ) 82 { 83 Set s = (Set ) inverseMap.get( t ); 84 return s == null 85 ? (ExtendedIterator) NullIterator.instance 86 : WrappedIterator.create( s.iterator() ); 87 } 88 89 protected ExtendedIterator allTriples( TripleMatch tm ) 90 { 91 Triple pattern = tm.asTriple(); 92 Node tag = pattern.getSubject(); 93 if (tag.isConcrete()) 94 { 95 Triple x = getTriple( tag ); 96 return x == null ? NullIterator.instance : explodeTriple( pattern, tag, x ); 97 } 98 else 99 { 100 final Iterator it = forwardMap.entrySet().iterator(); 101 return new FragmentTripleIterator( pattern, it ) 102 { 103 public void fill( GraphAdd ga, Node n, Object fragmentsObject ) 104 { 105 SimpleReifier.graphAddQuad( ga, n, (Triple) fragmentsObject ); 106 } 107 }; 108 } 109 } 110 111 115 public static ExtendedIterator explodeTriple( Triple pattern, Node tag, Triple toExplode ) 116 { 117 GraphAddList L = new GraphAddList( pattern ); 118 SimpleReifier.graphAddQuad( L, tag, toExplode ); 119 return WrappedIterator.create( L.iterator() ); 120 } 121 122 127 public Graph asGraph() 128 { 129 return new GraphBase() 130 { public ExtendedIterator graphBaseFind( TripleMatch tm ) { return allTriples( tm ); } }; 131 } 132 133 public ExtendedIterator find( TripleMatch m ) 134 { return allTriples( m ); } 135 136 public int size() 137 { return forwardMap.size() * 4; } 138 139 142 public ExtendedIterator tagIterator() 143 { return WrappedIterator.create( forwardMap.keySet().iterator() ); } 144 } 145 146 | Popular Tags |