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

📄 bifparser.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        throw new BIFFormatException("Probability Table for "+n.ID+
        " contains more values than it should");
      else if(parntOutcomes*n.outcomes.length < tk.countTokens())
        throw new BIFFormatException("Probability Table for "+n.ID+
        " contains less values than it should");
      else {
        n.probs = new double[parntOutcomes][n.outcomes.length];
        for(int r=0; r<parntOutcomes; r++)     //row
          for(int c=0; c<n.outcomes.length; c++) //column
            try {
              n.probs[r][c] =  Double.parseDouble( tk.nextToken() );
            }
            catch(NumberFormatException ne) { throw ne; }
      } // end of creating probability table
    } //endfor (for edges)
    
    
    //int tmpMatrix[][] = new int[m_nodes.size()][m_nodes.size()];
    //for(int i=0; i<m_edges.size(); i++)
    //    tmpMatrix[((GraphEdge)m_edges.elementAt(i)).src]
    //	       [((GraphEdge)m_edges.elementAt(i)).dest] =
    //                                   ((GraphEdge)m_edges.elementAt(i)).type;
    //for(int i=0; i<m_nodes.size(); i++) {
    //    GraphNode n = (GraphNode)m_nodes.elementAt(i);
    //    n.edges = tmpMatrix[i];
    //}
    
    //Adding parents, and those edges to a node which are coming out from it
    int tmpEdges[], noOfEdgesOfNode[]=new int[m_nodes.size()];
    int noOfPrntsOfNode[]=new int[m_nodes.size()];
    for(int i=0; i<m_edges.size(); i++) {
      GraphEdge e = (GraphEdge)m_edges.elementAt(i);
      noOfEdgesOfNode[e.src]++;
      noOfPrntsOfNode[e.dest]++;
    }
    
    for(int i=0; i<m_edges.size(); i++) {
      GraphEdge e  = (GraphEdge)m_edges.elementAt(i);
      GraphNode n  = (GraphNode)m_nodes.elementAt(e.src);
      GraphNode n2 = (GraphNode)m_nodes.elementAt(e.dest);
      if(n.edges==null) {
        n.edges = new int[noOfEdgesOfNode[e.src]][2];
        for(int k=0; k<n.edges.length; k++)
          n.edges[k][0]=-1;
      }
      if(n2.prnts==null) {
        n2.prnts = new int[noOfPrntsOfNode[e.dest]];
        for(int k=0; k<n2.prnts.length; k++)
          n2.prnts[k]=-1;
      }
      
      int k=0;
      while(n.edges[k][0]!=-1) k++;
      n.edges[k][0] = e.dest;
      n.edges[k][1] = e.type;
      
      k=0;
      while(n2.prnts[k]!=-1) k++;
      n2.prnts[k] = e.src;
    }
    
    //processGraph();
    //setAppropriateSize();
    return graphName;
  } //end readBIF
  
  
  /**
   * This method writes a graph in XMLBIF ver. 0.3 format to a file.
   * However, if is reloaded in GraphVisualizer we would need to layout
   * the graph again to display it correctly.
   *
   * @param filename  - The name of the file to write in. (will overwrite)
   * @param graphName - The name of the graph. (will be the name of network
   * tag in XMLBIF)
   * @param nodes     - Vector containing all the nodes
   * @param edges     - Vector containing all the edges
   */
  public static void writeXMLBIF03(String filename, String graphName,
  FastVector nodes, FastVector edges) {
    try {
      FileWriter outfile = new FileWriter(filename);
      
      StringBuffer text = new StringBuffer();
      
      text.append("<?xml version=\"1.0\"?>\n");
      text.append("<!-- DTD for the XMLBIF 0.3 format -->\n");
      text.append("<!DOCTYPE BIF [\n");
      text.append("	<!ELEMENT BIF ( NETWORK )*>\n");
      text.append("	      <!ATTLIST BIF VERSION CDATA #REQUIRED>\n");
      text.append("	<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFI"+
      "NITION )* )>\n");
      text.append("	<!ELEMENT NAME (#PCDATA)>\n");
      text.append("	<!ELEMENT VARIABLE ( NAME, ( OUTCOME |  PROPERTY )* )"+
      " >\n");
      text.append("	      <!ATTLIST VARIABLE TYPE (nature|decision|utility"+
      ") \"nature\">\n");
      text.append("	<!ELEMENT OUTCOME (#PCDATA)>\n");
      text.append("	<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY"+
      " )* >\n");
      text.append("	<!ELEMENT FOR (#PCDATA)>\n");
      text.append("	<!ELEMENT GIVEN (#PCDATA)>\n");
      text.append("	<!ELEMENT TABLE (#PCDATA)>\n");
      text.append("	<!ELEMENT PROPERTY (#PCDATA)>\n");
      text.append("]>\n");
      text.append("\n");
      text.append("\n");
      text.append("<BIF VERSION=\"0.3\">\n");
      text.append("<NETWORK>\n");
      text.append("<NAME>" + XMLNormalize(graphName)  + "</NAME>\n");
      
      //Writing all the node names and their outcomes
      //If outcome is null(ie if the graph was loaded from DOT file) then
      //simply write TRUE
      for(int nodeidx=0; nodeidx<nodes.size(); nodeidx++) {
        GraphNode n = (GraphNode)nodes.elementAt(nodeidx);
        if(n.nodeType!=GraphNode.NORMAL)
          continue;
        
        text.append("<VARIABLE TYPE=\"nature\">\n");
        text.append("\t<NAME>" + XMLNormalize(n.ID) + "</NAME>\n");
        
        if(n.outcomes!=null)
          for(int outidx=0; outidx<n.outcomes.length; outidx++)
            text.append("\t<OUTCOME>" + XMLNormalize(n.outcomes[outidx])+
            "</OUTCOME>\n");
        else
          text.append("\t<OUTCOME>true</OUTCOME>\n");
        
        text.append("\t<PROPERTY>position = ("+n.x+","+n.y+")</PROPERTY>\n");
        text.append("</VARIABLE>\n");
      }
      
      //Writing all the nodes definitions and their probability tables
      //If probability table is null then simply write 1 for all
      //the posible outcomes of the parents
      for (int nodeidx=0; nodeidx<nodes.size(); nodeidx++) {
        GraphNode n = (GraphNode) nodes.elementAt(nodeidx);
        if(n.nodeType!=GraphNode.NORMAL)
          continue;
        
        text.append("<DEFINITION>\n");
        text.append("<FOR>" + XMLNormalize(n.ID) + "</FOR>\n");
        int parntOutcomes = 1;
        if(n.prnts!=null)
          for(int pidx=0; pidx<n.prnts.length; pidx++) {
            GraphNode prnt = (GraphNode)nodes.elementAt(n.prnts[pidx]);
            text.append("\t<GIVEN>" + XMLNormalize(prnt.ID) + "</GIVEN>\n");
            if(prnt.outcomes!=null)
              parntOutcomes *= prnt.outcomes.length;
          }
        
        text.append("<TABLE>\n");
        for(int i=0; i<parntOutcomes; i++) {
          if(n.outcomes!=null)
            for(int outidx=0; outidx<n.outcomes.length; outidx++){
              text.append(n.probs[i][outidx]+" ");
            }
          else
            text.append("1");
          text.append('\n');
        }
        text.append("</TABLE>\n");
        text.append("</DEFINITION>\n");
      }
      
      text.append("</NETWORK>\n");
      text.append("</BIF>\n");
      
      outfile.write(text.toString());
      outfile.close();
    }
    catch(IOException ex) { ex.printStackTrace(); }
  } // writeXMLBIF
  
  /** XMLNormalize converts the five standard XML entities in a string
   * g.e. the string V&D's is returned as V&amp;D&apos;s
   * @author Remco Bouckaert (rrb@xm.co.nz)
   * @param sStr string to normalize
   * @return normalized string
   */
  private static String XMLNormalize(String sStr) {
    StringBuffer sStr2 = new StringBuffer();
    for (int iStr = 0; iStr < sStr.length(); iStr++) {
      char c = sStr.charAt(iStr);
      switch (c) {
        case '&': sStr2.append("&amp;"); break;
        case '\'': sStr2.append("&apos;"); break;
        case '\"': sStr2.append("&quot;"); break;
        case '<': sStr2.append("&lt;"); break;
        case '>': sStr2.append("&gt;"); break;
        default:
          sStr2.append(c);
      }
    }
    return sStr2.toString();
  } // XMLNormalize
  
  
} // BIFParser

⌨️ 快捷键说明

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