// /////////////////////////////////////////////////////////////////
  // // 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 + "'");
    }
  }
  /**
   * Evaluate the specified command.
   *
   * @param command The command.
   * @return The return value of the command, or null if there is none.
   * @exception Exception If something goes wrong processing the command.
   */
  public String evaluateCommand(String command) throws Exception {
    if (command.trim().equals("")) {
      return "";
    }

    PtParser parser = new PtParser();
    ASTPtRootNode node = parser.generateSimpleAssignmentParseTree(command);
    String targetName = null;

    // Figure out if we got an assignment... if so, then get the
    // identifier name and only evaluated the expression part.
    if (node instanceof ASTPtAssignmentNode) {
      ASTPtAssignmentNode assignmentNode = (ASTPtAssignmentNode) node;
      targetName = assignmentNode.getIdentifier();
      node = assignmentNode.getExpressionTree();
    }

    final NamedObj model = ((ExpressionShellEffigy) getContainer()).getModel();
    ParserScope scope =
        new ModelScope() {
          public ptolemy.data.Token get(String name) throws IllegalActionException {
            Variable result = getScopedVariable(null, model, name);

            if (result != null) {
              return result.getToken();
            } else {
              return null;
            }
          }

          public ptolemy.data.type.Type getType(String name) throws IllegalActionException {
            Variable result = getScopedVariable(null, model, name);

            if (result != null) {
              return result.getType();
            } else {
              return null;
            }
          }

          public InequalityTerm getTypeTerm(String name) throws IllegalActionException {
            Variable result = getScopedVariable(null, model, name);

            if (result != null) {
              return result.getTypeTerm();
            } else {
              return null;
            }
          }

          public Set identifierSet() {
            return getAllScopedVariableNames(null, model);
          }
        };

    Token result = _evaluator.evaluateParseTree(node, scope);

    // If a target was specified, instantiate a new token.
    if (targetName != null) {
      Attribute attribute = model.getAttribute(targetName);

      if (attribute != null && !(attribute instanceof Parameter)) {
        attribute.setContainer(null);
        attribute = null;
      }

      if (attribute == null) {
        attribute = new Parameter(model, targetName);
      }

      ((Parameter) attribute).setToken(result);
    }

    if (result == null) {
      return "";
    } else {
      return result.toString();
    }
  }