Skip to content

Commit

Permalink
POJO: Store content of NAMES field in a separated class and
Browse files Browse the repository at this point in the history
fill it recursivelly.
  • Loading branch information
mmalohlava authored and tomkraljevic committed Jun 1, 2015
1 parent e65d70b commit 6ab73dc
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions src/main/java/water/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ public SB toJava( SB sb ) {
sb.nl();
sb.p("public class ").p(modelName).p(" extends water.genmodel.GeneratedModel {").nl(); // or extends GenerateModel
toJavaInit(sb, fileContextSB).nl();
toJavaNAMES(sb);
toJavaNAMES(sb, fileContextSB);
toJavaNCLASSES(sb);
toJavaDOMAINS(sb, fileContextSB);
toJavaPROB(sb);
Expand All @@ -636,15 +636,7 @@ public SB toJava( SB sb ) {
sb.p(fileContextSB).nl(); // Append file
return sb;
}
// Same thing as toJava, but as a Javassist CtClass
private CtClass makeCtClass() throws CannotCompileException {
CtClass clz = ClassPool.getDefault().makeClass(JCodeGen.toJavaId(_key.toString()));
clz.addField(CtField.make(toJavaNAMES (new SB()).toString(),clz));
clz.addField(CtField.make(toJavaNCLASSES(new SB()).toString(),clz));
toJavaInit(clz); // Model-specific top-level goodness
clz.addMethod(CtMethod.make(toJavaPredict(new SB(), new SB()).toString(),clz)); // FIX ME
return clz;
}

/** Generate implementation for super class. */
protected SB toJavaSuper( SB sb ) {
sb.nl();
Expand All @@ -656,12 +648,14 @@ protected SB toJavaSuper( SB sb ) {

return sb;
}
private SB toJavaNAMES( SB sb ) {
//
int limit = ((1<<16) /* Max size of class */ - 4 * 500 /* Free space for loading any static stuff */ ) / (4*2*2); // Static initialized needs 4 instructions to load String from constant pool + load ColInfo
return _names.length < limit ?
JCodeGen.toStaticVar(sb, "NAMES", _names, "Names of columns used by model.") :
JCodeGen.toStaticVar(sb, "NAMES", JCodeGen.EMPTY_SA, "Names of columns used by model. WARNING: It is too large to be generated!");
private SB toJavaNAMES(SB sb, SB fileContextSB) {
String namesHolderClassName = "NamesHolder";
sb.i().p("// ").p("Names of columns used by model.").nl();
sb.i().p("public static final String[] NAMES = NamesHolder.VALUES;").nl();
// Generate class which fills the names into array
fileContextSB.i().p("// The class representing training column names ").nl();
JCodeGen.toClassWithArray(fileContextSB, null, namesHolderClassName, _names);
return sb;
}
protected SB toJavaNCLASSES( SB sb ) { return isClassifier() ? JCodeGen.toStaticVar(sb, "NCLASSES", nclasses(), "Number of output classes included in training data response column.") : sb; }
private SB toJavaDOMAINS( SB sb, SB fileContextSB ) {
Expand Down Expand Up @@ -718,21 +712,6 @@ private SB toJavaPredict(SB ccsb, SB fileCtxSb) { // ccsb = classContext

protected String toJavaDefaultMaxIters() { return "-1"; }

// Convenience method for testing: build Java, convert it to a class &
// execute it: compare the results of the new class's (JIT'd) scoring with
// the built-in (interpreted) scoring on this dataset. Throws if there
// is any error (typically an AssertionError).
public void testJavaScoring( Frame fr ) {
try {
//System.out.println(toJava());
Class clz = ClassPool.getDefault().toClass(makeCtClass());
Object modelo = clz.newInstance();
}
catch( CannotCompileException cce ) { throw new Error(cce); }
catch( InstantiationException cce ) { throw new Error(cce); }
catch( IllegalAccessException cce ) { throw new Error(cce); }
}

/** Generates code which unify preds[1,...NCLASSES] */
protected void toJavaUnifyPreds(SB bodySb) {
}
Expand Down

0 comments on commit 6ab73dc

Please sign in to comment.