Open eyala opened 7 years ago
This would be really nice to have as I am currently researching a way to convert Pmml to PFA using Hadrian in Java. However, there is very little documentation on java support.
We would like to integrate PFA into KNIME and are currently also struggling to use PFA in Java. Even calling the "static" PFAEngine.fromJson() function does not work. Could you provide some hints how we can create an instance of PFAEngine from a JSON file in Java?
We are currently integrating PFA, and the work around I found to this is to use the Engine factory from antinous.
PFAEngineFactory factory = new PFAEngineFactory();
PFAEngine<Object, Object> engine = factory.engineFromJson(PFAFile);
engine = factory.engineFromYaml(PFAFile);
However this requires that you use the entire hadrian-standalone, which is quite large. A better way would be greatly appreciated.
Here is an example using PFAEngine in Java, inspired by @hannsta's comment above:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.opendatagroup.antinous.pfainterface.PFAEngineFactory;
import com.opendatagroup.hadrian.jvmcompiler.PFAEngine;
public class Main {
public static String readJSON(String path) {
try {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded);
} catch (IOException e) {
return "";
}
}
public static void main(String[] args) {
PFAEngineFactory factory = new PFAEngineFactory();
String modelJSON = readJSON("model.pfa");
PFAEngine<Object, Object> engine = factory.engineFromJson(modelJSON);
String testDataJSON = readJSON("test.json");
Object output = engine.action(engine.jsonInput(testDataJSON));
System.out.println(engine.jsonOutput(output));
}
}
test.json:
{
"sepal_length_cm": 5.1,
"sepal_width_cm": 3.5,
"petal_length_cm": 1.4,
"petal_width_cm": 0.2,
"class": "Iris-setosa"
}
model.pfa:
{
"input": {"type": "record",
"name": "Iris",
"fields": [
{"name": "sepal_length_cm", "type": "double"},
{"name": "sepal_width_cm", "type": "double"},
{"name": "petal_length_cm", "type": "double"},
{"name": "petal_width_cm", "type": "double"},
{"name": "class", "type": "string"}
]},
"output": "string",
"action": [
{"if": {"<": ["input.petal_length_cm", 2.5]},
"then": {"string": "Iris-setosa"},
"else":
{"if": {"<": ["input.petal_length_cm", 4.8]},
"then": {"string": "Iris-versicolor"},
"else":
{"if": {"<": ["input.petal_width_cm", 1.7]},
"then": {"string": "Iris-versicolor"},
"else": {"string": "Iris-virginica"}}
}
}
]
}
You have tutorial for Scala - what about one for Java? Or is no one using Hadrian embedded in Java yet?
It would also be nice to add something to the Hadrian FAQ - for example, my previous question - does it work well in Java?
(thanks!)