Search in sources :

Example 1 with Assembly

use of water.rapids.Assembly in project h2o-3 by h2oai.

the class AssemblyHandler method fit.

public AssemblyV99 fit(int version, AssemblyV99 ass) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    if (ass == null)
        return null;
    if (ass.steps == null)
        return ass;
    // process assembly:
    //   of the form [name__class__ast__inplace__names, name__class__ast__inplace__names, ...]
    // s[0] : stepName
    // s[1] : transform class
    // s[2] : ast (can be noop)
    // s[3] : inplace
    // s[4] : names
    ArrayList<Transform> steps = new ArrayList<>();
    for (String step : ass.steps) {
        String[] s = step.split("__");
        Class transformClass = Class.forName("water.rapids.transforms." + s[1]);
        Class[] constructorTypes = new Class[] { String.class, /*name*/
        String.class, /*ast*/
        boolean.class, /*inplace*/
        String[].class };
        Object[] constructorArgs = new Object[] { s[0], s[2], Boolean.valueOf(s[3]), s[4].equals("|") ? null : s[4].split("\\|") };
        steps.add((Transform) transformClass.getConstructor(constructorTypes).newInstance(constructorArgs));
    }
    Assembly assembly = new Assembly(Key.make("assembly_" + Key.make().toString()), steps.toArray(new Transform[steps.size()]));
    ass.result = new KeyV3.FrameKeyV3(assembly.fit((Frame) DKV.getGet(ass.frame.key()))._key);
    ass.assembly = new KeyV3.AssemblyKeyV3(assembly._key);
    DKV.put(assembly);
    return ass;
}
Also used : Frame(water.fvec.Frame) ArrayList(java.util.ArrayList) KeyV3(water.api.schemas3.KeyV3) Transform(water.rapids.transforms.Transform) Assembly(water.rapids.Assembly)

Example 2 with Assembly

use of water.rapids.Assembly in project h2o-3 by h2oai.

the class RequestServer method serveSchema.

private static NanoResponse serveSchema(Schema s, RequestType type) {
    // Convert Schema to desired output flavor
    String http_response_header = H2OError.httpStatusHeader(HttpResponseStatus.OK.getCode());
    // If we're given an http response code use it.
    if (s instanceof SpecifiesHttpResponseCode) {
        http_response_header = H2OError.httpStatusHeader(((SpecifiesHttpResponseCode) s).httpStatus());
    }
    // If we've gotten an error always return the error as JSON
    if (s instanceof SpecifiesHttpResponseCode && HttpResponseStatus.OK.getCode() != ((SpecifiesHttpResponseCode) s).httpStatus()) {
        type = RequestType.json;
    }
    if (s instanceof H2OErrorV3) {
        return new NanoResponse(http_response_header, MIME_JSON, s.toJsonString());
    }
    if (s instanceof StreamingSchema) {
        StreamingSchema ss = (StreamingSchema) s;
        NanoResponse r = new NanoStreamResponse(http_response_header, MIME_DEFAULT_BINARY, ss.getStreamWriter());
        // Needed to make file name match class name
        r.addHeader("Content-Disposition", "attachment; filename=\"" + ss.getFilename() + "\"");
        return r;
    }
    // TODO: remove this entire switch
    switch(type) {
        // return JSON for html requests
        case html:
        case json:
            return new NanoResponse(http_response_header, MIME_JSON, s.toJsonString());
        case xml:
            throw H2O.unimpl("Unknown type: " + type.toString());
        case java:
            if (s instanceof AssemblyV99) {
                // TODO: fix the AssemblyV99 response handler so that it produces the appropriate StreamingSchema
                Assembly ass = DKV.getGet(((AssemblyV99) s).assembly_id);
                NanoResponse r = new NanoResponse(http_response_header, MIME_DEFAULT_BINARY, ass.toJava(((AssemblyV99) s).pojo_name));
                r.addHeader("Content-Disposition", "attachment; filename=\"" + JCodeGen.toJavaId(((AssemblyV99) s).pojo_name) + ".java\"");
                return r;
            } else {
                throw new H2OIllegalArgumentException("Cannot generate java for type: " + s.getClass().getSimpleName());
            }
        default:
            throw H2O.unimpl("Unknown type to serveSchema(): " + type);
    }
}
Also used : AssemblyV99(water.api.schemas99.AssemblyV99) H2OErrorV3(water.api.schemas3.H2OErrorV3) Assembly(water.rapids.Assembly)

Aggregations

Assembly (water.rapids.Assembly)2 ArrayList (java.util.ArrayList)1 H2OErrorV3 (water.api.schemas3.H2OErrorV3)1 KeyV3 (water.api.schemas3.KeyV3)1 AssemblyV99 (water.api.schemas99.AssemblyV99)1 Frame (water.fvec.Frame)1 Transform (water.rapids.transforms.Transform)1