/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.biohackathon.SPARQLBuilder.OWL; /** * * @author atsuko */ import java.util.*; public class OWLClassGraph extends LabeledMultiDigraph{ //String startClass; //String endClass; int nsteps = 4; int limit = 100; int th = 1; double cth = 1.0; // 0.0(no path) - 1.0(all paths) List nodeType; ArrayList> connectionTable; String sparqlEndpoint; Set visited; boolean askcheck; List> edgeweight; List nodeweight; Map checkedpaths; public class LinkAndPath{ String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL ClassLink classLink; List path; Set classURIs; // apearing class URIs in the path public LinkAndPath(ClassLink classLink, List path){ this.classLink = classLink; this.path = path; } public LinkAndPath(ClassLink classLink, List path, String originalClassURI){ this.classLink = classLink; this.path = path; this.originalClassURI = originalClassURI; } public LinkAndPath(ClassLink classLink, List path, String originalClassURI, Set classURIs){ this.classLink = classLink; this.path = path; this.originalClassURI = originalClassURI; this.classURIs = classURIs; } } public OWLClassGraph(){ // not used super(); nodeType = new LinkedList(); } public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ // for experiment super(); nodeType = new LinkedList(); this.askcheck = false; setClassGraph(rdfsa); } public OWLClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass, boolean askcheck){ // used super(); nodeType = new LinkedList(); this.askcheck = askcheck; setPartClassGraph(rdfsa, sparqlEndpoint, startClass); } public int getNumberOfEdge(String url){ Integer node = labelednodes.get(url); if (node == null){ return 0; } return adjlist.get(node).size(); } public boolean visitedNode(String classURI){ if ( visited.contains(labelednodes.get(classURI)) ){ return true; } return false; } public Path[] getPaths(String startClass, String endClass){ List> paths = searchPaths(startClass, endClass); List sortedpaths = new LinkedList(); ListIterator> pit = paths.listIterator(); int j = 0; while ( pit.hasNext() ){ Path path = new Path(); path.setStartClass(startClass); List crrpath = pit.next(); path.setClassLinks(crrpath); ListIterator cit = crrpath.listIterator(); int min = Integer.MAX_VALUE; while ( cit.hasNext() ){ ClassLink cl = cit.next(); if ( cl.getNumOfLinks() < min ){ min = cl.getNumOfLinks(); } } // using length of path int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() ); path.setWidth(rankwidth); sortedpaths.add(path); j++; } Path[] patharray = new Path[paths.size()]; Collections.sort(sortedpaths); Iterator pait = sortedpaths.listIterator(); int i = 0; while ( pait.hasNext() ){ patharray[paths.size()-i-1] = pait.next(); i++; } return patharray; } public HashSet getConnectionList(Integer node){ return connectionTable.get(node); } private List> searchPaths(String startClass, String endClass){ //int asked = 0; checkedpaths = new HashMap(); List> paths = new ArrayList<>(); Integer snode = labelednodes.get(startClass); Integer enode = labelednodes.get(endClass); List> simplePaths = searchSimplePaths(snode, enode); ListIterator> pit = simplePaths.listIterator(); //System.out.println("SPATH:"); //System.out.println(simplePaths.size()); while( pit.hasNext()){ List spath = pit.next(); List> convertedPaths = convertSimplePathToPaths(spath); paths.addAll(convertedPaths); } //System.out.println("PATH:"); //System.out.println(paths.size()); return paths; } private List> searchSimplePaths(Integer snode, Integer enode){ List> simplePaths = new LinkedList<>(); List> lp = new LinkedList<>(); List ini = new LinkedList(); // initial path ini.add(snode); lp.add(ini); for (int i = 0; i < nsteps; i++ ){ ListIterator> lit = lp.listIterator(); List> nextlp = new LinkedList<>(); while ( lit.hasNext() ){ List crrpath = lit.next(); Integer crrnode = crrpath.get(crrpath.size()-1); Set nexts = gadjlist.get(crrnode).keySet(); Iterator nit = nexts.iterator(); while( nit.hasNext() ){ Integer nextnode = nit.next(); if ( crrpath.contains(nextnode) ){ continue; } List nextpath = new LinkedList(crrpath); // copy nextpath.add(nextnode); // tmp if ( i >= 1 && askcheck == true ){ int wn = nodeweight.get(crrnode); int in = edgeweight.get(crrpath.get(crrpath.size()-2)).get(crrnode); int out = edgeweight.get(nextnode).get(crrnode); if ( wn > in + out ){ /* String key1 = nextnode.toString().concat("-").concat(crrpath.get(crrpath.size()-1).toString()) .concat("-").concat(crrpath.get(crrpath.size()-2).toString()); String key2 = crrpath.get(crrpath.size()-2).toString().concat("-").concat(crrpath.get(crrpath.size()-1).toString()) .concat("-").concat(nextnode.toString()); if ( checkedpaths.containsKey(key1) ){ if ( checkedpaths.get(key1) == false ){ continue; } }else if ( checkedpaths.containsKey(key2) ){ if ( checkedpaths.get(key2) == false ){ continue; } }else{ boolean chk = EndpointAccess.check3SimplePath(nextnode, crrpath.get(crrpath.size()-1), crrpath.get(crrpath.size()-2), this, sparqlEndpoint); checkedpaths.put(key1, chk); if ( chk == false ){ continue; } } */ continue; } } if ( nextnode.equals(enode) ){ simplePaths.add(nextpath); continue; } nextlp.add(nextpath); } } lp = nextlp; } return simplePaths; } private List> convertSimplePathToPaths(List simplePath){ List> paths = new LinkedList>(); ListIterator spit = simplePath.listIterator(); Integer start = spit.next(); String startClass = this.labels.get(start); Integer end = spit.next(); List edges = gadjlist.get(start).get(end); ListIterator eit = edges.listIterator(); while ( eit.hasNext() ){ List cl = new LinkedList(); cl.add((ClassLink)eit.next().getLabel()); paths.add(cl); } start = end; while( spit.hasNext() ){ end = spit.next(); // start-end edges = gadjlist.get(start).get(end); List> tmppaths = new LinkedList>(); // current path ListIterator> pit = paths.listIterator(); while ( pit.hasNext() ){ List basepath = pit.next(); eit = edges.listIterator(); while ( eit.hasNext() ){ ClassLink cl = (ClassLink) eit.next().label; List addedpath = new LinkedList(basepath); addedpath.add(cl); // check if (checkPath(startClass, addedpath)){ tmppaths.add(addedpath); } } } paths = tmppaths; start = end; } return paths; } private void setClassGraph(RDFSchemaAnalyzer rdfsa){ // setNodes SClass[] classes = null; try{ classes = rdfsa.getOWLClasses(null, null, null, true); }catch(Exception e){ System.err.println(e); return; } for (int i = 0 ; i < classes.length; i++){ addNode(classes[i].getClassURI()); nodeType.add("class"); } // setEdges for (int i = 0 ; i < classes.length; i++ ){ try{ ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true); for (int j = 0 ; j < classLinks.length; j++){ Integer n = labelednodes.get(classLinks[j].getLinkedClassURI()); if ( n != null ){ addEdge(i, n, classLinks[j]); }else{ n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI()); if ( n == null ){ addNode(classLinks[j].getLinkedLiteralDatatypeURI()); n = nodeType.size(); nodeType.add("literal"); } addEdge(i, n, classLinks[j]); } } }catch(Exception e){ System.err.println(e); } } } public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){ // set endpoint this.sparqlEndpoint = sparqlEndpoint; visited = new HashSet(); edgeweight = new LinkedList>(); nodeweight = new LinkedList(); // setNodes for all classes SClass[] classes = null; try{ classes = rdfsa.getOWLClasses(null, null, null, true); }catch(Exception e){ System.err.println(e); return; } for (int i = 0 ; i < classes.length; i++){ addNode(classes[i].getClassURI()); nodeType.add("class"); edgeweight.add(new HashMap()); nodeweight.add(classes[i].getNumOfInstances()); } // setEdges Integer snode = labelednodes.get(startClass); Set nodes = new HashSet(); nodes.add(snode); visited.add(snode); for (int i = 0 ; i < nsteps; i++ ){ Iterator nit = nodes.iterator(); Set nextnodes = new HashSet(); while ( nit.hasNext() ){ Integer crr = nit.next(); try{ ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true); for (int j = 0 ; j < classLinks.length; j++){ Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI()); if ( nn == null ){ continue; } if ( !visited.contains(nn) ){ nextnodes.add(nn); } addEdge(crr, nn, classLinks[j]); updateWeight(crr, nn, classLinks[j]); } }catch(Exception e){ e.printStackTrace(); } } nodes = nextnodes; visited.addAll(nodes); } // cut visited Iterator nit = visited.iterator(); while(nit.hasNext()){ Integer node = nit.next(); if ( ! node.equals(snode) ){ List> paths = searchSimplePaths(snode, node); if ( paths.isEmpty()){ nit.remove(); } } } } private ArrayList> createConnectionTable(){ // not used ArrayList> ct = new ArrayList>(); for (int i = 0; i < labels.size(); i++ ){ // each node HashSet cl = createConnectionList(i); ct.add(cl); } return ct; } private HashSet createConnectionList(Integer node){ // not used HashSet cl = new HashSet(); HashSet crrnodes = new HashSet(); crrnodes.add(node); cl.add(node); for ( int i = 0 ; i < nsteps; i++ ){ Set nextnodes = new HashSet(); Iterator cit = crrnodes.iterator(); while ( cit.hasNext() ){ Integer crr = cit.next(); Set nexts = gadjlist.get(crr).keySet(); Iterator nit = nexts.iterator(); while ( nit.hasNext() ){ Integer next = nit.next(); if ( !cl.contains(next) ){ nextnodes.add(next); cl.add(next); } } } crrnodes.clear(); crrnodes.addAll(nextnodes); } return cl; } private void updateWeight(Integer node1, Integer node2, ClassLink edge){ Map weight = edgeweight.get(node1); Integer crr = weight.get(node2); if (crr == null ){ crr = edge.getNumOfLinkedClassInstances(); weight.put(node2, crr); } if ( crr < edge.getNumOfLinkedClassInstances() ){ crr = edge.getNumOfLinkedClassInstances(); weight.put(node2, crr); } weight = edgeweight.get(node2); crr = weight.get(node1); if (crr == null ){ crr = edge.getNumOfOriginClassInstances(); weight.put(node1, crr); } if ( crr < edge.getNumOfOriginClassInstances() ){ crr = edge.getNumOfOriginInstances(); weight.put(node1, crr); } } private boolean checkPath(String startClass, List paths){ // KOKO return true; } // old codes /* public Path[] getPaths_old(RDFSchemaAnalyzer rdfsa, boolean countLink, String startClass, String endClass){ List> paths = null; paths = searchPathsbyVisitingNodes(rdfsa, countLink, startClass, endClass); NavigableSet sortedpath = new TreeSet(); ListIterator> pit = paths.listIterator(); int j = 0; while ( pit.hasNext() ){ Path path = new Path(); path.setStartClass(startClass); List crrpath = pit.next(); path.setClassLinks(crrpath); ListIterator cit = crrpath.listIterator(); int min = Integer.MAX_VALUE; while ( cit.hasNext() ){ ClassLink cl = cit.next(); if ( cl.getNumOfLinks() < min ){ min = cl.getNumOfLinks(); } } path.setWidth(min); sortedpath.add(path); j++; } Path[] patharray = new Path[paths.size()]; Iterator pait = sortedpath.descendingIterator(); int i = 0; while ( pait.hasNext() ){ patharray[i] = pait.next(); i++; } return patharray; } private List> searchPaths_old(String startClass, String endClass){ List> paths = new ArrayList<>(); List> simplePaths = new LinkedList<>(); Integer snode = labelednodes.get(startClass); Integer enode = labelednodes.get(endClass); List> lp = new LinkedList<>(); List ini = new LinkedList(); // initial path ini.add(snode); lp.add(ini); for (int i = 0; i < nsteps; i++ ){ ListIterator> lit = lp.listIterator(); List> nextlp = new LinkedList<>(); while ( lit.hasNext() ){ List crrpath = lit.next(); Integer crrnode = crrpath.get(crrpath.size()-1); Set nexts = gadjlist.get(crrnode).keySet(); Iterator nit = nexts.iterator(); while( nit.hasNext() ){ Integer nextnode = nit.next(); if ( crrpath.contains(nextnode) ){ continue; } List nextpath = new LinkedList(crrpath); // copy nextpath.add(nextnode); if ( nextnode.equals(enode) ){ simplePaths.add(nextpath); continue; } nextlp.add(nextpath); } } lp = nextlp; } ListIterator> pit = simplePaths.listIterator(); while( pit.hasNext()){ List spath = pit.next(); List> convertedPaths = convertSimplePathToPaths(spath); paths.addAll(convertedPaths); } return paths; } */ /* private List> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ List> paths = new ArrayList<>(); List lp = new LinkedList<>(); lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList(), "")); try{ for ( int i = 0; i < nsteps; i++ ){ ListIterator lit = lp.listIterator(); List nextlp = new LinkedList<>(); while ( lit.hasNext() ){ LinkAndPath crrlp = lit.next(); ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); for ( int j = 0 ; j < classLinks.length; j++ ){ List crrpath = new LinkedList<>(crrlp.path); crrpath.add(classLinks[j]); if ( classLinks[j].getLinkedClassURI() == null ){ continue; } if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ paths.add(new LinkedList<>(crrpath)); continue; } if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ continue; } if ( i >= 2 ){ if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && crrlp.classLink.getDirection() != classLinks[j].getDirection() && crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ continue; } } nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); } } lp = nextlp; } }catch(Exception e){ System.err.println(e); } return paths; } */ }