-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.java
367 lines (367 loc) · 13.2 KB
/
Network.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
import java.io.Serializable;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.Math;
/*******************************************************************************
*
* AUTHOR: Jonathan Wright
* PURPOSE: This is a class that is the social media network, it contains a graph
* and extra details retaining to the network such as followChance and likeChance.
* DATE: 26/10/2019
*
******************************************************************************/
public class Network implements Serializable
{
private DSAGraph graph;
private double followChance;
private double likeChance;
/**************************************************************************
* Purpose: Alternate Constructor
* IMPORT: DSAGraph inGraph
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public Network(DSAGraph inGraph) /* Network Will Start Without Chances. */
{
graph = inGraph;
followChance = 0; /* Needs To Be Set Via Options */
likeChance = 0; /* Needs To Be Set Via Options */
}
/**************************************************************************
* Purpose: Default Constructor
* IMPORT: none
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public Network() /* Network Will Start Without Chances. */
{
graph = new DSAGraph();
followChance = 0; /* Needs To Be Set Via Options */
likeChance = 0; /* Needs To Be Set Via Options */
}
/**************************************************************************
* Purpose: Resets the graph of network to new graph.
* IMPORT: none
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void resetGraph()
{ /* Reset The graph. */
graph = new DSAGraph();
}
/**************************************************************************
* Purpose: Changes graph to newGraph
* IMPORT: DSAGraph newGraph
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void redirectGraph(DSAGraph newGraph)
{ /* Dereference old graph, use new graph. */
graph = newGraph;
}
/**************************************************************************
* Purpose: Returns string of UserPosts.
* IMPORT: String name
* EXPORT: string UserPosts
* DATE: 27/10/2019
**************************************************************************/
public String getUserPosts(String name)
{
return graph.getUserPosts(graph.getVertex(name));
}
/**************************************************************************
* Purpose: Returns string of following users.
* IMPORT: String name
* EXPORT: string followingUsers
* DATE: 27/10/2019
**************************************************************************/
public String getFollowing(String name)
{
return graph.getFollowing(graph.getVertex(name));
}
/**************************************************************************
* Purpose: Returns string of follwers of user.
* IMPORT: Sttring name
* EXPORT: String follwers
* DATE: 27/10/2019
**************************************************************************/
public String getFollowers(String name)
{
return graph.getFollowers(graph.getVertex(name));
}
/**************************************************************************
* Purpose: Returns graph.
* IMPORT: none
* EXPORT: returns graph
* DATE: 27/10/2019
**************************************************************************/
public DSAGraph getGraph()
{
return graph;
}
/**************************************************************************
* Purpose: Displays Graph based off input.
* IMPORT: char request
* EXPORT: String outNetwork
* DATE: 27/10/2019
**************************************************************************/
public String displayNetwork(char request)
{
String outNetwork = "";
if (request == 'L')
{
outNetwork = graph.displayAsList();
}
else if (request == 'M')
{
outNetwork = graph.displayAsMatrix();
}
return outNetwork;
}
/**************************************************************************
* Purpose: follows from to
* IMPORT: String from String to
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void follow(String from, String to)
{
try
{
graph.getVertex(from);
graph.getVertex(to);
graph.follow(from, to);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
/**************************************************************************
* Purpose: Unfollows from to
* IMPORT: String from, String to
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void unfollow(String from, String to)
{
try
{
graph.unfollow(from, to);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
/**************************************************************************
* Purpose: Return String of all vertexs.
* IMPORT: none
* EXPORT: String vertexList
* DATE: 27/10/2019
**************************************************************************/
public String getVerticiesList()
{ /* Returns A String 'List' of all the verticies in graph, used in NetworkIO */
return graph.getVerticiesList();
}
/**************************************************************************
* Purpose: Return String of all edges.
* IMPORT: none
* EXPORT: String edgeList
* DATE: 27/10/2019
**************************************************************************/
public String getEdgeList()
{ /* Return A String 'List' of all the edges in graph in the format FROM:TO */
return graph.getEdgesList();
}
/**************************************************************************
* Purpose: constructs new message in network.
* IMPORT: String origin, String contents
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void newMessage(String origin, String contents)
{
try
{
graph.newMessage(graph.getVertex(origin), contents);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
/**************************************************************************
* Purpose: Constructs new message in network with clickbait
* IMPORT: String origin, String contents, double clickBait
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void newMessageC(String origin, String contents, double clickBait)
{
try
{
graph.newMessageC(graph.getVertex(origin), contents, clickBait);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
/**************************************************************************
* Purpose: Add user to network.
* IMPORT: String nodeName
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void addUser(String nodeName)
{
try
{
graph.addVertex(nodeName, new Integer(1)); /* Value isnt important. */
/* USER HAS BEEN ADDED SUCCESSFULLY */
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
/**************************************************************************
* Purpose: remove user from network.
* IMPORT: String nodeName
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void removeUser(String nodeName)
{
try
{
graph.removeNode(nodeName);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
/**************************************************************************
* Purpose: returns string of users ranked by popularity.
* IMPORT: none
* EXPORT: String orderedUsers
* DATE: 27/10/2019
**************************************************************************/
public String userPopularity()
{
return graph.orderedUsers();
}
/**************************************************************************
* Purpose: returns a string of ranked messages.
* IMPORT: none
* EXPORT: String rankedMessages
* DATE: 27/10/2019
**************************************************************************/
public String rankMessages()
{
return graph.rankMessages();
}
/**************************************************************************
* Purpose: returns followChance
* IMPORT: none
* EXPORT: returns followchance.
* DATE: 27/10/2019
**************************************************************************/
public double getFollowChance()
{
return graph.getFollowChance();
}
/**************************************************************************
* Purpose: Performs a timestep in the network.
* IMPORT: none
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void timeStep()
{
Calendar timeCurr = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-mm-dd_hh-mm-ss");
String uniqueFilename = df.format(timeCurr.getTime()) + "_timestep" + "("
+ (Math.random() * Math.random() * Math.random() * Math.random()) + ")" + ".txt";
System.out.println("Outputting time step to file: " + uniqueFilename + "\nThis is stored in the timeSteps folder.");
System.out.println("===================TIMESTEP===================");
graph.timeStep(uniqueFilename);
}
/**************************************************************************
* Purpose: Returns likeChance
* IMPORT: none
* EXPORT: likeChance
* DATE: 27/10/2019
**************************************************************************/
public double getLikeChance()
{
return graph.getLikeChance();
}
/**************************************************************************
* Purpose: Returns a int[] of likes.
* IMPORT: none
* EXPORT: int[] likes
* DATE: 27/10/2019
**************************************************************************/
public int[] getLikes()
{
return graph.getLikes();
}
/**************************************************************************
* Purpose: sets follow chance to inval
* IMPORT: double inFollowChance
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void setFollowChance(double inFollowChance)
{ /* Expects it as a decimal. */
if (Math.round(inFollowChance) <= 1 && Math.round(inFollowChance) >= 0)
{
graph.setFollowChance(inFollowChance);
}
else
{
throw new IllegalArgumentException("Can only have a chance between 0.00 and 1.00");
}
}
/**************************************************************************
* Purpose: sets like chance to inval
* IMPORT: double inLikeChance
* EXPORT: none
* DATE: 27/10/2019
**************************************************************************/
public void setLikeChance(double inLikeChance)
{ /* Expects it as a decimal. */
if (Math.round(inLikeChance) <= 1 && Math.round(inLikeChance) >= 0)
{
graph.setLikeChance(inLikeChance);
}
else
{
throw new IllegalArgumentException("Can only have a chance between 0.00 and 1.00");
}
}
/**************************************************************************
* Purpose: Returns count of vertexs.
* IMPORT: none
* EXPORT: vertexCount int
* DATE: 27/10/2019
**************************************************************************/
public int amount()
{
return graph.getVertexCount();
}
/**************************************************************************
* Purpose: Returns string of all messages.
* IMPORT: none
* EXPORT: String messages
* DATE: 27/10/2019
**************************************************************************/
public String displayMessages()
{
return graph.getMessages();
}
}