Skip to content

Commit

Permalink
add work in progress on command line args for quick runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Dec 10, 2024
1 parent f7fd94d commit 7f2b650
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
2 changes: 2 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies {
implementation(project(":io"))
implementation(project(":gui"))

implementation("com.beust:jcommander:1.82")

// The following are all necessary for gRPC
implementation("io.grpc:grpc-netty-shaded:1.68.1")
implementation("io.grpc:grpc-protobuf:1.68.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ public class BasicScenario extends Scenario {

public static void main(final String[] args) {
final BasicScenario scenario = new BasicScenario();
final Path scenarioPath = Path.of("/home/nicolas/Desktop/scenario.yaml");
new ScenarioWriter().write(
scenario,
Path.of("/home/nicolas/Desktop/scenario.yaml")
scenarioPath
);
new QuickRunner(scenario).runFor(Period.ofYears(1));

new QuickRunner(scenarioPath, Period.ofYears(1)).run();
}
}
56 changes: 51 additions & 5 deletions examples/src/main/java/uk/ac/ox/poseidon/examples/QuickRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,49 @@

package uk.ac.ox.poseidon.examples;

import lombok.RequiredArgsConstructor;
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.converters.PathConverter;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import uk.ac.ox.poseidon.core.Scenario;
import uk.ac.ox.poseidon.core.Simulation;
import uk.ac.ox.poseidon.io.ScenarioLoader;

import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.TemporalAmount;

@RequiredArgsConstructor
public class QuickRunner {
private final Scenario scenario;
@NoArgsConstructor
@AllArgsConstructor
public class QuickRunner implements Runnable {

public void runFor(final TemporalAmount temporalAmount) {
@Parameter(
names = {"-s", "--scenario"},
description = "Path to the scenario file in YAML format",
converter = PathConverter.class
)
private Path scenarioPath;

@Parameter(
names = {"-p", "--period"},
description =
"Period to run the simulation for in ISO-8601 format (e.g., P2Y for two years). " +
"See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/" +
"Period.html#parse(java.lang.CharSequence).",
converter = PeriodConverter.class
)
private Period period;

public static void main(final String[] args) {
// TODO
}

private void run(
final Scenario scenario,
final TemporalAmount temporalAmount
) {
final Simulation simulation = scenario.newSimulation();
simulation.start();
final LocalDateTime end =
Expand All @@ -45,4 +76,19 @@ public void runFor(final TemporalAmount temporalAmount) {
}
simulation.finish();
}

@Override
public void run() {
run(
new ScenarioLoader().load(scenarioPath.toFile()),
period
);
}

private static class PeriodConverter implements IStringConverter<Period> {
@Override
public Period convert(final String value) {
return Period.parse(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,11 @@ public class ExternalScenario extends Scenario {

public static void main(final String[] args) {
final ExternalScenario scenario = new ExternalScenario();
final Path scenarioPath = Path.of("/home/nicolas/Desktop/scenario.yaml");
new ScenarioWriter().write(
scenario,
Path.of("/home/nicolas/Desktop/scenario.yaml")
scenarioPath
);
new QuickRunner(new ExternalScenario()).runFor(Period.ofYears(10));
new QuickRunner(scenarioPath, Period.ofYears(1)).run();
}
}

0 comments on commit 7f2b650

Please sign in to comment.