-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkIO.java
439 lines (438 loc) · 17.5 KB
/
NetworkIO.java
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.*;
/*******************************************************************************
*
* AUTHOR: Jonathan Wright
* PURPOSE: This is a class the deals with the reading and writing of networks.
* DATE: 26/10/2019
*
******************************************************************************/
public class NetworkIO
{
private static final char[] FUNCTIONS = {'F', 'P', 'A', 'U', 'R'};
/* F = Follow, P = Post, A = Add, U = Unfollow, R = Remove */
/**************************************************************************
* Purpose: Selects format to read in network
* IMPORT: Network network, int format
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void formatSelection(Network network, int format)
{
switch (format)
{
case 1:
readGraph(network);
break;
case 2:
try
{
readSerializedGraph(network);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
break;
}
}
/**************************************************************************
* Purpose: reads a serialzed graph to network.
* IMPORT: Network network
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void readSerializedGraph(Network network)
{
FileInputStream in;
ObjectInputStream objIn;
Scanner sc = new Scanner(System.in);
System.out.println("Enter filename for network to load in.");
String filename = sc.nextLine();
try
{
in = new FileInputStream(filename);
objIn = new ObjectInputStream(in);
DSAGraph inGraph = (DSAGraph)objIn.readObject();
network.redirectGraph(inGraph); /* Dereference Old Graph, Use New Graph. */
System.out.println("Network Successfully Loaded.");
}
catch (ClassNotFoundException e)
{
System.out.println("Class not found! " + e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e) /* Lecture Notes Show To Catch base exception. */
{
throw new IllegalArgumentException("Unable to load object!");
}
}
/**************************************************************************
* Purpose: reads a graph from csv to network.
* IMPORT: Network network
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void readGraph(Network network)
{
network.resetGraph();
String filename;
Scanner sc = new Scanner(System.in);
DSAGraph inGraph = network.getGraph();
System.out.println("Enter filename for network to load in.");
filename = sc.nextLine();
if (filename.length() >= 4 && !(filename.substring(filename.length() - 4, filename.length()).equals(".csv")) )
{
filename += ".csv";
}
System.out.printf("Attempting to read %s...%n", filename);
try
{
FileInputStream file = new FileInputStream(filename);
InputStreamReader inputStream = new InputStreamReader(file);
BufferedReader buffReader = new BufferedReader(inputStream);
String currLine = buffReader.readLine();
while (currLine != null)
{
String[] edge = currLine.split(":");
if (edge.length == 1)
{ /* Wants To Insert Vertex */
try
{
/* Characters To CamelCase as its more appealing to human eye. */
currLine = Utilities.camelCase(currLine);
if (Character.isWhitespace(currLine.charAt(0)))
{
currLine = currLine.substring(1, currLine.length());
currLine = Utilities.camelCase(currLine);
}
inGraph.addVertex(currLine, currLine); /*Add User To network*/
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
else
{ /* Wants To Insert Edge */
try
{
edge[0] = Utilities.camelCase(edge[0]);
edge[1] = Utilities.camelCase(edge[1]);
if (Character.isWhitespace(edge[0].charAt(0)))
{
edge[0] = edge[0].substring(1, edge[0].length());
edge[0] = Utilities.camelCase(edge[0]);
}
if (Character.isWhitespace(edge[1].charAt(0)))
{
edge[1] = edge[1].substring(1, edge[1].length());
edge[1] = Utilities.camelCase(edge[1]);
}
inGraph.addEdge(edge[1], edge[0], edge[1] + ":" + edge[0], 0);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage() + " on line: " + currLine);
}
}
currLine = buffReader.readLine();
}
buffReader.close();
System.out.println("Successfully Loaded Network.");
}
catch (IOException e)
{
System.out.println("ERROR: " + e.getMessage());
inGraph = null; /* Return Null If Error Occurs. */
}
}
/**************************************************************************
* Purpose: reads graph from csv with filename being given.
* IMPORT: Network network, String filename
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void readGraph(Network network, String filename) throws IOException
{
network.resetGraph();
DSAGraph inGraph = network.getGraph();
if ( !(filename.substring(filename.length() - 4, filename.length()).equals(".csv")) )
{
filename += ".csv";
}
System.out.printf("Attempting to read %s...%n", filename);
try
{
FileInputStream file = new FileInputStream(filename);
InputStreamReader inputStream = new InputStreamReader(file);
BufferedReader buffReader = new BufferedReader(inputStream);
String currLine = buffReader.readLine();
while (currLine != null)
{
String[] edge = currLine.split(":");
if (edge.length == 1)
{ /* Wants To Insert Vertex */
try
{
/* Characters To CamelCase as its more appealing to human eye. */
currLine = Utilities.camelCase(currLine);
if (Character.isWhitespace(currLine.charAt(0)))
{
currLine = currLine.substring(1, currLine.length());
currLine = Utilities.camelCase(currLine);
}
inGraph.addVertex(currLine, currLine); /*Add User To network*/
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
else
{ /* Wants To Insert Edge */
try
{
edge[0] = Utilities.camelCase(edge[0]);
edge[1] = Utilities.camelCase(edge[1]);
if (Character.isWhitespace(edge[0].charAt(0)))
{
edge[0] = edge[0].substring(1, edge[0].length());
edge[0] = Utilities.camelCase(edge[0]);
}
if (Character.isWhitespace(edge[1].charAt(0)))
{
edge[1] = edge[1].substring(1, edge[1].length());
edge[1] = Utilities.camelCase(edge[1]);
}
inGraph.addEdge(edge[1], edge[0], edge[1] + ":" + edge[0], 0);
}
catch (IllegalArgumentException e)
{
//System.out.println(e.getMessage() + " on line: " + currLine);
throw new IllegalArgumentException(e.getMessage() + " on line: " + currLine);
}
}
currLine = buffReader.readLine();
}
buffReader.close();
System.out.println("Successfully Loaded Network.");
}
catch (IOException e)
{
//System.out.println("ERROR: " + e.getMessage());
throw e;
}
}
/**************************************************************************
* Purpose: picks whether to save as CSV or Serial based off format.
* IMPORT: Network network, int format, String filename
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void saveGraph(Network network, int format, String filename)
{ // WRAPPER METHOD
switch (format)
{
case 1:
saveCSV(network, filename);
break;
case 2:
saveSerial(network, filename);
break;
}
}
/**************************************************************************
* Purpose: saves graph to csv
* IMPORT: Network network, String filename
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
private static void saveCSV(Network network, String filename)
{
FileOutputStream out;
PrintWriter pw;
try
{
out = new FileOutputStream(filename + ".csv");
pw = new PrintWriter(out);
pw.println(network.getVerticiesList());
pw.println(network.getEdgeList());
pw.close();
out.close();
System.out.println("Successfully saved to " + filename + ".csv");
}
catch(IOException e)
{
System.out.println("ERROR: " + e.getMessage());
}
}
/**************************************************************************
* Purpose: serializes graph
* IMPORT: Network network, String filename
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
private static void saveSerial(Network network, String filename)
{
FileOutputStream out;
ObjectOutputStream oOut;
try
{
out = new FileOutputStream(filename);
oOut = new ObjectOutputStream(out);
oOut.writeObject(network.getGraph());
oOut.close();
}
catch(Exception e) /* Lecture Slides show to catch base exception. */
{
System.out.println("ERROR: " + e.getMessage() + " is not serializable.");
}
}
/**************************************************************************
* Purpose: Appends/Creates file to write timeStep into.
* IMPORT: String filename, String inData
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void timeStepAppend(String filename, String inData)
{
FileOutputStream out;
PrintWriter pw;
try
{
File test = new File(System.getProperty("user.dir") + "/timeSteps/" + filename);
if (!test.exists())
{
test.createNewFile();
}
out = new FileOutputStream(System.getProperty("user.dir") + "/timeSteps/" + filename, true); /* Append = true */
pw = new PrintWriter(out);
pw.println(inData);
pw.close();
out.close();
}
catch(IOException e)
{
System.out.println("ERROR APPENDING TO TIMESTEP: " + e.getMessage());
}
}
/**************************************************************************
* Purpose: Reads eventFile
* IMPORT: Network network, String filename
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public static void readEventFile(Network network, String filename) throws IOException, IllegalArgumentException
{
FileInputStream in;
InputStreamReader inR;
BufferedReader buffR;
try
{
in = new FileInputStream(filename);
inR = new InputStreamReader(in);
buffR = new BufferedReader(inR);
String currLine = buffR.readLine();
while (currLine != null)
{
String[] lineSplit = currLine.split(":");
if (lineSplit[0].length() != 1)
{
throw new IllegalArgumentException("That is not a valid operation in the event file. Line: " + currLine);
}
if (!isFunction(lineSplit[0].charAt(0)))
{
throw new IllegalArgumentException("That is not a valid operation in the event file. Line: " + currLine);
}
performFunction(lineSplit, network);
currLine = buffR.readLine();
}
}
catch (IOException e)
{
throw e;
}
catch (IllegalArgumentException e)
{
throw e;
}
}
/**************************************************************************
* Purpose: determines if event file has stated a valid function.
* IMPORT: char inFunction
* EXPORT: boolean valid
* DATE: 27/10/2019
**************************************************************************/
private static boolean isFunction(char inFunction)
{
boolean valid = false;
for (char currF: FUNCTIONS)
{
if (inFunction == currF)
{
valid = true;
}
}
return valid;
}
/**************************************************************************
* Purpose: performs chosen function with input data.
* IMPORT: String[] lineSplit, Network network
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
private static void performFunction(String[] lineSplit, Network network) throws IllegalArgumentException
{
switch (lineSplit[0].charAt(0))
{
case 'F': /* FOLLOW */
if (lineSplit.length != 3)
{
throw new IllegalArgumentException("Expected F:User1:User2, recieved " + lineSplit[0] + ":" + lineSplit[1] + ":" + lineSplit[2]);
}
network.follow(Utilities.camelCase(lineSplit[2]), Utilities.camelCase(lineSplit[1])); /* Assumption is made its the same as the network input file! e.g. TO:FROM */
break;
case 'P': /* POST */
if (lineSplit.length != 3 && lineSplit.length != 4)
{
throw new IllegalArgumentException("Expected P:User1:Post Contents:ClickBaitFactor, recieved " + lineSplit[0] + ":" + lineSplit[1] + ":" + lineSplit[2]);
}
if (lineSplit.length == 4 && lineSplit[3] != null)
{
network.newMessageC(Utilities.camelCase(lineSplit[1]), Utilities.camelCase(lineSplit[2]), Double.parseDouble(lineSplit[3]));
}
else
{
network.newMessage(Utilities.camelCase(lineSplit[1]), Utilities.camelCase(lineSplit[2]));
}
break;
case 'A': /* ADD */
if (lineSplit.length != 2)
{
throw new IllegalArgumentException("Expected A:User, recieved " + lineSplit[0] + ":" + lineSplit[1] + " Unknown additional Parameter.");
}
network.addUser(Utilities.camelCase(lineSplit[1]));
break;
case 'U': /* UNFOLLOW */
if (lineSplit.length != 3)
{
throw new IllegalArgumentException("Expected U:User1:User2, recieved " + lineSplit[0] + ":" + lineSplit[1] + ":" + lineSplit[2]);
}
network.unfollow(Utilities.camelCase(lineSplit[2]),Utilities.camelCase(lineSplit[1]));
break;
case 'R': /* REMOVE */
if (lineSplit.length != 2)
{
throw new IllegalArgumentException("Expected R:User, recieved " + lineSplit[0] + ":" + lineSplit[1] + " Unknown additional Parameter.");
}
network.removeUser(Utilities.camelCase(lineSplit[1]));
break;
}
}
}