⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphvisualizer.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
              if(n.edges[k][1]==-DOUBLE) {
                drawArrow(g, x+x2, y+y2, x+x1, y+y1);
                if(n2.nodeType!=SINGULAR_DUMMY) //!n2.dummy)
                  drawArrow(g, x+x1, y+y1, x+x2, y+y2);
              }
              
              int tmpIndex=k;
              while(n2.nodeType!=NORMAL) { //n2.dummy==true) {
                g.drawLine(x+n2.x+paddedNodeWidth/2,
                y+n2.y+nodeHeight, x+n2.x+paddedNodeWidth/2, y+n2.y);
                x1=n2.x+paddedNodeWidth/2; y1=n2.y;
                for(int m=0; m<n2.edges.length; m++) {
                  if(n2.edges[m][1]<0) {
                    n2 = (GraphNode) m_nodes.elementAt(n2.edges[m][0]); //m_nodes.elementAt(m);
                    g.drawLine(x+x1, y+y1,
                    x+n2.x+paddedNodeWidth/2, y+n2.y+nodeHeight);
                    tmpIndex=m;
                    if(n2.nodeType!=SINGULAR_DUMMY) //!n2.dummy)
                      drawArrow(g, x+x1, y+y1,
                      x+n2.x+paddedNodeWidth/2, y+n2.y+nodeHeight);
                    break;
                  }
                }
              }
            }
          }
      }
    }
  }
  
  
  /**
   * Table Model for the Table that shows the probability
   * distribution for a node
   */
  private class GraphVisualizerTableModel extends AbstractTableModel {
    final String[] columnNames;
    final double[][] data;
    
    
    public GraphVisualizerTableModel(double[][] d, String[] c) {
      data = d;
      columnNames = c;
    }
    
    public int getColumnCount() {
      return columnNames.length;
    }
    
    public int getRowCount() {
      return data.length;
    }
    
    public String getColumnName(int col) {
      return columnNames[col];
    }
    
    public Object getValueAt(int row, int col) {
      return new Double(data[row][col]);
    }
    
   /*
    * JTable uses this method to determine the default renderer/
    * editor for each cell.
    */
    public Class getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }
    
    /*
     * Implemented this to make sure the table is uneditable.
     */
    public boolean isCellEditable(int row, int col) {
      return false;
    }
  }
  
  
  
  /**
   * Listener class for processing mouseClicked
   */
  private class GraphVisualizerMouseListener extends MouseAdapter {
    int x, y, nx, ny; Rectangle r;
    
    /**
     * If the mouse is clicked on a node then this method
     * displays a dialog box with the probability distribution
     * table for that node IF it exists
     */
    public void mouseClicked(MouseEvent me) {
      GraphNode n;
      Dimension d = m_gp.getPreferredSize();
      //System.out.println("Preferred Size: "+this.getPreferredSize()+
      //                   " Actual Size: "+this.getSize());
      x=y=nx=ny=0;
      
      if(d.width < m_gp.getWidth())
        nx = (int)((nx + m_gp.getWidth()/2 - d.width/2)/scale);
      if(d.height < m_gp.getHeight())
        ny = (int)((ny + m_gp.getHeight()/2 - d.height/2)/scale);
      
      r=new Rectangle(0, 0, 
                     (int)(paddedNodeWidth*scale), (int)(nodeHeight*scale));
      x += me.getX(); y += me.getY();
      
      int i;
      for(i=0; i<m_nodes.size(); i++) {
        n = (GraphNode) m_nodes.elementAt(i);
        r.x = (int)((nx+n.x)*scale); r.y = (int)((ny+n.y)*scale);
        if(r.contains(x,y)) {
          if(n.probs==null)
            return;
          
          int noOfPrntsOutcomes = 1;
          if(n.prnts!=null) {
            for(int j=0; j<n.prnts.length; j++) {
              GraphNode n2 = (GraphNode)m_nodes.elementAt(n.prnts[j]);
              noOfPrntsOutcomes *= n2.outcomes.length;
            }
            if(noOfPrntsOutcomes>511) {
              System.err.println("Too many outcomes of parents ("+noOfPrntsOutcomes+
                                 ") can't display probabilities");
              return;
            }
          }
          
          GraphVisualizerTableModel tm = 
                             new GraphVisualizerTableModel(n.probs, n.outcomes);
          
          JTable jTblProbs = new JTable(tm); //JTable(probabilities, (Object[])n.outcomes);
          
          JScrollPane js = new JScrollPane(jTblProbs);
          
          if(n.prnts!=null) {
            GridBagConstraints gbc = new GridBagConstraints();
            JPanel jPlRowHeader = new JPanel( new GridBagLayout() );
            
            //indices of the parent nodes in the Vector
            int [] idx = new int[n.prnts.length]; 
            //max length of values of each parent
            int [] lengths = new int[n.prnts.length];  
            
            //System.out.println("n.probs.length "+n.probs.length+
            //                   " should be "+noOfPrntsOutcomes);
            //System.out.println("n.probs[0].length "+n.probs[0].length+
            //                   " should be "+n.outcomes.length);
            //System.out.println("probabilities are: ");
            //for(int j=0; j<probabilities.length; j++) {
            //    for(int k=0; k<probabilities[j].length; k++)
            //	   System.out.print(probabilities[j][k]+" ");
            //     System.out.println("");
            //}
            
            //Adding labels for rows
            gbc.anchor = gbc.NORTHWEST;
            gbc.fill = gbc.HORIZONTAL;
            gbc.insets = new Insets(0,1,0,0);
            int addNum=0, temp=0;
            boolean dark=false;
            while(true){
              GraphNode n2;
              gbc.gridwidth = 1;
              for(int k=0; k<n.prnts.length; k++) {
                n2 = (GraphNode)m_nodes.elementAt(n.prnts[k]);
                JLabel lb = new JLabel(n2.outcomes[idx[k]]);
                lb.setFont( new Font("Dialog", Font.PLAIN, 12) );
                lb.setOpaque( true );
                lb.setBorder( BorderFactory.createEmptyBorder( 1,2,1,1 ) );
                lb.setHorizontalAlignment( JLabel.CENTER );
                if(dark) {
                  lb.setBackground( lb.getBackground().darker() );
                  lb.setForeground( Color.white );
                }
                else
                  lb.setForeground( Color.black );
                
                temp = lb.getPreferredSize().width;
                //System.out.println("Preferred width "+temp+
                //                   " for "+n2.outcomes[idx[k]]);
                lb.setPreferredSize(
                                 new Dimension(temp, jTblProbs.getRowHeight())
                                 );
                if(lengths[k]<temp)
                  lengths[k] = temp;
                temp=0;
                
                if(k==n.prnts.length-1) {
                  gbc.gridwidth = gbc.REMAINDER;
                  dark = (dark==true) ?  false:true;
                }
                jPlRowHeader.add(lb, gbc);
                addNum++;
              }
              
              for(int k=n.prnts.length-1; k>=0; k--) {
                n2 = (GraphNode) m_nodes.elementAt(n.prnts[k]);
                if(idx[k]==n2.outcomes.length-1 && k!=0) {
                  idx[k]=0;
                  continue;
                }
                else {
                  idx[k]++;
                  break;
                }
              }
              
              n2 = (GraphNode) m_nodes.elementAt(n.prnts[0]);
              if(idx[0]==n2.outcomes.length) {
                JLabel lb= (JLabel) jPlRowHeader.getComponent(addNum-1);
                jPlRowHeader.remove(addNum-1);
                lb.setPreferredSize( new Dimension(lb.getPreferredSize().width, 
                                                   jTblProbs.getRowHeight()) );
                gbc.gridwidth = gbc.REMAINDER;
                gbc.weighty = 1;
                jPlRowHeader.add(lb, gbc);
                gbc.weighty=0;
                break;
              }
            }
            
            
            gbc.gridwidth = 1;
            //The following panel contains the names of the parents
            //and is displayed above the row names to identify
            //which value belongs to which parent
            JPanel jPlRowNames = new JPanel(new GridBagLayout());
            for(int j=0; j<n.prnts.length; j++) {
              JLabel lb2;
              JLabel lb1 = 
                   new JLabel( ((GraphNode)m_nodes.elementAt(n.prnts[j])).lbl );
              lb1.setBorder( BorderFactory.createEmptyBorder( 1,2,1,1 ) );
              Dimension tempd = lb1.getPreferredSize();
              //System.out.println("lengths[j]: "+lengths[j]+
              //                   " tempd.width: "+tempd.width);
              if(tempd.width<lengths[j]) {
                lb1.setPreferredSize( new Dimension(lengths[j], tempd.height) );
                lb1.setHorizontalAlignment( JLabel.CENTER );
                lb1.setMinimumSize( new Dimension(lengths[j], tempd.height) );
              }
              else if(tempd.width>lengths[j]) {
                lb2 = (JLabel) jPlRowHeader.getComponent(j);
                lb2.setPreferredSize( new Dimension(tempd.width, 
                                               lb2.getPreferredSize().height) );
              }
              jPlRowNames.add(lb1, gbc);
              //System.out.println("After adding "+lb1.getPreferredSize());
            }
            js.setRowHeaderView(jPlRowHeader);
            js.setCorner( JScrollPane.UPPER_LEFT_CORNER, jPlRowNames );
          }
          
          
          JDialog jd = 
                new JDialog((Frame)GraphVisualizer.this.getTopLevelAncestor(),
                            "Probability Distribution Table For "+n.lbl, true);
          jd.setSize(500, 400);
          jd.setLocation(GraphVisualizer.this.getLocation().x+
                            GraphVisualizer.this.getWidth()/2-250,
                         GraphVisualizer.this.getLocation().y+
                            GraphVisualizer.this.getHeight()/2-200 );
          
          jd.getContentPane().setLayout( new BorderLayout() );
          jd.getContentPane().add(js, BorderLayout.CENTER);
          jd.setVisible(true);
          
          return;
        }
      }
    }
    
  }
  
  
  /**
   * private class for handling mouseMoved events
   * to highlight nodes if the the mouse is moved on
   * one
   */
  private class GraphVisualizerMouseMotionListener extends MouseMotionAdapter {
    int x, y, nx, ny; Rectangle r;
    GraphNode lastNode;
    
    public void mouseMoved(MouseEvent me) {
      GraphNode n;
      Dimension d = m_gp.getPreferredSize();
      //System.out.println("Preferred Size: "+this.getPreferredSize()+
      //                   " Actual Size: "+this.getSize());
      x=y=nx=ny=0;
      
      if(d.width < m_gp.getWidth())
        nx = (int)((nx + m_gp.getWidth()/2 - d.width/2)/scale);
      if(d.height < m_gp.getHeight())
        ny = (int)((ny + m_gp.getHeight()/2 - d.height/2)/scale);
      
      r=new Rectangle(0, 0, 
                     (int)(paddedNodeWidth*scale), (int)(nodeHeight*scale));
      x += me.getX(); y += me.getY();
      
      int i;
      for(i=0; i<m_nodes.size(); i++) {
        n = (GraphNode) m_nodes.elementAt(i);
        r.x = (int)((nx+n.x)*scale); r.y = (int)((ny+n.y)*scale);
        if(r.contains(x,y)) {
          if(n!=lastNode) {
            m_gp.highLight(n);
            if(lastNode!=null)
              m_gp.highLight(lastNode);
            lastNode = n; //lastIndex = i;
          }
          break;
        }
      }
      if(i==m_nodes.size()  && lastNode!=null) {
        m_gp.repaint();
        //m_gp.highLight(lastNode);
        lastNode=null;
      }
    }
  }
  
  /**
   * Main method to load a text file with the
   * description of a graph from the command
   * line
   */
  public static void main(String [] args) {
    JFrame jf = new JFrame("Graph Visualizer");
    GraphVisualizer g = new GraphVisualizer();
    
    try{
      if(args[0].endsWith(".xml")) {
        //StringBuffer sb = new StringBuffer();
        //FileReader infile = new FileReader(args[0]);
        //int i;
        //while( (i=infile.read())!=-1) {
        //    sb.append((char)i);
        //}
        //System.out.println(sb.toString());
        //g.readBIF(sb.toString() );
        g.readBIF( new FileInputStream(args[0]) );
      }
      else {
        //BufferedReader infile=new BufferedReader();
        g.readDOT(new FileReader(args[0])); //infile);
      }
    }
    catch(IOException ex) { ex.printStackTrace(); }
    catch(BIFFormatException bf) { bf.printStackTrace(); System.exit(-1); }
    
    jf.getContentPane().add(g);
    //RepaintManager.currentManager(jf.getRootPane()).setDoubleBufferingEnabled(false);
    jf.setDefaultCloseOperation( jf.EXIT_ON_CLOSE );
    jf.setSize(800,600);
    //jf.pack();
    jf.setVisible(true);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -