ProjektGraph/OurApplication/OurAlgorithm.java
2024-07-09 01:55:13 +02:00

104 lines
3.1 KiB
Java

package OurApplication;
import graph.*;
import logging.Algorithm;
import logging.LogElementList;
import javax.swing.*;
import java.util.Objects;
/**
* This class provides an example of using logging.Algorithm.
* It implements a summation algorithm that computes the sum of integers starting from 0 up to a specified maximum value from the parameter area.
*
* @see Algorithm
*/
public class OurAlgorithm extends Algorithm {
private graph.Graph<VertexMarking, EdgeMarking> currentGraph;
private OurMethodButtons methodButtons;
/**
* Standard constructor.
* Initializes an instance of OurAlgorithm.
*/
public OurAlgorithm() {
super();
}
/**
* Constructs a summation algorithm with parameters from the parameter area.
*
* @param parameterArea the parameter area providing the selected graph for the algorithm
* @param methodButtons the method buttons determining the algorithm type (Dijkstra or A-Star)
*/
public OurAlgorithm(OurParameterArea parameterArea, OurMethodButtons methodButtons) {
super(parameterArea, "Thema II: Kürzeste Wege");
this.methodButtons = methodButtons;
}
/**
* Overridden method from the superclass.
* Executes the summation algorithm.
* Computes the shortest path in the current graph based on the selected method (Dijkstra or A-Star).
*
* @return a LogElementList containing the single steps of the algorithm processing
*/
public LogElementList<OurLogElement> run() {
try{
this.setCurrentGraph(((OurParameterArea) this.getParameterArea()).getSelectedGraph());
MarkedVertex<VertexMarking> start = null;
for (MarkedVertex<VertexMarking> i : this.currentGraph.getAllVertexes()) {
if (Objects.equals(i.getName(), "Start")) {
start = i;
}
}
MarkedVertex<VertexMarking> end = null;
for (MarkedVertex<VertexMarking> i : this.currentGraph.getAllVertexes()) {
if (Objects.equals(i.getName(), "Ende")) {
end = i;
}
}
if (this.methodButtons.getSelectedMethod()) {
this.currentGraph.getShortestPathDijkstra(start, end);
} else {
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();
}
/**
* Sets the current graph for the algorithm.
*
* @param graph the graph to be set as the current graph
*/
public void setCurrentGraph(graph.Graph<VertexMarking, EdgeMarking> graph) {
this.currentGraph = graph;
}
}