Exemplo n.º 1
0
 /**
  * Send a random number with a Gaussian distribution to the output. This number is only changed in
  * the prefire() method, so it will remain constant throughout an iteration.
  *
  * @exception IllegalActionException If there is no director.
  */
 @Override
 public void fire() throws IllegalActionException {
   mean.update();
   standardDeviation.update();
   super.fire();
   output.send(0, new DoubleToken(_current));
 }
Exemplo n.º 2
0
  /**
   * Perform pattern matching and substring replacement, and output the modified string. If no match
   * is found, output the unmodified stringToEdit string.
   *
   * @exception IllegalActionException If there is no director.
   */
  public void fire() throws IllegalActionException {
    super.fire();

    replacement.update();
    stringToEdit.update();
    pattern.update();

    String replacementValue = ((StringToken) replacement.getToken()).stringValue();
    String stringToEditValue = ((StringToken) stringToEdit.getToken()).stringValue();
    boolean replaceAllTokens = ((BooleanToken) replaceAll.getToken()).booleanValue();
    boolean regularExpressionValue = ((BooleanToken) regularExpression.getToken()).booleanValue();

    if (regularExpressionValue) {
      if (_pattern == null) {
        try {
          String patternValue = ((StringToken) pattern.getToken()).stringValue();
          _pattern = Pattern.compile(patternValue);
        } catch (PatternSyntaxException ex) {
          String patternValue = ((StringToken) pattern.getToken()).stringValue();
          throw new IllegalActionException(
              this, ex, "Failed to compile regular expression \"" + patternValue + "\"");
        }
      }
      Matcher match = _pattern.matcher(stringToEditValue);
      String outputString = "";

      // Unfortunately, the String class throws runtime exceptions
      // if something goes wrong, so we have to catch them.
      try {
        if (replaceAllTokens) {
          outputString = match.replaceAll(replacementValue);
        } else {
          outputString = match.replaceFirst(replacementValue);
        }
      } catch (Exception ex) {
        throw new IllegalActionException(this, ex, "String replace failed.");
      }

      output.send(0, new StringToken(outputString));
    } else {
      // No regular expression.
      String outputString;
      if (replaceAllTokens) {
        outputString = stringToEditValue.replaceAll(_patternValue, replacementValue);
      } else {
        outputString = stringToEditValue.replace(_patternValue, replacementValue);
      }
      output.send(0, new StringToken(outputString));
    }
  }
Exemplo n.º 3
0
  /**
   * Read multiple arrays of XMLTokens from the input and combine them according to the specified
   * template. If the template contains invalid delimiters, then return the template file with the
   * valid ones replaced and the invalid ones unmodified.
   *
   * @exception IllegalActionException If thrown by the parent class, while reading a parameter or
   *     while reading the input.
   */
  @Override
  public void fire() throws IllegalActionException {
    super.fire();
    template.update();
    String outputString = removeHeader(template.getToken());
    String all = "";
    for (int j = 0; j < input.getWidth(); j++) {
      ArrayToken a = (ArrayToken) input.get(j);

      // FIXME: use StringBuffer instead of concatenating a String.
      String allInArray = "";
      int i;
      for (i = 0; i < a.length(); i++) {
        String elemInArray = removeHeader(a.getElement(i));
        if (i == 0) {
          allInArray = allInArray.concat(elemInArray);
        } else {
          allInArray = allInArray.concat('\n' + elemInArray);
        }
        String elemTag = "$input" + Integer.toString(j) + ',' + Integer.toString(i);
        outputString = outputString.replace(elemTag, elemInArray);
      }
      String arrayTag = "$input" + Integer.toString(j) + ",n";
      outputString = outputString.replace(arrayTag, allInArray);
      if (j == 0) {
        all = all.concat(allInArray);
      } else {
        all = all.concat('\n' + allInArray);
      }
    }
    outputString = outputString.replace("$inputn", all);
    String ADDheader = headerParameter.stringValue() + "\n";
    ADDheader = ADDheader.concat(outputString);
    try {
      XMLToken out = new XMLToken(ADDheader);
      output.broadcast(out);
    } catch (Exception e) {
      // FIXME: throw an exception that uses "this" so we
      // know in which actor the error is located
      throw new InternalErrorException(e);
    }
  }
Exemplo n.º 4
0
  /**
   * Read the control token input, transfer input tokens, and invoke prefire() of the selected
   * refinement.
   *
   * @exception IllegalActionException If there is no director, or if the director's prefire()
   *     method throws it, or if this actor is not opaque.
   */
  public boolean prefire() throws IllegalActionException {
    if (_debugging) {
      _debug("Calling prefire()");
    }

    try {
      _workspace.getReadAccess();
      super.prefire();

      Case container = (Case) getContainer();
      // Read from port parameters, including the control port.
      Iterator portParameters = container.attributeList(PortParameter.class).iterator();
      while (portParameters.hasNext()) {
        PortParameter portParameter = (PortParameter) portParameters.next();
        portParameter.update();
      }

      String controlValue = container.control.getToken().toString();
      ComponentEntity refinement = container.getEntity(controlValue);
      if (!(refinement instanceof Refinement)) {
        refinement = container._default;
      }
      container._current = (Refinement) refinement;

      // Transfer input tokens.
      for (Iterator inputPorts = container.inputPortList().iterator();
          inputPorts.hasNext() && !_stopRequested; ) {
        IOPort port = (IOPort) inputPorts.next();

        if (!(port instanceof ParameterPort)) {
          Receiver[][] insideReceivers = port.deepGetReceivers();
          for (int i = 0; i < port.getWidth(); i++) {
            if (port.hasToken(i)) {
              Token token = port.get(i);
              if ((insideReceivers != null) && (insideReceivers[i] != null)) {
                for (int j = 0; j < insideReceivers[i].length; j++) {
                  if (insideReceivers[i][j].getContainer().getContainer() == refinement) {
                    insideReceivers[i][j].put(token);
                    if (_debugging) {
                      _debug(
                          getFullName(),
                          "transferring input from "
                              + port.getFullName()
                              + " to "
                              + (insideReceivers[i][j]).getContainer().getFullName());
                    }
                  }
                }
              }
            }
          }
        }
      }
      if (_stopRequested) {
        return false;
      }
      return container._current.prefire();
    } finally {
      _workspace.doneReading();
    }
  }
Exemplo n.º 5
0
  /**
   * Consume at most one array from the input ports and produce the index of the first bin that
   * breaks the threshold.
   *
   * @exception IllegalActionException If there is no director.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    start.update();

    if (array.hasToken(0)) {
      ArrayToken inputArray = (ArrayToken) array.get(0);
      int inputSize = inputArray.length();

      int startValue = ((IntToken) start.getToken()).intValue();

      if ((startValue >= inputSize) || (startValue < 0)) {
        throw new IllegalActionException(this, "start is out of range: " + startValue);
      }

      int increment = -1;

      if (((BooleanToken) forwards.getToken()).booleanValue()) {
        increment = 1;
      }

      double reference = ((DoubleToken) inputArray.getElement(startValue)).doubleValue();

      double thresholdValue = ((DoubleToken) threshold.getToken()).doubleValue();

      String scaleValue = scale.stringValue();

      boolean aboveValue = ((BooleanToken) above.getToken()).booleanValue();

      if (scaleValue.equals("relative amplitude decibels")) {
        if (aboveValue) {
          thresholdValue = reference * Math.pow(10.0, (thresholdValue / 20));
        } else {
          thresholdValue = reference * Math.pow(10.0, (-thresholdValue / 20));
        }
      } else if (scaleValue.equals("relative power decibels")) {
        if (aboveValue) {
          thresholdValue = reference * Math.pow(10.0, (thresholdValue / 10));
        } else {
          thresholdValue = reference * Math.pow(10.0, (-thresholdValue / 10));
        }
      } else if (scaleValue.equals("relative linear")) {
        if (aboveValue) {
          thresholdValue = reference + thresholdValue;
        } else {
          thresholdValue = reference - thresholdValue;
        }
      }

      // Default output if we don't find a crossing.
      int bin = -1;

      for (int i = startValue; (i < inputSize) && (i >= 0); i += increment) {
        double currentValue = ((DoubleToken) inputArray.getElement(i)).doubleValue();

        if (aboveValue) {
          // Searching for values above the threshold.
          if (currentValue > thresholdValue) {
            bin = i;
            break;
          }
        } else {
          // Searching for values below the threshold.
          if (currentValue < thresholdValue) {
            bin = i;
            break;
          }
        }
      }

      output.send(0, new IntToken(bin));
    }
  }