com.sliit.views.SVMView.java Source code

Java tutorial

Introduction

Here is the source code for com.sliit.views.SVMView.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 com.sliit.views;

import com.sliit.svmanalysis.SvmAnalyser;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.mllib.classification.SVMModel;
import org.apache.spark.mllib.classification.SVMWithSGD;
import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.util.MLUtils;
import scala.Tuple2;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.evaluation.ThresholdCurve;
import weka.core.Instances;
import weka.core.Utils;
import weka.gui.visualize.PlotData2D;
import weka.gui.visualize.ThresholdVisualizePanel;

/**
 *
 * @author Heshani
 */
public class SVMView extends javax.swing.JPanel {

    private String dataset;
    private String modal;

    /**
     * Creates new form SVMView
     */
    public SVMView() {
        initComponents();
        loadingGif.hide();
    }

    public SVMView(String dataset, String modal) {
        this();
        this.dataset = dataset;
        this.modal = modal;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        svmPredictButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conclutionText = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        iterartionText = new javax.swing.JTextField();
        loadingGif = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        svmPredictButton.setText("Process");
        svmPredictButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                svmPredictButtonActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Output"));

        conclutionText.setColumns(20);
        conclutionText.setForeground(new java.awt.Color(255, 0, 0));
        conclutionText.setRows(5);
        jScrollPane1.setViewportView(conclutionText);

        jLabel3.setText("Conclusion : ");

        outputText.setColumns(20);
        outputText.setRows(5);
        jScrollPane2.setViewportView(outputText);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 644,
                                        Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        jPanel1Layout.createSequentialGroup().addComponent(jLabel3).addGap(0, 0,
                                                Short.MAX_VALUE)))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                        .addGap(18, 18, 18).addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

        jLabel1.setText("No of Iterartions : ");

        iterartionText.setText("100");

        loadingGif.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/loading.gif"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addGap(99, 99, 99).addComponent(
                                        loadingGif, javax.swing.GroupLayout.PREFERRED_SIZE, 150,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGap(20, 20, 20).addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                                        false)
                                                .addComponent(
                                                        svmPredictButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(iterartionText,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 196,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1).addComponent(iterartionText,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42).addComponent(svmPredictButton).addGap(41, 41, 41)
                                .addComponent(loadingGif, javax.swing.GroupLayout.PREFERRED_SIZE, 76,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup().addGap(28, 28, 28).addComponent(jPanel1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(31, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void svmPredictButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_svmPredictButtonActionPerformed

        loadingGif.show();

        new Thread(new Runnable() {
            public void run() {
                SvmAnalyser svmAnalyser = new SvmAnalyser();
                System.out.println(dataset);
                String output = svmAnalyser.perfomeAnalysis(PredictorPanel.locationText.getText(),
                        PredictorPanel.modalText.getText());
                outputText.setText(output);

                Double auROC = svmAnalyser.getauRoc();

                String conclusion = "";

                //excellent 
                if (auROC >= 0.9 && auROC <= 1) {
                    conclusion = "Your Machine is Protected.\nSuspicious network traffic not detected.";

                    //good
                } else if (auROC >= 0.8 && auROC <= 0.9) {
                    conclusion = "Your Machine is Protected.\nSuspicious network traffic not detected.You might face network treats";

                    //fair 
                } else if (auROC >= 0.7 && auROC <= 0.8) {
                    conclusion = "Your Machine is Not Protected.\nSuspicious network traffic not detected.";

                    //poor
                } else if (auROC >= 0.6 && auROC <= 0.7) {
                    conclusion = "Your Machine is Highly Vulnerable.\nSuspicious network traffic detected.";

                    //fail
                } else if (auROC >= 0.5 && auROC <= 0.6) {
                    conclusion = "Your Machine is Highly Vulnerable for an Attack.\nYou will face network treats";
                } else {
                    conclusion = "Your Machine is Attacked";
                }

                conclutionText.setText(conclusion);

                loadingGif.hide();
            }
        }).start();

    }//GEN-LAST:event_svmPredictButtonActionPerformed

    /**
     * draw ROC curve
     */
    void getRocCurve() {
        try {
            Instances data;
            data = new Instances(new BufferedReader(new FileReader(PredictorPanel.modalText.getText())));
            data.setClassIndex(data.numAttributes() - 1);

            //train classifier
            Classifier cl = new NaiveBayes();
            Evaluation eval = new Evaluation(data);
            eval.crossValidateModel(cl, data, 10, new Random(1));

            // generate curve
            ThresholdCurve tc = new ThresholdCurve();
            int classIndex = 0;
            Instances result = tc.getCurve(eval.predictions(), classIndex);

            // plot curve
            ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
            vmc.setROCString("(Area under ROC = " + Utils.doubleToString(tc.getROCArea(result), 4) + ")");
            vmc.setName(result.relationName());
            PlotData2D tempd = new PlotData2D(result);
            tempd.setPlotName(result.relationName());
            tempd.addInstanceNumberAttribute();
            // specify which points are connected
            boolean[] cp = new boolean[result.numInstances()];
            for (int n = 1; n < cp.length; n++) {
                cp[n] = true;
            }
            tempd.setConnectPoints(cp);
            // add plot
            vmc.addPlot(tempd);

            //            rocPanel.removeAll();
            //            rocPanel.add(vmc, "vmc", 0);
            //            rocPanel.revalidate();
        } catch (IOException ex) {
            Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea conclutionText;
    private javax.swing.JTextField iterartionText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel loadingGif;
    public javax.swing.JTextArea outputText;
    private javax.swing.JButton svmPredictButton;
    // End of variables declaration//GEN-END:variables
}