1 6 package com.hp.hpl.jena.graph.impl; 7 8 import java.util.Iterator ; 9 import java.util.Map ; 10 import java.util.Set ; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.util.CollectionFactory; 14 import com.hp.hpl.jena.util.iterator.*; 15 import com.hp.hpl.jena.vocabulary.RDF; 16 17 23 public class SimpleReifierFragmentsMap implements ReifierFragmentsMap 24 { 25 26 protected Map forwardMap = CollectionFactory.createHashedMap(); 27 28 protected Fragments getFragments( Node tag ) 29 { return (Fragments) forwardMap.get( tag ); } 30 31 protected void removeFragments( Node key ) 32 { forwardMap.remove( key ); } 33 34 37 protected Fragments putFragments( Node key, Fragments value ) 38 { 39 forwardMap.put( key, value ); 40 return value; 41 } 42 43 protected ExtendedIterator allTriples( TripleMatch tm ) 44 { 45 Triple t = tm.asTriple(); 46 Node subject = t.getSubject(); 47 if (subject.isConcrete()) 48 { 49 Fragments x = (Fragments) forwardMap.get( subject ); 50 return x == null 51 ? NullIterator.instance 52 : explodeFragments( t, subject, x ) 53 ; 54 } 55 else 56 { 57 final Iterator it = forwardMap.entrySet().iterator(); 58 return new FragmentTripleIterator( t, it ) 59 { 60 public void fill( GraphAdd ga, Node n, Object fragmentsObject ) 61 { 62 ((Fragments) fragmentsObject).includeInto( ga ); 63 } 64 }; 65 } 66 } 67 68 74 protected ExtendedIterator explodeFragments( Triple t, Node subject, Fragments x ) 75 { 76 GraphAddList L = new GraphAddList( t ); 77 x.includeInto( L ); 78 return WrappedIterator.create( L.iterator() ); 79 } 80 81 public ExtendedIterator find( TripleMatch m ) 82 { return allTriples( m ); } 83 84 public int size() 85 { 86 int result = 0; 87 Iterator it = forwardMap.entrySet().iterator(); 88 while (it.hasNext()) 89 { 90 Map.Entry e = (Map.Entry ) it.next(); 91 Fragments f = (Fragments) e.getValue(); 92 result += f.size(); 93 } 94 return result; 95 } 96 97 101 public ReifierFragmentHandler getFragmentHandler( Triple t ) 102 { 103 Node p = t.getPredicate(); 104 ReifierFragmentHandler x = (ReifierFragmentHandler) selectors.get( p ); 105 if (x == null || (p.equals( RDF.Nodes.type ) && !t.getObject().equals( RDF.Nodes.Statement ) ) ) return null; 106 return x; 107 } 108 109 protected void putAugmentedTriple( SimpleReifierFragmentHandler s, Node tag, Node object, Triple reified ) 110 { 111 Fragments partial = new Fragments( tag, reified ); 112 partial.add( s, object ); 113 putFragments( tag, partial ); 114 } 115 116 protected Triple reifyCompleteQuad( SimpleReifierFragmentHandler s, Triple fragment, Node tag, Node object ) 117 { 118 Fragments partial = getFragments( tag ); 119 if (partial == null) putFragments( tag, partial = new Fragments( tag ) ); 120 partial.add( s, object ); 121 if (partial.isComplete()) 122 { 123 removeFragments( fragment.getSubject() ); 124 return partial.asTriple(); 125 } 126 else 127 return null; 128 } 129 130 protected Triple removeFragment( SimpleReifierFragmentHandler s, Node tag, Triple already, Triple fragment ) 131 { 132 Fragments partial = getFragments( tag ); 133 Fragments fs = (already != null ? explode( tag, already ) 134 : partial == null ? putFragments( tag, new Fragments( tag ) ) 135 : (Fragments) partial); 136 fs.remove( s, fragment.getObject() ); 137 if (fs.isComplete()) 138 { 139 Triple result = fs.asTriple(); 140 removeFragments( tag ); 141 return result; 142 } 143 else 144 { 145 if (fs.isEmpty()) removeFragments( tag ); 146 return null; 147 } 148 } 149 150 protected Fragments explode( Node s, Triple t ) 151 { return putFragments( s, new Fragments( s, t ) ); } 152 153 public boolean hasFragments( Node tag ) 154 { return getFragments( tag ) != null; } 155 156 protected static class Fragments 157 { 158 159 164 private final Set [] slots = 165 {CollectionFactory.createHashedSet(), CollectionFactory.createHashedSet(), CollectionFactory.createHashedSet(), CollectionFactory.createHashedSet()}; 166 167 170 private Node anchor; 171 172 178 public Fragments( Node n ) 179 { this.anchor = n; } 180 181 public Fragments( Node n, Triple t ) 182 { 183 this( n ); 184 addTriple( t ); 185 } 186 187 public int size() 188 { return slots[0].size() + slots[1].size() + slots[2].size() + slots[3].size(); } 189 190 194 public boolean isComplete() 195 { return slots[0].size() == 1 && slots[1].size() == 1 && slots[2].size() == 1 && slots[3].size() == 1; } 196 197 201 public boolean isEmpty() 202 { return slots[0].isEmpty() && slots[1].isEmpty() && slots[2].isEmpty() && slots[3].isEmpty(); } 203 204 207 public void remove( SimpleReifierFragmentHandler w, Node n ) 208 { slots[w.which].remove( n ); } 209 210 213 public void add( SimpleReifierFragmentHandler w, Node n ) 214 { slots[w.which].add( n ); } 215 216 220 public void includeInto( GraphAdd g ) 221 { 222 includeInto( g, RDF.Nodes.subject, SUBJECTS_index ); 223 includeInto( g, RDF.Nodes.predicate, PREDICATES_index ); 224 includeInto( g, RDF.Nodes.object, OBJECTS_index ); 225 includeInto( g, RDF.Nodes.type, TYPES_index ); 226 } 227 228 233 private void includeInto( GraphAdd g, Node predicate, int which ) 234 { 235 Iterator it = slots[which].iterator(); 236 while (it.hasNext()) 237 g.add( Triple.create( anchor, predicate, (Node) it.next() ) ); 238 } 239 240 246 public Fragments addTriple( Triple t ) 247 { 248 slots[SUBJECTS_index].add( t.getSubject() ); 249 slots[PREDICATES_index].add( t.getPredicate() ); 250 slots[OBJECTS_index].add( t.getObject() ); 251 slots[TYPES_index].add( RDF.Nodes.Statement ); 252 return this; 253 } 254 255 261 Triple asTriple() 262 { return Triple.create( only( slots[SUBJECTS_index] ), only( slots[PREDICATES_index] ), only( slots[OBJECTS_index] ) ); } 263 264 269 private Node only( Set s ) 270 { return (Node) s.iterator().next(); } 271 272 275 public String toString() 276 { return anchor + " s:" + slots[SUBJECTS_index] + " p:" + slots[PREDICATES_index] + " o:" + slots[OBJECTS_index] + " t:" + slots[TYPES_index]; } 277 278 } 279 280 284 protected static final int TYPES_index = 0; 285 protected static final int SUBJECTS_index = 1; 286 protected static final int PREDICATES_index = 2; 287 protected static final int OBJECTS_index = 3; 288 289 protected final ReifierFragmentHandler TYPES = new SimpleReifierFragmentHandler( this, TYPES_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return false; } }; 290 protected final ReifierFragmentHandler SUBJECTS = new SimpleReifierFragmentHandler( this, SUBJECTS_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return !n.equals( reified.getSubject() ); } }; 291 protected final ReifierFragmentHandler PREDICATES = new SimpleReifierFragmentHandler( this, PREDICATES_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return !n.equals( reified.getPredicate() ); } }; 292 protected final ReifierFragmentHandler OBJECTS = new SimpleReifierFragmentHandler( this, OBJECTS_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return !n.equals( reified.getObject() ); } }; 293 294 public final Map selectors = makeSelectors(); 295 296 299 protected Map makeSelectors() 300 { 301 Map result = CollectionFactory.createHashedMap(); 302 result.put( RDF.Nodes.subject, SUBJECTS ); 303 result.put( RDF.Nodes.predicate, PREDICATES ); 304 result.put( RDF.Nodes.object, OBJECTS ); 305 result.put( RDF.Nodes.type, TYPES ); 306 return result; 307 } 308 } 309 310 | Popular Tags |