Skip to content

Commit

Permalink
Simplify launching executables
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Dec 30, 2023
1 parent 48d110c commit 3811180
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 50 deletions.
15 changes: 6 additions & 9 deletions src/main/java/org/moeaframework/benchmarks/HBV.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@

import java.io.File;

import org.apache.commons.lang3.SystemUtils;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.NativeCommand;
import org.moeaframework.problem.NativeProblem;

public class HBV extends NativeProblem {

public static final String PATH = "./native/HBV/bin/";

public static final double[] EPSILON = new double[] {
0.01, 0.025, 0.01, 0.01
};

public static final NativeCommand COMMAND = new NativeCommand("hbv",
new String[] { },
new File("./native/HBV/bin/"));

public HBV() {
super(createProcess());
super(COMMAND);
}

public static ProcessBuilder createProcess() {
String command = SystemUtils.IS_OS_WINDOWS ? PATH + "hbv.exe" : "./hbv";
return new ProcessBuilder().command(command).directory(new File(PATH));
}

@Override
public String getName() {
return "HBV";
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/org/moeaframework/benchmarks/LRGV.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@

import java.io.File;

import org.apache.commons.lang3.SystemUtils;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.NativeCommand;
import org.moeaframework.problem.NativeProblem;

public class LRGV extends NativeProblem {

public static final String PATH = "./native/LRGV/bin/";


public static final double[] EPSILON = {
0.0009, 0.002, 0.03, 0.004, 0.004
};

public LRGV() {
super(createProcess());
}
public static final NativeCommand COMMAND = new NativeCommand("lrgv",
new String[] { "-m", "std-io", "-b", "AllDecAll", "-c", "ten-year" },
new File("./native/LRGV/bin/"));

public static ProcessBuilder createProcess() {
String command = SystemUtils.IS_OS_WINDOWS ? PATH + "lrgv.exe" : "./lrgv";
return new ProcessBuilder()
.command(command, "-m", "std-io", "-b", "AllDecAll", "-c", "ten-year")
.directory(new File(PATH));
public LRGV() {
super(COMMAND);
}

@Override
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/moeaframework/benchmarks/LakeProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,23 @@

import java.io.File;

import org.apache.commons.lang3.SystemUtils;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.NativeCommand;
import org.moeaframework.problem.NativeProblem;

public class LakeProblem extends NativeProblem {

public static final String PATH = "./native/LakeProblem/bin/";

public static final double[] EPSILON = {
0.01, 0.01, 0.0001, 0.0001
};

public LakeProblem() {
super(createProcess());
}
public static final NativeCommand COMMAND = new NativeCommand("lake",
new String[] { },
new File("./native/LakePRoblem/bin/"));

public static ProcessBuilder createProcess() {
String command = SystemUtils.IS_OS_WINDOWS ? PATH + "lake.exe" : "./lake";
return new ProcessBuilder().command(command).directory(new File(PATH));
public LakeProblem() {
super(COMMAND);
}

@Override
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/org/moeaframework/benchmarks/WDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.io.File;

import org.apache.commons.lang3.SystemUtils;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.EncodingUtils;
import org.moeaframework.problem.NativeCommand;
import org.moeaframework.problem.NativeProblem;

/**
Expand Down Expand Up @@ -70,27 +70,19 @@ public int getNumberOfOptions() {
public double[] getEpsilon() {
return epsilon.clone();
}

public NativeCommand getCommand() {
return new NativeCommand(getName(), new String[] { }, new File("./native/WDS/bin/"));
}

}

public static final String PATH = "./native/WDS/bin/";


private final WDSInstance instance;

public WDS(WDSInstance instance) {
super(createProcess(instance));
super(instance.getCommand());
this.instance = instance;
}

public static ProcessBuilder createProcess(WDSInstance instance) {
String command = SystemUtils.IS_OS_WINDOWS ?
PATH + instance.getName() + ".exe" :
"./" + instance.getName();

return new ProcessBuilder()
.command(command)
.directory(new File(PATH));
}

@Override
public String getName() {
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/org/moeaframework/problem/NativeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.moeaframework.problem;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NativeCommand {

private final String executableName;

private final String[] arguments;

private final File workingDirectory;

public NativeCommand(String executableName, String[] arguments, File workingDirectory) {
super();
this.executableName = executableName;
this.arguments = arguments;
this.workingDirectory = workingDirectory;
}

public String getExecutableName() {
return executableName;
}

public String[] getArguments() {
return arguments;
}

public File getWorkingDirectory() {
return workingDirectory;
}

public Process exec() throws IOException {
return toProcessBuilder(OsType.getOsType()).start();
}

public ProcessBuilder toProcessBuilder(OsType osType) {
List<String> command = new ArrayList<String>();

switch (osType) {
case WINDOWS:
command.add(new File(getWorkingDirectory(), getExecutableName() + ".exe").getPath());
break;
case POSIX:
command.add(new File(".", getExecutableName()).getPath());
break;
default:
throw new IllegalArgumentException("Unsupported OS " + osType);
}

command.addAll(List.of(getArguments()));

return new ProcessBuilder()
.command(command)
.directory(getWorkingDirectory());
}

}
17 changes: 12 additions & 5 deletions src/main/java/org/moeaframework/problem/NativeProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
import org.moeaframework.core.FrameworkException;

public abstract class NativeProblem extends ExternalProblem {

public NativeProblem(ProcessBuilder builder) {
super(startProcessOrThrow(builder));

private final NativeCommand command;

public NativeProblem(NativeCommand command) {
super(startProcessOrThrow(command));
this.command = command;
}

public NativeCommand getCommand() {
return command;
}

private static final Process startProcessOrThrow(ProcessBuilder builder) {
private static final Process startProcessOrThrow(NativeCommand command) {
try {
return builder.start();
return command.toProcessBuilder(OsType.getOsType()).start();
} catch (IOException e) {
throw new FrameworkException("Failed to start native process", e);
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/moeaframework/problem/OsType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.moeaframework.problem;

import org.apache.commons.lang3.SystemUtils;

public enum OsType {

/**
* Windows.
*/
WINDOWS,

/**
* POSIX compatible systems, such as Unix, Linux, etc.
*/
POSIX;

public static OsType getOsType() {
if (SystemUtils.IS_OS_WINDOWS) {
return WINDOWS;
} else {
return POSIX;
}
}

}

0 comments on commit 3811180

Please sign in to comment.