Skip to content

Commit 8ef96a9

Browse files
refactoring to avoid long parameters list smell and towards JSON
1 parent 15fd275 commit 8ef96a9

13 files changed

Lines changed: 91 additions & 16 deletions

foofile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414

1515
# Spliting dataset between features (X) and label (y)
16-
X = df.drop(columns=["varietyyy"])
17-
y = df["varietyyy"]
16+
X = df.drop(columns=["variety"])
17+
y = df["variety"]
1818

1919
# pandas dataframe operations :
2020
# https://pandas.pydata.org/pandas-docs/stable/reference/frame.html

mml.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"file_path" : "iris.csv",
3+
"target" : "variety"
4+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@
2222
<artifactId>guava</artifactId>
2323
<version>30.1-jre</version>
2424
</dependency>
25+
<!-- https://mvnrepository.com/artifact/org.json/json -->
26+
<dependency>
27+
<groupId>org.json</groupId>
28+
<artifactId>json</artifactId>
29+
<version>20200518</version>
30+
</dependency>
2531
</dependencies>
2632
</project>

src/main/java/ConfigurationML.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
public class ConfigurationML {
3+
4+
private String file_path; // dataset
5+
private String target; // targeted feature (column name)
6+
7+
// TODO
8+
9+
public ConfigurationML(String file_path, String target) {
10+
this.file_path = file_path;
11+
this.target = target;
12+
}
13+
14+
15+
public String getFilePath() {
16+
return file_path;
17+
}
18+
19+
public String getTarget() {
20+
return target;
21+
}
22+
23+
}

src/main/java/MLExecutor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
public abstract class MLExecutor {
44

5-
public abstract void generateCode(String file_path, String target) throws IOException;
5+
protected ConfigurationML configuration;
6+
7+
public abstract void generateCode() throws IOException;
68
public abstract void run() throws IOException;
79

810
}

src/main/java/MMLMain.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Paths;
3+
4+
import org.json.JSONObject;
5+
16
public class MMLMain {
27

38

@@ -12,13 +17,30 @@ public static void main(String[] args) throws Exception {
1217
// System.err.println(args[0]);
1318
// System.err.println(args[1]);
1419

15-
TargetLanguage tl = TargetLanguage.PYTHON; // TargetLanguage.R
20+
TargetLanguage tl = TargetLanguage.PYTHON; // TargetLanguage.PYTHON; //
21+
22+
23+
//String str = Files.readString(Paths.get("mml.json"));
24+
25+
/*
26+
String str = "{ \"file_path\": \"iris.csv\", \"target\": 'variety' }";
27+
JSONObject obj = new JSONObject(str);
28+
String f = obj.getString("file_path");
29+
String t = obj.getString("target");
30+
ConfigurationML configuration = new ConfigurationML(f, t);
31+
*/
32+
33+
34+
// TODO: instead of command line arguments, we will use JSON files to configure the compilers
35+
ConfigurationML configuration = new ConfigurationML(args[0], args[1]);
1636
MLExecutor ex = null;
37+
38+
1739
if (tl == TargetLanguage.PYTHON) {
18-
ex = new PythonMLExecutor();
40+
ex = new PythonMLExecutor(configuration);
1941
}
2042
else if (tl == TargetLanguage.R) {
21-
ex = new RLanguageMLExecutor();
43+
ex = new RLanguageMLExecutor(configuration);
2244
}
2345

2446
else if (tl == TargetLanguage.JULIA) {
@@ -32,8 +54,8 @@ else if (tl == TargetLanguage.JULIA) {
3254
// TODO
3355
}
3456

35-
// TODO: instead of command line arguments, we will use JSON files to configure the compilers
36-
ex.generateCode(args[0], args[1]);
57+
58+
ex.generateCode();
3759
ex.run();
3860

3961

src/main/java/PythonMLExecutor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77

88
public class PythonMLExecutor extends MLExecutor {
99

10-
private final String PYTHON_OUTPUT = "foofile.py";
10+
private final String PYTHON_OUTPUT = "foofile.py";
11+
12+
public PythonMLExecutor(ConfigurationML configuration) {
13+
this.configuration = configuration;
14+
}
1115

1216
// TODO: refactoring of the code is needed since anti-pattern/bad smell https://fr.wikipedia.org/wiki/Code_smell#Long_Parameter_List
13-
public void generateCode(String file_path, String target) throws IOException {
17+
public void generateCode() throws IOException {
18+
19+
String file_path = configuration.getFilePath();
20+
String target = configuration.getTarget();
21+
22+
1423
// Python code
1524
String pythonCode = "import pandas as pd\n"
1625
+ "from sklearn.model_selection import train_test_split\n"

src/main/java/RLanguageMLExecutor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
public class RLanguageMLExecutor extends MLExecutor {
99

1010
private static final String R_OUTPUT = "foofile.R";
11+
12+
public RLanguageMLExecutor(ConfigurationML configuration) {
13+
this.configuration = configuration;
14+
}
1115

12-
public void generateCode(String file_path, String target) throws IOException {
16+
public void generateCode() throws IOException {
17+
18+
String file_path = configuration.getFilePath();
19+
String target = configuration.getTarget();
20+
1321
// R code
1422
String Rcode = "library(rpart)\n"
1523
+ "\n"

src/test/java/MMLPythonTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ public class MMLPythonTest {
66
@Test
77
public void testPython1() throws Exception {
88

9-
MLExecutor ex = new PythonMLExecutor();
10-
ex.generateCode("iris.csv", "variety");
9+
ConfigurationML conf = new ConfigurationML("iris.csv", "variety");
10+
MLExecutor ex = new PythonMLExecutor(conf);
11+
ex.generateCode();
1112
ex.run();
1213
// TODO: check assertions over return value (eg it is indeed a float value)
1314

1415
}
1516

1617
@Test
1718
public void testPython2() throws Exception {
18-
19-
MLExecutor ex = new PythonMLExecutor();
20-
ex.generateCode("iris.csv", "varietyyy");
19+
ConfigurationML conf = new ConfigurationML("iris.csv", "variety");
20+
MLExecutor ex = new PythonMLExecutor(conf);
21+
ex.generateCode();
2122
ex.run();
2223
// TODO: should raise an exception
2324

target/classes/MLExecutor.class

2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)