jpmml / jpmml-transpiler

Java Transpiler (Translator + Compiler) API for PMML
GNU Affero General Public License v3.0
28 stars 2 forks source link

convert pmml to evalutor by code #2

Closed kobeliuke closed 4 years ago

kobeliuke commented 4 years ago

as subject, sometimes i want to convert the pmml to a java evalutor object directly, any api can help, thanks a lot

vruusmann commented 4 years ago

i want to convert the pmml to a java evalutor object directly

What do you mean by "directly"?

Please see the "Usage" section of the README file. What's wrong with the proposed transpilation (aka conversion) procedure - read PMML XML file, perform in-memory transpilation, and then use the resulting org.jpmml.evaluator.Evaluator instance for evaluation?

kobeliuke commented 4 years ago

i want to convert the pmml to a java evalutor object directly

What do you mean by "directly"?

Thanks for your reply, i mean: if i want to use this transpiler,may be i need do something like this: java -jar jpmml-transpiler-executable-1.0-SNAPSHOT.jar --xml-input LightGBMAudit.pmml --jar-output LightGBMAudit.pmml.jar

i wanna to transform pmml like this:

 PMML xmlPmml;
try(InputStream  is = ...){
    xmlPmml = PMMLUtil.unmarshal(is);
}
JCodeModel codeModel = TranspilerUtil.transpile(xmlPmml, "com.mycompany.MyModel");
TranspilerUtil.compile(codeModel);

after this ,The README file tell me output the codeModel as a jar, and in docker env, this is not easy, so i wanna to This API: new ServiceLoadingModelEvaluatorBuilder().loadService(pmmlURL),

this just provide a pmml url, how can i load a JavaModelEvaluator with code , not url

vruusmann commented 4 years ago

The JPMML-CodeModel library (is a transitive dependency of the JPMML-Transpiler library) provides class org.jpmml.codemodel.JCodeModelClassLoader, which defines an in-memory Java class loader object based on a JCodeModel object:

JCodeModel codeModel = TranspilerUtil.transpile(..);
TranspilerUtil.compile(codeModel);

// THIS!
ClassLoader classLoader = new JCodeModelClassLoader(codeModel);

You can then use this JCodeModelClassLoader in place of URLClassLoader:

EvaluatorBuilder evaluatorBuilder = new ServiceLoadingModelEvaluatorBuilder()
  .loadService(classLoader, null);

Evaluator evaluator = evaluatorBuilder.build();
kobeliuke commented 4 years ago

Thanks you very much !!!!

vruusmann commented 4 years ago

However, when using JCodeModelClassLoader, then there is a risk that the original JCodeModel object will be staying in the RAM for as long as the Evaluator object exists.

If you manage to dump the JCodeModel object to a JAR file, then this breaks the "chain of reference", and JVM will be able to garbage-collect the original JCodeModel object (can be huge, many times greater in size than the PMML class model object).

kobeliuke commented 4 years ago

Thanks a lot, it have a great help,appreciate it.