Skip to content

Commit 4b4f3d5

Browse files
committed
Refactor module that runs SWAN
1 parent 54587d8 commit 4b4f3d5

File tree

4 files changed

+123
-126
lines changed

4 files changed

+123
-126
lines changed

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/ui/dialog/RunAnalysisDialog.form

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
</component>
149149
</children>
150150
</grid>
151-
<grid id="7ce0b" binding="toolkitPanel" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
151+
<grid id="7ce0b" binding="toolkitPanel" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
152152
<margin top="0" left="0" bottom="0" right="0"/>
153153
<constraints>
154154
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -181,6 +181,11 @@
181181
<text resource-bundle="dialog_messages" key="Launcher.mekaRadioButton"/>
182182
</properties>
183183
</component>
184+
<hspacer id="5d45f">
185+
<constraints>
186+
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
187+
</constraints>
188+
</hspacer>
184189
</children>
185190
</grid>
186191
</children>

swan-assist/src/main/java/de/fraunhofer/iem/swan/assist/ui/dialog/RunAnalysisDialog.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import com.intellij.openapi.ui.popup.JBPopupFactory;
1616
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
1717
import de.fraunhofer.iem.swan.assist.util.Constants;
18+
import org.apache.commons.io.FileUtils;
1819
import org.jetbrains.annotations.Nullable;
1920

2021
import javax.swing.*;
2122
import javax.swing.filechooser.FileNameExtensionFilter;
2223
import java.awt.*;
23-
import java.awt.event.*;
24+
import java.awt.event.ActionEvent;
25+
import java.awt.event.ActionListener;
2426
import java.io.File;
2527
import java.io.IOException;
2628
import java.io.InputStream;
@@ -87,13 +89,13 @@ public RunAnalysisDialog(Project project, boolean modal) {
8789
File configurationFile = new File(JSONFileLoader.getConfigurationFile(true));
8890

8991
//Use value for source path, if available. Otherwise set the default path
90-
if (PropertiesComponent.getInstance(project).isValueSet(Constants.SOURCE_DIRECTORY))
91-
sourceDirTextbox.setText(project.getBasePath()+"/target");
92+
if (!PropertiesComponent.getInstance(project).isValueSet(Constants.SOURCE_DIRECTORY))
93+
sourceDirTextbox.setText(project.getBasePath() + "/target/classes");
9294
else
9395
sourceDirTextbox.setText(PropertiesComponent.getInstance(project).getValue(Constants.SOURCE_DIRECTORY));
9496

9597
//Use value for the output path, if available. Otherwise set the default path
96-
if (PropertiesComponent.getInstance(project).isValueSet(Constants.OUTPUT_DIRECTORY))
98+
if (!PropertiesComponent.getInstance(project).isValueSet(Constants.OUTPUT_DIRECTORY))
9799
outputDir.setText(project.getBasePath() + File.separator + config.getProperty("output_dir_name"));
98100
else
99101
outputDir.setText(PropertiesComponent.getInstance(project).getValue(Constants.OUTPUT_DIRECTORY));
@@ -190,7 +192,9 @@ protected void doOKAction() {
190192
if (isOKActionEnabled()) {
191193

192194
//ensure that required fields are populated
193-
if (sourceDirTextbox.getText().trim().isEmpty() || outputDir.getText().isEmpty()) {
195+
if (sourceDirTextbox.getText().trim().isEmpty() || outputDir.getText().isEmpty()
196+
|| !(new File(sourceDirTextbox.getText().trim()).exists())
197+
|| (FileUtils.listFiles(new File(sourceDirTextbox.getText()), new String[]{"class", "jar"}, true)).size() == 0) {
194198

195199
JBPopupFactory.getInstance()
196200
.createHtmlTextBalloonBuilder(resourceBundle.getString("Messages.Error.PathNotFound"), MessageType.ERROR, null)
@@ -205,6 +209,8 @@ protected void doOKAction() {
205209
} else {
206210

207211
setParameters();
212+
//Notification analysisCompleted = new Notification(Constants.PLUGIN_GROUP_DISPLAY_ID, "Starting Analysis", "Analysis completed", NotificationType.INFORMATION);
213+
//analysisCompleted.notify();
208214
}
209215
}
210216
}
@@ -226,9 +232,9 @@ private void setParameters() {
226232
//set the configuration file to default
227233
parameters.put(Constants.CONFIGURATION_FILE, config.getProperty("swan_default_param_value"));
228234

229-
if(!MEKARadioButton.isSelected()){
235+
if (MEKARadioButton.isSelected()) {
230236
parameters.put(Constants.TOOLKIT, MEKARadioButton.getText());
231-
}else{
237+
} else {
232238
parameters.put(Constants.TOOLKIT, WEKARadioButton.getText());
233239
}
234240
parameters.put(Constants.SOURCE_DIRECTORY, sourceDirTextbox.getText());

0 commit comments

Comments
 (0)