Skip to content

Commit a7d5bdd

Browse files
committed
Fix bugs and cleanup code
1 parent 2a87510 commit a7d5bdd

File tree

4 files changed

+52
-47
lines changed

4 files changed

+52
-47
lines changed

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/SwanPipeline.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package de.fraunhofer.iem.swan;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import de.fraunhofer.iem.swan.cli.SwanOptions;
5-
import de.fraunhofer.iem.swan.features.FeaturesHandler;
4+
import de.fraunhofer.iem.swan.features.FeatureSetSelector;
5+
import de.fraunhofer.iem.swan.features.IFeatureSet;
66
import de.fraunhofer.iem.swan.features.code.soot.SourceFileLoader;
77
import de.fraunhofer.iem.swan.io.dataset.SrmList;
88
import de.fraunhofer.iem.swan.io.dataset.SrmListUtils;
99
import de.fraunhofer.iem.swan.model.ModelEvaluator;
10-
import de.fraunhofer.iem.swan.model.engine.Meka;
1110
import de.fraunhofer.iem.swan.util.Util;
1211
import org.slf4j.Logger;
1312
import org.slf4j.LoggerFactory;
14-
import java.io.File;
13+
1514
import java.io.IOException;
1615

1716
/**
@@ -53,7 +52,7 @@ public void run() throws IOException, InterruptedException {
5352
IFeatureSet featureSet = featureSetSelector.select(dataset, testDataset, options);
5453

5554
//Train and evaluate model for SRM and CWE categories
56-
ModelEvaluator modelEvaluator = new ModelEvaluator(featuresHandler, options, testDataset.getMethods());
55+
ModelEvaluator modelEvaluator = new ModelEvaluator(featureSet, options, testDataset.getMethods());
5756
modelEvaluator.trainModel();
5857

5958
long analysisTime = System.currentTimeMillis() - startAnalysisTime;

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/data/Method.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,38 @@ public class Method {
4242

4343
public Method() {
4444
cwe = new HashSet<>();
45+
srm = new HashSet<>();
4546
}
4647

4748
public Method(String name, String returnType, String className) {
4849
this.name = name;
4950
this.className = className;
5051
this.returnType = returnType;
5152
this.parameters = new ArrayList<>();
53+
cwe = new HashSet<>();
54+
srm = new HashSet<>();
5255
}
5356

54-
public Method(String name, List<String> parameters, String returnType, String className) {
57+
public Method(String name, List<String> parameters, String returnType) {
5558
this.name = name;
56-
this.className = className;
5759
this.returnType = returnType;
5860
this.parameters = parameters;
61+
cwe = new HashSet<>();
62+
srm = new HashSet<>();
63+
framework = "";
64+
link = "";
65+
comment = "";
66+
discovery = "";
67+
dataIn = new RelevantPart();
68+
dataOut = new RelevantPart();
69+
sourceJar = "";
5970
}
6071

6172
public Method(SootMethod sm) {
6273
this.name = sm.getName();
6374
this.className = sm.getDeclaringClass().getName();
6475
this.returnType = sm.getReturnType().toString();
65-
this.parameters = new ArrayList<String>();
76+
this.parameters = new ArrayList<>();
6677
for (Type p : sm.getParameterTypes())
6778
this.parameters.add(p.toString());
6879
}
@@ -71,7 +82,7 @@ public Method(Method methodAndClass) {
7182
this.name = methodAndClass.name;
7283
this.className = methodAndClass.className;
7384
this.returnType = methodAndClass.returnType;
74-
this.parameters = new ArrayList<String>(methodAndClass.parameters);
85+
this.parameters = new ArrayList<>(methodAndClass.parameters);
7586
}
7687

7788
public Method deriveWithNewClass(String className) {
@@ -355,9 +366,7 @@ public boolean equals(Object another) {
355366
return false;
356367
if (!this.parameters.equals(otherMethod.parameters))
357368
return false;
358-
if (!this.className.equals(otherMethod.className))
359-
return false;
360-
return true;
369+
return this.className.equals(otherMethod.className);
361370
}
362371

363372
@Override

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/model/MonteCarloValidator.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.fraunhofer.iem.swan.model;
22

3-
import de.fraunhofer.iem.swan.util.Util;
43
import weka.classifiers.Classifier;
54
import weka.classifiers.Evaluation;
65
import weka.classifiers.evaluation.output.prediction.AbstractOutput;
@@ -40,12 +39,10 @@ public HashMap<String, ArrayList<Double>> getFMeasure() {
4039
* @param classifier classifier to model creation
4140
* @param trainPercentage percentage of instances for train set
4241
* @param iterations number of evaluation iterations
43-
* @return average F-score for iterations
4442
*/
45-
public HashMap<String, ArrayList<Double>> monteCarloValidate(Instances instances, Classifier classifier, double trainPercentage, int iterations) {
43+
public void monteCarloValidate(Instances instances, Classifier classifier, double trainPercentage, int iterations) {
4644

4745
for (int i = 0; i < iterations; i++) {
48-
Util.exportInstancesToArff(instances);
4946

5047
int trainSize = (int) Math.round(instances.numInstances() * trainPercentage);
5148
int testSize = instances.numInstances() - trainSize;
@@ -56,12 +53,11 @@ public HashMap<String, ArrayList<Double>> monteCarloValidate(Instances instances
5653
Instances trainInstances = new Instances(instances, 0, trainSize);
5754
Instances testInstances = new Instances(instances, trainSize, testSize);
5855

59-
evaluate(classifier, trainInstances, testInstances, i);
56+
evaluate(classifier, trainInstances, testInstances);
6057
}
61-
return fMeasure;
6258
}
6359

64-
public ArrayList<String> evaluate(Classifier classifier, Instances trainInstances, Instances testInstances, int iteration) {
60+
public ArrayList<String> evaluate(Classifier classifier, Instances trainInstances, Instances testInstances) {
6561

6662
Evaluation eval = null;
6763
try {
@@ -83,7 +79,7 @@ public ArrayList<String> evaluate(Classifier classifier, Instances trainInstance
8379
for (String result : output) {
8480
String[] entry = result.split(",");
8581

86-
if (!entry[2].contains("none")) {
82+
if (!entry[2].contains("0")) {
8783
predictions.add(entry[5].replace("'", ""));
8884
}
8985
}
@@ -100,7 +96,7 @@ public void updateResultSet(Instances instances, Evaluation eval) {
10096

10197
String currentClass = instances.classAttribute().value(c);
10298

103-
if (!currentClass.contentEquals("none")) {
99+
if (!currentClass.contentEquals("0")) {
104100
if (!fMeasure.containsKey(currentClass))
105101
fMeasure.put(currentClass, new ArrayList<>(Collections.singletonList(eval.fMeasure(c))));
106102
else {

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/util/Util.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
import java.util.*;
88
import java.util.zip.ZipEntry;
99
import java.util.zip.ZipInputStream;
10-
1110
import de.fraunhofer.iem.swan.SwanPipeline;
1211
import org.slf4j.Logger;
1312
import org.slf4j.LoggerFactory;
14-
1513
import de.fraunhofer.iem.swan.features.code.type.IFeature.Type;
1614
import de.fraunhofer.iem.swan.data.Category;
1715
import de.fraunhofer.iem.swan.data.Method;
@@ -33,7 +31,7 @@ public class Util {
3331
* @return The purged set of methods without duplicates
3432
*/
3533
public static Set<Method> sanityCheck(Set<Method> methods, Set<Method> init) {
36-
Map<String, Method> signatureToMethod = new HashMap<String, Method>();
34+
Map<String, Method> signatureToMethod = new HashMap<>();
3735
for (Method m1 : methods) {
3836
String sig = m1.getSignature();
3937
Method m2 = signatureToMethod.get(sig);
@@ -45,7 +43,7 @@ else if (!m1.equals(m2)) {
4543
}
4644
}
4745

48-
Set<Method> ret = new HashSet<Method>();
46+
Set<Method> ret = new HashSet<>();
4947
for (Method m : signatureToMethod.values()) {
5048
if (!init.contains(m))
5149
ret.add(m);
@@ -57,7 +55,7 @@ else if (!m1.equals(m2)) {
5755
}
5856

5957
public static void printStatistics(String message, Set<Method> methods) {
60-
Map<Category, Integer> counters = new HashMap<Category, Integer>();
58+
Map<Category, Integer> counters = new HashMap<>();
6159
for (Method am : methods) {
6260
for (Category category : am.getSrm()) {
6361
if (counters.containsKey(category)) {
@@ -68,17 +66,17 @@ public static void printStatistics(String message, Set<Method> methods) {
6866
}
6967
}
7068

71-
logger.info(message + ": total methods={}, categories={}", methods.size(), counters.toString());
69+
logger.info(message + ": total methods={}, categories={}", methods.size(), counters);
7270
}
7371

7472
public static Set<String> getAllClassesFromDirectory(String dir) throws IOException {
75-
Set<String> classes = new HashSet<String>();
73+
Set<String> classes = new HashSet<>();
7674
File folder = new File(dir);
7775
File[] listOfFiles = folder.listFiles();
7876
if (listOfFiles != null) {
79-
for (int i = 0; i < listOfFiles.length; i++) {
80-
if (listOfFiles[i].getName().endsWith(".jar"))
81-
classes.addAll(getAllClassesFromJar(listOfFiles[i].getAbsolutePath()));
77+
for (File listOfFile : listOfFiles) {
78+
if (listOfFile.getName().endsWith(".jar"))
79+
classes.addAll(getAllClassesFromJar(listOfFile.getAbsolutePath()));
8280
}
8381
}
8482
return classes;
@@ -89,12 +87,12 @@ public static Map<String, String> getAllClassesFromDir(String dir) throws IOExce
8987
File folder = new File(dir);
9088
File[] listOfFiles = folder.listFiles();
9189
if (listOfFiles != null) {
92-
for (int i = 0; i < listOfFiles.length; i++) {
93-
if (listOfFiles[i].getName().endsWith(".jar"))
90+
for (File listOfFile : listOfFiles) {
91+
if (listOfFile.getName().endsWith(".jar"))
9492

9593

96-
for (String str : getAllClassesFromJar(listOfFiles[i].getAbsolutePath())) {
97-
classes.put(str, listOfFiles[i].getName());
94+
for (String str : getAllClassesFromJar(listOfFile.getAbsolutePath())) {
95+
classes.put(str, listOfFile.getName());
9896
}
9997

10098
// classes.put(listOfFiles[i].getName() , getAllClassesFromJar(listOfFiles[i].getAbsolutePath()));
@@ -104,7 +102,7 @@ public static Map<String, String> getAllClassesFromDir(String dir) throws IOExce
104102
}
105103

106104
private static Set<String> getAllClassesFromJar(String jarFile) throws IOException {
107-
Set<String> classes = new HashSet<String>();
105+
Set<String> classes = new HashSet<>();
108106
ZipInputStream zip = new ZipInputStream(new FileInputStream(jarFile));
109107
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
110108
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
@@ -138,7 +136,7 @@ public static String buildCP(String dir) {
138136

139137
/**
140138
* Creates artificial annotations for non-overridden methods in subclasses. If
141-
* the class A implements some method foo() which is marked as e.g. a source and
139+
* class A implements some method foo() which is marked as e.g. a source and
142140
* class B extends A, but does not overwrite foo(), B.foo() must also be a
143141
* source.
144142
*
@@ -214,16 +212,16 @@ private Method findMethod(SootMethod sm) {
214212

215213
public static Set<String> getFiles(String fileInDirectory) throws IOException {
216214
String directory = fileInDirectory.substring(0, fileInDirectory.lastIndexOf(File.separator));
217-
String fileName = fileInDirectory.substring(fileInDirectory.lastIndexOf(File.separator) + 1,
218-
fileInDirectory.length());
219-
Set<String> files = new HashSet<String>();
215+
String fileName = fileInDirectory.substring(fileInDirectory.lastIndexOf(File.separator) + 1
216+
);
217+
Set<String> files = new HashSet<>();
220218
File folder = new File(directory);
221219
File[] listOfFiles = folder.listFiles();
222-
for (int i = 0; i < listOfFiles.length; i++) {
223-
if (listOfFiles[i].isFile() && listOfFiles[i].getName().endsWith(".txt")
224-
&& !listOfFiles[i].getName().endsWith("_" + Category.NONE + ".txt")
225-
&& !listOfFiles[i].getName().equals(fileName)) {
226-
files.add(listOfFiles[i].getCanonicalPath());
220+
for (File listOfFile : listOfFiles) {
221+
if (listOfFile.isFile() && listOfFile.getName().endsWith(".txt")
222+
&& !listOfFile.getName().endsWith("_" + Category.NONE + ".txt")
223+
&& !listOfFile.getName().equals(fileName)) {
224+
files.add(listOfFile.getCanonicalPath());
227225
}
228226
}
229227
return files;
@@ -272,7 +270,7 @@ public static String getClassName(Instances instances) {
272270
/**
273271
* Export the instances to an ARFF file.
274272
*
275-
* @param instances
273+
* @param instances WEKA instances to be exported
276274
*/
277275
public static String exportInstancesToArff(Instances instances) {
278276
ArffSaver saver = new ArffSaver();
@@ -283,7 +281,10 @@ public static String exportInstancesToArff(Instances instances) {
283281

284282
try {
285283

286-
String relationName = instances.relationName().substring(0, instances.relationName().indexOf(":"));
284+
String relationName = instances.relationName();
285+
286+
if (instances.relationName().contains(":"))
287+
relationName = relationName.substring(0, instances.relationName().indexOf(":"));
287288

288289
String arffFile = SwanPipeline.options.getOutputDir() + File.separator + "arff-data" + File.separator + relationName + ".arff";
289290
saver.setFile(new File(arffFile));

0 commit comments

Comments
 (0)