1 6 7 package com.hp.hpl.jena.graph.impl; 8 9 15 16 import com.hp.hpl.jena.graph.*; 17 import com.hp.hpl.jena.shared.*; 18 import com.hp.hpl.jena.util.iterator.*; 19 20 import com.hp.hpl.jena.vocabulary.RDF; 21 22 public class SimpleReifier implements Reifier 23 { 24 protected final GraphBase parent; 25 protected final boolean intercepting; 26 protected final boolean concealing; 27 protected final ReificationStyle style; 28 29 protected ReifierFragmentsMap fragmentsMap; 30 protected ReifierTripleMap tripleMap; 31 32 protected boolean closed = false; 33 34 40 public SimpleReifier( GraphBase parent, ReificationStyle style ) 41 { this( parent, new SimpleReifierTripleMap(), new SimpleReifierFragmentsMap(), style ); } 42 43 public SimpleReifier 44 ( GraphBase parent, ReifierTripleMap tm, ReifierFragmentsMap fm, ReificationStyle style ) 45 { 46 this.parent = parent; 47 this.fragmentsMap = fm; 48 this.tripleMap = tm; 49 this.intercepting = style.intercepts(); 50 this.concealing = style.conceals(); 51 this.style = style; 52 } 53 54 public ReificationStyle getStyle() 55 { return style; } 56 57 58 public Graph getParentGraph() 59 { return parent; } 60 61 62 public Triple getTriple( Node n ) 63 { return tripleMap.getTriple( n ); } 64 65 66 public boolean hasTriple( Node n ) 67 { return getTriple( n ) != null; } 68 69 70 public ExtendedIterator allNodes() 71 { return tripleMap.tagIterator(); } 72 73 public ExtendedIterator allNodes( Triple t ) 74 { return tripleMap.tagIterator( t ); } 75 76 80 public Node reifyAs( Node tag, Triple toReify ) 81 { 82 Triple existing = (Triple) tripleMap.getTriple( tag ); 83 if (existing != null) 84 { if (!toReify.equals( existing )) throw new AlreadyReifiedException( tag ); } 85 else 86 reifyNewTriple( tag, toReify ); 87 if (concealing == false) graphAddQuad( parent, tag, toReify ); 88 return tag; 89 } 90 91 97 protected void reifyNewTriple( Node tag, Triple toReify ) 98 { 99 if (fragmentsMap.hasFragments( tag )) 100 { 101 graphAddQuad( parent, tag, toReify ); 102 if (tripleMap.getTriple( tag ) == null) throw new CannotReifyException( tag ); 103 } 104 else 105 tripleMap.putTriple( tag, toReify ); 106 } 107 108 112 public void remove( Node n, Triple t ) 113 { 114 Triple x = (Triple) tripleMap.getTriple( n ); 115 if (t.equals( x )) 116 { tripleMap.removeTriple( n, t ); 117 if (!concealing) parentRemoveQuad( n, t ); } 118 } 119 120 public void remove( Triple t ) 121 { tripleMap.removeTriple( t ); } 122 123 public boolean hasTriple( Triple t ) 124 { return tripleMap.hasTriple( t ); } 125 126 public boolean handledAdd( Triple fragment ) 127 { 128 if (intercepting) 129 { 130 ReifierFragmentHandler s = fragmentsMap.getFragmentHandler( fragment ); 131 if (s == null) 132 return false; 133 else 134 { 135 addFragment( s, fragment ); 136 return true; 137 } 138 } 139 else 140 return false; 141 } 142 143 149 protected void addFragment( ReifierFragmentHandler s, Triple fragment ) 150 { 151 Node tag = fragment.getSubject(), object = fragment.getObject(); 152 Triple reified = tripleMap.getTriple( tag ); 153 if (reified == null) 154 updateFragments( s, fragment, tag, object ); 155 else if (s.clashedWith( object, reified )) 156 tripleMap.removeTriple( tag, reified ); 157 } 158 159 165 private void updateFragments( ReifierFragmentHandler s, Triple fragment, Node tag, Node object ) 166 { 167 Triple t = s.reifyIfCompleteQuad( fragment, tag, object ); 168 if (t instanceof Triple) tripleMap.putTriple( tag, t ); 169 } 170 171 public boolean handledRemove( Triple fragment ) 172 { 173 if (intercepting) 174 { 175 ReifierFragmentHandler s = fragmentsMap.getFragmentHandler( fragment ); 176 if (s == null) 177 return false; 178 else 179 { 180 removeFragment( s, fragment ); 181 return true; 182 } 183 } 184 else 185 return false; 186 } 187 188 192 private void removeFragment( ReifierFragmentHandler s, Triple fragment ) 193 { 194 Node tag = fragment.getSubject(); 195 Triple already = tripleMap.getTriple( tag ); 196 Triple complete = s.removeFragment( tag, already, fragment ); 197 if (complete == null) 198 tripleMap.removeTriple( tag ); 199 else 200 tripleMap.putTriple( tag, complete ); 201 } 202 203 public ExtendedIterator find( TripleMatch m ) 204 { return tripleMap.find( m ).andThen( fragmentsMap.find( m ) ); } 205 206 public ExtendedIterator findExposed( TripleMatch m ) 207 { return findEither( m, false ); } 208 209 public ExtendedIterator findEither( TripleMatch m, boolean showHidden ) 210 { return showHidden == concealing ? find( m ) : NullIterator.instance; } 211 212 public int size() 213 { return concealing ? 0 : tripleMap.size() + fragmentsMap.size(); } 214 215 219 private void parentRemoveQuad( Node n, Triple t ) 220 { 221 parent.delete( Triple.create( n, RDF.Nodes.type, RDF.Nodes.Statement ) ); 222 parent.delete( Triple.create( n, RDF.Nodes.subject, t.getSubject() ) ); 223 parent.delete( Triple.create( n, RDF.Nodes.predicate, t.getPredicate() ) ); 224 parent.delete( Triple.create( n, RDF.Nodes.object, t.getObject() ) ); 225 } 226 227 public static void graphAddQuad( GraphAdd g, Node node, Triple t ) 228 { 229 g.add( Triple.create( node, RDF.Nodes.subject, t.getSubject() ) ); 230 g.add( Triple.create( node, RDF.Nodes.predicate, t.getPredicate() ) ); 231 g.add( Triple.create( node, RDF.Nodes.object, t.getObject() ) ); 232 g.add( Triple.create( node, RDF.Nodes.type, RDF.Nodes.Statement ) ); 233 } 234 235 239 public String toString() 240 { return "<R " + fragmentsMap + "|" + tripleMap + ">"; } 241 242 245 public void close() 246 { 247 fragmentsMap = null; 248 tripleMap = null; 249 closed = true; 250 } 251 252 255 public boolean isClosed() 256 { return closed; } 257 } 258 259 288 | Popular Tags |