| 76 | | prunecut = 100; |
| 77 | | } |
| 78 | | |
| 79 | | public Path[] getPaths(RDFSchemaAnalyzer rdfsa, boolean countLink){ |
| | 76 | } |
| | 77 | */ |
| | 78 | |
| | 79 | public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ |
| | 80 | super(); |
| | 81 | nodeType = new LinkedList<String>(); |
| | 82 | setClassGraph(rdfsa); |
| | 83 | connectionTable = createConnectionTable(); |
| | 84 | } |
| | 85 | |
| | 86 | public int getNumberOfEdge(String url){ |
| | 87 | Integer node = labelednodes.get(url); |
| | 88 | if (node == null){ |
| | 89 | return 0; |
| | 90 | } |
| | 91 | return adjlist.get(node).size(); |
| | 92 | } |
| | 93 | |
| | 94 | public Path[] getPaths(String startClass, String endClass){ |
| | 95 | List<List<ClassLink>> paths = searchPaths(startClass, endClass); |
| | 96 | |
| | 97 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
| | 98 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
| | 99 | int j = 0; |
| | 100 | while ( pit.hasNext() ){ |
| | 101 | Path path = new Path(); |
| | 102 | path.setStartClass(startClass); |
| | 103 | List<ClassLink> crrpath = pit.next(); |
| | 104 | path.setClassLinks(crrpath); |
| | 105 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
| | 106 | int min = Integer.MAX_VALUE; |
| | 107 | while ( cit.hasNext() ){ |
| | 108 | ClassLink cl = cit.next(); |
| | 109 | if ( cl.getNumOfLinks() < min ){ |
| | 110 | min = cl.getNumOfLinks(); |
| | 111 | } |
| | 112 | } |
| | 113 | // using length of path |
| | 114 | int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() ); |
| | 115 | path.setWidth(rankwidth); |
| | 116 | sortedpath.add(path); |
| | 117 | j++; |
| | 118 | } |
| | 119 | Path[] patharray = new Path[paths.size()]; |
| | 120 | Iterator<Path> pait = sortedpath.descendingIterator(); |
| | 121 | int i = 0; |
| | 122 | while ( pait.hasNext() ){ |
| | 123 | patharray[i] = pait.next(); |
| | 124 | i++; |
| | 125 | } |
| | 126 | return patharray; |
| | 127 | } |
| | 128 | |
| | 129 | public HashSet<Integer> getConnectionList(Integer node){ |
| | 130 | return connectionTable.get(node); |
| | 131 | } |
| | 132 | |
| | 133 | public Path[] getPaths_old(RDFSchemaAnalyzer rdfsa, boolean countLink, String startClass, String endClass){ |
| 112 | | |
| | 165 | |
| | 166 | private List<List<ClassLink>> searchPaths(String startClass, String endClass){ |
| | 167 | |
| | 168 | List<List<ClassLink>> paths = new ArrayList<>(); |
| | 169 | List<List<Integer>> simplePaths = new LinkedList<>(); |
| | 170 | Integer snode = labelednodes.get(startClass); |
| | 171 | Integer enode = labelednodes.get(endClass); |
| | 172 | List<List<Integer>> lp = new LinkedList<>(); |
| | 173 | List<Integer> ini = new LinkedList<Integer>(); // initial path |
| | 174 | ini.add(snode); |
| | 175 | lp.add(ini); |
| | 176 | for (int i = 0; i < nsteps; i++ ){ |
| | 177 | ListIterator<List<Integer>> lit = lp.listIterator(); |
| | 178 | List<List<Integer>> nextlp = new LinkedList<>(); |
| | 179 | while ( lit.hasNext() ){ |
| | 180 | List<Integer> crrpath = lit.next(); |
| | 181 | Integer crrnode = crrpath.get(crrpath.size()-1); |
| | 182 | Set<Integer> nexts = gadjlist.get(crrnode).keySet(); |
| | 183 | Iterator<Integer> nit = nexts.iterator(); |
| | 184 | while( nit.hasNext() ){ |
| | 185 | Integer nextnode = nit.next(); |
| | 186 | if ( crrpath.contains(nextnode) ){ continue; } |
| | 187 | List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy |
| | 188 | nextpath.add(nextnode); |
| | 189 | if ( nextnode.equals(enode) ){ |
| | 190 | simplePaths.add(nextpath); |
| | 191 | continue; |
| | 192 | } |
| | 193 | nextlp.add(nextpath); |
| | 194 | } |
| | 195 | } |
| | 196 | lp = nextlp; |
| | 197 | } |
| | 198 | |
| | 199 | ListIterator<List<Integer>> pit = simplePaths.listIterator(); |
| | 200 | while( pit.hasNext()){ |
| | 201 | List<Integer> spath = pit.next(); |
| | 202 | List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath); |
| | 203 | paths.addAll(convertedPaths); |
| | 204 | } |
| | 205 | return paths; |
| | 206 | } |
| | 207 | |
| | 208 | private List<List<ClassLink>> convertSimplePathToPaths(List<Integer> simplePath){ |
| | 209 | List<List<ClassLink>> paths = new LinkedList<List<ClassLink>>(); |
| | 210 | //List<List<LabeledEdge>> multiedges = new LinkedList<List<LabeledEdge>>(); |
| | 211 | ListIterator<Integer> spit = simplePath.listIterator(); |
| | 212 | Integer start = spit.next(); |
| | 213 | Integer end = spit.next(); |
| | 214 | List<LabeledEdge> edges = gadjlist.get(start).get(end); |
| | 215 | ListIterator<LabeledEdge> eit = edges.listIterator(); |
| | 216 | while ( eit.hasNext() ){ |
| | 217 | List<ClassLink> cl = new LinkedList<ClassLink>(); |
| | 218 | cl.add((ClassLink)eit.next().getLabel()); |
| | 219 | paths.add(cl); |
| | 220 | } |
| | 221 | start = end; |
| | 222 | while( spit.hasNext() ){ |
| | 223 | end = spit.next(); |
| | 224 | // start-end |
| | 225 | edges = gadjlist.get(start).get(end); |
| | 226 | List<List<ClassLink>> tmppaths = new LinkedList<List<ClassLink>>(); |
| | 227 | // current path |
| | 228 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
| | 229 | while ( pit.hasNext() ){ |
| | 230 | List<ClassLink> basepath = pit.next(); |
| | 231 | eit = edges.listIterator(); |
| | 232 | while ( eit.hasNext() ){ |
| | 233 | ClassLink cl = (ClassLink) eit.next().label; |
| | 234 | List<ClassLink> addedpath = new LinkedList<ClassLink>(basepath); |
| | 235 | addedpath.add(cl); |
| | 236 | tmppaths.add(addedpath); |
| | 237 | } |
| | 238 | } |
| | 239 | paths = tmppaths; |
| | 240 | start = end; |
| | 241 | } |
| | 242 | return paths; |
| | 243 | } |
| | 244 | |
| | 245 | /* |
| 195 | | |
| 196 | | /* |
| 197 | | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
| 198 | | List<List<ClassLink>> paths = new ArrayList<>(); |
| 199 | | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
| 200 | | List<LinkAndPath> lp = new LinkedList<>(); |
| 201 | | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
| 202 | | try{ |
| 203 | | for ( int i = 0; i < nsteps; i++ ){ |
| 204 | | ListIterator<LinkAndPath> lit = lp.listIterator(); |
| 205 | | List<LinkAndPath> nextlp = new LinkedList<>(); |
| 206 | | while ( lit.hasNext() ){ |
| 207 | | LinkAndPath crrlp = lit.next(); |
| 208 | | ClassLink[] classLinks = null; |
| 209 | | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); |
| 210 | | for ( int j = 0 ; j < classLinks.length; j++ ){ |
| 211 | | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } |
| 212 | | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
| 213 | | crrpath.add(classLinks[j]); |
| 214 | | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
| 215 | | paths.add(new LinkedList<>(crrpath)); |
| 216 | | continue; |
| 217 | | } |
| 218 | | if (classLinks[j].getNumOfLinks() <= th ){ |
| 219 | | continue; //cut by the number of instances |
| 220 | | } |
| 221 | | // Divergence & Convergence Decision |
| 222 | | boolean con = false; |
| 223 | | boolean div = false; |
| 224 | | if ( decideConvergence(classLinks[j]) ){ // convergence |
| 225 | | con = true; |
| 226 | | } |
| 227 | | if ( decideDivergence(classLinks[j]) ){ // divergence |
| 228 | | div = true; |
| 229 | | } |
| 230 | | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
| 231 | | continue; // cut by the differences of entropies |
| 232 | | } |
| 233 | | // crr & next are the same arcs |
| 234 | | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
| 235 | | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
| 236 | | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
| 237 | | continue; |
| 238 | | } |
| 239 | | |
| 240 | | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); |
| 241 | | } |
| 242 | | } |
| 243 | | lp = nextlp; |
| 244 | | } |
| 245 | | }catch(Exception e){ |
| 246 | | System.err.println(e); |
| 247 | | } |
| 248 | | return paths; |
| 249 | | } |
| 250 | | */ |
| 251 | | |
| 252 | | private boolean checkPruning(ClassLink classLink1, ClassLink classLink2){ |
| 253 | | // true -> prune link2, false -> add link2 |
| 254 | | int noi1 = classLink1.getNumOfOriginInstances(); |
| 255 | | int nli1 = classLink1.getNumOfLinkedInstances(); |
| 256 | | int noi2 = classLink2.getNumOfOriginInstances(); |
| 257 | | int nli2 = classLink2.getNumOfLinkedInstances(); |
| 258 | | if ( noi1 == 0 || nli1 == 0 || noi2 == 0 || nli2 == 0 ){ |
| 259 | | return true; |
| 260 | | } |
| 261 | | return ( noi1 / nli1 > prunecut && nli2 / noi2 > prunecut ); |
| 262 | | } |
| 263 | | |
| 264 | | /* private boolean decideConvergence(ClassLink classLink){ |
| 265 | | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
| 266 | | classLink.getNumOfLinkedInstances(), |
| 267 | | classLink.getNumOfLinks()); |
| 268 | | if ( con > concut ){ |
| 269 | | return true; |
| 270 | | } |
| 271 | | return false; |
| 272 | | } |
| 273 | | */ |
| 274 | | |
| 275 | | /* |
| 276 | | private boolean decideDivergence(ClassLink classLink){ |
| 277 | | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
| 278 | | classLink.getNumOfLinkedInstances(), |
| 279 | | classLink.getNumOfLinks()); |
| 280 | | if ( con < divcut ){ |
| 281 | | return true; |
| 282 | | } |
| 283 | | return false; |
| 284 | | } |
| 285 | | */ |
| 286 | | |
| 287 | | /* |
| 288 | | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
| 289 | | //return (double) numOfLinks / (double) numOfLinkedInstances ; |
| 290 | | // Convergence plus, Divergence minus |
| 291 | | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); |
| 292 | | } |
| 293 | | */ |
| 294 | | |
| 295 | | public void setWholeGraph(RDFSchemaAnalyzer rdfsa){ |
| | 330 | |
| | 331 | private void setClassGraph(RDFSchemaAnalyzer rdfsa){ |
| 311 | | addEdge(i, labelednodes.get(classLinks[j].getLinkedClassURI()), |
| 312 | | classLinks[j].getPropertyURI(), |
| 313 | | classLinks[j].getDirection(), |
| 314 | | classLinks[j].getNumOfLinkedInstances()); |
| | 348 | Integer n = labelednodes.get(classLinks[j].getLinkedClassURI()); |
| | 349 | if ( n != null ){ |
| | 350 | addEdge(i, n, classLinks[j]); |
| | 351 | }else{ |
| | 352 | n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI()); |
| | 353 | if ( n == null ){ |
| | 354 | addNode(classLinks[j].getLinkedLiteralDatatypeURI()); |
| | 355 | n = nodeType.size(); |
| | 356 | nodeType.add("literal"); |
| | 357 | } |
| | 358 | addEdge(i, n, classLinks[j]); |
| | 359 | /* |
| | 360 | ClassLink rev = new ClassLink( classLinks[j].getPropertyURI(), |
| | 361 | //classLinks[j].getLinkedClassURI(), |
| | 362 | classes[i].getClassURI(), |
| | 363 | classLinks[j].getLinkedLiteralDatatypeURI(), |
| | 364 | classLinks[j].getDirection(), classLinks[j].getNumOfLinks(), |
| | 365 | classLinks[j].getNumOfOriginInstances(), classLinks[j].getNumOfLinkedInstances(), |
| | 366 | classLinks[j].getNumOfOriginClassInstances(), classLinks[j].getNumOfLinkedClassInstances(), |
| | 367 | classLinks[j].isDomainClassLimitedQ(), classLinks[j].isRangeClassLimitedQ() ); |
| | 368 | rev.setDirection(Direction.reverse); |
| | 369 | addEdge(n, i, rev); |
| | 370 | */ |
| | 371 | } |
| 322 | | /* |
| 323 | | public Path[] getPathsByWholeGraph(RDFSchemaAnalyzer rdfsa){ |
| 324 | | setWholeGraph(rdfsa); |
| 325 | | List<List<ClassLink>> paths = null; |
| 326 | | //paths = searchPaths(rdfsa, countLink); |
| 327 | | paths = searchPathsbyVisitingNodes(rdfsa, countLink); |
| 328 | | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
| 329 | | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
| 330 | | int j = 0; |
| 331 | | while ( pit.hasNext() ){ |
| 332 | | Path path = new Path(); |
| 333 | | path.setStartClass(startClass); |
| 334 | | List<ClassLink> crrpath = pit.next(); |
| 335 | | path.setClassLinks(crrpath); |
| 336 | | ListIterator<ClassLink> cit = crrpath.listIterator(); |
| 337 | | int min = Integer.MAX_VALUE; |
| 338 | | while ( cit.hasNext() ){ |
| 339 | | ClassLink cl = cit.next(); |
| 340 | | if ( cl.getNumOfLinks() < min ){ |
| 341 | | min = cl.getNumOfLinks(); |
| 342 | | } |
| 343 | | } |
| 344 | | path.setWidth(min); |
| 345 | | sortedpath.add(path); |
| 346 | | j++; |
| 347 | | } |
| 348 | | Path[] patharray = new Path[paths.size()]; |
| 349 | | Iterator<Path> pait = sortedpath.descendingIterator(); |
| 350 | | int i = 0; |
| 351 | | while ( pait.hasNext() ){ |
| 352 | | patharray[i] = pait.next(); |
| 353 | | i++; |
| 354 | | } |
| 355 | | return patharray; |
| 356 | | } |
| 357 | | */ |
| | 379 | private ArrayList<HashSet<Integer>> createConnectionTable(){ |
| | 380 | ArrayList<HashSet<Integer>> ct = new ArrayList<HashSet<Integer>>(); |
| | 381 | for (int i = 0; i < labels.size(); i++ ){ // each node |
| | 382 | HashSet<Integer> cl = createConnectionList(i); |
| | 383 | ct.add(cl); |
| | 384 | } |
| | 385 | return ct; |
| | 386 | } |
| | 387 | |
| | 388 | private HashSet<Integer> createConnectionList(Integer node){ |
| | 389 | HashSet<Integer> cl = new HashSet<Integer>(); |
| | 390 | HashSet<Integer> crrnodes = new HashSet<Integer>(); |
| | 391 | crrnodes.add(node); |
| | 392 | cl.add(node); |
| | 393 | for ( int i = 0 ; i < nsteps; i++ ){ |
| | 394 | Set<Integer> nextnodes = new HashSet<Integer>(); |
| | 395 | Iterator<Integer> cit = crrnodes.iterator(); |
| | 396 | while ( cit.hasNext() ){ |
| | 397 | Integer crr = cit.next(); |
| | 398 | Set<Integer> nexts = gadjlist.get(crr).keySet(); |
| | 399 | Iterator<Integer> nit = nexts.iterator(); |
| | 400 | while ( nit.hasNext() ){ |
| | 401 | Integer next = nit.next(); |
| | 402 | if ( !cl.contains(next) ){ |
| | 403 | nextnodes.add(next); |
| | 404 | cl.add(next); |
| | 405 | } |
| | 406 | } |
| | 407 | } |
| | 408 | crrnodes.clear(); |
| | 409 | crrnodes.addAll(nextnodes); |
| | 410 | } |
| | 411 | return cl; |
| | 412 | } |