Exemplo n.º 1
0
  public static void main(String args[]) {

    if (args.length < 3) {
      System.err.println("Usage: Generate model-file ( -s length | -c count length )");
      System.exit(1);
    }

    try {
      SAXParserFactory saxFactory = SAXParserFactory.newInstance();
      saxFactory.setNamespaceAware(true);
      SAXParser saxParser = saxFactory.newSAXParser();
      XMLReader xmlReader = saxParser.getXMLReader();

      XMLModelReader modelReader = new XMLModelReader(xmlReader);
      FileReader modelInput = new FileReader(args[0]);
      InputSource source = new InputSource(modelInput);

      Model model = modelReader.load(source);

      modelInput.close();

      model.check();

      if (args[1].equals("-s")) {
        int[][] result = model.generateSequence(Integer.parseInt(args[2]));

        List lexicon = model.getLexicon();
        for (int i = 0; i < result[0].length; i++) {
          System.out.print((Character) lexicon.get(result[0][i]));
        }
        System.out.println();
        int numberOfStates = model.getNumberOfStates();
        if (numberOfStates > 9) {
          for (int i = 0; i < result[1].length; i++) {
            System.out.print(result[1][i]);
            System.out.print(',');
          }
        } else {
          for (int i = 0; i < result[1].length; i++) {
            System.out.print(result[1][i]);
          }
        }
        System.out.println();
        for (int i = 1; i < numberOfStates; i++) {
          System.out.println(i + " = " + model.getStateName(i));
        }
      } else if (args[1].equals("-c")) {
        List lexicon = model.getLexicon();
        int count = Integer.parseInt(args[2]);
        int length = Integer.parseInt(args[3]);
        ListOfSequences uniqueSeqs = new ListOfSequences();
        for (int i = 0; i < count; i++) {
          int[][] result = model.generateSequence(length);
          uniqueSeqs.addSequence(result[0]);
        }
        Iterator seqs = uniqueSeqs.iterator();
        while (seqs.hasNext()) {
          SequenceCount seq = (SequenceCount) seqs.next();
          int[] key = seq.getSequence();
          for (int i = 0; i < key.length; i++) {
            System.out.print((Character) lexicon.get(key[i]));
          }
          System.out.print(',');
          System.out.println(seq.getCount());
        }
      }

    } catch (java.io.IOException ex) {
      ex.printStackTrace();
    } catch (org.xml.sax.SAXException ex) {
      System.err.println(ex.getMessage());
    } catch (javax.xml.parsers.ParserConfigurationException ex) {
      ex.printStackTrace();
    }
  }
  /** Executes model and exports values. */
  public HashMap<String, String> executeCommand() {
    HashMap<String, String> levelNodeMap = new HashMap<String, String>();

    // (1) check if export file already exists
    if (new File(exportFileName).exists()) {
      System.out.println("ERROR: Export file already exists.");
      System.exit(1);
    }
    // (2) read model from XML file
    ArrayList<Model> model = null;
    try {
      model = XMLModelReader.readXMLModel(xmlModelFileName);

      levelNodeMap = XMLModelReader.getLevelNodes();
    } catch (AuxiliaryNodesCycleDependencyException e) {
      System.out.println(
          "ERROR: Invalid XML file: The model's auxiliary nodes have a cycle dependency.");
      System.exit(1);
    } catch (XMLModelReaderWriterException e) {
      System.out.println(
          "ERROR: An error occured during reading from the XML file: "
              + e.getException().getMessage());
      System.exit(1);
    } catch (XMLNodeParameterOutOfRangeException e) {
      System.out.println(
          "ERROR: Invalid XML file: A parameter of the node with the ID "
              + e.getXMLNodeId()
              + "is out of range.");
      System.exit(1);
    } catch (XMLRateNodeFlowException e) {
      System.out.println(
          "ERROR: Invalid XML file: The rate node with the ID"
              + e.getXMLNodeId()
              + "has no incoming or no outgoing flow.");
      System.exit(1);
    } catch (XMLUselessNodeException e) {
      System.out.println(
          "ERROR: Invalid XML file: The node with the ID"
              + e.getXMLNodeId()
              + "is useless for the model.");
      System.exit(1);
    }

    System.out.println("Model successfully read from XML file.");

    // (3) validate model and set unchangeable
    for (int k = 0; k < model.size(); k++) {
      try {
        model.get(k).validateModelAndSetUnchangeable(0);
      } catch (Exception e) {
        // There should be no exceptions here because the model was already validated while
        // reading from XML file.
        System.out.println("ERROR: Model is not valide.");
        System.exit(1);
      }

      // (4) execute model and export values
      LevelNode[] levelNodes = model.get(k).getLevelNodes().toArray(new LevelNode[0]);
      // sort level nodes alphabetically

      Arrays.sort(levelNodes);

      try {
        if (exportCSV) {
          // (2a) CSV export
          String[] columnNames = new String[levelNodes.length];
          for (int i = 0; i < columnNames.length; i++) {
            columnNames[i] = levelNodes[i].getNodeName();
          }
          System.out.println("EXPORTERRORPOTENTIAL");
          CSVExport csvExport =
              new CSVExport(exportFileName, model.get(k).getModelName(), columnNames);
          System.out.println("AFTEREXPORTERRORPOTENTIAL");
          int percent = 0;
          System.out.print("Export: 00%");

          double[] values = new double[levelNodes.length];
          for (int j = 0; j < values.length; j++) {
            values[j] = levelNodes[j].getCurrentValue();
          }
          csvExport.write(values);
          for (int i = 0; i < numberRounds; i++) {
            int newPercent = 100 * i / numberRounds;
            if (newPercent > percent) {
              percent = newPercent;
              // Backspaces do not work within Eclipse IDE - but on normal console!
              System.out.print("\b\b\b");
              if (percent < 10) {
                System.out.print("0");
              }
              System.out.print(percent + "%");
            }
            model.get(k).computeNextValues();
            values = new double[levelNodes.length];
            for (int j = 0; j < values.length; j++) {
              values[j] = levelNodes[j].getCurrentValue();
            }
            csvExport.write(values);
          }
          csvExport.close();
        } else {
          // (2b) XML export
          String[] nodeNames = new String[levelNodes.length];
          for (int i = 0; i < nodeNames.length; i++) {
            nodeNames[i] = levelNodes[i].getNodeName();
          }

          XMLExport xmlExport =
              new XMLExport(exportFileName, model.get(k).getModelName(), numberRounds, nodeNames);

          int percent = 0;
          System.out.print("Export: 00%");

          double[] values = new double[levelNodes.length];
          for (int j = 0; j < values.length; j++) {
            values[j] = levelNodes[j].getCurrentValue();
          }
          xmlExport.write(values);
          for (int i = 0; i < numberRounds; i++) {
            int newPercent = 100 * i / numberRounds;
            if (newPercent > percent) {
              percent = newPercent;
              // Backspaces do not work within Eclipse IDE - but on normal console!
              System.out.print("\b\b\b");
              if (percent < 10) {
                System.out.print("0");
              }
              System.out.print(percent + "%");
            }
            model.get(k).computeNextValues();
            values = new double[levelNodes.length];
            for (int j = 0; j < values.length; j++) {
              values[j] = levelNodes[j].getCurrentValue();
            }
            xmlExport.write(values);
          }
          xmlExport.close();
        }
      } catch (IOException e) {
        System.out.println("ERROR: An IOException occured during export.");
        System.exit(1);
      }

      System.out.println();
      System.out.println("Export finished successfully.");
      return levelNodeMap;
    }
    return levelNodeMap;
  }