77
88package de .fraunhofer .iem .swan .assist .actions ;
99
10- import com .intellij .openapi .progress .ProgressIndicator ;
10+ import com .intellij .openapi .progress .PerformInBackgroundOption ;
1111import com .intellij .openapi .progress .ProgressManager ;
12- import com .intellij .openapi .progress .Task ;
1312import com .intellij .openapi .project .Project ;
14- import com .intellij .util .messages .MessageBus ;
15- import de .fraunhofer .iem .swan .assist .comm .SwanNotifier ;
1613import 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-
2514import java .io .File ;
26- import java .io .IOException ;
27- import java .time .LocalDateTime ;
28- import java .time .format .DateTimeFormatter ;
2915import java .util .HashMap ;
3016import 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}
0 commit comments