/** {@inheritDoc} */
  @Override
  public final Object read(final InputStream is) {
    final BayesianNetwork result = new BayesianNetwork();
    final EncogReadHelper in = new EncogReadHelper(is);
    EncogFileSection section;
    String queryType = "";
    String queryStr = "";
    String contentsStr = "";

    while ((section = in.readNextSection()) != null) {
      if (section.getSectionName().equals("BAYES-NETWORK")
          && section.getSubSectionName().equals("BAYES-PARAM")) {
        final Map<String, String> params = section.parseParams();
        queryType = params.get("queryType");
        queryStr = params.get("query");
        contentsStr = params.get("contents");
      }
      if (section.getSectionName().equals("BAYES-NETWORK")
          && section.getSubSectionName().equals("BAYES-TABLE")) {

        result.setContents(contentsStr);

        // first, define relationships (1st pass)
        for (String line : section.getLines()) {
          result.defineRelationship(line);
        }

        result.finalizeStructure();

        // now define the probabilities (2nd pass)
        for (String line : section.getLines()) {
          result.defineProbability(line);
        }
      }
      if (section.getSectionName().equals("BAYES-NETWORK")
          && section.getSubSectionName().equals("BAYES-PROPERTIES")) {
        final Map<String, String> params = section.parseParams();
        result.getProperties().putAll(params);
      }
    }

    // define query, if it exists
    if (queryType.length() > 0) {
      BayesianQuery query = null;
      if (queryType.equals("EnumerationQuery")) {
        query = new EnumerationQuery(result);
      } else {
        query = new SamplingQuery(result);
      }

      if (query != null && queryStr.length() > 0) {
        result.setQuery(query);
        result.defineClassificationStructure(queryStr);
      }
    }

    return result;
  }
  /** {@inheritDoc} */
  @Override
  public final void save(final OutputStream os, final Object obj) {
    final EncogWriteHelper out = new EncogWriteHelper(os);
    final BayesianNetwork b = (BayesianNetwork) obj;
    out.addSection("BAYES-NETWORK");
    out.addSubSection("BAYES-PARAM");
    String queryType = "";
    String queryStr = b.getClassificationStructure();

    if (b.getQuery() != null) {
      queryType = b.getQuery().getClass().getSimpleName();
    }

    out.writeProperty("queryType", queryType);
    out.writeProperty("query", queryStr);
    out.writeProperty("contents", b.getContents());
    out.addSubSection("BAYES-PROPERTIES");
    out.addProperties(b.getProperties());

    out.addSubSection("BAYES-TABLE");
    for (BayesianEvent event : b.getEvents()) {
      for (TableLine line : event.getTable().getLines()) {
        if (line == null) continue;
        StringBuilder str = new StringBuilder();
        str.append("P(");

        str.append(BayesianEvent.formatEventName(event, line.getResult()));

        if (event.getParents().size() > 0) {
          str.append("|");
        }

        int index = 0;
        boolean first = true;
        for (BayesianEvent parentEvent : event.getParents()) {
          if (!first) {
            str.append(",");
          }
          first = false;
          int arg = line.getArguments()[index++];
          if (parentEvent.isBoolean()) {
            if (arg == 0) {
              str.append("+");
            } else {
              str.append("-");
            }
          }
          str.append(parentEvent.getLabel());
          if (!parentEvent.isBoolean()) {
            str.append("=");
            if (arg >= parentEvent.getChoices().size()) {
              throw new BayesianError(
                  "Argument value " + arg + " is out of range for event " + parentEvent.toString());
            }
            str.append(parentEvent.getChoice(arg));
          }
        }
        str.append(")=");
        str.append(line.getProbability());
        str.append("\n");
        out.write(str.toString());
      }
    }

    out.flush();
  }