コード例 #1
0
ファイル: QUDG.java プロジェクト: manoj-g-nair/idswsn
  /**
   * The default constructor for this class.
   *
   * <p>The first time this constructor is called, it initializes the static parameters of this
   * class. This approach is necessary as to give proper error reporting if one of the
   * initialization steps fails.
   *
   * @throws CorruptConfigurationEntryException If one of the initialization steps fails.
   */
  public QUDG() throws CorruptConfigurationEntryException {
    // only call the first time a QUDG object is created
    if (!initialized) {
      // speed up by comparing the squared distances (needs not take the square root to get the
      // distance)
      r_min = Configuration.getDoubleParameter("QUDG/rMin");
      r_min_squared = r_min * r_min;

      r_max = Configuration.getDoubleParameter("QUDG/rMax");
      r_max_squared = r_max * r_max;

      // Sanity check
      double geomNodeRMax = Configuration.getDoubleParameter("GeometricNodeCollection/rMax");
      if (r_max > geomNodeRMax) { // dangerous! This is probably not what the user wants!
        Main.minorError(
            "WARNING: The maximum transmission range used for the QUDG connectivity model is larger than the maximum transmission range specified for the GeometricNodeCollection.\nAs a result, not all connections will be found! Either fix the problem in the project-specific configuration file or the '-overwrite' command line argument.");
      }
      if (r_max <= r_min) {
        Main.minorError(
            "WARNING: The maximum transmission range used for the QUDG connectivity model is not larger than the minimum tansmission range.\nEither fix the problem in the project-specific configuration file or the '-overwrite' command line argument.");
      }

      // TODO: rewrite the docu of this class
      String type = Configuration.getStringParameter("QUDG/ProbabilityType");
      if (type.toLowerCase().equals("constant")) {
        probabilityType = 0;
        probability = Configuration.getDoubleParameter("QUDG/connectionProbability");
      } else if (type.toLowerCase().equals("linear")) {
        probabilityType = 1;
        m = 1 / (r_min - r_max);
        q = r_max / (r_max - r_min);
      } else if (type.toLowerCase().equals("quadratic")) {
        probabilityType = 2;
        throw new NotYetImplementedException(
            "QUDG does not yet support quadratic probability distributions.");
      } else {
        // TODO: rewrite the following exception, rewrite docu as well
        throw new CorruptConfigurationEntryException(
            "The QUDG connectivity model requires an entry in the project"
                + " configuration file that specifies the kind of probability to be applied if the distance between two nodes "
                + "lies between rMin and rMax. Possible values for ProbabilityType are 'constant', 'linear', and 'quadratic'.\n\n"
                + "'constant' requires yet another entry 'connectionProbability', which specifies the constant probability at which the connection exists.\n\n"
                + "'linear' applies a linear regression that decreases from 1 to 0 from rMin to rMax.\n\n"
                + "'quadratic' applies a quadratic regression that decreases from 1 to 0 from rMin to rMax.\n\n");
      }

      probability = Configuration.getDoubleParameter("QUDG/connectionProbability");
      initialized = true;
    }
  }
コード例 #2
0
 /**
  * Show the description for a given project
  *
  * @param projectName The project name
  */
 private void generateGUIDescription(String projectName) {
   File proj =
       new File(
           Configuration.sourceDirPrefix
               + "/"
               + Configuration.projectDirInSourceFolder
               + "/"
               + projectName
               + "/"
               + Configuration.descriptionFileName);
   try {
     if (!proj.exists()) {
       descriptionText.setText("There is no description-file in the currently selected project.");
     } else {
       LineNumberReader r = new LineNumberReader(new FileReader(proj));
       String description = "";
       String tmp = null;
       while ((tmp = r.readLine()) != null) {
         description += tmp + "\n";
       }
       descriptionText.setText(description);
       descriptionText.setCaretPosition(0);
     }
   } catch (FileNotFoundException e) {
     descriptionText.setText("There is no description-file in the currently selected project.");
   } catch (IOException e) {
     Main.minorError(e);
     descriptionText.setText("There is no description-file in the currently selected project.");
   }
 }
コード例 #3
0
 /**
  * Validates the custom configuration and displays, if necessary, error messages.
  *
  * @return The XML document describing the custom configuration, null if the configuration did not
  *     parse properly.
  */
 private Document validateCustomFields() {
   Document doc = null;
   try {
     String xml =
         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Document><Custom>"
             + customParameters.getText()
             + "</Custom></Document>";
     doc = new SAXBuilder().build(new StringReader(xml));
   } catch (JDOMException e) {
     Main.minorError("Invalid XML in the custom configuration:\n\n" + e.getMessage());
     return null;
   } catch (IOException e) {
     Main.minorError(
         "Cannot parse custom configuration due to I/O exception:\n\n" + e.getMessage());
     return null;
   }
   return doc;
 }
コード例 #4
0
  private void storeConfig(boolean isTemporary) {
    // Test that the custom config is OK
    Document customConfig = validateCustomFields();
    if (customConfig == null) {
      return;
    }

    // Build the framework config XML tree
    Document doc = new Document();
    Element root = new Element("Document");
    doc.setRootElement(root);
    Element framework = new Element("Framework");
    root.addContent(framework);
    Element custom = new Element("Custom");
    root.addContent(custom);
    custom.addContent(
        new Element(
            "_xml_custom_")); // this tag will be replaced by the config text in a second step

    for (ConfigEntry e : projectEntries) {
      if (e.valueComponent != null) { // there is a value field in the GUI
        if (e.comment != "") { // the comment is not "", add it
          framework.addContent(
              new Comment(e.comment.replace("\n", " "))); // without the newline chars
        }
        // get the value of this entry from the GUI
        String value = "";
        if (e.valueComponent instanceof JComboBox) {
          value = (String) ((JComboBox) e.valueComponent).getSelectedItem();
        } else if (e.valueComponent instanceof JTextComponent) {
          value = ((JTextComponent) e.valueComponent).getText();
        }
        // create and add a new entry in the XML file
        Element elem = new Element(e.key);
        elem.setAttribute("value", value);
        framework.addContent(elem);
        framework.addContent(
            new Element(
                "_xml_NL_")); // after each entry, we would like a new-line in the document - these
                              // tags are replaced in a second step
      } else {
        // this is a section entry, which will be inserted as comment
        // String line = " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ";
        String line = "***********************************************************************";
        framework.addContent(new Comment(line));
        String name = "  " + e.key;
        while (name.length()
            < line.length()) { // fill the name with spaces s.t. the '-->' are aligned
          name += " ";
        }
        framework.addContent(new Comment(name));
        framework.addContent(new Comment(line));
      }
    }

    String outputPath =
        Configuration.sourceDirPrefix
            + "/"
            + Configuration.projectDirInSourceFolder
            + "/"
            + selectedProjectName;
    File outputFile =
        new File(outputPath + "/" + Configuration.configfileFileName + (isTemporary ? ".run" : ""));

    // And write the xml tree to the file
    XMLOutputter outputter = new XMLOutputter();
    Format f = Format.getPrettyFormat();
    f.setIndent("\t");
    outputter.setFormat(f);
    File tempOutputFile = new File(outputPath + "/" + Configuration.configfileFileName + ".temp");
    try {
      FileWriter fW = new FileWriter(tempOutputFile);
      outputter.output(doc, fW);
      fW.flush();
      fW.close();
    } catch (IOException e) {
      Main.minorError("Could not write a temporary configuration file!\n\n" + e.getMessage());
      return;
    }

    // in a second step, parse the temp file, replace the _xml_nl_ by new-lines and the _xml_custom_
    // by the custom text

    try {
      BufferedWriter output = new BufferedWriter(new FileWriter(outputFile));
      LineNumberReader input = new LineNumberReader(new FileReader(tempOutputFile));
      String line = input.readLine();
      while (line != null) {
        if (line.contains("<_xml_NL_")) {
          output.newLine();
        } else if (line.contains("<_xml_custom_")) {
          output.write(customParameters.getText());
        } else {
          output.write(line);
          output.newLine();
        }
        line = input.readLine();
      }
      output.flush();
      output.close();
      input.close();
      tempOutputFile.delete();
    } catch (IOException e1) {
      Main.minorError("Could not write the configuration file!\n\n" + e1.getMessage());
    }

    userInputListener.reset(); // finally reset the 'modified' flag
  }