2020-04-05 19:11:50 +00:00
|
|
|
package de.wwwu.awolf.presenter;
|
|
|
|
|
2020-04-05 20:27:56 +00:00
|
|
|
import de.wwwu.awolf.model.dao.Line;
|
|
|
|
import de.wwwu.awolf.model.dao.LineModel;
|
|
|
|
import de.wwwu.awolf.model.communication.Message;
|
2020-04-05 19:11:50 +00:00
|
|
|
import de.wwwu.awolf.presenter.algorithms.Algorithm;
|
|
|
|
import de.wwwu.awolf.presenter.algorithms.AlgorithmHandler;
|
|
|
|
import de.wwwu.awolf.presenter.data.DataHandler;
|
|
|
|
import de.wwwu.awolf.presenter.evaluation.EvaluatationHandler;
|
|
|
|
import de.wwwu.awolf.presenter.util.Logging;
|
|
|
|
import de.wwwu.awolf.view.ViewController;
|
2020-04-06 11:07:10 +00:00
|
|
|
import java.util.Map;
|
2020-04-05 19:11:50 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Flow;
|
|
|
|
import javafx.application.Platform;
|
|
|
|
import javafx.beans.property.BooleanProperty;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
|
|
*
|
|
|
|
* @Author: Armin Wolf
|
|
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
|
|
* @Date: 10.09.2017.
|
|
|
|
*/
|
2020-04-05 20:27:56 +00:00
|
|
|
public abstract class AbstractPresenter implements Flow.Subscriber<Message> {
|
2020-04-05 19:11:50 +00:00
|
|
|
|
|
|
|
private final ExecutorService executor;
|
|
|
|
private LineModel model;
|
|
|
|
private ViewController view;
|
|
|
|
private EvaluatationHandler evaluatationHandler;
|
|
|
|
private DataHandler dataHandler;
|
|
|
|
private AlgorithmHandler algorithmHandler;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Konstruktor
|
|
|
|
*/
|
|
|
|
public AbstractPresenter() {
|
|
|
|
Logging.logDebug("Create instance of Presenter.");
|
|
|
|
this.executor = Executors.newCachedThreadPool();
|
|
|
|
this.dataHandler = new DataHandler(this);
|
|
|
|
this.algorithmHandler = AlgorithmHandler.getInstance();
|
|
|
|
//empty model
|
|
|
|
this.model = new LineModel();
|
|
|
|
//init values null
|
|
|
|
this.view = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSubscribe(Flow.Subscription subscription) {
|
|
|
|
Logging.logInfo("New Subscription: " + subscription.toString());
|
|
|
|
subscription.request(15);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-04-05 20:27:56 +00:00
|
|
|
public void onNext(Message message) {
|
|
|
|
Logging.logDebug("Presenter received message. Type: " + message.getType());
|
|
|
|
switch (message.getType()) {
|
|
|
|
case EVALUATION:
|
|
|
|
evaluatedDatas(message);
|
2020-04-05 19:11:50 +00:00
|
|
|
break;
|
|
|
|
case ALGORITHM:
|
2020-04-05 20:27:56 +00:00
|
|
|
visualizeAlgorithm(message);
|
|
|
|
break;
|
|
|
|
case DATA_PROVIDER:
|
|
|
|
enableStartAlgorithmButton();
|
2020-04-05 19:11:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComplete() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an algorithm specified by a type
|
|
|
|
*
|
|
|
|
* @param type algorithm type
|
|
|
|
* @param lines set of lines
|
|
|
|
*/
|
2020-04-06 11:07:10 +00:00
|
|
|
public void executeAlgorithmByType(Algorithm.Type type, final Map<String, String> properties, Set<Line> lines) {
|
|
|
|
this.algorithmHandler.runAlgorithmByType(type, properties, lines);
|
2020-04-05 19:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an algorithm specified by a type
|
|
|
|
*
|
|
|
|
* @param type algorithm type
|
|
|
|
* @param lines set of lines
|
|
|
|
*/
|
2020-04-06 11:07:10 +00:00
|
|
|
public void executeAlgorithmByType(Algorithm.Type type, final Map<String, String> properties, Set<Line> lines,
|
2020-04-05 19:11:50 +00:00
|
|
|
BooleanProperty guiFlag) {
|
2020-04-06 11:07:10 +00:00
|
|
|
this.algorithmHandler.runAlgorithmByType(type, properties, lines, guiFlag);
|
2020-04-05 19:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an algorithm specified by a type (use the Lines from the LineModel)
|
|
|
|
*
|
|
|
|
* @param type algorithm type
|
|
|
|
*/
|
2020-04-06 11:07:10 +00:00
|
|
|
public Line executeAlgorithmByType(Algorithm.Type type, final Map<String, String> properties, BooleanProperty guiFlag) {
|
2020-04-05 19:11:50 +00:00
|
|
|
if (getModel().getSize() == 0) {
|
|
|
|
Logging.logDebug("No lines in the Model. Nothing to calculate.");
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
} else {
|
|
|
|
Logging.logDebug("AlgorithmHandler will start " + type.getLabel() + ", with " + getModel().getSize());
|
2020-04-06 11:07:10 +00:00
|
|
|
return this.algorithmHandler.runAlgorithmByType(type, properties, getModel().getLines(), guiFlag);
|
2020-04-05 19:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public LineModel getModel() {
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ViewController getView() {
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerView(ViewController view) {
|
|
|
|
this.view = view;
|
|
|
|
Logging.logDebug("View has been set.");
|
|
|
|
|
|
|
|
//customize gui
|
|
|
|
Platform.runLater(() -> this.view.initGui());
|
|
|
|
}
|
|
|
|
|
|
|
|
EvaluatationHandler getEvaluatationHandler() {
|
|
|
|
return evaluatationHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataHandler getDataHandler() {
|
|
|
|
return dataHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ExecutorService getExecutor() {
|
|
|
|
return Objects.requireNonNullElseGet(executor, Executors::newCachedThreadPool);
|
|
|
|
}
|
2020-04-05 20:27:56 +00:00
|
|
|
|
|
|
|
public AlgorithmHandler getAlgorithmHandler() {
|
|
|
|
return algorithmHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void enableStartAlgorithmButton();
|
|
|
|
|
|
|
|
protected abstract void visualizeAlgorithm(Message message);
|
|
|
|
|
|
|
|
protected abstract void evaluatedDatas(Message message);
|
2020-04-05 19:11:50 +00:00
|
|
|
}
|