-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPMMLConverter.java
73 lines (67 loc) · 3.02 KB
/
PMMLConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.hpccsystems.pmml2ecl;
import java.util.LinkedList;
import com.hpccsystems.pmml2ecl.ecl.ECLElement;
import com.hpccsystems.pmml2ecl.ecl.ECLParser;
import com.hpccsystems.pmml2ecl.pmml.PMMLElement;
import com.hpccsystems.pmml2ecl.pmml.PMMLParser;
import com.hpccsystems.pmml2ecl.pmml.algorithms.ClassificationForest;
import com.hpccsystems.pmml2ecl.pmml.algorithms.LinearRegression;
import com.hpccsystems.pmml2ecl.pmml.algorithms.LogisticRegression;
public class PMMLConverter {
private LinkedList<ECLElement> ecl;
/**
* Intakes a file path for a .xml/pmml file to convert into a .ecl file
* with the corresponding ML model as a variable. The final file resides in /output.
* @param absoluteFilePath the path of the .xml/.pmml file to convert.
* @throws Exception
*/
public PMMLConverter(String absoluteFilePath) throws Exception {
PMMLParser parser = new PMMLParser(absoluteFilePath);
PMMLElement root = parser.getRoot();
PMMLElement model = root.firstNodeWithKey("algorithmName");
String functionName = model.getValue("algorithmName");
ecl = new LinkedList<>();
ecl.add(new ECLElement("IMPORT ML_Core;"));
ecl.add(new ECLElement("IMPORT ML_Core.Types;"));
ecl.add(new ECLElement("IMPORT ML_Core.ModelOps2 as ModelOps2;"));
switch (functionName) {
case "LinearRegression":
ecl.addAll(new LinearRegression(model).getEclFromModel());
break;
case "LogisticRegression":
ecl.addAll(new LogisticRegression(model).getEclFromModel());
break;
case "randomForest":
ecl.addAll(new ClassificationForest(root.firstNodeWithTag("Segmentation")).getEclFromModel());
break;
default:
break;
}
ECLParser.writeToFile(ecl);
}
public PMMLConverter(String absoluteFilePath, String outputPath) throws Exception {
PMMLParser parser = new PMMLParser(absoluteFilePath);
PMMLElement root = parser.getRoot();
PMMLElement model = root.firstNodeWithKey("algorithmName");
String functionName = model.getValue("algorithmName");
ecl = new LinkedList<>();
ecl.add(new ECLElement("IMPORT ML_Core;"));
ecl.add(new ECLElement("IMPORT ML_Core.Types;"));
ecl.add(new ECLElement("IMPORT ML_Core.ModelOps2 as ModelOps2;"));
switch (functionName) {
case "LinearRegression":
ecl.addAll(new LinearRegression(model).getEclFromModel());
break;
case "LogisticRegression":
ecl.addAll(new LogisticRegression(model).getEclFromModel());
break;
case "randomForest":
ecl.addAll(new ClassificationForest(model).getEclFromModel());
break;
default:
ecl.add(new ECLElement("OUTPUT('Unable to parse stored model.');"));
break;
}
ECLParser.writeToFile(ecl, outputPath);
}
}