final File train;
try {
train = trainingFile(dataSet);
} catch (IOException e) {
throw new RecommenderBuildException("error preparing training file", e);
}
final File test;
try {
test = testFile(dataSet);
} catch (IOException e) {
throw new RecommenderBuildException("error preparing test file", e);
}
final File output = getFile("predictions.csv");
List<String> args = Lists.transform(algorithm.getCommand(), new Function<String, String>() {
@Nullable
@Override
public String apply(@Nullable String input) {
if (input == null) {
throw new NullPointerException("command element");
}
String s = input.replace("{OUTPUT}", output.getAbsolutePath());
s = s.replace("{TRAIN_DATA}", train.getAbsolutePath());
s = s.replace("{TEST_DATA}", test.getAbsolutePath());
return s;
}
});
logger.info("running {}", StringUtils.join(args, " "));
Process proc;
try {
proc = new ProcessBuilder().command(args)
.directory(algorithm.getWorkDir())
.start();
} catch (IOException e) {
throw new RecommenderBuildException("error creating process", e);
}
Thread listen = new LoggingStreamSlurper("external-algo", proc.getErrorStream(),
logger, "external: ");
listen.run();
int result = -1;
boolean done = false;
while (!done) {
try {
result = proc.waitFor();
done = true;
} catch (InterruptedException e) {
logger.info("thread interrupted, killing subprocess");
proc.destroy();
throw new RecommenderBuildException("recommender build interrupted", e);
}
}
if (result != 0) {
logger.error("external command exited with status {}", result);
throw new RecommenderBuildException("recommender exited with code " + result);
}
Long2ObjectMap<SparseVector> vectors;
try {
vectors = readPredictions(output);
} catch (FileNotFoundException e) {
logger.error("cannot find expected output file {}", output);
throw new RecommenderBuildException("recommender produced no output", e);
}
userPredictions = vectors;
userTrainingEvents = dataSet.getTrainingData().getUserEventDAO();