Example #1
0
  /**
   * Write a Bayesian network to an output stream in BIF format.
   *
   * @param os The output stream to write to.
   * @param network The network to write.
   */
  public static void writeBIF(OutputStream os, BayesianNetwork network) {
    WriteXML xml = new WriteXML(os);
    xml.beginDocument();
    xml.addAttribute("VERSION", "0.3");
    xml.beginTag("BIF");
    xml.beginTag("NETWORK");
    xml.addProperty("NAME", "Bayes Network, Generated by Encog");
    // write variables
    for (BayesianEvent event : network.getEvents()) {
      xml.addAttribute("TYPE", "nature");
      xml.beginTag("VARIABLE");
      xml.addProperty("NAME", event.getLabel());
      for (BayesianChoice str : event.getChoices()) {
        xml.addProperty("OUTCOME", str.getLabel());
      }
      xml.endTag();
    }

    // write relations
    for (BayesianEvent event : network.getEvents()) {
      xml.beginTag("DEFINITION");
      xml.addProperty("FOR", event.getLabel());
      for (BayesianEvent parentEvent : event.getParents()) {
        xml.addProperty("GIVEN", parentEvent.getLabel());
      }
      xml.addAttribute("TABLE", generateTable(event));
      xml.endTag();
    }

    xml.endTag();
    xml.endTag();
    xml.endDocument();
  }