예제 #1
0
  /**
   * Parses a Genie Network from an inputstream pointing to an XDSL file
   *
   * @param inputStream
   * @throws SAXException
   * @throws ParserConfigurationException
   * @throws IOException
   */
  private void parseNetworkFromXDSL(InputStream inputStream)
      throws SAXException, ParserConfigurationException, IOException {

    // get a SAX parser factory
    SAXParserFactory spf = SAXParserFactory.newInstance();

    // get a new instance of parser
    SAXParser sp = spf.newSAXParser();

    // Parses the file (this method blocks until parsing is complete)
    sp.parse(inputStream, this);

    System.out.println("Parsed Genie file and completed Network setup.");

    Iterator i = searchSpace.getNodeIterator();

    while (i.hasNext()) {
      BayesNode temp = (BayesNode) i.next();
      System.out.println("Node " + temp.label);
      Matrix matrix = temp.getMatrix();
      matrix.print(3, 6);
    }
  }
예제 #2
0
  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    // Do nothing when we've finished reading an XML tag element
    //		if(qName.equalsIgnoreCase("href")) {
    //			//Save Href link, and replace "&" with "&"
    //			//href = temp.replace("&", "&");
    //			href = temp;
    //		}

    // When we reache the end of the 'cpt' element, we have all the information for one node
    if (qName.equalsIgnoreCase("cpt")) {
      // Create a node based on the currently saved information
      System.out.println("----------------------");
      System.out.println(nodeName + " node is being created...");
      // nextInt is normally exclusive of the top value,
      // so add 1 to make it inclusive
      int randomX = rand.nextInt(max - min + 1) + min;
      int randomY = rand.nextInt(max - min + 1) + min;

      // Create new node with parsed information (label, x, y)
      BayesNode newNode = new BayesNode(nodeName, randomX, randomY);
      LinkXY link = null;

      // Convert probabilities to Matrix
      System.out.println("Node probabilities: " + probabilities.toString().trim());
      String parsedProb[] = probabilities.toString().trim().split(" ");
      double numProb[] = new double[parsedProb.length];
      // System.out.println("Parsed probs:");
      for (int i = 0; i < parsedProb.length; i++) {
        // System.out.println(Double.valueOf(parsedProb[i]));
        numProb[i] = Double.valueOf(parsedProb[i]);
      }

      Matrix matrix = new Matrix(numProb, rowCount);
      newNode.setMatrix(matrix);

      // System.out.println("Row Names (total of " + rowCount + "):");
      //            Iterator iterator = rowNames.iterator();
      //            while(iterator.hasNext())
      //			System.out.println(iterator.next().toString());

      newNode.setRowNames((ArrayList) rowNames.clone());

      // Gets parents of this new node
      if (parents.length() != 0) {
        System.out.println("Parents = " + parents.toString().trim());
        String parsedParents[] = parents.toString().trim().split(" ");
        for (int i = 0; i < parsedParents.length; i++) {
          if (!parsedParents[i].trim().equals("")) {
            // System.out.println(parsedParents[i]);
            // Since parents should always proceed children in teh XDSL file, the below line should
            // always return a node
            BayesNode parentNode = (BayesNode) searchSpace.findNode(parsedParents[i]);
            newNode.addConnToParent(parentNode); // Connect new node to parent
            parentNode.addConnToChild(newNode); // Connect parent to new node

            // Create link for drawing the graph
            link = new LinkXY(parentNode, newNode);
          }
        }
      } else {
        System.out.println("No parents for " + newNode.label);
      }

      // Add new node to search space list
      searchSpace.add(newNode);

      if (link != null) {
        searchSpace.add(link);
      }

      // Reset row count
      rowCount = 0;
      rowNames.clear();
      // Reset parents
      parents.setLength(0);
    }

    // System.out.println("endElement: " + uri + ", " + localName + ", " + qName);

  }