/**
   * Topology analysis to get a list of agents. The agents should be TypedCompositeActor with
   * parameter charonMode as null (by default) or true.
   *
   * @param actor Container to be analyzed to return agent list.
   * @return List of agents defined in model.
   */
  private LinkedList _agents(CompositeActor actor) throws IllegalActionException {
    LinkedList agentList = new LinkedList();
    ListIterator agentsIterator = actor.entityList(TypedCompositeActor.class).listIterator();

    while (agentsIterator.hasNext()) {
      TypedCompositeActor agent = (TypedCompositeActor) agentsIterator.next();
      Parameter charonAgent = (Parameter) agent.getAttribute("charonAgent");

      if ((charonAgent == null) || ((BooleanToken) charonAgent.getToken()).booleanValue()) {
        agentList.add(agent);
      }
    }

    return agentList;
  }
  /**
   * Transform the graphic block diagram to text expression. Assume container only contains atomic
   * actors.
   *
   * @param container contains actors.
   * @return txtExpression of graphic block diagram.
   */
  private String _graphToText(CompositeActor container) throws IllegalActionException {
    // It is not trivial to transform graph to text.
    // Here, we assume there is only one Integrator and one expression actor.
    String txtString = "";
    LinkedList actors = new LinkedList(container.entityList());
    ListIterator actorIterator = actors.listIterator();

    // we begin with Integrator.
    AtomicActor beginActor = new AtomicActor();

    while (actorIterator.hasNext()) {
      Actor actor = (Actor) actorIterator.next();

      if (Integrator.class.isInstance(actor)) {
        beginActor = (AtomicActor) actor;
        break;
      }
    }

    if (beginActor == null) {
      throw new IllegalActionException("Integrator is needed!");
    } else {
      // we trace the output of the Integrator
      // we assume the output of the integrator is connectted
      // to the container output directly and they have same names
      // for simplicity at this time.
      // FIXME: we really need to reconsider the methods of ports.
      List outputs = beginActor.outputPortList();
      ListIterator outputIterator = outputs.listIterator();
      String outputName = "";

      if (outputs.size() != 1) {
        throw new IllegalActionException("Integrator only have one output!  " + outputs.size());
      } else {
        TypedIOPort output = (TypedIOPort) outputIterator.next();
        ListIterator sinkIterator = output.connectedPortList().listIterator();

        while (sinkIterator.hasNext()) {
          TypedIOPort sink = (TypedIOPort) sinkIterator.next();

          if (sink.isOutput()) {
            // FIXME: we need to consider depth in hierarchy
            // to avoid two outputs connected to same output
            // of composite actor
            outputName = sink.getName();
          }
        }

        txtString += ("diff { d(" + outputName + ") == ");
      }

      // we trace the input of the integrator
      List inputs = beginActor.inputPortList();
      ListIterator inputIterator = inputs.listIterator();

      if (inputs.size() != 1) {
        throw new IllegalActionException("Integrator only have one input!");
      } else {
        TypedIOPort input = (TypedIOPort) inputIterator.next();
        List sources = input.connectedPortList();

        if (sources.size() != 1) {
          throw new IllegalActionException("There is only one connection to the input!");
        } else {
          TypedIOPort source = (TypedIOPort) sources.get(0);

          // if there is just an integrator
          if (source.isInput()) {
            txtString += (source.getName() + " ; }" + _endLine);
          }
          // if there is some expression actor
          else {
            AtomicActor expressionActor = (AtomicActor) source.getContainer();

            if (Expression.class.isInstance(expressionActor)) {
              Parameter expPara = (Parameter) expressionActor.getAttribute("expression");
              txtString += (expPara.getExpression() + " ; } " + _endLine);
            } else {
              throw new IllegalActionException("This should be Expression Atomic Actor!");
            }
          }
        }
      }
    }

    return txtString;
  }
 // Topology analysis to get the list of assertions.
 private LinkedList _assertions(CompositeActor actor) throws IllegalActionException {
   LinkedList assertionList = new LinkedList();
   assertionList = (LinkedList) actor.entityList(Assertion.class);
   return assertionList;
 }