-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventGenerator.java
More file actions
194 lines (166 loc) · 5.53 KB
/
EventGenerator.java
File metadata and controls
194 lines (166 loc) · 5.53 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
package com.fordham.cisc.project;
/**********************************************************************
* CISC-5835 : Big Data Algorithm
* Final Programming Assignment : Approximate counting using Morris++ Algorithm
* Project Team Size : 4 Person
* Project Team (4) : Vaibhav Dixit <vdixit@fordham.edu>, Matias Berretta Magarinos <mberrettamagarinos@fordham.edu>
* Rohini Mandge <rmandge@fordham.edu>, Kwami Nyaku <kwami.nyaku@gmail.com>
* Submission Date : 12 December 2017
*
* This program generates the random events and writes into a file (events.txt).
* time.
*
* This program takes the following input argument
* - Number of events to be generated - Integer
* - Granularity - Normal distribution of the generated events. ( Floating point number between 0(excluding) and 1(excluding) )
*
* Details about assignment is at http://www.dsm.fordham.edu/~agw/big-data-alg/hw/project-description.pdf
*
* Author: Vaibhav Dixit <vdixit@fordham.edu>
*
* Date: 12 December 2017
*
**********************************************************************/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author vdixit
*
*/
public class EventGenerator {
/**
* @param args
*/
// Logger instance to log the error message.
static Logger LOGGER = Logger.getLogger("GenEvents");
// Name of the event file to ge generated.
static final String fileName = "events.txt";
/**
* This is the main method of the program.
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello Java");
long startTime = System.currentTimeMillis();
if(!(args.length ==2) ){
errorMessage("Please pass the required argument to the program - (NumberOfEvents , Granularity) ", true, 0);
}
int noOfEvents = 0;
try {
noOfEvents = Integer.valueOf(args[0]);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
errorMessage("Number of events has to be integer number", true, null);
// e.printStackTrace();
// Program terminating message
errorMessage(null, true, 0);
}
double granularity = 0;
try {
granularity = Double.valueOf(args[1]);
if(0 >= granularity || granularity >= 1.0){
errorMessage("Granularity has to be a floating point number and between 0 and 1", true, 0);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
errorMessage("Granularity has to be a floating point number and less then 1", true, null);
// e.printStackTrace();
// Program terminating message
errorMessage("", true, 0);
}
//System.out.println("noOfEvents == " + noOfEvents);
//System.out.println("granularity == " + granularity);
List<Double> eventList = GanerateRandomEvents(noOfEvents, granularity);
// Write event list to a file
WriteToFile(eventList);
long endTime = System.currentTimeMillis();
System.out.println(" Elapse Time (In seconds) :- " + ((endTime - startTime)/1000.0));
}
/**
* This method generates the random event
*
* @param noOfEvents - Number of events to be generated.
* @param granularity - Normal distribution range of generated events.
* @return List of generated Events
*/
public static List<Double> GanerateRandomEvents(int noOfEvents, double granularity){
System.out.println(" ********* Generating Random Events and saving in events file ********* ");
List<Double> eventList = new ArrayList<Double>();
for(int counter=0; counter < noOfEvents; counter++){
// System.out.println(randonGenrator.nextDouble());
double tmp = ThreadLocalRandom.current().nextDouble(0, granularity);
//System.out.println(tmp);
eventList.add(tmp);
}
return eventList;
}
/**
* Utility method to print the console message with appropriate log level. Also, terminate the program in case of a severe error.
*
* @param msg - String message to print on console
* @param isError - Log Level
* @param exitCode - System exit code in case of termination
*/
public static void errorMessage(String msg, boolean isError, Integer exitCode){
if (msg != null && !(msg.equals(""))) {
if (isError)
LOGGER.log(Level.SEVERE, msg);
else
LOGGER.log(Level.WARNING, msg);
}
if(exitCode != null){
LOGGER.log(Level.SEVERE, "******* Terminating program ******* ");
System.exit(0);
}
}
/**
* This method is to write the random generated events in the event file.
*
* @param eventList
*/
public static void WriteToFile(List<Double> eventList){
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter out = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
out = new PrintWriter(bw);
for(double event : eventList){
out.println( event );
}
out.close();
} catch (IOException e) {
// exception handling left as an exercise for the reader
} finally {
try {
if (out != null)
out.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (bw != null)
bw.close();
} catch (IOException e) {
// exception handling left as an exercise for the reader
}
try {
if (fw != null)
fw.close();
} catch (IOException e) {
// exception handling left as an exercise for the reader
}
}
}
}