Menu

[r66]: / ls / graph / Controller.java  Maximize  Restore  History

Download this file

333 lines (301 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package ls.graph;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import ls.graph.script.GraphScriptEngine;
import ls.graph.script.ScriptableGraph;
import ls.graph.serialize.DefaultGraphReader;
import ls.graph.serialize.GraphReader;
import ls.graph.ui.MainWindow;
import ls.util.Utils;
import edu.uci.ics.jung.graph.DirectedEdge;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.EdgeWeightLabeller;
import edu.uci.ics.jung.graph.decorators.EdgeWeightLabellerStringer;
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.DirectedSparseGraph;
import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;
import edu.uci.ics.jung.random.generators.GraphGenerator;
import edu.uci.ics.jung.random.generators.SimpleRandomGenerator;
public class Controller
{
// private SwingEngine swingEngine;
// private JSplitPane spltMain;
// private ScriptPanel scriptPanel;
private static Properties initProps = new Properties();
private ScriptableGraph graph = null;
private GraphScriptEngine graphScriptEngine = null;
private List<ControlListener> listeners = new LinkedList<ControlListener>();
static
{
BufferedReader br = null;
try
{
File f = new File("graphscript.ini");
br = new BufferedReader(new FileReader(f));
initProps.load(br);
}
catch (IOException exn)
{
Utils.err("while loading ini file: " + exn.getMessage());
}
finally
{
try { if (br != null) br.close(); }
catch (IOException exn) { Utils.exn(exn); }
}
}
private void start()
{
// graph = new ScriptableGraph(initGraph());
initGraph();
graphScriptEngine = new GraphScriptEngine(this);
String defScript = getDefaultScriptText();
// MainWindow mainWindow = new MainWindow(eng,graph,eng.getScriptUIUtils(),defScript);
MainWindow mainWindow = new MainWindow(this,defScript);
mainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainWindow.setVisible(true);
}
public void addListener(ControlListener l)
{
this.listeners.add(l);
}
private void fireGraphChanged()
{
ScriptableGraph sg = getGraph();
for (ControlListener l : listeners)
l.graphChanged(sg);
}
public void fireScriptLoaded(File f, String code)
{
if (f != null && code != null)
for (ControlListener l : listeners)
{
l.scriptLoaded(f, code);
}
}
public void fireGraphLoaded(File f)
{
if (f != null)
for (ControlListener l : listeners)
l.graphLoaded(f);
}
private String getDefaultScriptText()
{
String defFilename = getProperty("DEFAULT_SCRIPT",null);
if (defFilename != null)
{
File f = new File(defFilename);
if (!f.isAbsolute())
{
String dir = getProperty("SCRIPTS_DIR",null);
if (dir == null) return null;
f = new File(dir + File.separator + defFilename);
}
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try
{
br = new BufferedReader(new FileReader(f));
String line = br.readLine();
while (line != null)
{
sb.append(line).append('\n');
line = br.readLine();
}
}
catch (IOException exn)
{
Utils.exn(exn);
return null;
}
finally
{
try {if (br != null) br.close(); }
catch (IOException exn) { Utils.exn(exn); }
}
return sb.toString();
}
return null;
}
public static void setProperty(String key,String value)
{
initProps.setProperty(key, value);
}
public static String getProperty(String key)
{
String val = initProps.getProperty(key);
return val;
}
public static List<String> getPropertyValues(String key)
{
String sValues = getProperty(key,"");
if ("".equals(sValues) || sValues == null)
return Arrays.asList(new String[] {}); //return empty list
return Arrays.asList(sValues.split(","));
}
public static String getProperty(String key, String defVal)
{
String ret = getProperty(key);
return ret == null ? defVal : ret;
}
public static ImageIcon getIcon(String path)
{
URL imgURL = Controller.class.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL);
}
else
{
Utils.err("Couldn't find file: " + path);
return null;
}
}
/**
* @param args
*/
public static void main(String[] args)
{
Controller m = new Controller();
m.start();
}
public ScriptableGraph getGraph()
{
return graph;
}
public void createNewRandomGraph(int nodeCount, int edgeCount,boolean directed)
throws Exception
{
GraphGenerator gGen = new SimpleRandomGenerator(nodeCount,edgeCount);
Graph g = (Graph)gGen.generateGraph();
ScriptableGraph sg = null;
if (directed)
{
DirectedGraph dg = convertToDirected(g);
sg = new ScriptableGraph(dg);
}
else
sg = new ScriptableGraph(g);
setNewGraph(sg);
}
private DirectedGraph convertToDirected(Graph g)
{
if (g == null) return null;
DirectedGraph dg = new DirectedSparseGraph();
Map<Vertex,DirectedSparseVertex> vMap = new HashMap<Vertex,DirectedSparseVertex>();
for (Vertex v : ((Set<Vertex>)g.getVertices()))
{
DirectedSparseVertex dv = new DirectedSparseVertex();
dv.importUserData(v);
vMap.put(v, dv);
dg.addVertex(dv);
}
for (Edge e : ((Set<Edge>)g.getEdges()))
{
Vertex v = (Vertex)(e.getEndpoints().getFirst());
DirectedSparseVertex dv1 = vMap.get(v);
v = (Vertex)(e.getEndpoints().getSecond());
DirectedSparseVertex dv2 = vMap.get(v);
if (dv1 == null || dv2 == null)
{
Utils.err("While converting to a directed graph - one of the vertices is missing");
continue;
}
else
{
DirectedEdge de = new DirectedSparseEdge(dv1,dv2);
dg.addEdge(de);
}
}
return dg;
}
public void setNewGraph(ScriptableGraph g)
{
if (g != null)
{
graph = g;
String weighted = getProperty("WEIGHTED","FALSE");
boolean isWeighted = "TRUE".equalsIgnoreCase(weighted);
if (isWeighted)
{
String sDefWeight = getProperty("DEF_EDGE_WEIGHT","1");
int defWeight = Integer.parseInt(sDefWeight);
EdgeWeightLabeller ewl = graph.getEdgeWeightLabeller();
for (Edge e : ((Set<Edge>)graph.getEdges()))
{
getGraph().setEdgeWeight(e, defWeight);
}
}
fireGraphChanged();
}
}
public void setGraphFromText(String gt)
{
BufferedReader br = new BufferedReader(new StringReader(gt));
GraphReader gr = new DefaultGraphReader();
try
{
ScriptableGraph sg = gr.read(br);
setNewGraph(sg);
}
catch (IOException exn)
{
Utils.exn(exn);
}
}
public static void msg(String m)
{
Utils.msg("SYSTEM: " + m);
}
private Graph initGraph()
{
int nc,ec;
try
{ nc = Integer.parseInt(getProperty("DEF_GRAPH_NODE_COUNT","5")); }
catch (NumberFormatException exn) {Utils.exn(exn); nc = 5;}
try
{ ec = Integer.parseInt(getProperty("DEF_GRAPH_EDGE_COUNT","8")); }
catch (NumberFormatException exn) {Utils.exn(exn); ec = 8;}
try
{
createNewRandomGraph(nc,ec,false);
}
catch (Exception exn)
{
Utils.exn(exn);
}
return graph;
}
// private void setGraph(Graph g)
// {
// if (!(g instanceof ScriptableGraph))
// this.graph = new ScriptableGraph(g);
// else
// this.graph = (ScriptableGraph)g;
// fireGraphChanged();
// }
public GraphScriptEngine getGraphScriptEngine()
{
return graphScriptEngine;
}
public void setGraphScriptEngine(GraphScriptEngine graphScriptEngine)
{
this.graphScriptEngine = graphScriptEngine;
}
}
MongoDB Logo MongoDB