Package randomevents

Source Code of randomevents.RandomEventsMainController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package randomevents;

import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import randomevents.discreteEvents.Archer;
import randomevents.discreteEvents.Tourney;
import randomevents.generator.GeneratorChecking;
import randomevents.generator.NormalBoxMullerRandomGenerator;
import randomevents.generator.NormalCLTRandomGenerator;
import randomevents.generator.RandomGenerator;
import randomevents.generator.Statistical;
import randomevents.generator.UniformRandomGenerator;
import randomevents.generator.UsersRandomGenerator;
import randomevents.hive.Hive;
import randomevents.telephone.Request;
import randomevents.telephone.TelephoneCenter;

/**
*
* @author FormularHunter
*/
public class RandomEventsMainController implements Initializable {

    @FXML
    private TextField rTextField;
    @FXML
    private TextField countTextField;
    @FXML
    private TextField partitionTextField;
    @FXML
    private NumberAxis statisticalHistogramXAxis;
    @FXML
    private NumberAxis randomHistogramXAxis;
    @FXML
    private AreaChart randomHistogram;
    @FXML
    private AreaChart statisticalHistogram;
    @FXML
    private static TextArea logTextArea;
    @FXML
    private TextField randomNumbersCountTextField;
    @FXML
    private Label dispersionLabel;
    @FXML
    private Label expectationLabel;
    @FXML
    private Label secondInitialMomentLabel;
    @FXML
    private Label thirdInitialMomentLabel;
    @FXML
    private WebView chiWebView;
    @FXML
    private WebView lambdaWebView;
    @FXML
    private TextField excellentArcherCountField;
    @FXML
    private TextField goodArcherCountField;
    @FXML
    private TextField mediocreArcherCountField;
    @FXML
    private TextField badArcherCountField;
    @FXML
    private TextField excellentArcherHitPField;
    @FXML
    private TextField goodArcherHitPField;
    @FXML
    private TextField mediocreArcherHitPField;
    @FXML
    private TextField badArcherHitPField;
    @FXML
    private TextField countEventsTextField;
    @FXML
    private TextField beeStepsTextField;
    @FXML
    private ComboBox<String> reliabilityComboBox;
    @FXML
    private Label excellentArcherChoosenLabel;
    @FXML
    private Label goodArcherChoosenLabel;
    @FXML
    private Label mediocreArcherChoosenLabel;
    @FXML
    private Label badArcherChoosenLabel;
    @FXML
    private Label confidenceIntervalLabel;
    @FXML
    private Label pHitLabel;
    @FXML
    private Label analitycalPLabel;
    @FXML
    private RadioButton UniformRadioButton;
    @FXML
    private RadioButton UsersRandomRadioButton;
    @FXML
    private RadioButton NormalCLTRadioButton;
    @FXML
    private RadioButton NormalBoxMullerRadioButton;
    @FXML
    private RadioButton BeeInHiveRadioButton;
    @FXML
    private TextField beginTextField;
    @FXML
    private TextField endTextField;
    @FXML
    private Label maxYlabel;
    @FXML
    private TextField expectationTextField;
    @FXML
    private TextField dispersionTextField;
    private WebEngine chiWebEngine;
    private WebEngine lambdaWebEngine;
    private XYChart.Series<String, Number> randomHistogramSeries = new XYChart.Series<>();
    private XYChart.Series<String, Number> statisticalHistogramSeries = new XYChart.Series<>();
    private Histogram histogram = new Histogram();
    private Double[] randoms;
    private int partition;
    private Statistical statistical = new Statistical();
    @FXML
    private TextField deviceCountTextField;
    @FXML
    private TextField requestCountTextField;
    @FXML
    private TextField arriveTimeTextField;
    @FXML
    private TextField serveTimeTextField;
    @FXML
    private TableView telephoneTable;
    @FXML
    private TableView devicesTable;
    @FXML
    private Label statusLabel;

    @FXML
    private void startButtonAction(ActionEvent event) {
        if (UniformRadioButton.isSelected()) {
            beginTextField.setText("0");
            endTextField.setText("1");
        }
        if (NormalCLTRadioButton.isSelected() || NormalBoxMullerRadioButton.isSelected()) {
            beginTextField.setText("0");
            endTextField.setText("2");
        }
        if (UsersRandomRadioButton.isSelected()) {
            beginTextField.setText("0");
            endTextField.setText("5");
        }
        if (BeeInHiveRadioButton.isSelected()) {
            beginTextField.setText("0");
            beeStepsTextField.setText("8");
            endTextField.setText("9");
            partitionTextField.setText("9");
        }
        compute();
    }

    @FXML
    private void plusOneAction(ActionEvent event) {
        int partition = Integer.parseInt(partitionTextField.getText());
        partition++;
        partitionTextField.setText(Integer.toString(partition));
        updateHistograms();
    }

    @FXML
    private void minusOneAction(ActionEvent event) {
        int partition = Integer.parseInt(partitionTextField.getText());
        partition--;
        partitionTextField.setText(Integer.toString(partition));
        updateHistograms();
    }

    @FXML
    private void keyReleasedGenerateField(KeyEvent event) {
        compute();
    }

    @FXML
    private void keyReleasedHistogramField(KeyEvent event) {
        updateHistograms();
    }

    @FXML
    private void keyReleasedRandomNumbersCountTextField(KeyEvent event) {
        printRandomNumbers();
    }

    @FXML
    private void clearTextAreaAction(ActionEvent event) {
        logTextArea.clear();
    }

    @FXML
    private void tourneyButtonAction(ActionEvent event) {
        Archer[] archers = new Archer[4];


        int r;
        int n;
        String reliability;
        try {
            int excellentArcherCount = Integer.parseInt(excellentArcherCountField.getText());
            int goodArcherCount = Integer.parseInt(goodArcherCountField.getText());
            int mediocreArcherCount = Integer.parseInt(mediocreArcherCountField.getText());
            int badArcherCount = Integer.parseInt(badArcherCountField.getText());
            int archerCount = excellentArcherCount + goodArcherCount + mediocreArcherCount + badArcherCount;

            archers[0] = new Archer("Отличный",
                    Double.parseDouble(excellentArcherHitPField.getText()),
                    (double) excellentArcherCount / (double) archerCount, excellentArcherCount);
            archers[1] = new Archer("Хороший",
                    Double.parseDouble(goodArcherHitPField.getText()),
                    archers[0].getPChoosen() + (double) goodArcherCount / (double) archerCount, goodArcherCount);
            archers[2] = new Archer("Посредственный",
                    Double.parseDouble(mediocreArcherHitPField.getText()),
                    archers[1].getPChoosen() + (double) mediocreArcherCount / (double) archerCount, mediocreArcherCount);
            archers[3] = new Archer("Плохой",
                    Double.parseDouble(badArcherHitPField.getText()),
                    archers[2].getPChoosen() + (double) badArcherCount / (double) archerCount, badArcherCount);

            excellentArcherChoosenLabel.setText(String.format("%d...%4.2f", 0, archers[0].getPChoosen()));
            goodArcherChoosenLabel.setText(String.format("%4.2f...%4.2f", archers[0].getPChoosen(), archers[1].getPChoosen()));
            mediocreArcherChoosenLabel.setText(String.format("%4.2f...%4.2f", archers[1].getPChoosen(), archers[2].getPChoosen()));
            badArcherChoosenLabel.setText(String.format("%4.2f...%d", archers[2].getPChoosen(), 1));
            n = Integer.parseInt(countEventsTextField.getText());
            r = Integer.parseInt(rTextField.getText());
            reliability = reliabilityComboBox.getValue();
        } catch (Exception ex) {
            print(ex.getMessage());
            return;
        }
        UniformRandomGenerator randomGenerator = new UniformRandomGenerator(r);
        Tourney tourney = new Tourney();
        tourney.go(archers, randomGenerator, n);
        System.out.println(tourney.getConfidenceInterval(reliability));
        pHitLabel.setText(Double.toString(tourney.getPHit()));
        confidenceIntervalLabel.setText(tourney.getConfidenceInterval(reliability));
        analitycalPLabel.setText(String.format("%6.3f", tourney.analitycal(archers)));
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        randomHistogram.setLegendVisible(false);
        randomHistogram.getData().add(randomHistogramSeries);
        statisticalHistogram.setLegendVisible(false);
        statisticalHistogram.getData().add(statisticalHistogramSeries);
        dispersionLabel.setText("");
        expectationLabel.setText("");
        secondInitialMomentLabel.setText("");
        thirdInitialMomentLabel.setText("");
        lambdaWebEngine = lambdaWebView.getEngine();
        chiWebEngine = chiWebView.getEngine();
        reliabilityComboBox.setItems(FXCollections.observableArrayList(
                "0.90",
                "0.95",
                "0.96",
                "0.97",
                "0.98",
                "0.99",
                "0.999"));
        reliabilityComboBox.setValue("0.95");
        confidenceIntervalLabel.setText("");
        pHitLabel.setText("");
        analitycalPLabel.setText("");

        expectationTextField.setDisable(true);
        dispersionTextField.setDisable(true);
        beginTextField.setDisable(true);
        endTextField.setDisable(true);
        rTextField.setDisable(true);

        ((TableColumn) devicesTable.getColumns().get(0))
                .setCellValueFactory(new PropertyValueFactory<DeviceInfo, Integer>("number"));
        ((TableColumn) devicesTable.getColumns().get(1))
                .setCellValueFactory(new PropertyValueFactory<DeviceInfo, String>("load"));

        ((TableColumn) telephoneTable.getColumns().get(0))
                .setCellValueFactory(new PropertyValueFactory<RequestInfo, String>("arrive"));
        ((TableColumn) telephoneTable.getColumns().get(1))
                .setCellValueFactory(new PropertyValueFactory<RequestInfo, String>("serve"));
        ((TableColumn) telephoneTable.getColumns().get(2))
                .setCellValueFactory(new PropertyValueFactory<RequestInfo, String>("served"));
        ((TableColumn) telephoneTable.getColumns().get(3))
                .setCellValueFactory(new PropertyValueFactory<RequestInfo, String>("deviceNumber"));
    }

    public static void print(String message) {
        logTextArea.setText(logTextArea.getText() + '\n' + message);
    }

    private void printRandomNumbers() {
        String message = "";
        int n = 0;
        try {
            n = Integer.parseInt(randomNumbersCountTextField.getText());
            n = (n > randoms.length) ? randoms.length : n;
            message = String.format("Вывод сгенерированных чисел %d из %d:", n, randoms.length);
            for (int i = 0; i < n; i++) {
                message = message + '\n' + randoms[i];
            }
        } catch (Exception e) {
            return;
        } finally {
            print(message);
        }

    }
    int a = 0;
    int b = 1;

    /**
     * Расчёт значений гистограммы
     */
    private void compute() {
        showControlls();
        // Получение данных из интерфейса
        int r;
        int count;
        double m, d;
        int steps;
        try {
            r = Integer.parseInt(rTextField.getText());
            count = Integer.parseInt(countTextField.getText());
            partition = Integer.parseInt(partitionTextField.getText());
            m = Double.parseDouble(expectationTextField.getText());
            d = Double.parseDouble(dispersionTextField.getText());
            a = Integer.parseInt(beginTextField.getText());
            b = Integer.parseInt(endTextField.getText());
            steps = Integer.parseInt(beeStepsTextField.getText());
        } catch (Exception ex) {
            print(ex.getMessage());
            return;
        }

        RandomGenerator randomGenerator = new UniformRandomGenerator(r);
        if (UsersRandomRadioButton.isSelected()) {
            randomGenerator = new UsersRandomGenerator(a, b);
            maxYlabel.setText(String.format("%6.2f", ((UsersRandomGenerator) randomGenerator).maxY));
        } else if (NormalCLTRadioButton.isSelected()) {
            randomGenerator = new NormalCLTRandomGenerator(m, d);
        } else if (NormalBoxMullerRadioButton.isSelected()) {
            randomGenerator = new NormalBoxMullerRandomGenerator(m, d, 0, 1);
        } else if (BeeInHiveRadioButton.isSelected()) {
            randomGenerator = new Hive(steps);
        }
        randoms = randomGenerator.randNumbers(count);

        updateHistograms();

        statistical.compute(randoms);

        dispersionLabel.setText(String.format("%6.4f", statistical.getDispersion()));
        expectationLabel.setText(String.format("%6.4f", statistical.getExpectation()));
        secondInitialMomentLabel.setText(String.format("%6.4f", statistical.getSecondInitialMoment()));
        thirdInitialMomentLabel.setText(String.format("%6.4f", statistical.getThirdInitialMoment()));

        printRandomNumbers();
        GeneratorChecking generatorChecking = new GeneratorChecking();
        chiWebEngine.loadContent(String.format("<html><h2><center>χ<sup>2</sup> = %6.4f</center></h2></html>", generatorChecking.pearson(histogram, a, b, count, randomGenerator)));
        lambdaWebEngine.loadContent(String.format("<html><h2><center>λ = %6.4f</center></h2></html>", generatorChecking.kolmogorov(histogram, randoms, randomGenerator)));
    }

    private void updateHistograms() {
        partition = Integer.parseInt(partitionTextField.getText());
        histogram.create(a, b, partition, randoms);

        updateHistogram(randomHistogramXAxis, randomHistogramSeries, histogram.getCounts());
        updateHistogram(statisticalHistogramXAxis, statisticalHistogramSeries, histogram.getStatisticalDistributionCounts());
    }

    private void updateHistogram(NumberAxis histogramXAxis, XYChart.Series<String, Number> series, HashMap<Double, Double> randomCounts) {
        try {
            series.getData().clear();
        } catch (Exception ex) {
        }

        histogramXAxis.setUpperBound(b);
        // Сортировка новых данных по возрастанию
        Double[] keys = new Double[randomCounts.keySet().size()];
        randomCounts.keySet().toArray(keys);
        Arrays.sort(keys);

        for (int k = 0; k < keys.length - 1; k++) {
            series.getData().add(new XYChart.Data(keys[k], 0));
            series.getData().add(new XYChart.Data(keys[k], randomCounts.get(keys[k])));
            series.getData().add(new XYChart.Data(keys[k + 1], randomCounts.get(keys[k])));
            series.getData().add(new XYChart.Data(keys[k + 1], 0));
        }
        series.getData().add(new XYChart.Data(keys[keys.length - 1], 0));
        series.getData().add(new XYChart.Data(keys[keys.length - 1], randomCounts.get(keys[keys.length - 1])));
        series.getData().add(new XYChart.Data(b, randomCounts.get(keys[keys.length - 1])));
        series.getData().add(new XYChart.Data(b, 0));


    }

    private void showControlls() {
        expectationTextField.setDisable(!(NormalCLTRadioButton.isSelected() || NormalBoxMullerRadioButton.isSelected()));
        dispersionTextField.setDisable(!(NormalCLTRadioButton.isSelected() || NormalBoxMullerRadioButton.isSelected()));
        beginTextField.setDisable(!(UsersRandomRadioButton.isSelected()));
        endTextField.setDisable(!(UsersRandomRadioButton.isSelected() || NormalCLTRadioButton.isSelected() || NormalBoxMullerRadioButton.isSelected()));
        rTextField.setDisable(!(UniformRadioButton.isSelected()));
        maxYlabel.setVisible(UsersRandomRadioButton.isSelected());
        lambdaWebView.setVisible(UniformRadioButton.isSelected());
        beeStepsTextField.setDisable(!BeeInHiveRadioButton.isSelected());
        if (BeeInHiveRadioButton.isSelected()) {
            int steps = Integer.parseInt(beeStepsTextField.getText());
            endTextField.setText(Integer.toString(steps + 1));
            partitionTextField.setText(Integer.toString(steps + 1));
        }
    }

    @FXML
    private void telephoneActionButton(ActionEvent event) {
        telephoneTable.getItems().clear();
        devicesTable.getItems().clear();
        telephoneStart();
    }
   
    @FXML
    private void telephoneAction(KeyEvent event) {
        telephoneTable.getItems().clear();
        devicesTable.getItems().clear();
        telephoneStart();
    }

    private void telephoneStart() {
        int deviceCount, requestCount;
        double arriveTime, serveTime;
        try {
            deviceCount = Integer.parseInt(deviceCountTextField.getText());
            requestCount = Integer.parseInt(requestCountTextField.getText());
            arriveTime = Double.parseDouble(arriveTimeTextField.getText());
            serveTime = Double.parseDouble(serveTimeTextField.getText());
        } catch (Exception ex) {
            return;
        }

        TelephoneCenter telephoneCenter = new TelephoneCenter(deviceCount, serveTime, arriveTime);
        double[] deviceLoad = telephoneCenter.start(requestCount);

        for (int i = 0; i < deviceLoad.length; i++) {
            DeviceInfo deviceInfo = new DeviceInfo(i, deviceLoad[i]);
            devicesTable.getItems().add(deviceInfo);
        }
       
        Double[] arrives = new Double[telephoneCenter.getRequests().size()];
        Double[] serves = new Double[telephoneCenter.getRequests().size()];
        int i = 0;
        for (Request request : telephoneCenter.getRequests()) {
            telephoneTable.getItems().add(new RequestInfo(request));
            arrives[i] = request.getArrive();
            serves[i] = request.getServe();
            i++;
        }
        Statistical s = new Statistical();
        s.compute(arrives);
        double arriveD = s.getDispersion();
        s.compute(serves);
        double arriveS = s.getDispersion();
        statusLabel.setText(String.format("Обработано %d заявок, отказано в обработке %d заявке. Дисперсия поступления %6.2f; дисперсия времени обслуживания %6.2f",
                telephoneCenter.servedRequests,
                requestCount - telephoneCenter.servedRequests,
                arriveD,
                arriveS));
    }

    public static class DeviceInfo {

        private final SimpleIntegerProperty number;
        private final SimpleStringProperty load;

        private DeviceInfo(Integer number, Double load) {
            this.number = new SimpleIntegerProperty(number);
            this.load = new SimpleStringProperty(String.format("%6.2f", load));
        }

        public Integer getNumber() {
            return number.get();
        }

        public String getLoad() {
            return load.get();
        }
    }

    public static class RequestInfo {

        private final SimpleStringProperty arrive;
        private final SimpleStringProperty serve;
        private final SimpleStringProperty served;
        private final SimpleIntegerProperty deviceNumber;

        private RequestInfo(Request request) {
            this(request.getArrive(), request.getServe(), request.isServed(), request.getDeviceNumber());
        }

        private RequestInfo(double arrive, double serve, boolean served, int deviceNumber) {
            this.arrive = new SimpleStringProperty(String.format("%6.2f", arrive));
            this.serve = new SimpleStringProperty(String.format("%6.2f", serve));
            this.served = served ? new SimpleStringProperty("Да") : new SimpleStringProperty("Нет");
            this.deviceNumber = new SimpleIntegerProperty(deviceNumber);
        }

        public String getArrive() {
            return arrive.get();
        }

        public String getServe() {
            return serve.get();
        }

        public String getServed() {
            return served.get();
        }

        public Integer getDeviceNumber() {
            return deviceNumber.get();
        }
    }
}
TOP

Related Classes of randomevents.RandomEventsMainController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.