Skip to content

Commit 6ab73dc

Browse files
mmalohlavatomkraljevic
authored andcommitted
POJO: Store content of NAMES field in a separated class and
fill it recursivelly.
1 parent e65d70b commit 6ab73dc

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

src/main/java/water/Model.java

+10-31
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ public SB toJava( SB sb ) {
626626
sb.nl();
627627
sb.p("public class ").p(modelName).p(" extends water.genmodel.GeneratedModel {").nl(); // or extends GenerateModel
628628
toJavaInit(sb, fileContextSB).nl();
629-
toJavaNAMES(sb);
629+
toJavaNAMES(sb, fileContextSB);
630630
toJavaNCLASSES(sb);
631631
toJavaDOMAINS(sb, fileContextSB);
632632
toJavaPROB(sb);
@@ -636,15 +636,7 @@ public SB toJava( SB sb ) {
636636
sb.p(fileContextSB).nl(); // Append file
637637
return sb;
638638
}
639-
// Same thing as toJava, but as a Javassist CtClass
640-
private CtClass makeCtClass() throws CannotCompileException {
641-
CtClass clz = ClassPool.getDefault().makeClass(JCodeGen.toJavaId(_key.toString()));
642-
clz.addField(CtField.make(toJavaNAMES (new SB()).toString(),clz));
643-
clz.addField(CtField.make(toJavaNCLASSES(new SB()).toString(),clz));
644-
toJavaInit(clz); // Model-specific top-level goodness
645-
clz.addMethod(CtMethod.make(toJavaPredict(new SB(), new SB()).toString(),clz)); // FIX ME
646-
return clz;
647-
}
639+
648640
/** Generate implementation for super class. */
649641
protected SB toJavaSuper( SB sb ) {
650642
sb.nl();
@@ -656,12 +648,14 @@ protected SB toJavaSuper( SB sb ) {
656648

657649
return sb;
658650
}
659-
private SB toJavaNAMES( SB sb ) {
660-
//
661-
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
662-
return _names.length < limit ?
663-
JCodeGen.toStaticVar(sb, "NAMES", _names, "Names of columns used by model.") :
664-
JCodeGen.toStaticVar(sb, "NAMES", JCodeGen.EMPTY_SA, "Names of columns used by model. WARNING: It is too large to be generated!");
651+
private SB toJavaNAMES(SB sb, SB fileContextSB) {
652+
String namesHolderClassName = "NamesHolder";
653+
sb.i().p("// ").p("Names of columns used by model.").nl();
654+
sb.i().p("public static final String[] NAMES = NamesHolder.VALUES;").nl();
655+
// Generate class which fills the names into array
656+
fileContextSB.i().p("// The class representing training column names ").nl();
657+
JCodeGen.toClassWithArray(fileContextSB, null, namesHolderClassName, _names);
658+
return sb;
665659
}
666660
protected SB toJavaNCLASSES( SB sb ) { return isClassifier() ? JCodeGen.toStaticVar(sb, "NCLASSES", nclasses(), "Number of output classes included in training data response column.") : sb; }
667661
private SB toJavaDOMAINS( SB sb, SB fileContextSB ) {
@@ -718,21 +712,6 @@ private SB toJavaPredict(SB ccsb, SB fileCtxSb) { // ccsb = classContext
718712

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

721-
// Convenience method for testing: build Java, convert it to a class &
722-
// execute it: compare the results of the new class's (JIT'd) scoring with
723-
// the built-in (interpreted) scoring on this dataset. Throws if there
724-
// is any error (typically an AssertionError).
725-
public void testJavaScoring( Frame fr ) {
726-
try {
727-
//System.out.println(toJava());
728-
Class clz = ClassPool.getDefault().toClass(makeCtClass());
729-
Object modelo = clz.newInstance();
730-
}
731-
catch( CannotCompileException cce ) { throw new Error(cce); }
732-
catch( InstantiationException cce ) { throw new Error(cce); }
733-
catch( IllegalAccessException cce ) { throw new Error(cce); }
734-
}
735-
736715
/** Generates code which unify preds[1,...NCLASSES] */
737716
protected void toJavaUnifyPreds(SB bodySb) {
738717
}

0 commit comments

Comments
 (0)