Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from ptgrogan/headless-dev
Browse files Browse the repository at this point in the history
Headless dev
  • Loading branch information
ptgrogan authored Apr 13, 2022
2 parents b5d6251 + cb5be86 commit da28118
Show file tree
Hide file tree
Showing 23 changed files with 534 additions and 116 deletions.
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.mit</groupId>
<artifactId>spacenet</artifactId>
<version>2.5.1459</version>
<version>2.5.1460</version>
<name>spacenet</name>
<url>http://maven.apache.org</url>
<properties>
Expand Down Expand Up @@ -43,6 +43,21 @@
<version>[8.0.16,)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
158 changes: 158 additions & 0 deletions src/main/java/edu/mit/spacenet/SpaceNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,183 @@
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.google.gson.Gson;

import edu.mit.spacenet.gui.SpaceNetFrame;
import edu.mit.spacenet.gui.SpaceNetSettings;
import edu.mit.spacenet.gui.SplashScreen;
import edu.mit.spacenet.io.XStreamEngine;
import edu.mit.spacenet.io.gson.AggregatedDemandsAnalysis;
import edu.mit.spacenet.io.gson.RawDemandsAnalysis;
import edu.mit.spacenet.scenario.Scenario;
import edu.mit.spacenet.simulator.DemandSimulator;

/**
* This class is used to launch the SpaceNet application.
*
* @author Paul Grogan
*/
public class SpaceNet {
public static enum HeadlessMode {
DEMANDS_RAW("demands-raw"),
DEMANDS_AGGREGATED("demands-agg");

public final String label;

private HeadlessMode(String label) {
this.label = label;
}
}

/**
* Launches the SpaceNet application.
*
* @param args the args
*/
public static void main(String[] args) {
Options options = new Options();
Option headless = Option.builder("h")
.longOpt("headless")
.argName("mode")
.hasArg()
.desc("Headless execution mode. Alternatives: demand (demand simulator).")
.build();
options.addOption(headless);
Option input = Option.builder("i")
.longOpt("input")
.argName("file path")
.hasArg()
.desc("Input file path.")
.build();
options.addOption(input);
Option output = Option.builder("o")
.longOpt("output")
.argName("file path")
.hasArg()
.desc("Output file path.")
.build();
options.addOption(output);
Option confirm = Option.builder("y")
.longOpt("yes")
.hasArg(false)
.desc("Confirm overwrite output.")
.build();
options.addOption(confirm);

CommandLineParser parser = new DefaultParser();
HelpFormatter helper = new HelpFormatter();

try {
CommandLine line = parser.parse(options, args);
if(line.hasOption(headless)) {
String mode = line.getOptionValue(headless);

if(mode.equalsIgnoreCase(HeadlessMode.DEMANDS_RAW.label) || mode.equalsIgnoreCase(HeadlessMode.DEMANDS_AGGREGATED.label)) {
String scenarioFilePath = null;
if(line.hasOption(input)) {
scenarioFilePath = new File(line.getOptionValue(input)).getAbsolutePath();
} else {
System.err.println("Missing scenario file path.");
helper.printHelp("Usage:", options);
System.exit(0);
}
String outputFilePath = null;
if(line.hasOption(output)) {
outputFilePath = new File(line.getOptionValue(output)).getAbsolutePath();
} else {
System.err.println("Missing output file path.");
helper.printHelp("Usage:", options);
System.exit(0);
}

runDemandSimulator(scenarioFilePath, outputFilePath, line.hasOption(confirm), mode.equalsIgnoreCase(HeadlessMode.DEMANDS_RAW.label));
} else {
System.err.println("Unknown headless mode: " + mode);
System.exit(0);
}

} else {
String scenarioFilePath = null;
if(line.hasOption(input)) {
scenarioFilePath = new File(line.getOptionValue(input)).getAbsolutePath();
}
startGUI(scenarioFilePath);
}
} catch(ParseException ex) {
System.err.println("Parsing failed: " + ex.getMessage());
helper.printHelp("Usage:", options);
System.exit(0);
}
}

private static void runDemandSimulator(String scenarioFilePath, String outputFilePath, boolean isOverwriteConfirmed, boolean isRawDemands) {
Scenario scenario = null;
try {
scenario = XStreamEngine.openScenario(scenarioFilePath);
scenario.setFilePath(scenarioFilePath);
} catch(IOException ex) {
System.err.println("Failed to read scenario file: " + ex.getMessage());
System.exit(1);
}

DemandSimulator simulator = new DemandSimulator(scenario);
simulator.setDemandsSatisfied(false);
simulator.simulate();

File file = new File(outputFilePath);
if(file.exists() && !isOverwriteConfirmed) {
Scanner in = new Scanner(System.in);
String lastInput = "";
do {
System.out.println("Confirm to overwrite existing file " + outputFilePath + " (yes/no)");
lastInput = in.nextLine();
if(lastInput.equalsIgnoreCase("no") || lastInput.equalsIgnoreCase("n")) {
in.close();
return;
}
} while(!(lastInput.equalsIgnoreCase("yes") || lastInput.equalsIgnoreCase("y")));
in.close();
}

try {
BufferedWriter out = new BufferedWriter(new FileWriter(outputFilePath));
if(isRawDemands) {
new Gson().toJson(
RawDemandsAnalysis.createFrom(simulator),
out
);
} else {
new Gson().toJson(
AggregatedDemandsAnalysis.createFrom(simulator),
out
);
}
out.close();
} catch(IOException ex) {
System.err.println("Failed to output file: " + ex.getMessage());
System.exit(1);
}
}

private static void startGUI(String scenarioFilePath) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Expand Down Expand Up @@ -88,6 +243,9 @@ protected Void doInBackground() {
SpaceNetFrame.getInstance().setBounds(virtualBounds.intersection(SpaceNetSettings.getInstance().getLastBounds()));
}
SpaceNetFrame.getInstance().setVisible(true);
if(scenarioFilePath != null) {
SpaceNetFrame.getInstance().openScenario(scenarioFilePath);
}
} catch(Exception ex) {
// display error message if one occurs... since this
// is inside a worker thread, the stack trace will
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/edu/mit/spacenet/gui/demand/DemandsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import edu.mit.spacenet.scenario.ItemDiscretization;
import edu.mit.spacenet.scenario.Scenario;
import edu.mit.spacenet.scenario.SupplyEdge;
import edu.mit.spacenet.scenario.SupplyEdge.SupplyPoint;
import edu.mit.spacenet.scenario.SupplyPoint;
import edu.mit.spacenet.simulator.DemandSimulator;
import edu.mit.spacenet.simulator.SimDemand;

Expand Down Expand Up @@ -615,10 +615,10 @@ private void writeFile() {
out.write(delimiter);
out.write("" + demand.getAmount());
} else if(referenceCombo.getSelectedItem()==NAME_OUTPUT) {
out.write(supplyPoint.getNode().getName());
out.write("" + supplyPoint.getTime());
out.write(delimiter);
out.write(delimiter);
out.write("" + supplyPoint.getTime());
out.write(supplyPoint.getNode().getName());
out.write(delimiter);
out.write(delimiter);
out.write(demand.getResource().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ private JFreeChart buildTimeSeriesChart() {
estimatedDemands += demandsTab.getSimulator().getAggregatedNodeDemands().get(edge.getPoint()).getTotalMass();

if(optionsPanel.supplyEdgesModel.getSelectedObjects().contains(edge)) {
rawCapacity += edge.getRawCapacity();
remainingCapacity += edge.getCapacity();
rawCapacity += edge.getMaxCargoMass();
remainingCapacity += edge.getNetCargoMass();

if(optionsPanel.dataModel.getSelectedObjects().contains(ESTIMATED_DEMANDS))
estimatedDemandsSeries.addOrUpdate(hour, estimatedDemands);
Expand Down Expand Up @@ -552,8 +552,8 @@ private JFreeChart buildLineChart() {
estimatedDemands += demandsTab.getSimulator().getAggregatedNodeDemands().get(edge.getPoint()).getTotalMass();

if(optionsPanel.supplyEdgesModel.getSelectedObjects().contains(edge)) {
rawCapacity += edge.getRawCapacity();
remainingCapacity += edge.getCapacity();
rawCapacity += edge.getMaxCargoMass();
remainingCapacity += edge.getNetCargoMass();

if(optionsPanel.dataModel.getSelectedObjects().contains(ESTIMATED_DEMANDS)) {
dataset.addValue(estimatedDemands, ESTIMATED_DEMANDS, edge);
Expand Down Expand Up @@ -612,12 +612,12 @@ private JFreeChart buildBarChart() {
}
if(optionsPanel.dataModel.getSelectedObjects().contains(RAW_CAPACITY)
&& optionsPanel.supplyEdgesModel.getSelectedObjects().contains(edge)) {
dataset.addValue(edge.getRawCapacity(), RAW_CAPACITY, edge);
dataset.addValue(edge.getMaxCargoMass(), RAW_CAPACITY, edge);
r.setSeriesPaint(dataset.getRowCount() - 1, Color.BLUE);
}
if(optionsPanel.dataModel.getSelectedObjects().contains(REMAINING_CAPACITY)
&& optionsPanel.supplyEdgesModel.getSelectedObjects().contains(edge)) {
dataset.addValue(edge.getCapacity(), REMAINING_CAPACITY, edge);
dataset.addValue(edge.getNetCargoMass(), REMAINING_CAPACITY, edge);
r.setSeriesPaint(dataset.getRowCount() - 1, Color.GREEN);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import edu.mit.spacenet.gui.renderer.VisibilityTableCellHeaderRenderer;
import edu.mit.spacenet.scenario.Scenario;
import edu.mit.spacenet.scenario.SupplyEdge;
import edu.mit.spacenet.scenario.SupplyEdge.SupplyPoint;
import edu.mit.spacenet.scenario.SupplyPoint;
import edu.mit.spacenet.util.DateFunctions;

/**
Expand Down Expand Up @@ -503,11 +503,11 @@ private double getCapacity(SupplyEdge edge) {
capacity += carrier.getMaxCargoMass();
}
} else if(optionsPanel.transportDisplayCombo.getSelectedItem()==REMAINING_CAPACITY)
capacity = edge.getCapacity();
capacity = edge.getNetCargoMass();
else if(optionsPanel.transportDisplayCombo.getSelectedItem()==DEMANDS)
capacity = -demandsTab.getSimulator().getAggregatedEdgeDemands().get(edge).getTotalMass();
else
capacity = edge.getCapacity() - demandsTab.getSimulator().getAggregatedEdgeDemands().get(edge).getTotalMass();
capacity = edge.getNetCargoMass() - demandsTab.getSimulator().getAggregatedEdgeDemands().get(edge).getTotalMass();
return capacity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import edu.mit.spacenet.domain.resource.Demand;
import edu.mit.spacenet.domain.resource.DemandSet;
import edu.mit.spacenet.scenario.Manifest;
import edu.mit.spacenet.scenario.SupplyEdge.SupplyPoint;
import edu.mit.spacenet.scenario.SupplyPoint;

/**
* A table for visualizing aggregated demands, and the portion of each demand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import edu.mit.spacenet.domain.element.I_ResourceContainer;
import edu.mit.spacenet.domain.resource.Demand;
import edu.mit.spacenet.scenario.Manifest;
import edu.mit.spacenet.scenario.SupplyEdge.SupplyPoint;
import edu.mit.spacenet.scenario.SupplyPoint;

/**
* A table for viewing the packed contents of a container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import edu.mit.spacenet.scenario.Manifest;
import edu.mit.spacenet.scenario.Scenario;
import edu.mit.spacenet.scenario.SupplyEdge;
import edu.mit.spacenet.scenario.SupplyEdge.SupplyPoint;
import edu.mit.spacenet.scenario.SupplyPoint;
import edu.mit.spacenet.simulator.DemandSimulator;
import edu.mit.spacenet.util.GlobalParameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import edu.mit.spacenet.gui.renderer.ElementTableCellRenderer;
import edu.mit.spacenet.scenario.Manifest;
import edu.mit.spacenet.scenario.SupplyEdge;
import edu.mit.spacenet.scenario.SupplyEdge.SupplyPoint;
import edu.mit.spacenet.scenario.SupplyPoint;

/**
* The table used to view the available resource containers for demand packing.
Expand Down
Loading

0 comments on commit da28118

Please sign in to comment.