-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneBMMain.java
312 lines (218 loc) · 12.6 KB
/
OneBMMain.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
package onebm;
import config.ProgramConfiguration;
import config.ProgramConfigurationOption;
import database.DatabaseConnection;
import database.JOOQDatabaseInteractor;
import dataset.TargetTableManager;
import dataset.conversion.DatabaseToARFFConvertor;
import dataset.conversion.TableType;
import feature.featuretable.persistance.CompleteFeatureTablePersistance;
import globalsettings.FieldToFeatureTranformationSettings;
import graph.ForeignKeyEdge;
import graph.RelationalGraphBFSHandler;
import graph.TableGraph;
import org.jgrapht.Graph;
import org.jooq.Table;
import research.RefactoredJoinTableConstruction;
import utils.CleanUp;
import utils.CurrentDate;
import utils.NameGenerator;
import utils.Timer;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.util.*;
import java.util.logging.FileHandler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import static utils.CleanUp.cleanUp;
import static utils.CleanUp.cleanUpSingleTableUnsafe;
public class OneBMMain {
public static JOOQDatabaseInteractor jooqDatabaseInteractor;
public static ProgramConfiguration programConfiguration;
public static boolean cleanUpJoinTables = true;
public static NameGenerator joinTableNameGenerator;
public static final String oneBMTableName = "onebmtable";
public static final String outputRootDir = "output"+ File.separator + CurrentDate.getCurrentDateAsString() +
"_onebmtablebuilding_mod_target_tables";
public static final String overviewFileNameStem = "overview_onebmtablebuilding_run_times.csv";
public static final String propertiesFileNameStem = "runinfo";
public static final String propertiesSuffix = ".properties";
public static final int nbOfRuns = 1;
public static Logger oneBMRunTimeLogger;
static {
LogManager.getLogManager().reset();
}
public static String getOutputDirDataSetStr(String dataSetName){
return outputRootDir
+ File.separator + dataSetName;
}
public static String getPropertiesFileName(String dataSetName, int runIndex){
return getOutputDirDataSetStr(dataSetName) + File.separator +
propertiesFileNameStem + "_" + runIndex + "of"+nbOfRuns + propertiesSuffix;
}
public static Logger getRunTimeLogger(){
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler(outputRootDir + File.separator + "onebm_runtime_log.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return logger;
}
public static void setVerbosities(){
RefactoredJoinTableConstruction.VERBOSE = true;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
Path rootOutputDirPath = Paths.get(outputRootDir);
//create output directory if it does not yet exist
if (!Files.exists(rootOutputDirPath)) {
Files.createDirectories(rootOutputDirPath);
}
oneBMRunTimeLogger = getRunTimeLogger();
// setVerbosities();
Map<String, String> dataSetNameToPropertiesPathsMap = new LinkedHashMap<>();
// dataSetNameToPropertiesPathsMap.put("cora", "data/cora/config.properties");
// dataSetNameToPropertiesPathsMap.put("facebook", "data/facebook/config.properties");
// dataSetNameToPropertiesPathsMap.put("genes", "data/genes/config.properties");
// dataSetNameToPropertiesPathsMap.put("hepatitis_std", "data/hepatitis_std/config.properties");
// dataSetNameToPropertiesPathsMap.put("imdb_small", "data/imdb_small/config.properties");
// dataSetNameToPropertiesPathsMap.put("uw_cse", "data/uw_cse/config.properties");
// dataSetNameToPropertiesPathsMap.put("webkb", "data/webkb/config.properties");
// dataSetNameToPropertiesPathsMap.put("hepatitis_std", "data/hepatitis_std/config.properties");
// dataSetNameToPropertiesPathsMap.put("imdb_small", "data/imdb_small/config.properties");
// dataSetNameToPropertiesPathsMap.put("uw_cse", "data/uw_cse/config.properties");
//
dataSetNameToPropertiesPathsMap.put("financial", "data/financial/config.properties");
// dataSetNameToPropertiesPathsMap.put("university", "data/university/config.properties");
// dataSetNameToPropertiesPathsMap.put("financial_mod_target", "data/financial_mod_target/config.properties");
// dataSetNameToPropertiesPathsMap.put("hepatitis_std_mod_target", "data/hepatitis_std_mod_target/config.properties");
// dataSetNameToPropertiesPathsMap.put("imdb_small_mod_target", "data/imdb_small_mod_target/config.properties");
// dataSetNameToPropertiesPathsMap.put("uw_cse_mod_target", "data/uw_cse_mod_target/config.properties");
// dataSetNameToPropertiesPathsMap.put("university_mod_target", "data/university_mod_target/config.properties");
Map<String, List<Properties>> datasetToPropertiesList = new HashMap<>();
for (String dataSetName : dataSetNameToPropertiesPathsMap.keySet()) {
List<Properties> propertiesList = new ArrayList<>();
datasetToPropertiesList.put(dataSetName, propertiesList);
}
for (String dataSetName : dataSetNameToPropertiesPathsMap.keySet()) {
System.out.println("========================================================");
System.out.println("Start of " + dataSetName );
List<Properties> dataSetPropertiesList = datasetToPropertiesList.get(dataSetName);
String outputDirDataSetStr = getOutputDirDataSetStr(dataSetName);
Path outputDirPath = Paths.get(outputDirDataSetStr);
//create output directory if it does not yet exist
if (!Files.exists(outputDirPath)) {
Files.createDirectories(outputDirPath);
}
for (int i = 1; i <= nbOfRuns; i++) {
String propertiesPath = dataSetNameToPropertiesPathsMap.get(dataSetName);
System.out.println("Start of " + dataSetName + ", run " + i + " of " + nbOfRuns);
try {
Properties properties = runOneBM(dataSetName, propertiesPath);
dataSetPropertiesList.add(properties);
String propertiesOutputFileName = getPropertiesFileName(dataSetName, i);
FileOutputStream out = new FileOutputStream(propertiesOutputFileName);
properties.store(out, "run info for " + dataSetName + " for run " + i + " of " + nbOfRuns);
} catch (Exception e){
String message = "Catched exception for " + dataSetName + ", run " + i + "/" + nbOfRuns +"\n"
+ e.toString();
oneBMRunTimeLogger.info(message);
throw e;
}
System.out.println("End of " + dataSetName + ", run " + i + " of " + nbOfRuns);
System.out.println("--------------------------------------------------------");
}
System.out.println("========================================================");
System.out.println("========================================================");
}
String overviewCSVFileName = outputRootDir + File.separator + overviewFileNameStem;
OneBMPropertiesPrinter.toOverviewCSVFile(datasetToPropertiesList, overviewCSVFileName);
}
public static Properties runOneBM(String dataSetName, String propertiesPath) {
// String propertiesPath = args[0];
Logger logger = Logger.getLogger("m");
try {
OneBMMain.programConfiguration = new ProgramConfiguration(propertiesPath);
Connection databaseConnection = DatabaseConnection.getDatabaseConnection(programConfiguration);
OneBMMain.jooqDatabaseInteractor = new JOOQDatabaseInteractor(
databaseConnection,
programConfiguration
);
joinTableNameGenerator = new NameGenerator();
List<Table<?>> tables = jooqDatabaseInteractor.getTables();
cleanUp(tables, jooqDatabaseInteractor);
// create the relational graph. This is a DIRECTED graph
Graph<String, ForeignKeyEdge> relationalGraph = TableGraph.createFrom(jooqDatabaseInteractor.getTables());
System.out.println(TableGraph.asDOTString(relationalGraph));
TargetTableManager targetTableManager = TargetTableManager.getInstanceManager(jooqDatabaseInteractor, programConfiguration);
double distinctnessThreshold = 0.2;
int absoluteDistinctnessThreshold = 40;
FieldToFeatureTranformationSettings.setExistenceTestCreationDeciderToCategoricFieldUniqueValueCountController(
jooqDatabaseInteractor, distinctnessThreshold, absoluteDistinctnessThreshold
);
RelationalGraphBFSHandler<String> relationalGraphBFSHandler = new RelationalGraphBFSHandler<String>(relationalGraph, targetTableManager.getTargetTable().getName());
RefactoredJoinTableConstruction refactoredJoinTableConstruction = new RefactoredJoinTableConstruction(
relationalGraphBFSHandler, jooqDatabaseInteractor, joinTableNameGenerator, targetTableManager.getTargetID(),
targetTableManager
);
CompleteFeatureTablePersistance.GLOBAL_FEATURE_TABLE_NAME = oneBMTableName;
cleanUpSingleTableUnsafe(oneBMTableName, jooqDatabaseInteractor);
OneBMTableBuilder oneBMTableBuilder = new OneBMTableBuilder(refactoredJoinTableConstruction, jooqDatabaseInteractor);
long startTimeOneBMTableBuilding = System.nanoTime();
oneBMTableBuilder.build(targetTableManager);
long stopTimeOneBMTableBuilding = System.nanoTime();
double durationSec = (stopTimeOneBMTableBuilding - startTimeOneBMTableBuilding) * Timer.convertNanoTimeToSeconds;
System.out.println("time onebm table build (s):" + durationSec);
String outputDirStr = outputRootDir + File.separator +
programConfiguration.getConfigurationOption(ProgramConfigurationOption.SCHEMA);
Path outputDirPath = Paths.get(outputDirStr);
if (!Files.exists(outputDirPath)) {
Files.createDirectories(outputDirPath);
}
String fieldEncodingCSVString =
oneBMTableBuilder.getFeatureColumnEncoder().exportAsCSV(jooqDatabaseInteractor);
String fieldEncodingCSVFileStr =
outputDirStr + File.separator + "field_encodings.csv";
System.out.println(fieldEncodingCSVFileStr);
try (BufferedWriter fieldEncodingWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fieldEncodingCSVFileStr), "utf-8"))){
fieldEncodingWriter.write(fieldEncodingCSVString);
} catch (IOException i) {
i.printStackTrace();
}
String arffOutputFileStr = outputDirStr
+ File.separator
+ "onebmtable.arff";
DatabaseToARFFConvertor.convertDataSetToARFF(dataSetName, programConfiguration, TableType.ONEBM_TABLE, arffOutputFileStr);
System.out.println("ONEBM: nb of non-target table features constructed" + refactoredJoinTableConstruction.totalNbOfFeaturesConstructed);
Properties properties = new Properties();
properties.setProperty(InfoEnum.DURATION_SEC.toString(), Double.toString(durationSec));
properties.setProperty(InfoEnum.NB_OF_FEATURES_BUILD.toString(), Integer.toString(refactoredJoinTableConstruction.totalNbOfFeaturesConstructed));
return properties;
} catch(
Exception e
){
e.printStackTrace();
if(cleanUpJoinTables){
System.out.println("CLEANING UP TABLES: " + joinTableNameGenerator.getAllGeneratedNames());
for (String generatedTableName : joinTableNameGenerator.getAllGeneratedNames()) {
CleanUp.cleanUpSingleTable(generatedTableName, jooqDatabaseInteractor);
}
}
e.printStackTrace();
}
return null;
}
}