Skip to content

Commit e525ac3

Browse files
authored
Merge branch 'wip-upgrade-swan-assist' into wip-swan-assist-java-and-gradle-upgrade
2 parents f3b35e8 + c591d55 commit e525ac3

File tree

13 files changed

+212
-210
lines changed

13 files changed

+212
-210
lines changed

swan-assist/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.fraunhofer'
7-
version '1.3'
7+
version '1.5'
88

99
sourceCompatibility = JavaVersion.VERSION_11
1010
targetCompatibility = JavaVersion.VERSION_11

swan-assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/ExportAction.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
package de.fraunhofer.iem.swan.assist.actions;
99

10-
import com.intellij.notification.Notification;
11-
import com.intellij.notification.NotificationType;
12-
import com.intellij.notification.Notifications;
10+
import com.intellij.notification.*;
1311
import com.intellij.openapi.actionSystem.AnAction;
1412
import com.intellij.openapi.actionSystem.AnActionEvent;
1513
import com.intellij.openapi.actionSystem.CommonDataKeys;
@@ -31,8 +29,11 @@
3129

3230
public class ExportAction extends AnAction {
3331

32+
private static final NotificationGroup TOOL_GROUP = new NotificationGroup(Constants.PLUGIN_GROUP_DISPLAY_ID,
33+
NotificationDisplayType.TOOL_WINDOW, true);
3434
/**
3535
* Obtains list of methods and creates new JSON file in the location specified by the user.
36+
*
3637
* @param anActionEvent source event
3738
*/
3839
@Override
@@ -71,8 +72,8 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
7172
try {
7273
exportFile.writeToJsonFile(JSONFileLoader.getMethods(), filePath);
7374

74-
Notifications.Bus.notify(
75-
new Notification(Constants.PLUGIN_GROUP_DISPLAY_ID, "", JSONFileLoader.getMethods().size()+ " methods exported to: "+filePath, NotificationType.INFORMATION));
75+
Notification analysisCompleted = TOOL_GROUP.createNotification("", JSONFileLoader.getMethods().size() + " methods exported to: " + filePath, NotificationType.INFORMATION);
76+
Notifications.Bus.notify(analysisCompleted, project);
7677

7778
} catch (IOException e) {
7879
e.printStackTrace();
@@ -82,6 +83,7 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
8283

8384
/**
8485
* Controls whether the action is enabled or disabled
86+
*
8587
* @param event source event
8688
*/
8789
@Override

swan-assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/RunSwanAnalysisImpl.java

Lines changed: 5 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,11 @@
77

88
package de.fraunhofer.iem.swan.assist.actions;
99

10-
import com.intellij.openapi.progress.ProgressIndicator;
10+
import com.intellij.openapi.progress.PerformInBackgroundOption;
1111
import com.intellij.openapi.progress.ProgressManager;
12-
import com.intellij.openapi.progress.Task;
1312
import com.intellij.openapi.project.Project;
14-
import com.intellij.util.messages.MessageBus;
15-
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
1613
import de.fraunhofer.iem.swan.assist.util.Constants;
17-
import de.fraunhofer.iem.swan.cli.CliRunner;
18-
import de.fraunhofer.iem.swan.cli.SwanCli;
19-
import de.fraunhofer.iem.swan.cli.SwanOptions;
20-
import de.fraunhofer.iem.swan.io.dataset.SrmList;
21-
import de.fraunhofer.iem.swan.io.dataset.SrmListUtils;
22-
import org.apache.commons.io.FileUtils;
23-
import org.jetbrains.annotations.NotNull;
24-
2514
import java.io.File;
26-
import java.io.IOException;
27-
import java.time.LocalDateTime;
28-
import java.time.format.DateTimeFormatter;
2915
import java.util.HashMap;
3016
import java.util.ResourceBundle;
3117

@@ -36,8 +22,7 @@ public class RunSwanAnalysisImpl {
3622

3723
private static HashMap<String, String> parameters;
3824
private Project project;
39-
private long duration;
40-
private SwanCli swan;
25+
HashMap<String, String> results;
4126

4227
/**
4328
* Initializes builder.
@@ -49,6 +34,7 @@ public class RunSwanAnalysisImpl {
4934

5035
this.project = project;
5136
parameters = param;
37+
results = new HashMap<>();
5238
}
5339

5440
/**
@@ -63,106 +49,7 @@ public void run() {
6349
if (!outputFolder.exists())
6450
outputFolder.mkdir();
6551

66-
/*File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.OUTPUT_LOG));
67-
try {
68-
logFile.createNewFile();
69-
parameters.replace(Constants.OUTPUT_LOG, logFile.getPath());
70-
71-
FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
72-
73-
System.setOut(new PrintStream(fileOutputStream));
74-
75-
} catch (IOException e) {
76-
e.printStackTrace();
77-
}*/
78-
79-
ProgressManager.getInstance().run(new Task.Backgroundable(project, resource.getString("Messages.Title.Progress")) {
80-
@Override
81-
public void run(@NotNull ProgressIndicator indicator) {
82-
83-
long start = System.currentTimeMillis();
84-
85-
SwanOptions options = new CliRunner().initializeOptions();
86-
options.setTestDataDir(parameters.get(Constants.SOURCE_DIRECTORY));
87-
options.setOutputDir( parameters.get(Constants.OUTPUT_DIRECTORY));
88-
options.setToolkit(parameters.get(Constants.TOOLKIT).toLowerCase());
89-
options.setPhase("predict");
90-
options.setTrainDataDir("");
91-
92-
93-
swan = new SwanCli();
94-
try {
95-
swan.run(options);
96-
} catch (IOException | InterruptedException e) {
97-
throw new RuntimeException(e);
98-
} catch (Exception e) {
99-
throw new RuntimeException(e);
100-
}
101-
duration = System.currentTimeMillis() - start;
102-
}
103-
104-
@Override
105-
public void onCancel() {
106-
super.onCancel();
107-
}
108-
109-
@Override
110-
public void onSuccess() {
111-
super.onSuccess();
112-
113-
114-
SrmList srmList = swan.getSwanPipeline().getModelEvaluator().getPredictedSrmList();
115-
116-
String filename = parameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + "srm-" + getCurrentTimestamp() + ".json";
117-
118-
try {
119-
SrmListUtils.exportFile(srmList, filename);
120-
} catch (IOException e) {
121-
throw new RuntimeException(e);
122-
}
123-
124-
125-
//Create copy of file
126-
/*copyFile(new File(parameters.get(Constants.OUTPUT_FILE)));
127-
128-
JSONFileParser parser = new JSONFileParser(parameters.get(Constants.OUTPUT_FILE));
129-
HashMap<String, MethodWrapper> exportedMethods = parser.parseJSONFileMap();*/
130-
131-
HashMap<String, String> results = new HashMap<String, String>();
132-
results.put(Constants.OUTPUT_FILE, filename);
133-
results.put(Constants.OUTPUT_LOG, "");
134-
135-
int m = (int) (((duration / 1000) / 60) % 60);
136-
int s = (int) ((duration / 1000) % 60);
137-
138-
results.put(Constants.ANALYSIS_RESULT, srmList.getMethods().size() + " methods found in " + m + " mins " + s + " secs");
139-
140-
MessageBus messageBus = project.getMessageBus();
141-
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);
142-
publisher.launchSwan(results);
143-
}
144-
});
145-
}
146-
147-
private void copyFile(File original) {
148-
149-
File copied = new File(
150-
parameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + getCurrentTimestamp() + "-config.json");
151-
try {
152-
FileUtils.copyFile(original, copied);
153-
} catch (IOException e) {
154-
e.printStackTrace();
155-
}
156-
}
157-
158-
/**
159-
* Get the timestamp in a specified format.
160-
*
161-
* @return Formatted date
162-
*/
163-
private String getCurrentTimestamp() {
164-
165-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
166-
return LocalDateTime.now().format(formatter);
52+
ProgressManager.getInstance().run(new RunSwanTask(project, "Detecting SRMs", true,
53+
PerformInBackgroundOption.ALWAYS_BACKGROUND, parameters));
16754
}
16855
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package de.fraunhofer.iem.swan.assist.actions;
2+
3+
import com.intellij.openapi.progress.PerformInBackgroundOption;
4+
import com.intellij.openapi.progress.ProgressIndicator;
5+
import com.intellij.openapi.progress.Task;
6+
import com.intellij.openapi.project.Project;
7+
import com.intellij.openapi.util.NlsContexts;
8+
import com.intellij.util.messages.MessageBus;
9+
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
10+
import de.fraunhofer.iem.swan.assist.util.Constants;
11+
import de.fraunhofer.iem.swan.cli.CliRunner;
12+
import de.fraunhofer.iem.swan.cli.SwanCli;
13+
import de.fraunhofer.iem.swan.cli.SwanOptions;
14+
import de.fraunhofer.iem.swan.io.dataset.SrmList;
15+
import de.fraunhofer.iem.swan.io.dataset.SrmListUtils;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.time.LocalDateTime;
22+
import java.time.format.DateTimeFormatter;
23+
import java.util.Arrays;
24+
import java.util.HashMap;
25+
26+
public class RunSwanTask extends Task.Backgroundable {
27+
28+
private Project project;
29+
private HashMap<String, String> parameters;
30+
private long duration;
31+
32+
public RunSwanTask(@Nullable Project project, @NlsContexts.ProgressTitle @NotNull String title, boolean canBeCancelled, @Nullable PerformInBackgroundOption backgroundOption, HashMap<String, String> parameters) {
33+
super(project, title, canBeCancelled, backgroundOption);
34+
this.parameters = parameters;
35+
this.project = project;
36+
}
37+
38+
@Override
39+
public void run(@NotNull ProgressIndicator indicator) {
40+
41+
long start = System.currentTimeMillis();
42+
43+
indicator.setText("Configuring SWAN");
44+
SwanOptions options = new CliRunner().initializeOptions();
45+
options.setTestDataDir(parameters.get(Constants.SOURCE_DIRECTORY));
46+
options.setOutputDir(parameters.get(Constants.OUTPUT_DIRECTORY));
47+
options.setToolkit(parameters.get(Constants.TOOLKIT).toLowerCase());
48+
options.setSrmClasses(Arrays.asList("source", "sink"));
49+
options.setPhase("predict");
50+
options.setTrainDataDir("");
51+
52+
53+
indicator.setText("Running SWAN");
54+
SwanCli swan = new SwanCli();
55+
System.out.println("--1--");
56+
try {
57+
System.out.println("--2--");
58+
swan.run(options);
59+
} catch (Exception e) {
60+
throw new RuntimeException(e);
61+
}
62+
63+
duration = System.currentTimeMillis() - start;
64+
int m = (int) (((duration / 1000) / 60) % 60);
65+
int s = (int) ((duration / 1000) % 60);
66+
System.out.println("--3--");
67+
indicator.setText("Exporting SRMs");
68+
System.out.println("--4--");
69+
String filename = parameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + "srm-" + getCurrentTimestamp() + ".json";
70+
SrmList srmList = swan.getSwanPipeline().getModelEvaluator().getPredictedSrmList();
71+
try {
72+
System.out.println("--5--");
73+
SrmListUtils.exportFile(srmList, filename);
74+
} catch (IOException e) {
75+
throw new RuntimeException(e);
76+
}
77+
78+
HashMap<String, String> results = new HashMap<>();
79+
results.put(Constants.OUTPUT_FILE, filename);
80+
results.put(Constants.OUTPUT_LOG, "");
81+
results.put(Constants.ANALYSIS_RESULT, srmList.getMethods().size() + " methods found in " + m + " mins " + s + " secs");
82+
83+
MessageBus messageBus = project.getMessageBus();
84+
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);
85+
publisher.launchSwan(results);
86+
}
87+
88+
/**
89+
* Get the timestamp in a specified format.
90+
*
91+
* @return Formatted date
92+
*/
93+
private String getCurrentTimestamp() {
94+
95+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
96+
return LocalDateTime.now().format(formatter);
97+
}
98+
99+
}

swan-assist/src/main/java/de/fraunhofer/iem/swan/assist/data/JSONFileLoader.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public class JSONFileLoader {
2626

27-
static private HashMap<String, MethodWrapper> methods= new HashMap<>();
27+
static private HashMap<String, MethodWrapper> methods = new HashMap<>();
2828
static private String congFile = "";
2929
static public final int NEW_METHOD = 0;
3030
static public final int EXISTING_METHOD = 1;
@@ -232,13 +232,18 @@ public static Set<Category> getCategories() {
232232
*/
233233
public static Set<Category> getAllCategories() {
234234

235-
236235
Set<Category> categorySet = new HashSet<>();
237236

237+
Set<Category> exclude = new HashSet<>();
238+
exclude.add(Category.CWETEST);
239+
exclude.add(Category.NONE);
240+
exclude.add(Category.RELEVANT);
241+
exclude.add(Category.AUTHENTICATION);
242+
exclude.add(Category.CWE_NONE);
243+
238244
for (Category category : Category.values()) {
239245

240-
if (!category.toString().equals(de.fraunhofer.iem.swan.data.Constants.NONE)
241-
&& !category.equals(Category.CWETEST)) {
246+
if (!exclude.contains(category)) {
242247
categorySet.add(category);
243248
}
244249
}

0 commit comments

Comments
 (0)