예제 #1
0
  // /////////////////////////////////////////////////////////////////
  // // private methods ////
  // Recursively compute the set of free variables for all actors
  // deeply contained in the given model.
  private Set _freeVariables(Entity model) throws IllegalActionException {
    // First get the free variables of contained actors.
    Set set = new HashSet();

    if (model instanceof CompositeEntity) {
      for (Iterator entities = ((CompositeEntity) model).entityList().iterator();
          entities.hasNext(); ) {
        Entity entity = (Entity) entities.next();
        set.addAll(_freeVariables(entity));
      }
    }

    // Next, compute the set of variable names defined in this container.
    Set variableNames = new HashSet();

    for (Iterator variables = model.attributeList(Variable.class).iterator();
        variables.hasNext(); ) {
      Variable variable = (Variable) variables.next();
      variableNames.add(variable.getName());
    }

    variableNames = Collections.unmodifiableSet(variableNames);

    // Free variables of contained actors that are defined in this
    // container are not free variables of this container.
    set.removeAll(variableNames);

    // Iterate over all the variables of this container, and add in
    // any free variables they reference.
    PtParser parser = new PtParser();
    ParseTreeFreeVariableCollector collector = new ParseTreeFreeVariableCollector();

    for (Iterator variables = model.attributeList(Variable.class).iterator();
        variables.hasNext(); ) {
      Variable variable = (Variable) variables.next();
      String expression = variable.getExpression();
      ASTPtRootNode root;

      if (variable.isStringMode()) {
        root = parser.generateStringParseTree(expression);
      } else {
        root = parser.generateParseTree(expression);
      }

      Set freeIdentifiers = new HashSet(collector.collectFreeVariables(root));

      // Identifiers that reference other variables in the same container
      // are bound, not free.
      Set tempSet = new HashSet(variableNames);
      tempSet.remove(variable.getName());
      freeIdentifiers.removeAll(tempSet);

      set.addAll(freeIdentifiers);
    }

    _entityToFreeVariableNameSet.put(model, set);
    return set;
  }
예제 #2
0
    /**
     * Set the expression associated with the iteration count. The expression will probably be
     * something like "a2::in::tokenConsumptionRate/gcd(a2::in::tokenConsumptionRate,
     * a::out::tokenProductionRate)."
     *
     * @param expression The expression to be associated with the iteration count.
     */
    public void setIterationCount(String expression) {
      _expression = expression;

      // FIXME: Need better exception handling.
      try {
        PtParser parser = new PtParser();
        _parseTree = parser.generateParseTree(expression);
      } catch (Exception exception) {
        throw new RuntimeException(
            "Error setting iteration count to " + expression + ".\n" + exception.getMessage());
      }
    }
예제 #3
0
  /**
   * Create a new FunctionToken from the given string.
   *
   * @param init The initialization string, for example <code>function(x,y) 4+x+y</code>.
   * @exception IllegalActionException If an error occurs, or the string cannot be parsed into a
   *     function.
   */
  public FunctionToken(String init) throws IllegalActionException {
    PtParser parser = new PtParser();
    ASTPtRootNode tree = parser.generateParseTree(init);
    ParseTreeTypeInference inference = new ParseTreeTypeInference();
    inference.inferTypes(tree);

    Token token = (new ParseTreeEvaluator()).evaluateParseTree(tree);

    if (token instanceof FunctionToken) {
      _function = ((FunctionToken) token)._function;
      _type = ((FunctionToken) token)._type;
    } else {
      throw new IllegalActionException(
          "A function token cannot be" + " created from the expression '" + init + "'");
    }
  }