Пример #1
0
 /**
  * If the argument is the meanTime parameter, check that it is positive.
  *
  * @param attribute The attribute that changed.
  * @exception IllegalActionException If the meanTime value is not positive.
  */
 @Override
 public void attributeChanged(Attribute attribute) throws IllegalActionException {
   if (attribute == meanTime) {
     double mean = ((DoubleToken) meanTime.getToken()).doubleValue();
     if (mean <= 0.0) {
       throw new IllegalActionException(
           this, "meanTime is required to be positive.  meanTime given: " + mean);
     }
   } else if (attribute == values) {
     ArrayToken val = (ArrayToken) values.getToken();
     $ASSIGN$_length(val.length());
   } else if (attribute == stopTime) {
     double newStopTimeValue = ((DoubleToken) stopTime.getToken()).doubleValue();
     if (_executing) {
       Time newStopTime = new Time(getDirector(), newStopTimeValue);
       Director director = getDirector();
       if (director != null) {
         Time currentTime = director.getModelTime();
         if (newStopTime.compareTo(currentTime) > 0) {
           director.fireAt(this, newStopTime);
         } else {
           throw new IllegalActionException(
               this, "The stop time " + "is earlier than the current time.");
         }
       }
       $ASSIGN$_stopTime(newStopTime);
     }
   } else {
     super.attributeChanged(attribute);
   }
 }
Пример #2
0
 private Token _getValue(int index) throws IllegalActionException {
   ArrayToken val = (ArrayToken) values.getToken();
   if (val == null || index >= _length) {
     throw new IllegalActionException(this, "Index out of range of the values parameter.");
   }
   return val.getElement(index);
 }
Пример #3
0
  /**
   * Consume the input ArrayToken and produce the outputs.
   *
   * @exception IllegalActionException If a runtime type conflict occurs.
   */
  public void fire() throws IllegalActionException {
    super.fire();

    ArrayToken token = (ArrayToken) input.get(0);
    int rate = ((IntToken) arrayLength.getToken()).intValue();
    boolean enforce = ((BooleanToken) enforceArrayLength.getToken()).booleanValue();

    if (enforce && token.length() != rate) {
      throw new IllegalActionException(
          this,
          "The "
              + "number of elements in the input ArrayToken ("
              + token.length()
              + ") is not the same as the arrayLength "
              + "parameter ("
              + rate
              + ").");
    }

    Token[] elements = token.arrayValue();

    // We no longer send the complete array all at once, since this might in
    // for example PN lead to larger buffer sizes than strictly necessary.
    // output.send(0, elements, elements.length);

    for (Token newToken : elements) {
      output.send(0, newToken);
    }
  }
Пример #4
0
  /**
   * Update the state of the actor by moving to the next value in the <i>values</i> array.
   *
   * @exception IllegalActionException If there is no director.
   */
  public boolean postfire() throws IllegalActionException {
    boolean result = super.postfire();
    if (_outputProduced) {
      _outputProduced = false;
      _currentIndex += 1;

      ArrayToken valuesArray = (ArrayToken) values.getToken();

      if (_currentIndex >= valuesArray.length()) {
        boolean repeatValue = ((BooleanToken) repeat.getToken()).booleanValue();

        if (repeatValue) {
          _currentIndex = 0;
        } else {
          boolean holdLastOutputValue = ((BooleanToken) holdLastOutput.getToken()).booleanValue();
          if (holdLastOutputValue) {
            // To repeatedly produce the last output.
            _currentIndex = valuesArray.length() - 1;
          } else {
            // To prevent overflow.
            _currentIndex = valuesArray.length();
            // No more outputs to produce.
            result = false;
          }
        }
      }
    }

    return result;
  }
Пример #5
0
  /**
   * Consume one token from each channel of the input port, assemble those tokens into an
   * ArrayToken, and send the result to the output.
   *
   * @exception IllegalActionException If not enough tokens are available.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    int size = input.getWidth();
    Type type = input.getType();

    // if ((ArrayType.class).isInstance(type)) {
    // System.out.println("Input is an array!");
    // }
    // for now, simply assume inputs are arrays
    Vector v = new Vector();
    // Type datatype = null;
    for (int i = 0; i < size; i++) {
      ArrayToken at = (ArrayToken) input.get(i);
      // The type is not used anywhere but inquiring the type from the first token
      //  does not allow that array to be empty. So I commented out. Norbert Podhorszki
      // if (i == 0) {
      //	datatype = at.getElement(0).getType();
      // }
      for (int j = 0; j < at.length(); j++) {
        v.addElement(at.getElement(j));
      }
    }
    int vecLength = v.size();
    Token[] ta = new Token[vecLength];
    for (int k = 0; k < vecLength; k++) {
      ta[k] = (Token) v.elementAt(k);
    }
    if (vecLength > 0) output.send(0, new ArrayToken(ta));
    else // send empty array
    output.send(0, new ArrayToken(BaseType.NIL));
  }
Пример #6
0
  /**
   * Does up to three things, in this order: Set new remote address value, Set new remote socket
   * number, transmit data as a UDP packet over the ethernet. The first two can, of course, affect
   * where the datagram goes. Any remote address/socket values supplied are saved and become the
   * defaults for next time.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    String address = null;

    for (int jj = 0; jj < remoteAddress.getWidth(); jj++) {
      if (remoteAddress.hasToken(jj)) {
        address = ((StringToken) (remoteAddress.get(jj))).stringValue();
      }
    }

    if (address != null) {
      try {
        _address = InetAddress.getByName(address);
      } catch (UnknownHostException ex) {
        throw new IllegalActionException(
            this, ex, "The input remote " + "address specifies an unknown host");
      }
    }

    for (int jj = 0; jj < remoteSocketNumber.getWidth(); jj++) {
      if (remoteSocketNumber.hasToken(jj)) {
        // Valid socket numbers are 0..65535,
        // so keep only lower 16 bits.
        _remoteSocketNumber = 65535 & ((IntToken) remoteSocketNumber.get(jj)).intValue();
      }
    }

    if (data.hasToken(0)) {
      ArrayToken dataArrayToken = (ArrayToken) data.get(0);

      byte[] dataBytes = new byte[dataArrayToken.length()];

      for (int j = 0; j < dataArrayToken.length(); j++) {
        UnsignedByteToken token = (UnsignedByteToken) dataArrayToken.getElement(j);
        dataBytes[j] = token.byteValue();
      }

      DatagramPacket packet =
          new DatagramPacket(dataBytes, dataBytes.length, _address, _remoteSocketNumber);

      try {
        _socket.send(packet);
      } catch (IOException ex) {
        // ignore, UDP does not guarantee success
        // throw new InternalErrorException("socket.send failed");
        // FIXME  I don't believe that!  I think send guarantees that
        // it will send!!  Just not that anyone is listening.
        //    (yet when I ran it with 'throw...' uncommented
        //     then it threw it right away!? )
        // Would TCP stall here awaiting reply??  I doubt it!
      }

      triggerOutput.broadcast(new Token());
    }
  }
Пример #7
0
  public void fire() throws IllegalActionException {
    if (_debugging) {
      _debug("Called fire()");
    }

    ArrayToken lineStartToken = ((ArrayToken) lineStart.getToken());
    ArrayToken lineEndToken = ((ArrayToken) lineEnd.getToken());
    ArrayToken rgbColorValue = ((ArrayToken) rgbColor.getToken());
    DoubleToken widthValue = (DoubleToken) width.getToken();

    GL gl = ((GRODirector) getDirector()).getGL();

    gl.glLineWidth((float) widthValue.doubleValue());
    gl.glBegin(GL.GL_LINES);

    gl.glColor3d(
        ((DoubleToken) rgbColorValue.getElement(0)).doubleValue(),
        ((DoubleToken) rgbColorValue.getElement(1)).doubleValue(),
        ((DoubleToken) rgbColorValue.getElement(2)).doubleValue());

    // origin of the line
    gl.glVertex3d(
        ((DoubleToken) lineStartToken.getElement(0)).doubleValue(),
        ((DoubleToken) lineStartToken.getElement(1)).doubleValue(),
        ((DoubleToken) lineStartToken.getElement(2)).doubleValue());

    // ending point of the line
    gl.glVertex3d(
        ((DoubleToken) lineEndToken.getElement(0)).doubleValue(),
        ((DoubleToken) lineEndToken.getElement(1)).doubleValue(),
        ((DoubleToken) lineEndToken.getElement(2)).doubleValue());

    gl.glEnd();
  }
Пример #8
0
  public List<Inequality> constraintList() throws IllegalActionException {
    ArrayToken valuesArray = (ArrayToken) _actor.values.getToken();

    for (int i = 0; i < valuesArray.length(); i++) {
      setEquals(
          _actor.output,
          _lattice.convertJavaToCtype(
              valuesArray.getElement(i).getType(), valuesArray.getElement(i)));
    }

    return super.constraintList();
  }
Пример #9
0
  /**
   * If the <i>enable</i> input is connected, then if it has a true token, produce the next output.
   * If it is not connected, produce the next output unconditionally. Whether it is connected is
   * determined by checking the width of the port.
   *
   * @exception IllegalActionException If there is no director.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    if ((!enable.isOutsideConnected())
        || (enable.hasToken(0) && ((BooleanToken) enable.get(0)).booleanValue())) {
      ArrayToken valuesArray = (ArrayToken) values.getToken();

      if (_currentIndex < valuesArray.length()) {
        output.send(0, valuesArray.getElement(_currentIndex));
        _outputProduced = true;
      }
    }
  }
Пример #10
0
 /**
  * Override the base class to reset the resource pool to match the specified initialPool value.
  *
  * @param attribute The attribute that changed.
  * @exception IllegalActionException If the change is not acceptable to this container (not thrown
  *     in this base class).
  */
 public void attributeChanged(Attribute attribute) throws IllegalActionException {
   if (attribute == initialPool) {
     ArrayToken pool = (ArrayToken) initialPool.getToken();
     // Reset the pool.
     _pool.clear();
     // Copy the tokens into the pool.
     for (int i = 0; i < pool.length(); i++) {
       _pool.add(pool.getElement(i));
     }
   } else {
     super.attributeChanged(attribute);
   }
 }
Пример #11
0
  /**
   * Consume at most one token from the input port and produce the element at the index specified by
   * this token from the table array on the output port. If there is no token on the input or the
   * token is out of range, then no output is produced.
   *
   * @exception IllegalActionException If there is no director.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    if (input.hasToken(0)) {
      ArrayToken token = (ArrayToken) table.getToken();

      if (token != null) {
        int indexValue = ((IntToken) input.get(0)).intValue();

        if ((indexValue >= 0) && (indexValue < token.length())) {
          output.broadcast(token.getElement(indexValue));
        }
      }
    }
  }
Пример #12
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);
    }
  }
Пример #13
0
  public void initialize() throws IllegalActionException {
    ptolemy.data.Token[] tokens = initialOutputs_CGToken.arrayValue();
    int i = initialOutputs_CGToken.length();

    for (int i_1_ = 0; i_1_ < i; i_1_++) {
      ptolemy.data.Token token = tokens[i_1_];
      DoubleToken doubletoken;

      if (token instanceof DoubleToken) {
        doubletoken = (DoubleToken) token;
      } else {
        if (!(token instanceof IntToken)) {
          throw new IllegalActionException("Token Exception");
        }

        doubletoken = new DoubleToken(((IntToken) token).doubleValue());
      }

      System.out.println("token = " + doubletoken.toString());
    }
  }
Пример #14
0
  public Unboxing10() throws IllegalActionException {
    IntToken[] inttokens = new IntToken[1];
    inttokens[0] = new IntToken(0);
    (this).arraytoken = new ArrayToken(inttokens);

    // ArrayType arraytype = new ArrayType(BaseType.INT);
    ptolemy.data.Token[] tokens = (this).arraytoken.arrayValue();
    IntToken[] inttokens_0_ = new IntToken[tokens.length];

    for (int i = 0; i < tokens.length; i++) {
      ((ptolemy.data.Token[]) inttokens_0_)[i] = tokens[i];
    }

    (this).arraytoken2 = new ArrayToken(inttokens_0_);
    initialOutputs_CGToken = (this).arraytoken2;
    System.out.println("token = " + initialOutputs_CGToken.toString());
  }
Пример #15
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));
    }
  }
Пример #16
0
  /**
   * Create a new background figure. This overrides the base class to draw a box around the value
   * display, where the width of the box depends on the value.
   *
   * @return A new figure.
   */
  public Figure createBackgroundFigure() {
    NamedObj container = getContainer();
    CompositeFigure result = new CompositeFigure();
    if (container != null) {
      try {
        ArrayToken fieldsValue = (ArrayToken) fields.getToken();
        Attribute associatedAttribute = container.getAttribute(variableName.getExpression());
        if (associatedAttribute instanceof Variable) {
          Token value = ((Variable) associatedAttribute).getToken();
          if (value instanceof ArrayToken) {
            // Find the number of rows and columns.
            int numRows = ((ArrayToken) value).length();
            int numColumns = fieldsValue.length();
            if (numColumns == 0) {
              // All columns should be included.
              // Make a pass to figure out how many that is.
              for (int i = 0; i < numRows; i++) {
                Token row = ((ArrayToken) value).getElement(i);
                if (row instanceof RecordToken) {
                  int rowWidth = ((RecordToken) row).labelSet().size();
                  if (rowWidth > numColumns) {
                    numColumns = rowWidth;
                  }
                }
              }
            }

            // Find the width of each column and the height of each row.
            // All rows are the same height, but column widths can vary.
            double rowHeight = 0.0;
            double columnWidth[] = new double[numColumns];
            for (int i = 1; i < numColumns; i++) {
              columnWidth[i] = 0.0;
            }
            LabelFigure tableElement[][] = new LabelFigure[numRows][numColumns];
            // Iterate over rows.
            for (int i = 0; i < numRows; i++) {
              Token row = ((ArrayToken) value).getElement(i);
              if (row instanceof RecordToken) {
                if (fieldsValue.length() == 0) {
                  // Display all fields.
                  Iterator labelSet = ((RecordToken) row).labelSet().iterator();
                  int j = 0;
                  while (labelSet.hasNext()) {
                    String column = (String) labelSet.next();
                    tableElement[i][j] = _labelFigure((RecordToken) row, column);
                    Rectangle2D bounds = tableElement[i][j].getBounds();
                    double width = bounds.getWidth();
                    if (width > columnWidth[j]) {
                      columnWidth[j] = width;
                    }
                    double height = bounds.getHeight();
                    if (height > rowHeight) {
                      rowHeight = height;
                    }
                    j++;
                  }
                } else {
                  // Display specified fields.
                  for (int j = 0; j < fieldsValue.length(); j++) {
                    if (j >= numColumns) {
                      break;
                    }
                    String column = ((StringToken) fieldsValue.getElement(j)).stringValue();
                    tableElement[i][j] = _labelFigure((RecordToken) row, column);
                    Rectangle2D bounds = tableElement[i][j].getBounds();
                    double width = bounds.getWidth();
                    if (width > columnWidth[j]) {
                      columnWidth[j] = width;
                    }
                    double height = bounds.getHeight();
                    if (height > rowHeight) {
                      rowHeight = height;
                    }
                  }
                }
              }
            }

            // Now make a pass to position and add all the figures.
            double rowPosition = _VERTICAL_PADDING;
            // Iterate over rows.
            for (int i = 0; i < numRows; i++) {
              Token row = ((ArrayToken) value).getElement(i);
              if (row instanceof RecordToken) {
                if (fieldsValue.length() == 0) {
                  // Display all fields.
                  Iterator labelSet = ((RecordToken) row).labelSet().iterator();
                  int j = 0;
                  double columnPosition = _HORIZONTAL_PADDING;
                  while (labelSet.hasNext()) {
                    /*String column = (String) */ labelSet.next();
                    tableElement[i][j].translateTo(columnPosition, rowPosition);
                    result.add(tableElement[i][j]);
                    columnPosition += columnWidth[j] + _HORIZONTAL_PADDING;
                    j++;
                  }
                } else {
                  // Display specified fields.
                  double columnPosition = _HORIZONTAL_PADDING;
                  for (int j = 0; j < fieldsValue.length(); j++) {
                    // String column = ((StringToken)fieldsValue.getElement(j)).stringValue();
                    tableElement[i][j].translateTo(columnPosition, rowPosition);
                    result.add(tableElement[i][j]);
                    columnPosition += columnWidth[j] + _HORIZONTAL_PADDING;
                  }
                }
              }
              rowPosition += rowHeight + _VERTICAL_PADDING;
            }
          }
        }
      } catch (IllegalActionException e) {
        // Stick the error message in the icon.
        result.add(new LabelFigure(e.getMessage()));
      }
    }
    // Now put a box around it all.
    Rectangle2D bounds = result.getBounds();
    // Double the padding below to allow for both sides.
    double width = Math.floor(bounds.getWidth()) + _HORIZONTAL_PADDING * 2;
    double height = Math.floor(bounds.getHeight()) + _VERTICAL_PADDING * 2;
    Figure rectangle = new BasicRectangle(0, 0, width, height, boxColor.asColor(), 1);
    CompositeFigure finalResult = new CompositeFigure(rectangle);
    finalResult.add(result);
    return finalResult;
  }
Пример #17
0
  /**
   * Execute the filter model on each input array element until it gets as many elements as
   * specified by the <i>maxOutputLength</i> parameter. If there are no enough elements satisfying
   * the filter model, then only output all the satisfied elements. Before running the filter model,
   * this method update the filter model's <i>inputArrayElement</i> parameter for each array
   * element. After running the filter model, this method looks for the <i>evaluatedValue</i>
   * parameter and keep the input element if the evaluated value is ture, otherwise, skip the
   * element.
   *
   * @exception IllegalActionException If there is no director, or if the director's action methods
   *     throw it.
   */
  public void fire() throws IllegalActionException {
    super.fire();

    if (_model instanceof CompositeActor) {
      CompositeActor executable = (CompositeActor) _model;

      _manager = executable.getManager();

      if (_manager == null) {
        throw new InternalErrorException("No manager!");
      }

      if (_debugging) {
        _manager.addDebugListener(this);

        Director director = executable.getDirector();

        if (director != null) {
          director.addDebugListener(this);
        }
      } else {
        _manager.removeDebugListener(this);

        Director director = executable.getDirector();

        if (director != null) {
          director.removeDebugListener(this);
        }
      }

      int i = 0;
      int j = 0;
      LinkedList list = new LinkedList();
      ArrayToken array = (ArrayToken) inputArray.get(0);

      while ((i < _outputLength) && (j < array.length())) {
        Token t = array.getElement(j);
        _updateParameter(t);

        if (_debugging) {
          _debug("** Executing filter model.");
        }

        try {
          _manager.execute();
        } catch (KernelException ex) {
          throw new IllegalActionException(this, ex, "Execution failed.");
        }

        if (_getResult()) {
          i++;
          list.add(t);
        }

        j++;
      }

      Token[] result = new Token[list.size()];

      for (i = 0; i < list.size(); i++) {
        result[i] = (Token) list.get(i);
      }

      outputArray.send(0, new ArrayToken(array.getElementType(), result));
    }
  }