Skip to content

Commit 36be0f9

Browse files
authored
Merge pull request #4 from secure-software-engineering/feature/interruptable
Make Learner Algorithm interruptable
2 parents 0e9aaf3 + 39fd14d commit 36be0f9

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

swan_core/src/main/java/de/fraunhofer/iem/swan/Learner.java

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

33
import java.io.File;
4-
import java.io.FileInputStream;
54
import java.io.IOException;
65
import java.io.InputStream;
7-
import java.util.*;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Map;
812
import java.util.Map.Entry;
13+
import java.util.Properties;
14+
import java.util.Random;
15+
import java.util.Set;
916

1017
import de.fraunhofer.iem.swan.data.Category;
1118
import de.fraunhofer.iem.swan.data.Method;
12-
1319
import weka.classifiers.Evaluation;
1420
import weka.classifiers.bayes.BayesNet;
1521
import weka.classifiers.bayes.NaiveBayes;
@@ -53,7 +59,7 @@ public Learner(Writer writer) {
5359

5460
public double classify(Set<Method> trainingSet, Set<Method> testSet,
5561
Map<Category, Set<IFeature>> features, Set<Category> categories,
56-
String outputFile, boolean cweMode) throws IOException {
62+
String outputFile, boolean cweMode) throws IOException, InterruptedException {
5763

5864

5965
double fmeasure = 0;
@@ -317,6 +323,9 @@ sb, new Range(attributes.indexOf(idAttr) + 1
317323
writer.writeResultsToFiles(outputFile, methods, categories);
318324
//writer.writeResultsToFilesQWEL(outputFile, methods, categories);
319325

326+
if (Thread.currentThread().isInterrupted()) {
327+
throw new InterruptedException();
328+
}
320329
Runtime.getRuntime().gc();
321330
analysisTime = System.currentTimeMillis() - startAnalysisTime;
322331
System.out.println("Time to classify " + categories.toString() + ": " + analysisTime + " ms");

swan_core/src/main/java/de/fraunhofer/iem/swan/Main.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public static void main(String[] args) {
6161
// System.out.println("Done.");
6262
} catch (IOException e) {
6363
e.printStackTrace();
64+
} catch (InterruptedException e) {
65+
e.printStackTrace();
6466
}
6567
}
6668

@@ -89,8 +91,9 @@ public static void main(String[] args) {
8991
* @param outputDir Directory where the output should be written.
9092
* @throws IOException In case an error occurs during the preparation or
9193
* execution of the analysis.
94+
* @throws InterruptedException
9295
*/
93-
public void run(String sourceDir, String outputDir) throws IOException {
96+
public void run(String sourceDir, String outputDir) throws IOException, InterruptedException {
9497
run(sourceDir, null, null, outputDir);
9598
}
9699

@@ -108,8 +111,9 @@ public void run(String sourceDir, String outputDir) throws IOException {
108111
* @param outputDir Directory where the output should be written.
109112
* @throws IOException In case an error occurs during the preparation or
110113
* execution of the analysis.
114+
* @throws InterruptedException
111115
*/
112-
public void run(String sourceDir, String trainSourceCode, String trainJson, String outputDir) throws IOException {
116+
public void run(String sourceDir, String trainSourceCode, String trainJson, String outputDir) throws IOException, InterruptedException {
113117

114118
// This helper object keeps track of created temporary directories and files to
115119
// to be deleted before exiting the
@@ -138,7 +142,7 @@ public void run(String sourceDir, String trainSourceCode, String trainJson, Stri
138142
}
139143

140144
private void internalRun(String sourceDir, String trainSourceCode, String trainJson, String outputDir)
141-
throws IOException {
145+
throws IOException, InterruptedException {
142146

143147
int iterations = 0;
144148
if (runOAT)
@@ -236,7 +240,7 @@ private void internalRun(String sourceDir, String trainSourceCode, String trainJ
236240

237241
}
238242

239-
private double runClassifier(HashSet<Category> categories, boolean cweMode) throws IOException {
243+
private double runClassifier(HashSet<Category> categories, boolean cweMode) throws IOException, InterruptedException {
240244
parser.resetMethods();
241245
loader.resetMethods();
242246
// System.out.println("***** Starting classification for " +

0 commit comments

Comments
 (0)