-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConfigFile.java
More file actions
493 lines (421 loc) · 18 KB
/
ConfigFile.java
File metadata and controls
493 lines (421 loc) · 18 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.File;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.util.ArrayList;
import java.io.FileOutputStream;
import java.io.IOException;
public class ConfigFile{
public int minNumLines = 3;
public String database = null;
public int databaseFormat = 0;
public String project = null;
public String outputDir = null;
public int matchAlgorithm = 0;
public int matchMode = 0;
public int meshBlockSize = 0;
public boolean debug = false;
public boolean saveEmpty = false;
public boolean forceRetokenization = true;
public int gapSize = 0;
public String resultPath = null;
public boolean exportResults = false;
public boolean loadResults = false;
public int similarityRange = 0;
public boolean enableSimilarity = true;
public boolean enableRepetitive = false;
public boolean enableOneMethod = false;
public boolean buildTFIDF = false;
public int numberThreads = 0;
public int minNumberStatements = 0;
public boolean enablePercentageMatching = false;
public boolean loadDatabaseFilePaths = false;
public int aprioriMinSupport = 0;
public boolean enableQuery = false;
private String getTextValue(Element doc, String tag) {
String value = null;
NodeList nl;
nl = doc.getElementsByTagName(tag);
if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
value = nl.item(0).getFirstChild().getNodeValue();
}
return value;
}
private void loadHeuristic(Element doc) {
NodeList nl1;
Element firstNode;
NodeList nl2;
Element secondNode;
String value;
// process text similarity
nl1 = doc.getElementsByTagName("similarity");
firstNode = (Element) nl1.item(0);
nl2 = firstNode.getElementsByTagName("similarityRange");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("local")) {
similarityRange = 0;
} else if (value.equals("extended")) {
similarityRange = 1;
} else {
System.out.println("Invalid similarityRange option, must be local/extended");
System.exit(0);
}
System.out.println("Text similarity range: " + similarityRange);
nl2 = firstNode.getElementsByTagName("enableSimilarity");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
enableSimilarity = true;
} else if (value.equals("false")) {
enableSimilarity = false;
} else {
System.out.println("Invalid enableSimilarity option, must be true/false");
System.exit(0);
}
System.out.println("Text similarity status: " + enableSimilarity);
// process other heuristics
nl1 = doc.getElementsByTagName("others");
firstNode = (Element) nl1.item(0);
nl2 = firstNode.getElementsByTagName("enableRepetitive");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
enableRepetitive = true;
} else if (value.equals("false")) {
enableRepetitive = false;
} else {
System.out.println("Invalid enableRepetitive option, must be true/false");
System.exit(0);
}
nl2 = firstNode.getElementsByTagName("minNumberStatements");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
minNumberStatements = Integer.parseInt(value);
System.out.println("Min num statements: " + minNumberStatements);
nl2 = firstNode.getElementsByTagName("enablePercentageMatchingAuto");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
enablePercentageMatching = true;
} else if (value.equals("false")) {
enablePercentageMatching = false;
} else {
System.out.println("Invalid enablePercentageMatching option, must be true/false");
System.exit(0);
}
}
private void loadMatching(Element doc) {
NodeList nl1 = doc.getElementsByTagName("matching");
Element firstNode = (Element) nl1.item(0);
NodeList nl2;
Element secondNode;
String value;
nl2 = firstNode.getElementsByTagName("minNumLines");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
minNumLines = Integer.parseInt(value);
System.out.println("Min num lines: " + minNumLines);
nl2 = firstNode.getElementsByTagName("matchAlgorithm");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
matchAlgorithm = Integer.parseInt(value);
if (matchAlgorithm != 0 && matchAlgorithm != 1) {
System.out.println("Invalid alogrithm choice, must be 1 or 0");
System.exit(0);
}
if (matchAlgorithm == 1) {
System.out.println("Algorithm: Gapped - " + matchAlgorithm);
} else {
System.out.println("Algorithm: Non-gapped - " + matchAlgorithm);
}
nl2 = firstNode.getElementsByTagName("matchMode");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
matchMode = Integer.parseInt(value);
if (matchMode != 0 && matchMode != 1) {
System.out.println("Invalid matching mode, must be 1 (mesh) or 0 (between)");
System.exit(0);
}
if (matchMode == 0) {
System.out.println("Match Mode: Between Comparison - " + matchMode);
} else {
System.out.println("Match Mode: Full Mesh Comparison - " + matchMode);
}
nl2 = firstNode.getElementsByTagName("meshBlockSize");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
meshBlockSize = Integer.parseInt(value);
System.out.println("Mesh matching block size: " + meshBlockSize);
nl2 = firstNode.getElementsByTagName("numberThreads");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
numberThreads = Integer.parseInt(value);
if (numberThreads < 0) {
System.out.println("Invalid number of threads, must be 0 or larger");
System.exit(0);
}
System.out.println("Number of threads: " + value);
nl2 = firstNode.getElementsByTagName("gapSize");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
gapSize = Integer.parseInt(value);
if (gapSize < 1) {
System.out.println("Invalid gap size, must be 1 or higher");
System.out.println("A value of 1 means no gaps are allowed");
System.exit(0);
}
System.out.println("Gap size: " + gapSize);
}
private void loadFrequencySet(Element doc) {
NodeList nl1 = doc.getElementsByTagName("frequencySet");
Element firstNode = (Element) nl1.item(0);
NodeList nl2;
Element secondNode;
String value;
nl2 = firstNode.getElementsByTagName("aprioriMinSupport");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
aprioriMinSupport = Integer.parseInt(value);
if (aprioriMinSupport < 0) {
System.out.println("Invalid minimum apriori support, must be larger than 0");
System.exit(0);
}
System.out.println("Apriori Minimum Support: " + aprioriMinSupport);
}
private void loadProjects(Element doc) {
NodeList nl1 = doc.getElementsByTagName("projects");
Element firstNode = (Element) nl1.item(0);
NodeList nl2;
Element secondNode;
String value;
nl2 = firstNode.getElementsByTagName("database");
secondNode = (Element) nl2.item(0);
database = secondNode.getFirstChild().getNodeValue();
System.out.println("Database path: " + database);
nl2 = firstNode.getElementsByTagName("project");
secondNode = (Element) nl2.item(0);
project = secondNode.getFirstChild().getNodeValue();
System.out.println("Project path: " + project);
nl2 = firstNode.getElementsByTagName("outputDir");
secondNode = (Element) nl2.item(0);
outputDir = secondNode.getFirstChild().getNodeValue();
System.out.println("Output path: " + outputDir);
nl2 = firstNode.getElementsByTagName("forceRetokenization");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
forceRetokenization = true;
} else if (value.equals("false")) {
forceRetokenization = false;
} else {
System.out.println("Invalid forceRetokenization option, must be true/false");
System.exit(0);
}
System.out.println("Build database: " + forceRetokenization);
nl2 = firstNode.getElementsByTagName("buildTFIDF");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
buildTFIDF = true;
} else if (value.equals("false")) {
buildTFIDF = false;
} else {
System.out.println("Invalid buildTFIDF option, must be true/false");
System.exit(0);
}
System.out.println("Build tf-idf: " + buildTFIDF);
nl2 = firstNode.getElementsByTagName("loadDatabaseFilePaths");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
loadDatabaseFilePaths = true;
} else if (value.equals("false")) {
loadDatabaseFilePaths = false;
} else {
System.out.println("Invalid loadDatabaseFilePaths option, must be true/false");
System.exit(0);
}
System.out.println("Load cached database path list: " + loadDatabaseFilePaths);
}
private void loadOutputSettings(Element doc) {
NodeList nl1 = doc.getElementsByTagName("outputSettings");
Element firstNode = (Element) nl1.item(0);
NodeList nl2;
Element secondNode;
String value;
nl2 = firstNode.getElementsByTagName("debug");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
debug = true;
} else if (value.equals("false")) {
debug = false;
} else {
System.out.println("Invalid debug option, must be true/false");
System.exit(0);
}
System.out.println("Debug: " + debug);
nl2 = firstNode.getElementsByTagName("saveEmpty");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
saveEmpty = true;
} else if (value.equals("false")) {
saveEmpty = false;
} else {
System.out.println("Invalid saveEmpty option, must be true/false");
System.exit(0);
}
System.out.println("Remove empty: " + saveEmpty);
nl2 = firstNode.getElementsByTagName("exportResults");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
exportResults = true;
} else if (value.equals("false")) {
exportResults = false;
} else {
System.out.println("Invalid exportResults option, must be true/false");
System.exit(0);
}
System.out.println("Export results: " + exportResults);
if (exportResults) {
nl2 = firstNode.getElementsByTagName("resultPath");
secondNode = (Element) nl2.item(0);
resultPath = secondNode.getFirstChild().getNodeValue();
System.out.println("Result path: " + resultPath);
}
nl2 = firstNode.getElementsByTagName("enableQuery");
secondNode = (Element) nl2.item(0);
value = secondNode.getFirstChild().getNodeValue();
if (value.equals("true")) {
enableQuery = true;
} else if (value.equals("false")) {
enableQuery = false;
} else {
System.out.println("Invalid enableQuery option, must be true/false");
System.exit(0);
}
System.out.println("Query mode: " + enableQuery);
}
public void loadConfig(String filePath) {
Document dom;
// Make an instance of the DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// use the factory to take an instance of the document builder
DocumentBuilder db = dbf.newDocumentBuilder();
// parse using the builder to get the DOM mapping of the
// XML file
dom = db.parse(filePath);
Element doc = dom.getDocumentElement();
String value = getTextValue(doc, "loadResults");
if (value.equals("true")) {
loadResults = true;
} else if (value.equals("false")) {
loadResults = false;
} else {
System.out.println("Invalid loadResults option, must be true/false");
System.exit(0);
}
System.out.println("Load results: " + loadResults);
try {
loadFrequencySet(doc);
loadMatching(doc);
loadHeuristic(doc);
loadProjects(doc);
loadOutputSettings(doc);
} catch (Exception e) {
System.out.println("Error while parsing the xml config, maybe missing entry:");
System.out.println(e);
e.printStackTrace();
System.exit(0);
}
} catch (ParserConfigurationException pce) {
System.out.println(pce.getMessage());
} catch (SAXException se) {
System.out.println(se.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
}
public static void writeBaseline(String filePath) {
/*
Document dom;
Element e = null;
// instance of a DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// use factory to get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
// create instance of DOM
dom = db.newDocument();
// create the root element
Element rootEle = dom.createElement("configuration");
// create data elements and place them under root
e = dom.createElement("numLines");
e.appendChild(dom.createTextNode("3"));
rootEle.appendChild(e);
e = dom.createElement("database");
e.appendChild(dom.createTextNode("path A"));
rootEle.appendChild(e);
e = dom.createElement("project");
e.appendChild(dom.createTextNode("path B"));
rootEle.appendChild(e);
e = dom.createElement("algorithm");
e.appendChild(dom.createTextNode("A"));
rootEle.appendChild(e);
e = dom.createElement("debug");
e.appendChild(dom.createTextNode("A"));
rootEle.appendChild(e);
e = dom.createElement("removeEmpty");
e.appendChild(dom.createTextNode("false"));
rootEle.appendChild(e);
e = dom.createElement("buildDatabase");
e.appendChild(dom.createTextNode("false"));
rootEle.appendChild(e);
e = dom.createElement("gapSize");
e.appendChild(dom.createTextNode("false"));
rootEle.appendChild(e);
e = dom.createElement("resultPath");
e.appendChild(dom.createTextNode("path C"));
rootEle.appendChild(e);
e = dom.createElement("loadResults");
e.appendChild(dom.createTextNode("false"));
rootEle.appendChild(e);
e = dom.createElement("similarityRange");
e.appendChild(dom.createTextNode("extended"));
rootEle.appendChild(e);
dom.appendChild(rootEle);
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "config.dtd");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
// send DOM to file
File file = new File(filePath);
if (file.exists()) {
System.out.println("File exists, please remove existing file first");
return;
}
FileOutputStream fos = new FileOutputStream(file);
DOMSource source = new DOMSource(dom);
StreamResult result = new StreamResult(fos);
tr.transform(source, result);
} catch (TransformerException te) {
System.out.println("Transformer exception: " + te.getMessage());
} catch (IOException ioe) {
System.out.println("IOException: " + ioe.getMessage());
}
} catch (ParserConfigurationException pce) {
System.out.println("UsersXML: Error trying to instantiate DocumentBuilder " + pce);
}
*/
}
}