示例#1
0
 private void getVariableDefs(StringBuilder bldr, BayesNet net) {
   for (BayesNode node : net.getNodes()) {
     int offset = bldr.length();
     encodeStates(bldr, node);
     encodeParents(bldr, node);
     bldr.append('\n');
     encodeProbabilities(bldr, node);
     XMLUtil.surround(offset, bldr, CPT, ID, node.getName());
     bldr.append('\n');
   }
 }
示例#2
0
  private void encodeParents(StringBuilder bldr, BayesNode node) {
    int offset = bldr.length();
    for (BayesNode p : node.getParents()) {
      // XDSL can't handle names containing whitespaces!
      bldr.append(p.getName().trim().replaceAll("\\s+", "_"));
      bldr.append(' ');
    }
    if (!node.getParents().isEmpty()) {
      bldr.deleteCharAt(bldr.length() - 1); // delete last whitespace
    }

    XMLUtil.surround(offset, bldr, PARENTS);
  }
示例#3
0
  public String write(BayesNet net) {
    StringBuilder bldr = new StringBuilder();
    bldr.append(xmlHeader);
    bldr.append(comment);

    int offset = bldr.length();
    getVariableDefs(bldr, net);
    XMLUtil.surround(offset, bldr, "nodes");
    XMLUtil.surround(
        offset,
        bldr,
        "smile",
        "version",
        "1.0",
        ID,
        net.getName(),
        "numsamples",
        "1000",
        "discsamples",
        "10000");

    return bldr.toString();
  }
示例#4
0
 private void encodeProbabilities(StringBuilder bldr, BayesNode node) {
   if (node.getProbabilities().length == 0) {
     throw new IllegalArgumentException(
         "Bayesian Network is broken: "
             + node.getName()
             + " has an empty conditional probability table");
   }
   int offset = bldr.length();
   for (Number d : node.getFactor().getValues()) {
     bldr.append(d);
     bldr.append(' ');
   }
   bldr.deleteCharAt(bldr.length() - 1); // delete last whitespace
   XMLUtil.surround(offset, bldr, PROBABILITIES);
 }