Skip to content

Commit

Permalink
Merge pull request #98 from justinuliu/use-the-jre-running-weka
Browse files Browse the repository at this point in the history
Migrate AutoWeka to the JRE running WEKA
  • Loading branch information
Justin Liu authored Mar 4, 2022
2 parents 9f1f71d + 1bf9c90 commit ae87007
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Binary file added lib/jna-5.10.0.jar
Binary file not shown.
85 changes: 85 additions & 0 deletions src/java/autoweka/SetEnvironmentVariablesForAutoWeka.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package autoweka;

import com.sun.jna.Library;
import com.sun.jna.Native;

import java.io.File;

public class SetEnvironmentVariablesForAutoWeka {

public static LibCWrapper INSTANCE;

private static final Object library;

interface Msvcrt extends Library {
int _putenv(String name);

String getenv(String name);
}

interface LibC extends Library {
int setenv(String name, String value, int overwrite);

String getenv(String name);

int unsetenv(String name);
}

public static class LibCWrapper {

public int setenv(String name, String value, int overwrite) {
if (library instanceof LibC) {
return ((LibC) library).setenv(name, value, overwrite);
} else {
return ((Msvcrt) library)._putenv(name + "=" + value);
}
}

public String getenv(String name) {
if (library instanceof LibC) {
return ((LibC) library).getenv(name);
} else {
return ((Msvcrt) library).getenv(name);
}
}

public int unsetenv(String name) {
if (library instanceof LibC) {
return ((LibC) library).unsetenv(name);
} else {
return ((Msvcrt) library)._putenv(name + "=");
}
}
}

public static boolean needJavaOptions() {
String version = System.getProperty("java.version");
String major = version.split("\\.")[0];
return major.compareTo("1") > 0;
}

static {
if (System.getProperty("os.name").contains("Windows")) {
library = Native.load("msvcrt", Msvcrt.class);
} else {
library = Native.load("c", LibC.class);
}
INSTANCE = new LibCWrapper();

String javaHome = System.getProperty("java.home");
INSTANCE.setenv("JAVA_HOME", javaHome, 1);
String pathVar = INSTANCE.getenv("PATH");
if (pathVar == null) {
pathVar = "";
}
INSTANCE.setenv("PATH", javaHome + "/bin" + File.pathSeparatorChar + pathVar, 1);

if (needJavaOptions()) {
String existingOps = INSTANCE.getenv("_JAVA_OPTIONS");
if (existingOps == null) {
existingOps = "";
}
INSTANCE.setenv("_JAVA_OPTIONS", existingOps + " --add-opens=java.base/java.lang=ALL-UNNAMED", 1);
}
}
}
10 changes: 10 additions & 0 deletions src/java/weka/classifiers/meta/AutoWEKAClassifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ static enum Metric {
resamplingArgsMap.put(Resampling.CrossValidation, "numFolds=10");
resamplingArgsMap.put(Resampling.MultiLevel, "numLevels=2[$]autoweka.instancegenerators.CrossValidation[$]numFolds=10");
resamplingArgsMap.put(Resampling.RandomSubSampling, "numSamples=10:percent=66");
try {
Class.forName("autoweka.SetEnvironmentVariablesForAutoWeka");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
/** Arguments for the default evaluation method. */
static final String DEFAULT_RESAMPLING_ARGS = resamplingArgsMap.get(DEFAULT_RESAMPLING);
Expand Down Expand Up @@ -403,6 +409,10 @@ public void run() {
}
//log.info(line);
} else if(line.matches(".*WARN.*")) {
// filter out noisy warning message
if (line.matches(".*Picked up _JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED.*")) {
continue;
}
log.warn(line);
} else if(line.matches(".*ERROR.*")) {
log.error(line);
Expand Down

0 comments on commit ae87007

Please sign in to comment.