Search in sources :

Example 1 with ModelMojoWriter

use of hex.ModelMojoWriter in project h2o-3 by h2oai.

the class ModelsHandler method exportMojo.

public ModelExportV3 exportMojo(int version, ModelExportV3 mexport) {
    Model model = getFromDKV("model_id", mexport.model_id.key());
    try {
        // Really file, not dir
        URI targetUri = FileUtils.getURI(mexport.dir);
        Persist p = H2O.getPM().getPersistForURI(targetUri);
        OutputStream os = p.create(targetUri.toString(), mexport.force);
        ModelMojoWriter mojo = model.getMojo();
        mojo.writeTo(os);
        // Send back
        mexport.dir = "file".equals(targetUri.getScheme()) ? new File(targetUri).getCanonicalPath() : targetUri.toString();
    } catch (IOException e) {
        throw new H2OIllegalArgumentException("dir", "exportModel", e);
    }
    return mexport;
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel) ModelMojoWriter(hex.ModelMojoWriter) Persist(water.persist.Persist) URI(java.net.URI)

Example 2 with ModelMojoWriter

use of hex.ModelMojoWriter in project h2o-3 by h2oai.

the class ModelBuildersHandler method detectMojoVersion.

private String detectMojoVersion(ModelBuilder builder) {
    Class<? extends Model> modelClass = ReflectionUtils.findActualClassParameter(builder.getClass(), 0);
    try {
        Method getMojoMethod = modelClass.getDeclaredMethod("getMojo");
        Class<?> retClass = getMojoMethod.getReturnType();
        if (retClass == ModelMojoWriter.class || !ModelMojoWriter.class.isAssignableFrom(retClass))
            throw new RuntimeException("Method getMojo() in " + modelClass + " must return the concrete implementation " + "of the ModelMojoWriter class. The return type is declared as " + retClass);
        try {
            ModelMojoWriter mmw = (ModelMojoWriter) retClass.newInstance();
            return mmw.mojoVersion();
        } catch (InstantiationException e) {
            throw getMissingCtorException(retClass, e);
        } catch (IllegalAccessException e) {
            throw getMissingCtorException(retClass, e);
        }
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Model class " + modelClass + " is expected to have method getMojo();");
    }
}
Also used : ModelMojoWriter(hex.ModelMojoWriter) Method(java.lang.reflect.Method)

Aggregations

ModelMojoWriter (hex.ModelMojoWriter)2 Model (hex.Model)1 MojoModel (hex.genmodel.MojoModel)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 Persist (water.persist.Persist)1