Error Handling

This commit is contained in:
i23007 2024-07-09 01:55:13 +02:00
parent 4b59b88688
commit 2213a4385d
6 changed files with 85 additions and 45 deletions

View File

@ -4,6 +4,7 @@ import graph.*;
import logging.Algorithm; import logging.Algorithm;
import logging.LogElementList; import logging.LogElementList;
import javax.swing.*;
import java.util.Objects; import java.util.Objects;
@ -46,28 +47,47 @@ public class OurAlgorithm extends Algorithm {
*/ */
public LogElementList<OurLogElement> run() { public LogElementList<OurLogElement> run() {
this.setCurrentGraph(((OurParameterArea) this.getParameterArea()).getSelectedGraph()); try{
this.setCurrentGraph(((OurParameterArea) this.getParameterArea()).getSelectedGraph());
MarkedVertex<VertexMarking> start = null; MarkedVertex<VertexMarking> start = null;
for (MarkedVertex<VertexMarking> i : this.currentGraph.getAllVertexes()) { for (MarkedVertex<VertexMarking> i : this.currentGraph.getAllVertexes()) {
if (Objects.equals(i.getName(), "Start")) { if (Objects.equals(i.getName(), "Start")) {
start = i; start = i;
}
} }
}
MarkedVertex<VertexMarking> end = null; MarkedVertex<VertexMarking> end = null;
for (MarkedVertex<VertexMarking> i : this.currentGraph.getAllVertexes()) { for (MarkedVertex<VertexMarking> i : this.currentGraph.getAllVertexes()) {
if (Objects.equals(i.getName(), "Ende")) { if (Objects.equals(i.getName(), "Ende")) {
end = i; end = i;
}
} }
}
if (this.methodButtons.getSelectedMethod()) { if (this.methodButtons.getSelectedMethod()) {
this.currentGraph.getShortestPathDijkstra(start, end); this.currentGraph.getShortestPathDijkstra(start, end);
} else { } else {
this.currentGraph.getShortestPathAStar(start, end); this.currentGraph.getShortestPathAStar(start, end);
}
//Fehlerbehandlung
}catch(Throwable e){
JFrame info = new JFrame();
JPanel contentpane = new JPanel();
info.setContentPane(contentpane);
JOptionPane.showMessageDialog(
contentpane,
"Bitte geben SIe einen gültige Graphen an. Hilfestellung für das Erstellen von Knoten und Kanten:\n" +
"Knoten: Name;X-Coordinate;Y-Coordinate\nKanten: StartKnoten;Endknoten;Gewichtung\n" +
"Hinweis: ein Koten benötigt den Namen 'Startknoten', ein anderer 'Endknoten'\n" +
"(Festlegen der Start und Endknoten für die Algorithmen)",
"Eingabefehler",
JOptionPane.INFORMATION_MESSAGE,
null
);
LogElementList<OurLogElement> emptyLogList = new LogElementList<>();
emptyLogList.add(new OurLogElement());
return emptyLogList;
} }
return this.currentGraph.getLogList(); return this.currentGraph.getLogList();
} }

View File

@ -47,14 +47,16 @@ public class OurDrawArea extends DrawArea {
// Get the current log element from the log list // Get the current log element from the log list
OurLogElement logElement = (OurLogElement) logList.get(); OurLogElement logElement = (OurLogElement) logList.get();
// Draw the graph associated with the log element // Draw the graph associated with the log element, if available
logElement.getGraph().draw(g); if (logElement.getGraph() != null) {
logElement.getGraph().draw(g);
// Draw markings on edges // Draw markings on edges
for (Edge screenEdge : logElement.getGraph().getEdges()) { for (Edge screenEdge : logElement.getGraph().getEdges()) {
g.drawString(screenEdge.getMarking(), g.drawString(screenEdge.getMarking(),
(screenEdge.getSource().getXpos() + screenEdge.getDestination().getXpos()) / 2, (screenEdge.getSource().getXpos() + screenEdge.getDestination().getXpos()) / 2,
(screenEdge.getSource().getYpos() + screenEdge.getDestination().getYpos()) / 2); (screenEdge.getSource().getYpos() + screenEdge.getDestination().getYpos()) / 2);
}
} }
} }

View File

@ -66,7 +66,7 @@ public class OurParameterArea extends ParameterArea {
// Input text fields // Input text fields
textField1 = new JTextField("A-100-100;Ende-200-200;Start-100-200;D-200-100"); textField1 = new JTextField("A-100-100;Ende-200-200;Start-100-200;D-200-100");
textField2 = new JTextField("A-Ende-15;Start-D-10;D-A-3;A-Ende-15;Start-D-10;D-A-3"); textField2 = new JTextField("A-Ende-15;Start-D-10;D-A-3");
textField1.setPreferredSize(new Dimension(button5.getWidth(), button5.getHeight())); textField1.setPreferredSize(new Dimension(button5.getWidth(), button5.getHeight()));
textField2.setPreferredSize(new Dimension(button5.getWidth(), button5.getHeight())); textField2.setPreferredSize(new Dimension(button5.getWidth(), button5.getHeight()));
@ -135,31 +135,49 @@ public class OurParameterArea extends ParameterArea {
public DirectedGraph<VertexMarking, EdgeMarking> createGraph(String vertices, String edges) { public DirectedGraph<VertexMarking, EdgeMarking> createGraph(String vertices, String edges) {
DirectedGraph<VertexMarking, EdgeMarking> newGraph = new DirectedGraph<>(); DirectedGraph<VertexMarking, EdgeMarking> newGraph = new DirectedGraph<>();
String[] allVertices = vertices.split(";"); try{
String[] allEdges = edges.split(";"); String[] allVertices = vertices.split(";");
String[] allEdges = edges.split(";");
// Create vertices // Create vertices
for (String i: allVertices) { for (String i: allVertices) {
String[] current = i.split("-"); String[] current = i.split("-");
newGraph.addVertex(new MarkedVertex<>(Integer.parseInt(current[1]), Integer.parseInt(current[2]), current[0], null, Color.BLACK)); newGraph.addVertex(new MarkedVertex<>(Integer.parseInt(current[1]), Integer.parseInt(current[2]), current[0], null, Color.BLACK));
}
// Create edges
MarkedVertex<VertexMarking> start = null;
MarkedVertex<VertexMarking> end = null;
for (String i: allEdges) {
String[] current = i.split("-");
for (MarkedVertex<VertexMarking> j: newGraph.getAllVertexes()) {
if (Objects.equals(j.getName(), current[0])) {
start = j;
}
if (Objects.equals(j.getName(), current[1])) {
end = j;
}
} }
newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2]))));
// Create edges
MarkedVertex<VertexMarking> start = null;
MarkedVertex<VertexMarking> end = null;
for (String i: allEdges) {
String[] current = i.split("-");
for (MarkedVertex<VertexMarking> j: newGraph.getAllVertexes()) {
if (Objects.equals(j.getName(), current[0])) {
start = j;
}
if (Objects.equals(j.getName(), current[1])) {
end = j;
}
}
newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2]))));
}
//Fehlerbehandlung
}catch(Throwable e){
JFrame info = new JFrame();
JPanel contentpane = new JPanel();
info.setContentPane(contentpane);
JOptionPane.showMessageDialog(
contentpane,
"Fehler bei der Eingabe des Graphen. Beispieleingabe für Knoten und Kanten:\n" +
"Knoten: Name;X-Coordinate;Y-Coordinate\nKanten: StartKnoten;Endknoten;Gewichtung\n" +
"Hinweis: ein Koten benötigt den Namen 'Startknoten', ein anderer 'Endknoten'\n" +
"(Festlegen der Start und Endknoten für die Algorithmen)",
"Eingabefehler",
JOptionPane.INFORMATION_MESSAGE,
null
);
} }
return newGraph; return newGraph;
} }
} }