- 更新日時:
- 2016/03/15 18:40:34 (9 年 前)
- パス:
- SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/OWL
- ファイル:
-
- 3 変更
凡例:
- 変更なし
- 追加
- 削除
-
SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/OWL/LabeledMultiDigraph.java
r267 r273 13 13 14 14 public class LabeledMultiDigraph { 15 List<List<LabeledEdge>> adjlist; 15 //List<List<LabeledEdge>> adjlist; 16 List<Map<Integer,List<LabeledEdge>>> labelededges; 16 17 List<String> labels; 17 18 HashMap<String,Integer> labelednodes; 18 List<Map<Integer,List<LabeledEdge>>> gadjlist; // grouped adj list: node, node-edge list19 //List<Map<Integer,List<LabeledEdge>>> gadjlist; // grouped adj list: node, node-edge list 19 20 20 21 public class LabeledEdge{ 21 Integer node;22 boolean direction; 22 23 Object label; 23 24 24 public LabeledEdge( Integer node, Object label){25 this. node = node;25 public LabeledEdge(boolean dir, Object label){ 26 this.direction = dir; 26 27 this.label = label; 27 28 } … … 33 34 34 35 public LabeledMultiDigraph(){ 35 adjlist = new ArrayList<List<LabeledEdge>>(); 36 //adjlist = new ArrayList<List<LabeledEdge>>(); 37 labelededges = new ArrayList<Map<Integer,List<LabeledEdge>>>(); 36 38 labels = new LinkedList<String>(); 37 39 labelednodes = new HashMap<String, Integer>(); 38 gadjlist = new ArrayList<Map<Integer,List<LabeledEdge>>>();40 //gadjlist = new ArrayList<Map<Integer,List<LabeledEdge>>>(); 39 41 } 40 42 … … 42 44 labelednodes.put(label, labels.size()); 43 45 labels.add(label); 44 adjlist.add(new LinkedList<LabeledEdge>()); 45 gadjlist.add(new HashMap<Integer, List<LabeledEdge>>()); 46 //adjlist.add(new LinkedList<LabeledEdge>()); 47 //gadjlist.add(new HashMap<Integer, List<LabeledEdge>>()); 48 labelededges.add(new HashMap<Integer, List<LabeledEdge>>()); 46 49 } 47 50 48 public void addEdge(Integer node1, Integer node2, Object elabel){ 51 public void addEdge(Integer node1, Integer node2, Object elabel){ // node 1 -> node2 49 52 if ( labels.size() < node1 || labels.size() < node2 ){ 50 53 System.err.println("Error for Edge Addition: No Node for the Edge"); 51 54 return; 52 55 } 53 LabeledEdge edge = new LabeledEdge(node2, elabel);54 adjlist.get(node1).add(edge);55 56 56 Map<Integer, List<LabeledEdge>> edges = gadjlist.get(node1); 57 List<LabeledEdge> sedge = edges.get(node2); 58 if ( sedge == null ){ 59 sedge = new LinkedList<LabeledEdge>(); 60 edges.put(node2, sedge); 57 //LabeledEdge edge = new LabeledEdge(node2,elabel); 58 //adjlist.get(node1).add(edge); 59 List<LabeledEdge> edgesf = labelededges.get(node1).get(node2); 60 if ( edgesf == null ){ 61 edgesf = new LinkedList<LabeledEdge>(); 62 labelededges.get(node1).put(node2, edgesf); 61 63 } 62 sedge.add(edge); 64 edgesf.add(new LabeledEdge(true, elabel)); 65 66 List<LabeledEdge> edgesr = labelededges.get(node2).get(node1); 67 if ( edgesr == null ){ 68 edgesr = new LinkedList<LabeledEdge>(); 69 labelededges.get(node2).put(node1, edgesr); 70 } 71 edgesr.add(new LabeledEdge(false, elabel)); 63 72 } 64 73 } -
SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/OWL/OWLClassGraph.java
r271 r273 17 17 List<String> nodeType; 18 18 String sparqlEndpoint; 19 Map<String, Boolean> checkedpaths;20 19 21 /*22 public class LinkAndPath{23 String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL24 ClassLink classLink;25 List<ClassLink> path;26 Set<String> classURIs; // apearing class URIs in the path27 28 29 public LinkAndPath(ClassLink classLink, List<ClassLink> path){30 this.classLink = classLink;31 this.path = path;32 }33 34 public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI){35 this.classLink = classLink;36 this.path = path;37 this.originalClassURI = originalClassURI;38 }39 40 public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI, Set<String> classURIs){41 this.classLink = classLink;42 this.path = path;43 this.originalClassURI = originalClassURI;44 this.classURIs = classURIs;45 }46 }*/47 48 20 public OWLClassGraph(){ // not used 49 21 super(); … … 57 29 } 58 30 59 /* 60 public OWLClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){ // used 31 public OWLClassGraph(RDFSchemaAnalyzerFactory rdfsaf){ 61 32 super(); 62 33 nodeType = new LinkedList<String>(); 63 setPartClassGraph(rdfsa, sparqlEndpoint, startClass);34 //setClassGraph(rdfsaf); 64 35 } 65 */ 66 67 public int getNumberOfEdge(String url){ 68 Integer node = labelednodes.get(url); 69 if (node == null){ 70 return 0; 71 } 72 return adjlist.get(node).size(); 73 } 74 36 75 37 public Path[] getPaths(String startClass, String endClass){ 76 38 List<List<ClassLink>> paths = searchPaths(startClass, endClass); … … 93 55 } 94 56 // using length of path 95 //int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() );96 57 path.setWidth(500000 - crrpath.size()*100000 + min); 97 58 sortedpaths.add(path); … … 110 71 111 72 private List<List<ClassLink>> searchPaths(String startClass, String endClass){ 112 //int asked = 0;113 checkedpaths = new HashMap<String, Boolean>();114 73 List<List<ClassLink>> paths = new ArrayList<>(); 115 74 Integer snode = labelednodes.get(startClass); … … 118 77 119 78 ListIterator<List<Integer>> pit = simplePaths.listIterator(); 120 //System.out.println("SPATH:");121 //System.out.println(simplePaths.size());122 79 while( pit.hasNext()){ 123 80 List<Integer> spath = pit.next(); … … 142 99 List<Integer> crrpath = lit.next(); 143 100 Integer crrnode = crrpath.get(crrpath.size()-1); 144 Set<Integer> nexts = gadjlist.get(crrnode).keySet();101 Set<Integer> nexts = labelededges.get(crrnode).keySet(); 145 102 Iterator<Integer> nit = nexts.iterator(); 146 103 while( nit.hasNext() ){ … … 157 114 } 158 115 lp = nextlp; 159 } 116 } 160 117 return simplePaths; 161 118 } … … 168 125 String startClass = this.labels.get(start); 169 126 Integer end = spit.next(); 170 List<LabeledEdge> edges = gadjlist.get(start).get(end);127 List<LabeledEdge> edges = labelededges.get(start).get(end); 171 128 ListIterator<LabeledEdge> eit = edges.listIterator(); 172 129 while ( eit.hasNext() ){ … … 179 136 end = spit.next(); 180 137 // start-end 181 edges = gadjlist.get(start).get(end);138 edges = labelededges.get(start).get(end); 182 139 List<List<ClassLink>> tmppaths = new LinkedList<List<ClassLink>>(); 183 140 // current path … … 195 152 paths = tmppaths; 196 153 start = end; 197 } 154 } 198 155 return paths; 199 156 } … … 233 190 } 234 191 } 235 } 236 237 /* 238 public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){ 239 // set endpoint 240 this.sparqlEndpoint = sparqlEndpoint; 241 visited = new HashSet<Integer>(); 242 edgeweight = new LinkedList<Map<Integer,Integer>>(); 243 nodeweight = new LinkedList<Integer>(); 244 // setNodes for all classes 245 SClass[] classes = null; 246 try{ 247 classes = rdfsa.getOWLClasses(null, null, null, true); 248 }catch(Exception e){ 249 System.err.println(e); return; 250 } 251 for (int i = 0 ; i < classes.length; i++){ 252 addNode(classes[i].getClassURI()); 253 nodeType.add("class"); 254 edgeweight.add(new HashMap<Integer,Integer>()); 255 nodeweight.add(classes[i].getNumOfInstances()); 256 } 257 // setEdges 258 Integer snode = labelednodes.get(startClass); 259 Set<Integer> nodes = new HashSet<Integer>(); 260 nodes.add(snode); 261 visited.add(snode); 262 for (int i = 0 ; i < nsteps; i++ ){ 263 Iterator<Integer> nit = nodes.iterator(); 264 Set<Integer> nextnodes = new HashSet<Integer>(); 265 while ( nit.hasNext() ){ 266 Integer crr = nit.next(); 267 try{ 268 ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true); 269 for (int j = 0 ; j < classLinks.length; j++){ 270 Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI()); 271 if ( nn == null ){ 272 continue; 273 } 274 if ( !visited.contains(nn) ){ 275 nextnodes.add(nn); 276 } 277 addEdge(crr, nn, classLinks[j]); 278 updateWeight(crr, nn, classLinks[j]); 279 } 280 }catch(Exception e){ 281 e.printStackTrace(); 282 } 283 } 284 nodes = nextnodes; 285 visited.addAll(nodes); 286 } 287 // cut visited 288 Iterator<Integer> nit = visited.iterator(); 289 while(nit.hasNext()){ 290 Integer node = nit.next(); 291 if ( ! node.equals(snode) ){ 292 List<List<Integer>> paths = searchSimplePaths(snode, node); 293 if ( paths.isEmpty()){ 294 nit.remove(); 295 } 296 } 297 } 298 } 299 */ 300 /* 301 private void updateWeight(Integer node1, Integer node2, ClassLink edge){ 302 Map<Integer, Integer> weight = edgeweight.get(node1); 303 Integer crr = weight.get(node2); 304 if (crr == null ){ 305 crr = edge.getNumOfLinkedClassInstances(); 306 weight.put(node2, crr); 307 } 308 if ( crr < edge.getNumOfLinkedClassInstances() ){ 309 crr = edge.getNumOfLinkedClassInstances(); 310 weight.put(node2, crr); 311 } 312 weight = edgeweight.get(node2); 313 crr = weight.get(node1); 314 if (crr == null ){ 315 crr = edge.getNumOfOriginClassInstances(); 316 weight.put(node1, crr); 317 } 318 if ( crr < edge.getNumOfOriginClassInstances() ){ 319 crr = edge.getNumOfOriginInstances(); 320 weight.put(node1, crr); 321 } 322 } 323 324 public List<String> getReachableClasses(){ 325 List<String> clURIs = new LinkedList<String>(); 326 if ( visited == null ){ 327 return null; 328 } 329 Iterator<Integer> vit = visited.iterator(); 330 while( vit.hasNext() ){ 331 Integer vn = vit.next(); 332 clURIs.add(labels.get(vn)); 333 } 334 return clURIs; 335 } 336 */ 192 } 337 193 } -
SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/OWL/QueryPathGenerator.java
r271 r273 17 17 public class QueryPathGenerator { 18 18 private RDFSchemaAnalyzerFactory factory = null; 19 private HashMap<String, RDFSchemaAnalyzer> analyzers= null;19 RDFSchemaAnalyzer analyzer = null; 20 20 private OWLClassGraph graph; 21 21 … … 24 24 private static final String CDIR = "cdata"; 25 25 private static final String ODIR = "owldata"; 26 26 27 27 public QueryPathGenerator(){ 28 28 factory = new RDFSchemaAnalyzerFactory(CDIR); 29 try{ 30 analyzer = factory.create(); 31 }catch(Exception e){ 32 System.err.println(e); 33 } 29 34 } 30 35 31 /* 32 public QueryPathGenerator(String sparqlEndpoint){ 33 factory = new RDFSchemaAnalyzerFactory(CDIR); 34 setSPARQLendpoint(sparqlEndpoint); 35 } 36 */ 37 38 /*public void setOWLClassGraph(String startClass){ 39 graph = new OWLClassGraph(analyzer, sparqlEndpoint, startClass); 40 }*/ 41 42 public SClass[] getClassesByEndpoint(String ep){ 43 RDFSchemaAnalyzer analyzer = analyzers.get(ep); 44 try { 45 return analyzer.getOWLClasses(null, null); 46 }catch(Exception e){ 47 System.err.println(e); 48 return null; 49 } 50 } 51 52 public SClass[] getClasses(String keyword){ 53 return getClassList(keyword).toArray(new SClass[0]); 36 public SClass[] getClasses(){ 37 return getClassList().toArray(new SClass[0]); 54 38 } 55 39 56 public List<SClass> getClassList(String keyword){ 57 Iterator<RDFSchemaAnalyzer> ait = analyzers.values().iterator(); 58 List<SClass> cl = new LinkedList<SClass>(); 40 public List<SClass> getClassList(){ 41 List<SClass> cl = null; 59 42 try { 60 while ( ait.hasNext() ){ 61 RDFSchemaAnalyzer analyzer = ait.next(); 62 cl.addAll(analyzer.getOWLClassList(null, null)); 63 } 43 cl = new LinkedList<SClass>(analyzer.getOWLClassList(null, null)); 64 44 }catch(Exception e){ 65 45 System.err.println(e); … … 79 59 } 80 60 81 /*82 public void setSPARQLendpoint(String sparqlEndpoint){83 this.sparqlEndpoint = sparqlEndpoint;84 setAnalyzer();85 }86 */87 88 61 public RDFSchemaAnalyzerFactory getFactory(){ 89 62 return factory; 90 63 } 91 92 /*93 private void setAnalyzer(){94 try {95 analyzer = factory.create(sparqlEndpoint);96 } catch (Exception e) {97 System.err.println(e);98 }99 }100 */101 64 102 65 public String getClassLabel(String classURI){ … … 210 173 String label = getClassLabel(uri); 211 174 StringBuilder classbuilder = new StringBuilder(label); 212 classbuilder.append(" 175 classbuilder.append("\t"); 213 176 classbuilder.append(classes[i].getNumOfInstances()); 214 classbuilder.append(" 177 classbuilder.append("\t"); 215 178 classbuilder.append(uri); 216 179 sortedClasses.add(classbuilder.toString());