Java tutorial
/* * 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 Windows; import static Windows.windowWEKA.data; import java.awt.Dimension; import java.awt.Insets; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import static regulyasocjacyjne.Data.loadData; import regulyasocjacyjne.WekaApriori; import weka.core.Attribute; import weka.core.DenseInstance; import weka.core.FastVector; import weka.core.Instance; import weka.core.Instances; /** * * @author Dominika */ public class windowGenerating extends javax.swing.JFrame { private static int numOfNewInstance; private static String result; private static String[][] dataInTable; public static Instances oldData = windowWEKA.data; private static Instances newData; private static JTable jTabData; public static List<String> listOfHeather = windowWEKA.getListOfH(); public static void infoObj() throws Exception { Instances data = loadData("./src/date/osmolski.arff"); for (int i = 0; i < data.numInstances(); i++) //Przegladanie obiektow { System.out.println("Wiersz numer " + i + ":"); Instance instance = data.instance(i); //Pobranie obiektu (wiersza danych) o podanym numerze for (int j = 0; j < instance.numAttributes(); j++) //Przegladanie atrybutow w obiekcie { String textValue = instance.toString(j); //Pobranie wartosci atrybutu o podanym numerze (tzn. pobranie tekstowej reprezentacji wartosci) System.out.print(textValue + ", "); } System.out.println(); } } /** * Creates new form windowWEKA */ public windowGenerating() { initComponents(); } /** * 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() { jColorChooser1 = new javax.swing.JColorChooser(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); btnShowResults = new javax.swing.JButton(); btnSaveResults = new javax.swing.JButton(); btnShowNewData = new javax.swing.JButton(); tfMinCon = new javax.swing.JTextField(); tfNumberRules = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); taResults = new javax.swing.JTextArea(); jTable1.setModel( new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(jTable1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnShowResults.setText("Show new results"); btnShowResults.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnShowResultsActionPerformed(evt); } }); btnSaveResults.setText("Save results do file"); btnSaveResults.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSaveResultsActionPerformed(evt); } }); btnShowNewData.setText("Show new data"); btnShowNewData.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnShowNewDataActionPerformed(evt); } }); tfMinCon.setText("10"); tfMinCon.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tfMinConActionPerformed(evt); } }); tfNumberRules.setText("0.9"); tfNumberRules.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tfNumberRulesActionPerformed(evt); } }); jLabel1.setText("The minimum confidence of a rule."); jLabel2.setText("The required number of rules"); taResults.setColumns(20); taResults.setRows(5); jScrollPane2.setViewportView(taResults); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(13, 13, 13).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tfMinCon, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tfNumberRules, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(btnShowNewData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(btnShowResults, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSaveResults))) .addGap(18, 18, 18) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(btnShowNewData) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(tfMinCon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(8, 8, 8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(tfNumberRules, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnShowResults).addComponent(btnSaveResults))) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(546, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnShowResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowResultsActionPerformed try { WekaApriori wA = new WekaApriori(); result = wA.regulyAsocjacyjne(data, tfNumberRules.getText(), tfMinCon.getText()); taResults.setText(result); } catch (Exception ex) { Logger.getLogger(windowGenerating.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnShowResultsActionPerformed private void btnShowNewDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowNewDataActionPerformed dataInTable = dataFromInnstancesToTable(data); List<Set<String>> listOfSetValues = setListOfAtribValue(dataInTable); Set<List<String>> setOfListValues = tabToSetObj(dataInTable); Set<List<String>> permutateOfObjects = getCombinations(listOfSetValues); System.out.println("Rozmiar zbioru przed uswaniem " + permutateOfObjects.size()); Iterator iter = setOfListValues.iterator(); while (iter.hasNext()) { permutateOfObjects.remove(iter.next()); } numOfNewInstance = permutateOfObjects.size(); try { newData = setToInstances(listOfSetValues, permutateOfObjects); } catch (Exception ex) { Logger.getLogger(windowWEKA.class.getName()).log(Level.SEVERE, null, ex); } loadDataToTable(); }//GEN-LAST:event_btnShowNewDataActionPerformed private void btnSaveResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveResultsActionPerformed try { PrintWriter pw = new PrintWriter(new FileOutputStream(new File("reguly-asocjacyjne_nowych_obj.txt"))); pw.write(result); pw.close(); JOptionPane.showMessageDialog(null, "Saved successfully", "Saved", 1); } catch (Exception e) { JOptionPane.showMessageDialog(null, e, "Saved abord", 0); } }//GEN-LAST:event_btnSaveResultsActionPerformed private void tfMinConActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfMinConActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tfMinConActionPerformed private void tfNumberRulesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfNumberRulesActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tfNumberRulesActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(windowGenerating.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(windowGenerating.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(windowGenerating.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(windowGenerating.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new windowGenerating().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnSaveResults; private javax.swing.JButton btnShowNewData; private javax.swing.JButton btnShowResults; private javax.swing.JColorChooser jColorChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; private javax.swing.JTextArea taResults; private javax.swing.JTextField tfMinCon; private javax.swing.JTextField tfNumberRules; // End of variables declaration//GEN-END:variables /** * Metoda powinna uzupenia tabelk danymi wczytanymi z pliku *.arff * */ public void loadDataToTable() { try { int numberRow = numOfNewInstance; int numberColumn = newData.numAttributes(); jTabData = new JTable(numberRow, numberColumn); DefaultTableModel dtm = new DefaultTableModel(0, 0); String header[] = new String[numberColumn]; for (int i = 0; i < numberColumn; i++) { header[i] = newData.attribute(i).name(); listOfHeather.add(header[i]); } dtm.setColumnIdentifiers(header); jTabData.setModel(dtm); List<String[]> listWithInstance = new ArrayList<>(); for (int i = 0; i < newData.numInstances(); i++) { Instance instance = newData.instance(i); listWithInstance.add(instance.toString().split(Pattern.quote(","))); } for (int i = 0; i < numberRow; i++) { dtm.addRow(listWithInstance.get(i)); } jTabData.setVisible(true); Insets insets = this.getInsets(); JScrollPane jp = new JScrollPane(jTabData); jp.setSize(new Dimension(100, 60)); jp.setBounds(10 + insets.left, 180 + insets.top, this.getWidth() - insets.right - 30, this.getHeight() - 190); jp.setVisible(true); try { this.add(jp); } catch (Exception e) { System.out.println("Ju nie kwiczy magia JAVY :D"); } this.repaint(); System.out.println("Wierszy: " + numberRow); System.out.println("Kolumn " + +numberColumn); } catch (Exception ex) { Logger.getLogger(WindowMain.class.getName()).log(Level.SEVERE, null, ex); } } /** * Zapisuje do listy zbir niepowtarzalnych parametrw * * @param data * @return */ private static List setListOfAtribValue(String[][] data) { List<Set<String>> listOfAtribValue = new ArrayList<>(); Set<String> setKolumn; for (int i = 0; i < data[0].length; i++) { setKolumn = new HashSet<>(); for (String[] data1 : data) { setKolumn.add(data1[i]); } listOfAtribValue.add(setKolumn); System.out.println(setKolumn); } return listOfAtribValue; } /** * Metoda zwraca zbir wszystkich permutacji listy zbiorw wartoci * atrybutw * * @param <T> * @param lists lista z zbiorami niepowtarzalnych wartoci dla * poszczeglnych atrybutw * @return */ public static <T> Set<List<T>> getCombinations(List<Set<T>> lists) { Set<List<T>> combinations = new HashSet<>(); Set<List<T>> newCombinations; int index = 0; //wyodrbnia kady element z listy // i dodaje kady obiekt typu <T> jako now list for (T i : lists.get(0)) { List<T> newList = new ArrayList<>(); newList.add(i); combinations.add(newList); } index++; while (index < lists.size()) { Set<T> nextSet = lists.get(index); //tworzenie nowego zbioru i przypisanie do niego poszczeglnych elementw listy newCombinations = new HashSet<>(); for (List<T> first : combinations) { for (T second : nextSet) { List<T> newList = new ArrayList<>(); newList.addAll(first); newList.add(second); newCombinations.add(newList); } } combinations = newCombinations; index++; } return combinations; } public static Set tabToSetObj(String[][] data) { Set<List<String>> setObj = new HashSet<>(); List<String> row; for (String[] data1 : data) { row = new ArrayList<>(); for (int j = 0; j < data[0].length; j++) { row.add(data1[j]); } setObj.add(row); } return setObj; } /** * Metoda zamienia liste zbiorw na instance. Pierwsza ptla tworzy list * wartoci jakie mog przybiera atrybut. * * @param atr lista atryburw * @param s lista zawierajaca kombinajcie uzupenionych danych * @return * */ public static Instances setToInstances(List<Set<String>> atr, Set<List<String>> s) { ArrayList<Attribute> lAtrib = new ArrayList<>(); for (int i = 0; i < atr.size(); i++) { FastVector labels = new FastVector(); //Utworzenie obiektu kolekcji wartosci nowego atrybutu symbolicznego Set<String> setValuesAtr = atr.get(i); Iterator ite = setValuesAtr.iterator(); while (ite.hasNext()) { Object e = ite.next(); labels.addElement(e); } Attribute attrib = new Attribute(listOfHeather.get(i), labels); lAtrib.add(attrib); } Instances dataNewObj = new Instances("Nowa tablica", lAtrib, 0); for (int i = 0; i < numOfNewInstance; i++) { Instance n = new DenseInstance(lAtrib.size()); dataNewObj.add(n); } System.out.println(dataNewObj.numInstances() + " jest instancji nowo wygenerowanych"); int iteratorek = 0; Iterator iter = s.iterator(); while (iter.hasNext()) { Instance instance = dataNewObj.instance(iteratorek); //Pobranie obiektu o podanym numerze List<String> str = (List<String>) iter.next(); for (int j = 0; j < dataNewObj.numAttributes(); j++) { instance.setValue(j, str.get(j)); } iteratorek++; } return dataNewObj; } /** * Przpisuje dane z obiektu instances do tablicy dwuwymiarowej * * @param data * @return */ public String[][] dataFromInnstancesToTable(Instances data) { String buf[]; String[] obj; List<String[]> listObj = new ArrayList<>(); for (int i = 0; i < data.numInstances(); i++) { buf = data.instance(i).toString().split(","); obj = new String[buf.length]; for (int k = 0; k < buf.length; k++) { obj[k] = String.valueOf(buf[k]); } listObj.add(obj); } return listObj.toArray(new String[][] {}); } }