コード例 #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;
  }