1 19 20 package com.hp.hpl.jena.util; 23 24 25 26 import java.util.*; 29 30 import com.hp.hpl.jena.rdf.model.*; 31 32 33 34 44 public class ResourceUtils { 45 48 51 54 57 60 75 public static List maximalLowerElements( Collection resources, Property rel, boolean inverse ) { 76 return maximalLowerElements( resources.iterator(), rel, inverse ); 77 } 78 79 96 public static List maximalLowerElements( Iterator resources, Property rel, boolean inverse ) { 97 List in = new ArrayList(); 98 List out = new ArrayList(); 99 100 while (resources.hasNext()) { 101 in.add( resources.next() ); 102 } 103 104 while (! in.isEmpty()) { 105 boolean rCovered = false; 106 Resource r = (Resource) in.remove( 0 ); 107 108 for (Iterator i = in.iterator(); !rCovered && i.hasNext(); ) { 110 Resource next = (Resource) i.next(); 111 rCovered = inverse ? r.hasProperty( rel, next ) : next.hasProperty( rel, r ); 112 } 113 114 for (Iterator i = out.iterator(); !rCovered && i.hasNext(); ) { 116 Resource next = (Resource) i.next(); 117 rCovered = inverse ? r.hasProperty( rel, next ) : next.hasProperty( rel, r ); 118 } 119 120 if (!rCovered) { 122 out.add( r ); 123 } 124 } 125 126 return out; 127 } 128 129 130 144 public static List removeEquiv( List l, Property p, Resource ref ) { 145 List equiv = new ArrayList(); 146 147 for (Iterator i = l.iterator(); i.hasNext(); ) { 148 Resource r = (Resource) i.next(); 149 150 if (r.hasProperty( p, ref ) && ref.hasProperty( p, r )) { 151 equiv.add( r ); 153 } 154 } 155 156 l.removeAll( equiv ); 157 return equiv; 158 } 159 160 161 172 public static List partition( List l, Property p ) { 173 List source = new ArrayList(); 175 source.addAll( l ); 176 List parts = new ArrayList(); 177 178 while (!source.isEmpty()) { 179 Resource seed = (Resource) source.remove( 0 ); 182 List part = removeEquiv( source, p, seed ); 183 part.add( seed ); 184 185 parts.add( part ); 187 } 188 189 return parts; 190 } 191 192 193 211 public static Resource renameResource( Resource old, String uri ) { 212 Model m = old.getModel(); 213 List stmts = new ArrayList(); 214 215 for (Iterator i = old.listProperties(); i.hasNext(); stmts.add( i.next() ) ); 217 218 for (Iterator i = m.listStatements( null, null, old ); i.hasNext(); stmts.add( i.next() ) ); 220 221 Resource res = (uri == null) ? m.createResource() : m.createResource( uri ); 223 224 for (Iterator i = stmts.iterator(); i.hasNext(); ) { 226 Statement s = (Statement) i.next(); 227 228 s.remove(); 229 230 Resource subj = s.getSubject().equals( old ) ? res : s.getSubject(); 231 RDFNode obj = s.getObject().equals( old ) ? res : s.getObject(); 232 233 m.add( subj, s.getPredicate(), obj ); 234 } 235 236 return res; 237 } 238 239 240 248 public static Model reachableClosure( Resource root ) { 249 Model m = ModelFactory.createDefaultModel(); 250 251 Set seen = CollectionFactory.createHashedSet(); 253 254 List queue = new LinkedList(); 256 queue.add( root ); 257 258 while (!queue.isEmpty()) { 259 Resource r = (Resource) queue.remove( 0 ); 260 261 if (!seen.contains( r )) { 263 seen.add( r ); 264 265 for (StmtIterator i = r.listProperties(); i.hasNext(); ) { 267 Statement s = i.nextStatement(); 268 269 m.add( s ); 271 272 if (s.getObject() instanceof Resource) { 273 queue.add( s.getObject() ); 274 } 275 } 276 } 277 } 278 279 return m; 280 } 281 282 283 286 290 } 291 292 293 322 | Popular Tags |