RosAdProgram.model.Model.java Source code

Java tutorial

Introduction

Here is the source code for RosAdProgram.model.Model.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package RosAdProgram.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;
import RosAdProgram.view.ConfigGUI;
import RosAdProgram.view.RosAdGUI;
import RosAdProgram.view.StatisticsGUI;
import static java.lang.Math.ceil;
import static java.lang.Math.sqrt;
import static org.apache.commons.math3.special.Erf.erf;

/**
 *
 * @author 
 */
public class Model {
    private RosAdGUI rosAdGUI;
    private StatisticsGUI statisticsGUI;

    private final double[] errorRatioArray = new double[3]; // ?? ?  
    private long fileSize; //  
    private int blockError = 0; // ?  
    private int byteError = 0; // -- 
    private int bitError = 0; // -- 
    private boolean byteErrorFlag = false; //  ??   . ???   mask().
    private int blockSize = 8; //   ( )

    private File openPath, savePath; // ? ??     
    private double usefulVoltageValue = 7.35; // ??  ?  
    private int speedValue = 50; // ?   
    private double noiseVoltageValue = 0.6; // ? ? 
    private int frequencyValue = 50; // ? 
    private double mistakeProbability = 0; // ?? 

    public void setRosAdGUIreference(RosAdGUI rosAdGUI) {
        this.rosAdGUI = rosAdGUI; // ?? ConfigGUI ? Model
    }

    public void setStatisticsGUIreference(StatisticsGUI statistics) {
        this.statisticsGUI = statistics; // ?? StatisticsGUI ? Model
    }

    public int getBlockSize() {
        return blockSize;
    }

    public void setBlockSize(int size) {
        this.blockSize = size;
    }

    // Getter  Setter  
    public void setFileSize() {
        fileSize = getOpenPath().length(); //?   (??    )   
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setOpenPath(File openPath) {
        this.openPath = openPath;
    }

    public void setSavePath(File savePath) {
        this.savePath = savePath;
    }

    public File getOpenPath() {
        return this.openPath;
    }

    public File getSavePath() {
        return this.savePath;
    }

    // ?  ??  ?
    public void setUsefulVoltge(float numberInput) {
        usefulVoltageValue = numberInput;
    }

    public double getUsefulVoltage() {
        return usefulVoltageValue;
    }

    // ? 
    public void setSpeed(int value) {
        speedValue = value;
    }

    public int getSpeed() {
        return speedValue;
    }

    public String getStringSpeed() {
        String speed = String.valueOf(speedValue);
        return speed;
    }

    // ? ? 
    public void setNoiseVoltage(float value) {
        noiseVoltageValue = value;
    }

    public double getNoiseVoltage() {
        return noiseVoltageValue;
    }

    // Getter  Setter ? 
    public void setProbability(double prbability) {
        mistakeProbability = (1 - prbability);
    }

    public double getProbability() {
        return mistakeProbability;
    }

    //    ?     
    public int getPercent(long number) { // number - ? ?   
        long tempFileSize = getFileSize();
        int result = (int) (number / (tempFileSize * 0.01)); // ?  tempFileSize
        return result;
    }

    //  ?  ? ?      
    public void setBlockError(int number) {
        blockError = blockError + number;
    }

    public int getBlockError() {
        return blockError;
    }

    public void setByteError(int number) {
        byteError = byteError + number;
    }

    public int getByteError() {
        return byteError;
    }

    public void setBitError(int number) {
        bitError = bitError + number;
    }

    public int getBitError() {
        return bitError;
    }

    public void setByteErrorFlag(boolean c) {
        byteErrorFlag = c;
    }

    public boolean getByteErrorFlag() {
        return byteErrorFlag;
    }

    public void setBlockErrorFlag(boolean c) {
        byteErrorFlag = c;
    }

    public boolean getBlockErrorFlag() {
        return byteErrorFlag;
    }

    public void setFrequency(int numberInput) {
        frequencyValue = numberInput;
    }

    public int getFrequency() {
        return frequencyValue;
    }

    public long getFileSizeInBits() { //     
        return getFileSize() * 8;
    }

    public double getFileSizeInBlocks() { //    
        long tempFileSize = getFileSize(); //     
        int tempBlockSize = getBlockSize(); //     
        double blocks = ceil(tempFileSize / tempBlockSize); //  ?    ?
        return blocks;
    }

    public double ratioCalc(double error, double total) { // ?? ? 
        double ratio = error / total;
        return ratio;
    }

    public void resetErrors() { // ? ?    ?? ??
        bitError = 0;
        byteError = 0;
        blockError = 0;
    }

    public void fillErrorRatioArray() { // ? ?? ? 
        errorRatioArray[0] = ratioCalc(getBitError(), getFileSizeInBits()); //  
        System.out.println("  " + errorRatioArray[0]);
        errorRatioArray[1] = ratioCalc(getByteError(), getFileSize()); //  
        System.out.println("  " + errorRatioArray[1]);
        errorRatioArray[2] = ratioCalc(getBlockError(), getFileSizeInBlocks()); //  
        System.out.println("  " + errorRatioArray[2]);
    }

    public double[] getErrorRatioArray() {
        return errorRatioArray;
    }

    //     ?  ??   
    public void copy() {
        final File open = getOpenPath();
        final File save = getSavePath();
        final double probability = getProbability();
        resetErrors();

        SwingWorker<Void, Integer[]> worker = new SwingWorker<Void, Integer[]>() { //  ? progressBar
            @Override
            protected Void doInBackground() throws Exception {
                FileInputStream in = null;
                FileOutputStream out = null;

                try {
                    try {
                        in = new FileInputStream(open);
                    } catch (FileNotFoundException ex) {
                        System.out.println("   ");
                        Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        out = new FileOutputStream(save);
                    } catch (FileNotFoundException ex) {
                        System.out.println("?   ");
                        Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    int c;

                    try { // Noise adding 
                        Integer[] data = new Integer[6];
                        data[0] = 0; // ? ? 
                        data[1] = 0; // ?  
                        data[2] = 0; // ?  
                        data[3] = 0; // ?  
                        data[4] = 0; // ? ? 
                        data[5] = 0; // ? ? 

                        int currentBlockCount = 0; //  ? ? 
                        int blockSize = getBlockSize(); //    
                        int bytes = 0; // ? ? 

                        for (int bytesInBlockCounter = 0; (c = in.read()) != -1;) { // ? . (? ?  - ?  )
                            bytes++; // ? ? 
                            bytesInBlockCounter++;
                            int result = mask(probability, c); // result - 8   ? ? 
                            out.write(result); // ?  ?                         
                            if ((bytesInBlockCounter == blockSize) || (getFileSize() == bytes - 1)) { //      ?  
                                //      ?  (   ?  ,
                                //  ?    )
                                if (getByteErrorFlag()) {
                                    setBlockError(1);
                                    setBlockErrorFlag(false); //     
                                }
                                currentBlockCount++; //  ? 
                                bytesInBlockCounter = 0; // ?? ?   
                            }
                            data[0] = getPercent(bytes + 1);
                            data[1] = getBitError();
                            data[2] = getByteError();
                            data[3] = getBlockError();
                            data[4] = bytes;
                            data[5] = currentBlockCount;

                            publish(data);
                        }
                        System.out.println("? :");
                        System.out.println(" " + getBitError());
                        System.out.println(" " + getByteError());
                        System.out.println(" " + getBlockError());

                    } catch (IOException ex) {
                        System.out.println("   ?");
                        Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException ex) {
                            Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException ex) {
                            Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                return null;
            }

            @Override
            // This will be called if you call publish() from doInBackground()
            // Can safely update the GUI here.
            protected void process(List<Integer[]> chunks) { // ?    
                                                             // publish ?
                Integer[] value = chunks.get(chunks.size() - 1); // ?
                statisticsGUI.setProgress(value[0]); //  ??
                statisticsGUI.setValueButtomTable(value[1], value[2], value[3], value[4], value[5]); //  ?    ??

            }
        };
        worker.execute();
    }

    //  ?   2 8  ?    ? ? . ..  ..  
    //  : probaility - ?? ?? ; data - 8  ? .
    //  - 8 ?  ? ??
    //    ?     , ??  ? ?   .
    public int mask(double probability, int data) {
        int shiftVariable = 1;
        int tempMask = 0;
        int tempBitError = 0; //  ?   
        Random randomNumberGenerator = new Random();
        for (int i = 0; i != 8; shiftVariable = shiftVariable << 1, i++) {
            //System.out.println(" "+i);
            if (randomNumberGenerator.nextDouble() > probability) {
                if (i == 0) { //? ? ? 
                    //System.out.format("\n i = %d ? ? %h ",i, tempMask );
                    tempMask = tempMask | shiftVariable; //  ?  ? ? ?
                    tempBitError++; // ? ?  
                    // System.out.format(" %h .  %h\n", shiftVariable,tempMask );
                } else { // ?   
                         //System.out.format("i = %d ? ? %h ",i, tempMask );

                    tempMask = tempMask | shiftVariable;
                    tempBitError++;

                    //System.out.format(" %h .  %h\n", shiftVariable,tempMask );
                }
            } else {
                //System.out.println("  ("+i+")");
            }
        }
        setBitError(tempBitError); //  ? ? 
        if (tempBitError != 0) { // ?    ,   ?   .
            setBlockErrorFlag(true); //?   . ???  ?  .
            setByteError(1); //  ? ? 
        }
        //        System.out.format("\n\n\n ? ? = %h\n", tempMask );
        //        System.out.format("\n\n\n  = %h\n", data);
        //        System.out.format("\n\n\n  ? = %h\n", data ^ tempMask);
        //        System.out.println("-----------------------------------------------");
        return data ^ tempMask;
    }

    public void calculate() {
        double uSignal = getUsefulVoltage(); // ? ?  ?, 
        int b = getSpeed(); // ?? , 
        double c = getNoiseVoltage(); // ? ? , 
        int f = getFrequency(); // ? ?  ,  

        double e = uSignal * uSignal / b; // ??  ?
        final float deltaF = f * 1000 / 20; // ? ?, ??  
        double n = c * c / deltaF;

        double argument = sqrt(((2 * e) / n));
        double probability = 0.5 * (1 - erf(argument / sqrt(2)));
        System.out.println("e = " + e);
        System.out.println("deltaF = " + deltaF);
        System.out.println("n = " + n);
        System.out.println("probability = " + probability);
        setProbability(probability);
    }
}