Exemple #1
0
  /** @return whether or not the MetaDD should be written out at the end. */
  private static boolean processMetaDD(Config config) throws IOException {
    boolean needToWriteMetaDD = false;
    MetaDD mdd;
    //
    //	Either creates the metaDD file or read the existing one
    //
    if (config.getMddFile() != null || config.getMddIn() != null) {
      File file = config.getMddFile();

      if (config.getMddIn() == null && !file.exists()) {
        config.setDoGeneration(false);
        if (!config.isAuto()) {
          if (!askYesOrNo(
              config.messageOut,
              "The mdd file "
                  + config.getMddFile()
                  + // NOI18N
                  " doesn't exist. Should we create it (y/n)? ")) { // NOI18N
            config.messageOut.println("Generation aborted."); // NOI18N
            return false;
          }
        }
        needToWriteMetaDD = true;
        mdd = new MetaDD();
      } else {
        try {
          InputStream is = config.getMddIn();
          if (config.getMddIn() == null) {
            is = new FileInputStream(config.getMddFile());
            config.messageOut.println(Common.getMessage("MSG_UsingMdd", config.getMddFile()));
          }
          mdd = MetaDD.read(is);
          if (config.getMddIn() == null) {
            is.close();
          }
        } catch (IOException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        } catch (javax.xml.parsers.ParserConfigurationException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        } catch (org.xml.sax.SAXException e) {
          if (config.isTraceParse()) e.printStackTrace();
          throw new IOException(Common.getMessage("CantCreateMetaDDFile_msg", e.getMessage()));
        }
      }
    } else {
      // Create a MetaDD to look stuff up in later on, even though
      // it wasn't read in, and we're not going to write it out.
      mdd = new MetaDD();
    }
    config.setMetaDD(mdd);
    return needToWriteMetaDD;
  }
Exemple #2
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();
    }
  }