1 18 package org.objectweb.perseus.dependency.lib; 19 20 import org.objectweb.fractal.api.control.BindingController; 21 import org.objectweb.perseus.dependency.api.DependencyGraph; 22 import org.objectweb.util.monolog.api.Logger; 23 24 import java.util.*; 25 26 27 31 public final class BasicDependencyGraph 32 implements DependencyGraph, BindingController{ 33 34 public Map successors; 36 37 private List toBeVisited; 39 40 private List visited; 42 43 protected Logger logger = null; 44 45 public BasicDependencyGraph () { 46 successors = new HashMap(); 47 toBeVisited = new ArrayList(); 48 visited = new ArrayList(); 49 } 50 51 54 public String [] listFc() { 55 return new String [] {}; 56 } 57 58 public Object lookupFc(String s) { 59 return null; 60 } 61 62 public void bindFc(String s, Object o) { 63 if ("logger".equals(s)) { 64 logger = (Logger) o; 65 } 66 } 67 68 public void unbindFc(String s) { 69 } 70 71 72 75 79 public synchronized boolean addVertex (Object src, Object dst) { 80 Set s = (Set)successors.get(src); 81 if (s == null) { 82 s = new HashSet(); 83 successors.put(src, s); 84 } 85 if (src.equals(dst) || s.contains(dst)) { 86 return true; 87 } 88 89 toBeVisited.clear(); 90 visited.clear(); 91 92 toBeVisited.add(dst); 94 while (!toBeVisited.isEmpty()) { 95 Object node = toBeVisited.remove(toBeVisited.size() - 1); 96 if (node.equals(src)) { 97 return false; 100 } 101 visited.add(node); 102 103 Set t = (Set)successors.get(node); 105 if (t != null) { 106 Iterator i = t.iterator(); 107 while (i.hasNext()) { 108 Object successor = i.next(); 109 if (!visited.contains(successor)) { 110 toBeVisited.add(successor); 111 } 112 } 113 } 114 } 115 116 s.add(dst); 117 return true; 118 } 119 120 123 public synchronized void removeVertex (Object src, Object dst) { 124 Set s = (Set)successors.get(src); 125 if (s != null) { 126 s.remove(dst); 127 if (s.isEmpty()) { 128 successors.remove(src); 129 } 130 } 131 } 132 133 void print(Object src, Object dst) { 134 StringBuffer sb = new StringBuffer (); 135 sb.append("src="); 136 sb.append(src); 137 sb.append("\ndst="); 138 sb.append(dst); 139 sb.append("\n"); 140 sb.append("dependency Graph: "); 141 if (successors.size() > 0) { 142 List waiters = new ArrayList(successors.keySet()); 143 Set s = new HashSet(waiters.size() * 2); 144 s.addAll(successors.keySet()); 145 s.addAll(successors.values()); 146 for (int i = 0; i < waiters.size(); i++) { 147 sb.append("\n"); 148 sb.append(waiters.get(i)); 149 sb.append(" = > "); 150 sb.append(((Set)successors.get(waiters.get(i))).size()); 151 sb.append(": "); 152 sb.append(successors.get(waiters.get(i))); 153 } 154 } 155 System.out.println(sb.toString()); 156 } 157 158 } 159 | Popular Tags |