Add static network

This commit is contained in:
william 2022-06-07 21:07:49 -04:00
parent 8a0d743e6d
commit 704b40ecc9
24 changed files with 198 additions and 48 deletions

View File

@ -1,11 +1,13 @@
package configuration;
import java.io.File;
public interface ConfigurationParser {
/**
* Analyse et lit une configuration de simulation depuis un fichier au chemin donné.
* Analyse et lit une configuration de simulation depuis un fichier.
*
* @param path Le chemin vers le fichier de configuration.
* @param file Le fichier de configuration.
* @return La configuration de simulation correspondant au contenu du fichier.
*/
SimulationConfiguration parseConfiguration(String path) throws ConfigurationParsingException;
SimulationConfiguration parseConfiguration(File file) throws ConfigurationParsingException;
}

View File

@ -1,6 +1,6 @@
package configuration;
import model.metadata.BuildingMetadata;
import metadata.BuildingMetadata;
import java.util.Map;

View File

@ -1,2 +1,26 @@
package configuration;public class SingletonConfigurationSingleton {
package configuration;
public class SimulationConfigurationSingleton {
private static SimulationConfigurationSingleton instance;
public static SimulationConfigurationSingleton getInstance() {
if (instance == null) {
instance = new SimulationConfigurationSingleton();
}
return instance;
}
private SimulationConfiguration configuration;
private SimulationConfigurationSingleton() {
}
public void setConfiguration(SimulationConfiguration config) {
this.configuration = config;
}
public SimulationConfiguration getConfiguration() {
return configuration;
}
}

View File

@ -1,7 +1,7 @@
package configuration;
import model.Building;
import model.Route;
import simulation.Building;
import simulation.Route;
import java.util.Collection;

View File

@ -1,15 +1,16 @@
package configuration;
import model.*;
import model.metadata.*;
import metadata.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import simulation.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@ -36,8 +37,8 @@ public class XmlConfigurationParser implements ConfigurationParser {
public static final String ATTRIBUTE_TO = "vers";
@Override
public SimulationConfiguration parseConfiguration(String path) throws ConfigurationParsingException {
Document document = parseDocument(path);
public SimulationConfiguration parseConfiguration(File file) throws ConfigurationParsingException {
Document document = parseDocument(file);
Map<String, BuildingMetadata> buildingsMetadata = parseBuildingsMetadata(document);
SimulationData simulationData = parseSimulationData(document);
@ -45,12 +46,12 @@ public class XmlConfigurationParser implements ConfigurationParser {
return new SimulationConfiguration(buildingsMetadata, simulationData);
}
private Document parseDocument(String path) throws ConfigurationParsingException {
private Document parseDocument(File file) throws ConfigurationParsingException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(path);
Document document = builder.parse(file);
document.getDocumentElement().normalize();
return document;

View File

@ -1,4 +1,4 @@
package model.metadata;
package metadata;
import simulation.BuildingState;
@ -6,8 +6,8 @@ import java.util.Map;
import java.util.Objects;
public abstract class BuildingMetadata {
private final String type;
private final Map<BuildingState, String> iconsPaths;
protected final String type;
protected final Map<BuildingState, String> iconsPaths;
public BuildingMetadata(String type, Map<BuildingState, String> iconsPaths) {
this.type = type;

View File

@ -1,4 +1,4 @@
package model.metadata;
package metadata;
import simulation.ComponentType;

View File

@ -1,4 +1,4 @@
package model.metadata;
package metadata;
import simulation.BuildingState;

View File

@ -1,4 +1,4 @@
package model.metadata;
package metadata;
import simulation.ComponentType;

View File

@ -1,4 +1,4 @@
package model.metadata;
package metadata;
import simulation.ComponentType;

View File

@ -1,4 +1,4 @@
package model.metadata;
package metadata;
import simulation.BuildingState;
@ -13,6 +13,10 @@ public class WarehouseMetadata extends BuildingMetadata {
this.input = input;
}
public WarehouseInput getInput() {
return input;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -1,11 +1,11 @@
package model;
package simulation;
public abstract class Building {
private final int id;
private final String type;
private final int x;
private final int y;
private BuildingState state = BuildingState.EMPTY;
protected final int id;
protected final String type;
protected final int x;
protected final int y;
protected BuildingState state = BuildingState.EMPTY;
protected Building(int id, String type, int x, int y) {
this.id = id;

View File

@ -1,4 +1,4 @@
package model;
package simulation;
import java.util.Arrays;
import java.util.Objects;

View File

@ -1,4 +1,4 @@
package model;
package simulation;
import java.util.Objects;

View File

@ -1,4 +1,4 @@
package model;
package simulation;
public class ComponentRoute {
private final Building outputBuilding;

View File

@ -1,4 +1,4 @@
package model;
package simulation;
import java.util.Arrays;
import java.util.Objects;

View File

@ -1,4 +1,4 @@
package model;
package simulation;
public class Factory extends Building {
public Factory(int id, String type, int x, int y) {

View File

@ -1,4 +1,4 @@
package model;
package simulation;
import java.util.Objects;

View File

@ -1,14 +1,40 @@
package model;
package simulation;
import configuration.SimulationConfigurationSingleton;
import metadata.WarehouseMetadata;
import java.util.ArrayList;
import java.util.Collection;
public class Warehouse extends Building implements WarehouseSubject {
private final Collection<Component> planes = new ArrayList<>();
private final Collection<WarehouseObserver> observers = new ArrayList<>();
private final int capacity = ((WarehouseMetadata) SimulationConfigurationSingleton.getInstance()
.getConfiguration().getMetadata().get(getType()))
.getInput().getCapacity();
public class Warehouse extends Building {
public Warehouse(int id, String type, int x, int y) {
super(id, type, x, y);
}
@Override
public void processInput(Component input) {
planes.add(input);
}
private void sellComponent() {
}
public void attach(WarehouseObserver observer) {
observers.add(observer);
}
public void dettach(WarehouseObserver observer) {
observers.remove(observer);
}
public void notifyObservers() {
boolean isFull = planes.size() >= capacity;
observers.forEach(o -> o.update(isFull));
}
}

View File

@ -1,2 +1,10 @@
package simulation;public interface WarehouseObserver {
package simulation;
public interface WarehouseObserver {
/**
* Méthode appelée lorsque l'état d'un entrepot change.
*
* @param full Si l'entrepot est plein ou non
*/
void update(boolean full);
}

View File

@ -1,2 +1,7 @@
package simulation;public class WarehouseSubject {
package simulation;
public interface WarehouseSubject {
void attach(WarehouseObserver observer);
void dettach(WarehouseObserver observer);
void notifyObservers();
}

View File

@ -1,23 +1,88 @@
package view;
import configuration.SimulationConfiguration;
import configuration.SimulationConfigurationSingleton;
import configuration.SimulationData;
import metadata.BuildingMetadata;
import simulation.Building;
import simulation.BuildingState;
import simulation.Route;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serial;
import java.util.HashMap;
import java.util.Map;
public class MainPanel extends JPanel {
@Serial
private static final long serialVersionUID = 1L;
private Point position = new Point(0, 0);
private Point speed = new Point(1, 1);
private int size = 32;
private final Map<String, Image> icons = new HashMap<>();
private final Map<Integer, Building> buildingsById = new HashMap<>();
private SimulationConfiguration configuration;
private SimulationData simulationData;
public void reloadSimulation() {
configuration = SimulationConfigurationSingleton.getInstance().getConfiguration();
simulationData = configuration.getSimulationData();
buildingsById.clear();
simulationData.getBuildings().forEach(b -> buildingsById.put(b.getId(), b));
icons.clear();
configuration.getMetadata().values().forEach(m -> {
try {
loadImage(m);
} catch (IOException e) {
System.err.println("Could not load simulation icons");
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
// On ajoute à la position le delta x et y de la vitesse
position.translate(speed.x, speed.y);
g.fillRect(position.x, position.y, size, size);
// position.translate(speed.x, speed.y);
// g.fillRect(position.x, position.y, size, size);
if (configuration == null) return;
simulationData.getRoutes().forEach(r -> paintRoute(r, g));
simulationData.getBuildings().forEach(b -> paintBuilding(b, g));
}
private void paintBuilding(Building building, Graphics g) {
Image icon = icons.get(building.getType());
g.drawImage(icon, building.getX(), building.getY(), null);
}
private void paintRoute(Route route, Graphics g) {
Building fromBuilding = buildingsById.get(route.getFrom());
Image fromIcon = icons.get(fromBuilding.getType());
Building toBuilding = buildingsById.get(route.getTo());
Image toIcon = icons.get(toBuilding.getType());
g.drawLine(
fromBuilding.getX() + fromIcon.getWidth(null) / 2,
fromBuilding.getY() + fromIcon.getHeight(null) / 2,
toBuilding.getX() + toIcon.getWidth(null) / 2,
toBuilding.getY() + toIcon.getHeight(null) / 2
);
}
private void loadImage(BuildingMetadata metadata) throws IOException {
String iconPath = metadata.getIconsPaths().get(BuildingState.EMPTY);
BufferedImage image = ImageIO.read(new File(iconPath));
icons.put(metadata.getType(), image);
}
}

View File

@ -14,7 +14,7 @@ public class MainWindow extends JFrame implements PropertyChangeListener {
public MainWindow() {
MainPanel mainPanel = new MainPanel();
WindowMenu menu = new WindowMenu();
WindowMenu menu = new WindowMenu(mainPanel);
add(mainPanel);
add(menu, BorderLayout.NORTH);
@ -38,7 +38,7 @@ public class MainWindow extends JFrame implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("TEST")) {
repaint();
System.out.println(event.getNewValue());
// System.out.println(event.getNewValue());
}
}
}

View File

@ -1,5 +1,7 @@
package view;
import configuration.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
@ -18,7 +20,12 @@ public class WindowMenu extends JMenuBar {
private static final String MENU_HELP_TITLE = "Aide";
private static final String MENU_HELP_ABOUT = "À propos de...";
public WindowMenu() {
private final ConfigurationParser configParser = new XmlConfigurationParser();
private final MainPanel mainPanel;
public WindowMenu(MainPanel mainPanel) {
this.mainPanel = mainPanel;
addFileMenu();
addSimulationMenu();
addHelpMenu();
@ -42,9 +49,17 @@ public class WindowMenu extends JMenuBar {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
// TODO - Parser le fichier XML sélectionné
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getAbsoluteFile());
try {
SimulationConfiguration configuration = configParser.parseConfiguration(selectedFile);
SimulationConfigurationSingleton.getInstance().setConfiguration(configuration);
mainPanel.reloadSimulation();
} catch (ConfigurationParsingException ex) {
// TODO WN: Handle exception
throw new RuntimeException("Failed to parse config file", ex);
}
}
});
@ -85,7 +100,7 @@ public class WindowMenu extends JMenuBar {
aboutMenu.addActionListener((ActionEvent e) -> {
JOptionPane.showMessageDialog(null,
"<html><p>Application simulation une chaine de production d'avions.</p><br>" +
"<html><p>Application de simulation une chaine de production d'avions.</p><br>" +
"<p>&copy; &nbsp; 2017 &nbsp; Ghizlane El Boussaidi</p>" +
"<p>&copy; &nbsp; 2017 &nbsp; Dany Boisvert</p>" +
"<p>&copy; &nbsp; 2017 &nbsp; Vincent Mattard</p><br>" +