Skip to content

Commit a5448f2

Browse files
committed
Implement suggest algorithm
1 parent c05d9af commit a5448f2

File tree

9 files changed

+403
-91
lines changed

9 files changed

+403
-91
lines changed

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/SuggestAction.java

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+
*
4+
* Contributors:
5+
* Goran Piskachev (goran.piskachev@iem.fraunhofer.de) - initial implementation
6+
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - plugin integration
7+
******************************************************************************/
8+
9+
package de.fraunhofer.iem.swan.assist.actions.suggest;
10+
11+
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
12+
import de.fraunhofer.iem.swan.data.Method;
13+
14+
public class MethodPair {
15+
private MethodWrapper m1;
16+
private MethodWrapper m2;
17+
18+
/**
19+
* Initializes MethodPair object.
20+
* @param m1 first method
21+
* @param m2 second method
22+
*/
23+
MethodPair(MethodWrapper m1, MethodWrapper m2) {
24+
this.m1 = m1;
25+
this.m2 = m2;
26+
}
27+
28+
/**
29+
*
30+
* @return first method of pair.
31+
*/
32+
MethodWrapper getMethod1() {
33+
return m1;
34+
}
35+
36+
/**
37+
*
38+
* @return second method of pair.
39+
*/
40+
MethodWrapper getMethod2() {
41+
return m2;
42+
}
43+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+
*
4+
* Contributors:
5+
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - initial implementation
6+
******************************************************************************/
7+
8+
package de.fraunhofer.iem.swan.assist.actions.suggest;
9+
10+
import com.intellij.ide.util.PropertiesComponent;
11+
import com.intellij.openapi.actionSystem.AnAction;
12+
import com.intellij.openapi.actionSystem.AnActionEvent;
13+
import com.intellij.openapi.actionSystem.CommonDataKeys;
14+
import com.intellij.openapi.project.Project;
15+
import com.intellij.openapi.ui.DialogWrapper;
16+
import com.intellij.util.messages.MessageBus;
17+
import de.fraunhofer.iem.swan.assist.comm.SuggestNotifier;
18+
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
19+
import de.fraunhofer.iem.swan.assist.ui.dialog.SwanLauncherDialog;
20+
import de.fraunhofer.iem.swan.assist.util.Constants;
21+
22+
import java.io.File;
23+
import java.util.HashMap;
24+
25+
public class SuggestAction extends AnAction {
26+
27+
/**
28+
* Obtains suggested methods from SWAN and loads them in a dialog for user classification.
29+
*
30+
* @param e source event
31+
*/
32+
@Override
33+
public void actionPerformed(AnActionEvent e) {
34+
35+
final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
36+
MessageBus messageBus = project.getMessageBus();
37+
38+
//Set output directory for plugin, if not set
39+
if (!PropertiesComponent.getInstance(project).isValueSet(Constants.SWAN_OUTPUT_DIR)) {
40+
41+
setProjectPath(project);
42+
} else {
43+
File trainFile = new File(PropertiesComponent.getInstance(project).getValue(Constants.SWAN_OUTPUT_DIR));
44+
45+
if (!trainFile.exists())
46+
setProjectPath(project);
47+
}
48+
49+
SuggestThread suggestThread = new SuggestThread(project,
50+
"/Users/oshando/Projects/IdeaProjects/mois-evaluation/mois-rq3/resources/gxa-methods-r.json",
51+
"/Users/oshando/Projects/IdeaProjects/mois-evaluation/mois-executor/project-jars/gxa"
52+
);
53+
suggestThread.start();
54+
55+
SuggestNotifier suggestNotifier = messageBus.syncPublisher(SuggestNotifier.SUGGEST_METHOD_TOPIC);
56+
suggestNotifier.startSuggestMethod();
57+
}
58+
59+
private void setProjectPath(Project project) {
60+
61+
//Launch SWAN Properties Dialog
62+
SwanLauncherDialog dialog = new SwanLauncherDialog(project, true);
63+
dialog.show();
64+
65+
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
66+
67+
HashMap<String, String> swanParameters = dialog.getParameters();
68+
69+
File outputFolder = new File(swanParameters.get(Constants.SWAN_OUTPUT_DIR));
70+
71+
if(!outputFolder.exists())
72+
outputFolder.mkdir();
73+
74+
PropertiesComponent.getInstance(project).setValue(Constants.SWAN_OUTPUT_DIR, swanParameters.get(Constants.SWAN_OUTPUT_DIR));
75+
76+
77+
78+
79+
System.out.println("Set project path: : "+ swanParameters.get(Constants.SWAN_OUTPUT_DIR));
80+
}
81+
}
82+
83+
/**
84+
* Controls whether the action is enabled or disabled
85+
*
86+
* @param event source event
87+
*/
88+
@Override
89+
public void update(AnActionEvent event) {
90+
91+
//Disable/Enable action button
92+
if (JSONFileLoader.isFileSelected())
93+
event.getPresentation().setEnabled(true);
94+
else
95+
event.getPresentation().setEnabled(false);
96+
}
97+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+
*
4+
* Contributors:
5+
* Goran Piskachev (goran.piskachev@iem.fraunhofer.de) - initial implementation
6+
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - plugin integration
7+
******************************************************************************/
8+
9+
package de.fraunhofer.iem.swan.assist.actions.suggest;
10+
11+
import com.intellij.openapi.project.Project;
12+
import com.intellij.util.messages.MessageBus;
13+
import de.fraunhofer.iem.swan.FeatureHandler;
14+
import de.fraunhofer.iem.swan.IFeature;
15+
import de.fraunhofer.iem.swan.Parser;
16+
import de.fraunhofer.iem.swan.assist.comm.SuggestNotifier;
17+
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
18+
import de.fraunhofer.iem.swan.assist.data.TrainingFileManager;
19+
import de.fraunhofer.iem.swan.assist.ui.MethodListTree;
20+
import de.fraunhofer.iem.swan.data.Category;
21+
import de.fraunhofer.iem.swan.data.Method;
22+
23+
import java.util.HashMap;
24+
import java.util.HashSet;
25+
import java.util.Map;
26+
import java.util.Set;
27+
28+
public class SuggestThread extends Thread {
29+
30+
private Project project;
31+
private String configFilePath;
32+
private String projectPath;
33+
34+
SuggestThread(Project project, String filePath, String projectDirectory) {
35+
36+
this.project = project;
37+
this.configFilePath = filePath;
38+
this.projectPath = projectDirectory;
39+
}
40+
41+
/**
42+
* Executes code to suggest methods on a separate thread and returns the results.
43+
*/
44+
public void run() {
45+
46+
Map<Method, Set<IFeature>> matrix = new HashMap<Method, Set<IFeature>>();
47+
Set<Method> methods = new HashSet<Method>();
48+
49+
Parser parser = new Parser(configFilePath);
50+
parser.parse(configFilePath);
51+
methods = parser.methods();
52+
53+
FeatureHandler featureHandler = new FeatureHandler(projectPath);
54+
featureHandler.initializeFeatures(0);
55+
56+
Set<IFeature> features = featureHandler.features().get(Category.NONE);
57+
58+
for (Method m : methods) {
59+
Set<IFeature> mFeatures = new HashSet<IFeature>();
60+
for (IFeature f : features) {
61+
//If method conforms to the feature, add it to the matrix.
62+
if (f.applies(m).equals(IFeature.Type.TRUE))
63+
mFeatures.add(f);
64+
}
65+
matrix.put(m, mFeatures);
66+
}
67+
68+
Suggester suggester = new Suggester();
69+
Set<MethodWrapper> suggestedMethods = new HashSet<>();
70+
boolean methodPairFound = false;
71+
72+
//Get training methods
73+
Set<String> trainingMethods = new HashSet<>();
74+
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
75+
trainingMethods = trainingFileManager.getTrainingMethods().keySet();
76+
trainingMethods.addAll(MethodListTree.suggestedMethodsList);
77+
78+
while (!methodPairFound) {
79+
80+
MethodPair methodPair = suggester.suggestMethod(matrix, features);
81+
82+
if (methodPair != null) {
83+
84+
System.out.println("SUGMET: " + methodPair.getMethod1().getSignature(true) + " " + methodPair.getMethod2().getSignature(true));
85+
86+
if (trainingMethods.contains(methodPair.getMethod1().getSignature(true)) ||
87+
trainingMethods.contains(methodPair.getMethod2().getSignature(true))) {
88+
89+
System.out.println("skipping: " + methodPair.getMethod1().getSignature(true) + "\n" + methodPair.getMethod2().getSignature(true));
90+
continue;
91+
}
92+
93+
methodPair.getMethod1().setStatus(MethodWrapper.MethodStatus.SUGGESTED);
94+
suggestedMethods.add(methodPair.getMethod1());
95+
methodPair.getMethod2().setStatus(MethodWrapper.MethodStatus.SUGGESTED);
96+
suggestedMethods.add(methodPair.getMethod2());
97+
System.out.println("adding: ");
98+
methodPairFound = true;
99+
}
100+
}
101+
102+
MessageBus messageBus = project.getMessageBus();
103+
SuggestNotifier suggestNotifier = messageBus.syncPublisher(SuggestNotifier.SUGGEST_METHOD_TOPIC);
104+
suggestNotifier.endSuggestMethod(suggestedMethods);
105+
}
106+
}

0 commit comments

Comments
 (0)