286 lines
9.5 KiB
Java
286 lines
9.5 KiB
Java
package Presenter;
|
|
|
|
import Model.Arrangement;
|
|
import Model.Line;
|
|
import Model.Point;
|
|
import Presenter.Algorithms.LeastMedianOfSquaresEstimator;
|
|
import Presenter.Algorithms.RepeatedMedianEstimator;
|
|
import Presenter.Algorithms.TheilSenEstimator;
|
|
import Presenter.Import.DataImporter;
|
|
import View.MainFrame;
|
|
|
|
import javax.swing.*;
|
|
import java.io.File;
|
|
import java.util.LinkedList;
|
|
import java.util.Observable;
|
|
import java.util.Observer;
|
|
|
|
|
|
/**
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
*
|
|
* @Author: Armin Wolf
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
* @Date: 28.05.2017.
|
|
*/
|
|
public class Presenter implements Observer {
|
|
|
|
private Arrangement model;
|
|
private MainFrame view;
|
|
|
|
public Presenter(Arrangement model, MainFrame view) {
|
|
this.model = model;
|
|
this.view = view;
|
|
// Double[] x = {1d, 2d, 3d, 4d, 10d, 12d, 18d};
|
|
// Double[] y = {9d, 15d, 19d, 20d, 45d, 55d, 78d};
|
|
Double[] x = {18d, 24d, 30d, 34d, 38d};
|
|
Double[] y = {18d, 26d, 30d, 40d, 70d};
|
|
// Double[] x = {1d,3d,4d,5d,8d};
|
|
// Double[] y = {4d,2d,1d,0d,0d};
|
|
view.logHeading("Duale Darstellung der Punkte als Geraden:");
|
|
for (int j = 0; j < x.length; j++) {
|
|
Line p = new Line(x[j], y[j]);
|
|
p.setId(j+"");
|
|
view.log("f(x) = " + p.getM() + "x + " + p.getB());
|
|
this.model.addLine(p);
|
|
}
|
|
//
|
|
calcArrangementNodes();
|
|
// //print
|
|
// List<String> heading = new LinkedList<>();
|
|
// List<List<String>> rows = new LinkedList<>();
|
|
// heading.add("X - Koordinate");
|
|
// heading.add("Y - Koordinate");
|
|
// for (Point p : model.getNodes()) {
|
|
// LinkedList<String> rowEntry = new LinkedList<>();
|
|
// rowEntry.add(p.getX().toString());
|
|
// rowEntry.add(p.getY().toString());
|
|
// rows.add(rowEntry);
|
|
// }
|
|
// view.logHeading("Schnittpunkte der Dualen Geraden:");
|
|
// view.createTable(heading, rows);
|
|
}
|
|
|
|
|
|
@Override
|
|
public void update(Observable o, Object arg) {
|
|
String[] result = ((String[]) arg);
|
|
if (result[0] == "lms"){
|
|
SwingUtilities.invokeLater(() -> {
|
|
getView().visualizeLMS(Double.parseDouble(result[1]), Double.parseDouble(result[2]));
|
|
//getView().setLmsIsComplete(true);
|
|
getView().logHeading("Least Median of Squares");
|
|
getView().log("<b>m:</b> " + result[1]);
|
|
getView().log("<b>b:</b> " + result[2]);
|
|
getView().logSuccess("Berechnung wurde Erfolgreich durchgeführt <hr>");
|
|
});
|
|
}
|
|
|
|
if (result[0] == "rm"){
|
|
SwingUtilities.invokeLater(() -> {
|
|
getView().visualizeRM(Double.parseDouble(result[1]), Double.parseDouble(result[2]));
|
|
getView().logHeading("Repeated Median Estimator");
|
|
getView().log("<b>m:</b> " + result[1]);
|
|
getView().log("<b>b:</b> " + result[2]);
|
|
getView().logSuccess("Berechnung wurde Erfolgreich durchgeführt <hr>");
|
|
});
|
|
}
|
|
|
|
if (result[0] == "ts"){
|
|
SwingUtilities.invokeLater(() -> {
|
|
getView().visualizeTS(Double.parseDouble(result[1]), Double.parseDouble(result[2]));
|
|
getView().logHeading("Theil-Sen Estimator");
|
|
getView().log("<b>m:</b> " + result[1]);
|
|
getView().log("<b>b:</b> " + result[2]);
|
|
getView().logSuccess("Berechnung wurde Erfolgreich durchgeführt <hr>");
|
|
});
|
|
}
|
|
|
|
if (result[0] == "import"){
|
|
Double max = Double.parseDouble(result[1]);
|
|
Double current = Double.parseDouble(result[2]);
|
|
Integer progress = (int) (100 * (current/max));
|
|
//100% erreicht
|
|
if (progress == 100){
|
|
SwingUtilities.invokeLater(() -> {
|
|
getView().showImportProgress(progress);
|
|
getView().enableFunctionality();
|
|
getView().getProgressDialog().dispose();
|
|
});
|
|
setup();
|
|
} else {
|
|
SwingUtilities.invokeLater(() -> {
|
|
getView().showImportProgress(progress);
|
|
});
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void visualizeDualLines() {
|
|
view.createArrangement();
|
|
}
|
|
|
|
/***************************************************************************************************************************
|
|
* Ausführung der Algorithmen
|
|
***************************************************************************************************************************/
|
|
public void calculateLMS(String[] input) {
|
|
if (input[0] != null && input[1] != null){
|
|
Double constant = Double.parseDouble(input[0]);
|
|
Double error = Double.parseDouble(input[1]);
|
|
LeastMedianOfSquaresEstimator lms = new LeastMedianOfSquaresEstimator(getModel().getLines(), getModel().getNodes(), this);
|
|
lms.setConstant(constant);
|
|
lms.setQuantileError(error);
|
|
lms.addObserver(this);
|
|
lms.run();
|
|
lms.getResult();
|
|
}
|
|
}
|
|
|
|
public void calculateRM(String input){
|
|
if (input != null){
|
|
RepeatedMedianEstimator rm = new RepeatedMedianEstimator(getModel().getLines(), this);
|
|
Double parameter = Double.parseDouble(input);
|
|
rm.setBeta(parameter);
|
|
rm.addObserver(this);
|
|
rm.run();
|
|
rm.getResult();
|
|
}
|
|
}
|
|
|
|
|
|
public void calculateTS(String input){
|
|
if (input != null){
|
|
TheilSenEstimator ts = new TheilSenEstimator(getModel().getLines(), getModel().getNodes(),this);
|
|
ts.addObserver(this);
|
|
ts.run();
|
|
ts.getResult();
|
|
}
|
|
}
|
|
|
|
/***************************************************************************************************************************
|
|
* Hilfsmethoden
|
|
***************************************************************************************************************************/
|
|
public void setup(){
|
|
getView().logHeading("Duale Darstellung der Punkte als Geraden:");
|
|
|
|
for (int j = 0; j < getModel().getLines().size(); j++) {
|
|
Line p = getModel().getLines().get(j);
|
|
p.setId(j+"");
|
|
getView().log("f(x) = " + p.getM() + "x + " + p.getB());
|
|
}
|
|
getView().log("<hr>");
|
|
calcArrangementNodes();
|
|
|
|
//Darstellung der Schnittpunkte in einer Tabelle
|
|
// List<String> heading = new LinkedList<>();
|
|
// List<List<String>> rows = new LinkedList<>();
|
|
// heading.add("X - Koordinate");
|
|
// heading.add("Y - Koordinate");
|
|
// for (Point p : getModel().getNodes()) {
|
|
// LinkedList<String> rowEntry = new LinkedList<>();
|
|
// rowEntry.add(p.getX().toString());
|
|
// rowEntry.add(p.getY().toString());
|
|
// rows.add(rowEntry);
|
|
// }
|
|
// getView().logHeading("Schnittpunkte der Dualen Geraden:");
|
|
// getView().createTable(heading, rows);
|
|
}
|
|
|
|
public Point calcIntersection(Line a, Line b) {
|
|
Line p1 = a;
|
|
Line p2 = b;
|
|
|
|
Double x = (p1.getB() - p2.getB()) / (p2.getM() - p1.getM());
|
|
Double y = ((p1.getM() * p2.getB()) - (p2.getM() * p1.getB())) / (p1.getM() - p2.getM());
|
|
|
|
return new Point(x, y);
|
|
}
|
|
|
|
public void calcArrangementNodes() {
|
|
try {
|
|
Thread thread = new Thread(() -> {
|
|
Double xMinimum = Double.MAX_VALUE;
|
|
Double xMaximum = Double.MIN_VALUE;
|
|
Double yMinimum = Double.MAX_VALUE;
|
|
Double yMaximum = Double.MIN_VALUE;
|
|
for (int i = 0; i < getLines().size(); i++) {
|
|
for (int j = i; j < getLines().size(); j++) {
|
|
if (i != j) {
|
|
Point intersection = calcIntersection(getLines().get(j), getLines().get(i));
|
|
model.addNode(intersection);
|
|
if (intersection.getX() != Double.POSITIVE_INFINITY && intersection.getX() != Double.NEGATIVE_INFINITY &&
|
|
intersection.getY() != Double.POSITIVE_INFINITY && intersection.getY() != Double.NEGATIVE_INFINITY ){
|
|
xMinimum = xMinimum > intersection.getX() ? intersection.getX() : xMinimum;
|
|
xMaximum = xMaximum < intersection.getX() ? intersection.getX() : xMaximum;
|
|
yMinimum = yMinimum > intersection.getY() ? intersection.getY() : yMinimum;
|
|
yMaximum = yMaximum < intersection.getY() ? intersection.getY() : yMaximum;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
model.setxMinimum(xMinimum);
|
|
model.setxMaximum(xMaximum);
|
|
model.setyMaximum(yMaximum);
|
|
model.setyMinimum(yMinimum);
|
|
});
|
|
thread.start();
|
|
thread.join();
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public LinkedList<Line> calcArrangementLines() {
|
|
LinkedList<Line> lineCoordinates = new LinkedList<>();
|
|
double x1 = -1000;
|
|
double x2 = 1000;
|
|
|
|
for (Line point : model.getLines()) {
|
|
double y1 = (point.getM() * x1 + point.getB());
|
|
double y2 = (point.getM() * x2 + point.getB());
|
|
Line line = new Line(x1, x2, y1, y2);
|
|
line.setId(point.getId());
|
|
lineCoordinates.add(line);
|
|
}
|
|
|
|
return lineCoordinates;
|
|
}
|
|
|
|
public void startImport(File file){
|
|
DataImporter importer = new DataImporter(file, this);
|
|
importer.addObserver(this);
|
|
importer.run();
|
|
}
|
|
|
|
/***************************************************************************************************************************
|
|
* Getter und Setter Methoden
|
|
***************************************************************************************************************************/
|
|
|
|
|
|
public Arrangement getModel() {
|
|
return model;
|
|
}
|
|
|
|
public void setModel(Arrangement model) {
|
|
this.model = model;
|
|
}
|
|
|
|
public MainFrame getView() {
|
|
return view;
|
|
}
|
|
|
|
public void setView(MainFrame view) {
|
|
this.view = view;
|
|
}
|
|
|
|
public LinkedList<Line> getLines() {
|
|
return this.model.getLines();
|
|
}
|
|
|
|
public void setLines(LinkedList<Line> lines) {
|
|
this.model.setLines(lines);
|
|
}
|
|
|
|
}
|