Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tutorial for using Hadrian from Java #36

Open
eyala opened this issue May 23, 2017 · 4 comments
Open

Add tutorial for using Hadrian from Java #36

eyala opened this issue May 23, 2017 · 4 comments

Comments

@eyala
Copy link

eyala commented May 23, 2017

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!)

@ghost
Copy link

ghost commented Sep 8, 2017

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.

@AlexanderFillbrunn
Copy link

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?

@hannsta
Copy link

hannsta commented Oct 11, 2017

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.

@ethanyzhang
Copy link

ethanyzhang commented Mar 29, 2018

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"}}
             }
        }
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants