1 5 6 10 11 package org.joseki.util; 12 import java.util.* ; 13 import com.hp.hpl.jena.rdf.model.* ; 14 import org.apache.commons.logging.* ; 15 16 public class Closure 17 { 18 private static Log logger = LogFactory.getLog(Closure.class.getName()) ; 19 20 25 26 public static Model closure(Statement stmt) 27 { 28 return closure(stmt, new ClosureBNode()) ; 29 } 30 31 38 39 public static Model closure(Statement statement, ClosureTest test) 40 { 41 return closure(statement, test, ModelFactory.createDefaultModel()) ; 42 } 43 44 51 52 public static Model closure(Statement statement, Model model) 53 { 54 return closure(statement, new ClosureBNode(), model) ; 55 } 56 57 65 66 public static Model closure(Statement statement, ClosureTest test, Model model) 67 { 68 if ( logger.isDebugEnabled() ) 69 logger.debug("closure: "+statement) ; 70 List visited = new ArrayList() ; 72 73 closure(statement, model, visited, test) ; 74 return model ; 75 } 76 77 83 84 public static Model closure(Resource resource, boolean testThisNode) 85 { 86 return closure(resource, new ClosureBNode(), testThisNode) ; 87 } 88 89 96 97 public static Model closure(Resource resource, ClosureTest test, boolean testThisNode) 98 { 99 return closure(resource, test, testThisNode, ModelFactory.createDefaultModel()) ; 100 } 101 102 103 110 111 public static Model closure(Resource resource, boolean testThisNode, Model results) 112 { 113 return closure(resource, new ClosureBNode(), testThisNode, results) ; 114 } 115 116 117 125 126 public static Model closure(Resource resource, ClosureTest test, 127 boolean testThisNode, Model results) 128 { 129 130 if ( logger.isDebugEnabled() ) 131 logger.debug("closure: "+resource) ; 132 List visited = new ArrayList() ; 135 136 if ( ! testThisNode ) 137 closureNoTest(resource, results, visited, test) ; 138 else 139 closure(resource, results, visited, test) ; 140 return results ; 141 } 142 143 144 145 147 private static void closure(Statement stmt, 148 Model closureBlob, Collection visited, 149 ClosureTest test) 150 { 151 if ( logger.isDebugEnabled() ) 152 logger.trace(stmt.toString()) ; 153 if ( test.includeStmt(stmt) ) 154 closureBlob.add(stmt) ; 155 closure(stmt.getSubject(), closureBlob, visited, test) ; 156 closure(stmt.getObject(), closureBlob, visited, test) ; 157 } 158 159 160 private static void closure(RDFNode n, 161 Model closureBlob, Collection visited, 162 ClosureTest test) 163 { 164 if ( ! ( n instanceof Resource ) ) 165 { 166 if ( logger.isTraceEnabled() ) 167 logger.trace(" Not a resource: "+n) ; 168 return ; 169 } 170 171 Resource r = (Resource)n ; 172 173 if ( visited.contains(r) ) 174 { 175 if ( logger.isTraceEnabled() ) 176 logger.trace(" Already visited: "+r) ; 177 return ; 178 } 179 180 if ( ! test.traverse(r) ) 181 { 182 if ( logger.isTraceEnabled() ) 183 logger.trace(" End traverse: "+r) ; 184 return ; 185 } 186 187 closureNoTest(r, closureBlob, visited, test) ; 188 } 189 190 191 private static void closureNoTest(Resource r, 192 Model closureBlob, Collection visited, 193 ClosureTest test) 194 { 195 if (logger.isTraceEnabled() ) 196 logger.trace("@ "+r+" :: "+closureBlob.size()+" //"+dbg_string(visited)); 197 198 visited.add(r) ; 199 200 StmtIterator sIter = r.listProperties() ; 201 for ( ; sIter.hasNext() ; ) 202 { 203 Statement stmt = sIter.nextStatement() ; 204 closure(stmt, closureBlob, visited, test) ; 205 } 206 } 207 208 209 private static String dbg_string(Collection s) 210 { 211 String tmp = "" ; 212 for ( Iterator iter = s.iterator() ; iter.hasNext() ; ) 213 { 214 tmp = tmp+" "+iter.next().toString() ; 215 } 216 return tmp ; 217 } 218 219 221 public static class ClosureBNode implements ClosureTest 222 { 223 public boolean traverse(Resource r) 224 { 225 return r.isAnon() ; 226 } 227 228 public boolean includeStmt(Statement s) 229 { 230 return true ; 231 } 232 } 233 234 236 public static class ClosureReachable implements ClosureTest 237 { 238 public boolean traverse(Resource r) 239 { 240 return true ; 241 } 242 243 public boolean includeStmt(Statement s) 244 { 245 return true ; 246 } 247 } 248 249 250 } 251 252 253 279 280 | Popular Tags |