Ejemplo n.º 1
0
  // we assume the composite agent has no mode
  // only atomic agent has mode
  private String _compositeAgentCode(CompositeActor actor) throws IllegalActionException {
    if (FSMDirector.class.isInstance(actor.getDirector())) {
      //            System.out.println("in FSM");
      return _agentCode(actor);
    }

    LinkedList subAgents = _agents(actor);

    if (subAgents.size() == 0) {
      return _agentCode(actor);
    }

    String compositeCodeString = "";
    String subAgentCode = "";
    String privateVariables = "";

    ListIterator subAgentsIterator = subAgents.listIterator();

    // the output ports of composite agent
    // In fact, there is always at most one output
    List outputPorts = actor.outputPortList();
    ListIterator outputPortsIterator = actor.outputPortList().listIterator();

    if (outputPorts.size() > 1) {
      throw new IllegalActionException(" The agent has more than one output!");
    }

    // get the source subAgent name
    String outputAgentName = "";
    String outputPortName = "";
    String sourceForOutputName = "";

    while (outputPortsIterator.hasNext()) {
      TypedIOPort output = (TypedIOPort) outputPortsIterator.next();
      outputPortName = output.getName();

      ListIterator sourcePorts = output.insidePortList().listIterator();

      TypedIOPort sourcePort = new TypedIOPort();

      while (sourcePorts.hasNext()) {
        TypedIOPort port = (TypedIOPort) sourcePorts.next();

        if (port.isOutput()) {
          if (sourcePort == null) {
            throw new IllegalActionException(" The output has more than one source!");
          } else {
            sourcePort = port;
            sourceForOutputName = sourcePort.getName();

            Nameable sourceContainer = sourcePort.getContainer();
            outputAgentName = sourceContainer.getName();
          }
        }
      }
    }

    while (subAgentsIterator.hasNext()) {
      String subAgentConnectionInputs = "";
      String subAgentConnectionOutputs = "";

      CompositeActor subAgent = (CompositeActor) subAgentsIterator.next();

      if (outputAgentName.equals(subAgent.getName())) {
        // the inside output actually is input to outside environment
        subAgentConnectionOutputs += sourceForOutputName;
        subAgentConnectionInputs += outputPortName;
      }

      subAgentCode +=
          ("  agent " + subAgent.getName().toLowerCase() + " = " + subAgent.getName() + " ( ");

      if (actor.depthInHierarchy() == 0) {
        subAgentCode += _agentParameterTokens((NamedObj) subAgent);
      } else {
        subAgentCode += _agentParameters((NamedObj) subAgent, false);
      }

      subAgentCode += (" );" + _endLine);

      ListIterator subAgentInputs = subAgent.inputPortList().listIterator();

      while (subAgentInputs.hasNext()) {
        TypedIOPort input = (TypedIOPort) subAgentInputs.next();
        LinkedList sourceList = _shallowSourcePortList(input);
        ListIterator sources = sourceList.listIterator();
        boolean privateVariable = true;

        while (sources.hasNext()) {
          TypedIOPort source = (TypedIOPort) sources.next();

          if (source.depthInHierarchy() != input.depthInHierarchy()) {
            privateVariable = false;
          }

          if (!(source.getName().equals(input.getName()))) {
            if (subAgentConnectionOutputs == "") {
              subAgentConnectionOutputs += source.getName();
              subAgentConnectionInputs += input.getName();
            } else {
              subAgentConnectionOutputs += (", " + source.getName());
              subAgentConnectionInputs += (", " + input.getName());
            }
          }
        }

        if (privateVariable) {
          if (privateVariables == "") {
            privateVariables += ("private analog real " + input.getName());
          } else {
            privateVariables += (", " + input.getName());
          }
        }
      }

      if (subAgentConnectionInputs.length() != 0) {
        subAgentCode +=
            ("       [ "
                + subAgentConnectionInputs
                + " := "
                + subAgentConnectionOutputs
                + " ] ;"
                + _endLine);
      }

      compositeCodeString += _compositeAgentCode(subAgent);
    }

    compositeCodeString += "agent";

    String parameterString = "";
    String inputString = "";
    String outputString = "";
    String initString = "";
    String modeString = "";
    String modeParameterString = "";

    LinkedList parameterList = (LinkedList) actor.attributeList(Parameter.class);
    int parameterNumber = parameterList.size();
    ListIterator parameters = parameterList.listIterator();

    _inPorts = actor.inputPortList().iterator();

    while (_inPorts.hasNext()) {
      if (inputString == "") {
        inputString += ("read analog real " + ((NamedObj) _inPorts.next()).getName());
      } else {
        inputString += (", " + ((NamedObj) _inPorts.next()).getName());
      }
    }

    if (inputString != "") {
      inputString += ";";
    }

    _outPorts = actor.outputPortList().iterator();

    if (_outPorts.hasNext()) {
      String outportName = ((NamedObj) _outPorts.next()).getName();

      if (outputString == "") {
        outputString += ("write analog real " + outportName);
      } else {
        outputString += (", " + outportName);
      }
    }

    if (outputString != "") {
      outputString += ";";
    }

    if (privateVariables.length() != 0) {
      privateVariables += ";";
    }

    compositeCodeString += (" " + actor.getName() + " ( ");

    if (actor.depthInHierarchy() != 0) {
      compositeCodeString += _agentParameters((NamedObj) actor, true);
    }

    compositeCodeString +=
        (" )"
            + _endLine
            + "{"
            + _endLine
            + "  "
            + outputString
            + _endLine
            + "  "
            + inputString
            + _endLine
            + "  "
            + privateVariables
            + _endLine
            + subAgentCode
            + _endLine
            + "}"
            + _endLine);

    return compositeCodeString;
  }