1 19 20 package org.netbeans.modules.languages.parser; 21 22 import java.util.*; 23 24 28 public class DGUtils { 29 30 31 public static <N,E,K,V> DG<N,E,K,V> cloneDG (DG<N,E,K,V> dg, boolean cloneProperties, NodeFactory<N> nodeFactory) { 32 DG<N,E,K,V> ndg = DG.<N,E,K,V>createDG (); 33 Map<N,N> oldToNew = new HashMap<N,N> (); 34 Iterator<N> it = dg.getNodes ().iterator (); 35 while (it.hasNext ()) { 36 N oldNode = it.next (); 37 N newNode = oldToNew.get (oldNode); 38 if (newNode == null) { 39 newNode = nodeFactory.createNode (); 40 ndg.addNode (newNode); 41 oldToNew.put (oldNode, newNode); 42 if (cloneProperties) 43 ndg.putAllProperties (newNode, dg.getProperties (oldNode)); 44 } 45 Iterator<E> it2 = dg.getEdges (oldNode).iterator (); 46 while (it2.hasNext ()) { 47 E edge = it2.next (); 48 N oldEnd = dg.getNode (oldNode, edge); 49 N newEnd = oldToNew.get (oldEnd); 50 if (newEnd == null) { 51 newEnd = nodeFactory.createNode (); 52 ndg.addNode (newEnd); 53 oldToNew.put (oldEnd, newEnd); 54 if (cloneProperties) 55 ndg.putAllProperties (newEnd, dg.getProperties (oldEnd)); 56 } 57 ndg.addEdge (newNode, newEnd, edge); 58 if (cloneProperties) 59 ndg.putAllProperties (newNode, edge, dg.getProperties (oldNode, edge)); 60 } 61 if (dg.getEnds ().contains (oldNode)) 62 ndg.addEnd (newNode); 63 } 64 N newStart = oldToNew.get (dg.getStartNode ()); 65 ndg.setStart (newStart); 66 return ndg; 67 } 68 69 70 public static <N,E,K,V> DG<N,E,K,V> append (DG<N,E,K,V> dg1, DG<N,E,K,V> dg2, E star, NodeFactory<N> nodeFactory) { 71 DG<N,E,K,V> ndg = DG.<N,E,K,V>createDG (); 72 Set<N> newStart = new HashSet<N> (); 73 newStart.add (dg1.getStartNode ()); 74 if (dg1.getEnds ().contains (dg1.getStartNode ())) 75 newStart.add (dg2.getStartNode ()); 76 Map<Set<N>,N> newToOld = new HashMap<Set<N>,N> (); 77 merge (dg1, dg2, newStart, ndg, newToOld, dg1.getEnds (), dg2.getStartNode (), false, true, star, nodeFactory); 78 N nnn = newToOld.get (newStart); 79 ndg.setStart (nnn); 80 return ndg; 81 } 82 83 public static <N,E,K,V> DG<N,E,K,V> plus (DG<N,E,K,V> dg, E star, NodeFactory<N> nodeFactory) { 84 DG<N,E,K,V> ndg = DG.<N,E,K,V>createDG (); 85 N what = dg.getStartNode (); 86 Set<N> where = dg.getEnds (); 87 Set<N> nn = new HashSet<N> (); 88 nn.add (dg.getStartNode ()); 89 if (where.contains (dg.getStartNode ())) 90 nn.add (what); 91 Map<Set<N>,N> newToOld = new HashMap<Set<N>,N> (); 92 merge (dg, dg, nn, ndg, newToOld, where, what, true, true, star, nodeFactory); 93 N nnn = newToOld.get (nn); 94 ndg.setStart (nnn); 95 return ndg; 96 } 97 98 private static <N,E,K,V> void merge ( 99 DG<N,E,K,V> dg1, 100 DG<N,E,K,V> dg2, 101 Set<N> nn, 102 DG<N,E,K,V> ndg, 103 Map<Set<N>,N> newToOld, 104 Set<N> where, 105 N what, 106 boolean setEnds1, 107 boolean setEnds2, 108 E star, 109 NodeFactory<N> nodeFactory 110 ) { 111 N nnn = newToOld.get (nn); 112 if (nnn != null) return; 113 nnn = nodeFactory.createNode (); 114 newToOld.put (nn, nnn); 115 ndg.addNode (nnn); 116 117 Map<E,Set<N>> edges = new HashMap<E,Set<N>> (); 118 Map<E,Map<K,V>> properties = new HashMap<E,Map<K,V>> (); 119 Iterator<N> it = nn.iterator (); 120 while (it.hasNext ()) { 121 N n = it.next (); 122 DG<N,E,K,V> cdg = dg1.containsNode (n) ? dg1 : dg2; 123 ndg.putAllProperties (nnn, cdg.getProperties (n)); 124 if (setEnds1 && dg1.getEnds ().contains (n)) 125 ndg.addEnd (nnn); 126 if (setEnds2 && dg2.getEnds ().contains (n)) 127 ndg.addEnd (nnn); 128 Iterator<E> it2 = cdg.getEdges (n).iterator (); 129 while (it2.hasNext ()) { 130 E edge = it2.next (); 131 Set<N> ends = edges.get (edge); 132 Map<K,V> props = properties.get (edge); 133 if (ends == null) { 134 ends = new HashSet<N> (); 135 props = new HashMap<K,V> (); 136 edges.put (edge, ends); 137 properties.put (edge, props); 138 } 139 N en = cdg.getNode (n, edge); 140 ends.add (en); 141 props.putAll (cdg.getProperties (n, edge)); 142 if (where.contains (en)) 143 ends.add (what); 144 } 145 } 146 it = nn.iterator (); 147 while (it.hasNext ()) { 148 N n = it.next (); 149 DG<N,E,K,V> cdg = dg1.containsNode (n) ? dg1 : dg2; 150 N en = cdg.getNode (n, star); 151 if (en == null) continue; 152 Iterator<E> it2 = edges.keySet ().iterator (); 153 while (it2.hasNext ()) { 154 E e = it2.next (); 155 if (cdg.getNode (n, e) != null) continue; 156 edges.get (e).add (en); 157 properties.get (e).putAll (cdg.getProperties (n, e)); 158 if (where.contains (en)) 159 edges.get (e).add (what); 160 } 161 } 162 163 Iterator<E> it2 = edges.keySet ().iterator (); 164 while (it2.hasNext ()) { 165 E edge = it2.next (); 166 Set<N> en = edges.get (edge); 167 merge (dg1, dg2, en, ndg, newToOld, where, what, setEnds1, setEnds2, star, nodeFactory); 168 N enn = newToOld.get (en); 169 ndg.addEdge (nnn, enn, edge); 170 ndg.putAllProperties (nnn, edge, properties.get (edge)); 171 } 172 } 173 174 public static <N,E,K,V> DG<N,E,K,V> merge (DG<N,E,K,V> dg1, DG<N,E,K,V> dg2, E star, NodeFactory<N> nodeFactory) { 175 DG<N,E,K,V> ndg = DG.<N,E,K,V>createDG (); 176 Map<Set<N>,N> newToOld = new HashMap<Set<N>,N> (); 177 N startNode = merge ( 178 dg1, dg2, 179 dg1.getStartNode (), 180 dg2.getStartNode (), 181 ndg, 182 true, true, 183 star, 184 nodeFactory, 185 newToOld, 186 1 187 ); 188 ndg.setStart (startNode); 189 return ndg; 190 } 191 192 private static <N,E,K,V> N merge ( 193 DG<N,E,K,V> dg1, 194 DG<N,E,K,V> dg2, 195 N n1, 196 N n2, 197 DG<N,E,K,V> ndg, 198 boolean addEnds1, 199 boolean addEnds2, 200 E star, 201 NodeFactory<N> nodeFactory, 202 Map<Set<N>,N> newToOld, 203 int depth 204 ) { 205 if (depth == 100) 206 System.out.println("sto"); 207 Set<N> nNode = new HashSet<N> (); 208 nNode.add (n1); 209 nNode.add (n2); 210 if (newToOld.containsKey (nNode)) 211 return newToOld.get (nNode); 212 N dnode = nodeFactory.createNode (); 213 newToOld.put (nNode, dnode); 214 ndg.addNode (dnode); 215 ndg.putAllProperties (dnode, dg1.getProperties (n1)); 216 ndg.putAllProperties (dnode, dg2.getProperties (n2)); 217 if (addEnds1 && dg1.getEnds ().contains (n1)) 218 ndg.addEnd (dnode); 219 if (addEnds2 && dg2.getEnds ().contains (n2)) 220 ndg.addEnd (dnode); 221 222 Set<E> edges2 = new HashSet<E> (dg2.getEdges (n2)); 223 Iterator<E> it = dg1.getEdges (n1).iterator (); 224 while (it.hasNext ()) { 225 E edge = it.next (); 226 N nn1 = dg1.getNode (n1, edge); 227 N nn2 = dg2.getNode (n2, edge); 228 Map<K,V> properties = null; 229 if ( !edge.equals (star) && 230 edges2.contains (star) && 231 nn2 == null 232 ) { 233 nn2 = dg2.getNode (n2, star); 234 properties = dg2.getProperties (n2, star); 235 } else 236 if (nn2 != null) 237 properties = dg2.getProperties (n2, edge); 238 N nnode = nn2 == null ? 239 merge (dg1, nn1, ndg, addEnds1) : 240 merge (dg1, dg2, nn1, nn2, ndg, addEnds1, addEnds2, star, nodeFactory, newToOld, depth + 1); 241 ndg.addEdge (dnode, nnode, edge); 242 ndg.putAllProperties (dnode, edge, dg1.getProperties (n1, edge)); 243 if (properties != null) 244 ndg.putAllProperties (dnode, edge, properties); 245 edges2.remove (edge); 246 } 247 it = edges2.iterator (); 248 while (it.hasNext ()) { 249 E edge = it.next (); 250 N nn2 = dg2.getNode (n2, edge); 251 N nnode = null; 252 Map<K,V> properties = null; 253 if ( !edge.equals (star) && 254 dg1.getEdges (n1).contains (star) 255 ) { 256 nnode = merge (dg1, dg2, dg1.getNode (n1, star), nn2, ndg, addEnds1, addEnds2, star, nodeFactory, newToOld, depth + 1); 257 properties = dg1.getProperties (n1, star); 258 } else 259 nnode = merge (dg2, nn2, ndg, addEnds2); 260 ndg.addEdge (dnode, nnode, edge); 261 ndg.putAllProperties (dnode, edge, dg2.getProperties (n2, edge)); 262 if (properties != null) 263 ndg.putAllProperties (dnode, edge, properties); 264 } 265 return dnode; 266 } 267 268 private static <N,E,K,V> N merge ( 269 DG<N,E,K,V> dg, 270 N n, 271 DG<N,E,K,V> ndg, 272 boolean addEnds 273 ) { 274 if (ndg.containsNode (n)) return n; 275 ndg.addNode (n); 276 ndg.putAllProperties (n, dg.getProperties (n)); 277 if (addEnds && dg.getEnds ().contains (n)) 278 ndg.addEnd (n); 279 280 Iterator<E> it = dg.getEdges (n).iterator (); 281 while (it.hasNext ()) { 282 E edge = it.next (); 283 N nn = dg.getNode (n, edge); 284 N endN = merge (dg, nn, ndg, addEnds); 285 ndg.addEdge (n, endN, edge); 286 ndg.putAllProperties (n, edge, dg.getProperties (n, edge)); 287 } 288 return n; 289 } 290 291 static <N,E,K,V> DG<N,E,K,V> reduce (DG<N,E,K,V> dg, NodeFactory<N> nodeFactory) { 292 Map<Map<K,V>,Set<N>> ends = new HashMap<Map<K,V>,Set<N>> (); 293 Set<N> other = new HashSet<N> (); 294 Iterator<N> it = dg.getNodes ().iterator (); 295 while (it.hasNext ()) { 296 N node = it.next (); 297 if (!dg.getEnds ().contains (node)) 298 other.add (node); 299 else { 300 Set<N> e = ends.get (dg.getProperties (node)); 301 if (e == null) { 302 e = new HashSet<N> (); 303 ends.put (dg.getProperties (node), e); 304 } 305 e.add (node); 306 } 307 } 308 Set<Set<N>> newNodes = new HashSet<Set<N>> (); 309 if (other.size () > 0) newNodes.add (other); 310 newNodes.addAll (ends.values ()); 311 Map<Set<N>,Map<E,Set<N>>> ng = reduce (dg, newNodes); 312 313 DG<N,E,K,V> ndg = DG.<N,E,K,V>createDG (); 314 Map<Set<N>,N> oldToNewNode = new HashMap<Set<N>,N> (); 315 Iterator<Set<N>> it2 = ng.keySet ().iterator (); 316 while (it2.hasNext ()) { 317 Set<N> node = it2.next (); 318 N newNode = oldToNewNode.get (node); 319 if (newNode == null) { 320 newNode = nodeFactory.createNode (); 321 oldToNewNode.put (node, newNode); 322 ndg.addNode (newNode); 323 } 324 Map<E,Set<N>> edgeToNode = ng.get (node); 325 Iterator<E> it3 = edgeToNode.keySet ().iterator (); 326 while (it3.hasNext ()) { 327 E edge = it3.next (); 328 Set<N> end = edgeToNode.get (edge); 329 N newNode2 = oldToNewNode.get (end); 330 if (newNode2 == null) { 331 newNode2 = nodeFactory.createNode (); 332 oldToNewNode.put (end, newNode2); 333 ndg.addNode (newNode2); 334 } 335 ndg.addEdge (newNode, newNode2, edge); 336 } 337 } 338 ndg.setEnds (new HashSet<N> ()); 339 it2 = ng.keySet ().iterator (); 340 while (it2.hasNext ()) { 341 Set<N> node = it2.next (); 342 N newNode = oldToNewNode.get (node); 343 Iterator<N> it3 = node.iterator (); 344 while (it3.hasNext ()) { 345 N n = it3.next (); 346 if (dg.containsNode (n) && dg.getProperties (n) != null) 347 ndg.putAllProperties (newNode, dg.getProperties (n)); 348 Iterator<E> it4 = ndg.getEdges (newNode).iterator (); 349 while (it4.hasNext ()) { 350 E edge = it4.next (); 351 if (dg.containsNode (n) && dg.getProperties (n, edge) != null) 352 ndg.putAllProperties (newNode, edge, dg.getProperties (n, edge)); 353 } 354 if (dg.getEnds ().contains (n)) 355 ndg.addEnd (newNode); 356 if (dg.getStartNode ().equals (n)) 357 ndg.setStart (newNode); 358 } 359 } 360 return ndg; 361 } 362 363 private static <N,E,K,V> Map<Set<N>,Map<E,Set<N>>> reduce (DG<N,E,K,V> dg, Set<Set<N>> s) { 364 Map<N,Set<N>> m = new HashMap<N,Set<N>> (); 365 Iterator<Set<N>> it = s.iterator (); 366 while (it.hasNext ()) { 367 Set<N> nnode = it.next (); 368 Iterator<N> it2 = nnode.iterator (); 369 while (it2.hasNext ()) { 370 N node = it2.next (); 371 m.put (node, nnode); 372 } 373 } 374 375 Map<Set<N>,Map<E,Set<N>>> nnodes = new HashMap<Set<N>,Map<E,Set<N>>> (); 376 it = s.iterator (); 377 while (it.hasNext ()) { 378 Set<N> nnode = it.next (); 379 Map<Map<E,Set<N>>,Set<N>> nodes = new HashMap<Map<E,Set<N>>,Set<N>> (); 380 Iterator<N> it2 = nnode.iterator (); 381 while (it2.hasNext ()) { 382 N node = it2.next (); 383 Map<E,Set<N>> edges = new HashMap<E,Set<N>> (); 384 Iterator<E> it3 = dg.getEdges (node).iterator (); 385 while (it3.hasNext ()) { 386 E edge = it3.next (); 387 N endNode = dg.getNode (node, edge); 388 edges.put (edge, m.get (endNode)); 389 } 390 Set<N> n = nodes.get (edges); 391 if (n == null) { 392 n = new HashSet<N> (); 393 nodes.put (edges, n); 394 } 395 n.add (node); 396 } 397 Iterator<Map<E,Set<N>>> it3 = nodes.keySet ().iterator (); 398 while (it3.hasNext ()) { 399 Map<E,Set<N>> edges = it3.next (); 400 Set<N> newState = nodes.get (edges); 401 nnodes.put (newState, edges); 402 } 403 } 404 if (nnodes.size () > s.size ()) 405 return reduce (dg, nnodes.keySet ()); 406 return nnodes; 407 } 408 409 470 597 735 } 899 | Popular Tags |