Example #1
0
  /**
   * Generate the initialize code. Declare the variable state.
   *
   * @return The initialize code.
   * @exception IllegalActionException If thrown while generating the initialization code, while
   *     appending the code block or while converting the codeStream to a string.
   */
  @Override
  public String generateInitializeCode() throws IllegalActionException {
    super.generateInitializeCode();

    ptolemy.actor.lib.Ramp actor = (ptolemy.actor.lib.Ramp) getComponent();

    ArrayList<String> args = new ArrayList<String>();
    args.add(getCodeGenerator().codeGenType(actor.output.getType()));

    CodeStream codeStream = _templateParser.getCodeStream();
    codeStream.append(
        _eol
            + CodeStream.indent(
                getCodeGenerator().comment("initialize " + getComponent().getName())));
    if (actor.output.getType() == BaseType.STRING) {
      codeStream.appendCodeBlock("StringInitBlock");
    } else {
      codeStream.appendCodeBlock("CommonInitBlock", args);
      if (actor.output.getType() instanceof ArrayType) {
        Type elementType = ((ArrayType) actor.output.getType()).getElementType();

        args.set(0, "TYPE_" + getCodeGenerator().codeGenType(elementType));
        if (!actor.step.getType().equals(actor.output.getType())) {
          codeStream.appendCodeBlock("ArrayConvertStepBlock", args);
        }
        if (!actor.init.getType().equals(actor.output.getType())) {
          codeStream.appendCodeBlock("ArrayConvertInitBlock", args);
        }
      }
    }

    return processCode(codeStream.toString());
  }
Example #2
0
  /**
   * Generate the code for initializing the random number generator with the seed, if it has been
   * given. A seed of zero is interpreted to mean that no seed is specified. In such cases, a seed
   * based on the current time and this instance of a ColtRandomSource is used to be fairly sure
   * that two identical sequences will not be returned.
   *
   * @return The initialize code of this actor.
   * @exception IllegalActionException Not thrown in this class.
   */
  public String generateInitializeCode() throws IllegalActionException {
    super.generateInitializeCode();

    ptolemy.actor.lib.colt.ColtRandomSource actor =
        (ptolemy.actor.lib.colt.ColtRandomSource) getComponent();

    long seedValue = ((LongToken) (actor.seed.getToken())).longValue();

    ArrayList<String> args = new ArrayList<String>();
    CodeStream codeStream = _templateParser.getCodeStream();
    if (seedValue == 0) {
      args.add(Long.toString(actor.hashCode()));
      codeStream.appendCodeBlock("setSeedBlock0", args);
    } else { // Use fixed seed + actorDisplayName.hashCode().
      args.add(Long.toString(actor.getDisplayName().hashCode()));
      codeStream.appendCodeBlock("setSeedBlock1", args);
    }

    String generatorClassValue = ((StringToken) actor.generatorClass.getToken()).stringValue();
    if ((generatorClassValue == null) || generatorClassValue.equals("DRand")) {
      codeStream.appendCodeBlock("setRandomNumberGeneratorDRand");
    } else if (generatorClassValue.equals("MersenneTwister (MT19937)")) {
      codeStream.appendCodeBlock("setRandomNumberMersenneTwister");
    } else if (generatorClassValue.equals("Ranecu")) {
      codeStream.appendCodeBlock("setRandomNumberRanecu");
    } else if (generatorClassValue.equals("Ranlux")) {
      codeStream.appendCodeBlock("setRandomNumberRanlux");
    } else if (generatorClassValue.equals("Ranmar")) {
      codeStream.appendCodeBlock("setRandomNumberRanmar");
    }

    return processCode(codeStream.toString());
  }
Example #3
0
  /**
   * Generate fire code for the Scale actor.
   *
   * @return The generated code.
   * @exception IllegalActionException If the code stream encounters an error in processing the
   *     specified code block(s).
   */
  protected String _generateFireCode() throws IllegalActionException {
    super._generateFireCode();

    ptolemy.actor.lib.Scale actor = (ptolemy.actor.lib.Scale) getComponent();

    String type = getCodeGenerator().isPrimitive(actor.input.getType()) ? "" : "Token";

    _templateParser.getCodeStream().appendCodeBlock(type + "FireBlock", false);
    return processCode(_templateParser.getCodeStream().toString());
  }
Example #4
0
  /**
   * Generate fire code for the Ramp actor.
   *
   * @return The generated code.
   * @exception IllegalActionException If the code stream encounters an error in processing the
   *     specified code block(s).
   */
  @Override
  protected String _generateFireCode() throws IllegalActionException {
    super._generateFireCode();

    ptolemy.actor.lib.Ramp actor = (ptolemy.actor.lib.Ramp) getComponent();

    String type = getCodeGenerator().codeGenType(actor.output.getType());
    if (!getCodeGenerator().isPrimitive(type)) {
      type = "Token";
    }

    CodeStream codeStream = _templateParser.getCodeStream();
    codeStream.appendCodeBlock(type + "FireBlock");
    return processCode(codeStream.toString());
  }
Example #5
0
  /**
   * Generate fire code. The method reads in <code>fireBlock</code> from ElementsToArray.c, replaces
   * macros with their values and returns the processed code block.
   *
   * @return The generated code.
   * @exception IllegalActionException If the code stream encounters an error in processing the
   *     specified code block(s).
   */
  @Override
  protected String _generateFireCode() throws IllegalActionException {
    super._generateFireCode();

    CodeStream codeStream = _templateParser.getCodeStream();

    codeStream.append(super._generateFireCode());
    ptolemy.actor.lib.ElementsToArray actor = (ptolemy.actor.lib.ElementsToArray) getComponent();

    ArrayList args = new ArrayList();
    args.add(Integer.valueOf(0).toString());

    for (int i = 0; i < actor.input.getWidth(); i++) {
      args.set(0, Integer.valueOf(i).toString());
      codeStream.appendCodeBlock("fillArray", args);
    }
    codeStream.appendCodeBlock("sendOutput");

    return processCode(codeStream.toString());
  }
  /**
   * Generate preinitialize code. Read the <code>CommonPreinitBlock</code> from MovingAverage.c
   * replace macros with their values and return the processed code block.
   *
   * @return The generated code.
   * @exception IllegalActionException If the code stream encounters an error in processing the
   *     specified code block(s).
   */
  @Override
  public String generatePreinitializeCode() throws IllegalActionException {
    super.generatePreinitializeCode();

    ptolemy.actor.lib.MovingAverage actor = (ptolemy.actor.lib.MovingAverage) getComponent();

    ArrayList<String> args = new ArrayList<String>();
    CodeStream codeStream = _templateParser.getCodeStream();

    Type type = actor.output.getType();
    if (getCodeGenerator().isPrimitive(type)) {
      args.add(targetType(type));
      codeStream.appendCodeBlock("CommonPreinitBlock", args);
    } else {
      throw new IllegalActionException(
          "Non-primitive types " + type + " not yet supported by MovingAverage");
    }

    return processCode(codeStream.toString());
  }