algorithms-for-computing-li.../LinearRegressionTool/src/main/java/de/wwwu/awolf/presenter/AbstractPresenter.java

207 lines
5.6 KiB
Java

package de.wwwu.awolf.presenter;
import de.wwwu.awolf.model.LineModel;
import de.wwwu.awolf.model.communication.Data;
import de.wwwu.awolf.presenter.evaluation.EvaluateAlgorithms;
import de.wwwu.awolf.presenter.util.IntersectionComputer;
import de.wwwu.awolf.presenter.util.Logging;
import de.wwwu.awolf.view.MainFrame;
import javax.swing.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Flow;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
*
* @Author: Armin Wolf
* @Email: a_wolf28@uni-muenster.de
* @Date: 10.09.2017.
*/
public abstract class AbstractPresenter implements Flow.Subscriber<Data> {
private final ExecutorService executor;
private LineModel model;
private MainFrame view;
private EvaluateAlgorithms eval;
/**
* Konstruktor
*
* @param model Modell
* @param view View
*/
public AbstractPresenter(LineModel model, MainFrame view) {
this.model = model;
this.view = view;
executor = Executors.newCachedThreadPool();
}
@Override
public void onSubscribe(Flow.Subscription subscription) {
Logging.logInfo("New Subscription: " + subscription.toString());
subscription.request(15);
}
@Override
public void onNext(Data data) {
switch (data.getType()) {
case EVAL_D:
evaluatedData(data);
break;
case EVAL_DS:
evaluatedDatas(data);
break;
case EVAL_T:
evaluatedTypes(data);
break;
case LINES_RES:
visualizeResults(data);
break;
case LINES_RES_MULT:
visualizeMultipleData(data);
break;
case LMS:
visualizeLmsAlgorithm(data);
break;
case RM:
visualizeRmAlgorithm(data);
break;
case TS:
visualizeTsAlgorithm(data);
break;
case IMPORT:
handleImport(data);
break;
case EXPORT:
handleExport(data);
break;
case GENERATOR:
Logging.logInfo("Generierung war Erfolgreich");
break;
default:
break;
}
}
protected abstract void handleExport(Data data);
protected abstract void handleImport(Data data);
protected abstract void visualizeTsAlgorithm(Data data);
protected abstract void visualizeRmAlgorithm(Data data);
protected abstract void visualizeLmsAlgorithm(Data data);
protected abstract void visualizeMultipleData(Data data);
protected abstract void visualizeResults(Data data);
protected abstract void evaluatedTypes(Data data);
protected abstract void evaluatedDatas(Data data);
protected abstract void evaluatedData(Data data);
@Override
public void onError(Throwable throwable) {
}
@Override
public void onComplete() {
}
/**
* Startet das parallele Berechnen der Schnittpunkte der Geraden die im Modell enthalten sind.
*/
private void startIntersectionCalculation() {
getExecutor().submit(() -> {
getModel().resetRanges();
IntersectionComputer intersectionComputer = new IntersectionComputer(getModel().getLines());
getModel().setNodes(intersectionComputer.compute());
getModel().setxMaximum(intersectionComputer.getxMaximum());
getModel().setxMinimum(intersectionComputer.getxMinimum());
getModel().setyMaximum(intersectionComputer.getyMaximum());
getModel().setyMinimum(intersectionComputer.getyMinimum());
});
}
/**
* Wrapper Methode die das berechnen der Schnittpunkte anstößt und die Ergebnisse(Anzahl der Schnittpunkte)
* visuell darstellt.
*/
protected void computeIntersections() {
getExecutor().submit(() -> {
long start, end;
start = System.currentTimeMillis();
startIntersectionCalculation();
end = System.currentTimeMillis();
Logging.logInfo("Zeit: " + (end - start) / 1000);
});
//darstellung der Ergebnisse
SwingUtilities.invokeLater(() -> {
getView().enableFunctionality();
getView().getProgressDialog().dispose();
});
Logging.logInfo("Informationen zu dem aktuellen Modell");
Logging.logInfo("Anzahl der Geraden: " + getModel().getLines().size() + ".");
Logging.logInfo("Anzahl der Schnittpunkte: " + getModel().getNodes().size() + ".");
Logging.logInfo("Import war Erfolgreich!");
}
/**
* @return das zu grunde legende Modell
*/
public LineModel getModel() {
return model;
}
/**
* @param model das zu grunde legende Modell
*/
public void setModel(LineModel model) {
this.model = model;
}
/**
* @return die zu grunde legende View
*/
public MainFrame getView() {
return view;
}
/**
* @param view die zu grunde legende View
*/
public void setView(MainFrame view) {
this.view = view;
}
/**
* @return Evaluation
*/
public EvaluateAlgorithms getEval() {
return eval;
}
/**
* @param eval Evaluation
*/
public void setEval(EvaluateAlgorithms eval) {
this.eval = eval;
}
public ExecutorService getExecutor() {
return executor;
}
}