Beispiel #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());
  }
Beispiel #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());
  }