/** * Returns <code>null</code> as a Tree EditPart holds no children under it. * * @return <code>null</code> */ protected List getModelChildren() { NamedObj namedObjectModel = getNamedObjectModel(); List children = new ArrayList(); if (namedObjectModel instanceof AtomicActor) { AtomicActor actor = (AtomicActor) namedObjectModel; children.addAll(actor.attributeList(Parameter.class)); children.addAll(actor.inputPortList()); children.addAll(actor.outputPortList()); } else if (namedObjectModel instanceof CompositeActor) { CompositeActor composite = (CompositeActor) namedObjectModel; children.addAll(composite.attributeList(AtomicActor.class)); children.addAll(composite.attributeList(Parameter.class)); children.addAll(composite.inputPortList()); Enumeration enumeration = composite.getEntities(); while (enumeration.hasMoreElements()) { children.add(enumeration.nextElement()); } } else if (namedObjectModel instanceof IOPort) { IOPort text = (IOPort) namedObjectModel; children.addAll(text.attributeList(ptolemy.kernel.util.StringAttribute.class)); } else if (namedObjectModel instanceof Vertex) { Vertex text = (Vertex) namedObjectModel; children.addAll(text.attributeList(Vertex.class)); } else if (namedObjectModel instanceof TextAttribute) { TextAttribute text = (TextAttribute) namedObjectModel; children.addAll(text.attributeList(ptolemy.kernel.util.StringAttribute.class)); } else if (namedObjectModel instanceof Director) { Director director = (Director) namedObjectModel; children.addAll(director.attributeList(Parameter.class)); } return children; }
/** * Generate code for the agent. * * @param CompositeActor The agent generated code from. * @return The agent code. */ private String _agentCode(CompositeActor actor) throws IllegalActionException { // System.out.println("dealing with " + actor.getFullName()); String codeString = "agent"; String parameterString = ""; String inputString = ""; String outputString = ""; String initString = ""; String modeString = ""; String modeParameterString = ""; String typedModeParameterString = ""; String flowString = ""; String invariantString = ""; LinkedList parameterList = (LinkedList) actor.attributeList(Parameter.class); /* Parameter invariantPara = (Parameter) actor.getAttribute("_invariant"); if (invariantPara != null) { invariantString = "inv { " + ((StringToken)invariantPara.getToken()).stringValue() + " } "; //get rid of _invariant parameter parameterList.remove(invariantPara);; //FIXME: it seems that after getAttribute, //the attribute does not exist? } */ ListIterator assertions = _assertions(actor).listIterator(); while (assertions.hasNext()) { Assertion assertion = (Assertion) assertions.next(); if (invariantString.length() == 0) { invariantString = "inv { " + assertion.assertion.getExpression(); } else { invariantString += (" ; " + assertion.assertion.getExpression()); } invariantString += " } "; } 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(); int outportNumber = actor.outputPortList().size(); // since the parameters are either for mode or for outport, // we assume the number of the parameters for outport is the same // with the number of outport(s) boolean parameterForOutport = false; while (parameters.hasNext()) { String parameterName = ((NamedObj) parameters.next()).getName(); if (parameterName.startsWith("_")) { continue; } if (parameters.nextIndex() > (parameterNumber - outportNumber)) { parameterForOutport = true; } if (parameterString == "") { parameterString += ("real " + parameterName); } else { parameterString += (", real " + parameterName); } if (parameterForOutport) { if (_outPorts.hasNext()) { String outportName = ((NamedObj) _outPorts.next()).getName(); if (outputString == "") { outputString += ("write analog real " + outportName); } else { outputString += (", " + outportName); } initString += (outportName + " = " + parameterName + " ;"); } } else { if (modeParameterString == "") { modeParameterString += parameterName; typedModeParameterString += ("real " + parameterName); } else { modeParameterString += (", " + parameterName); typedModeParameterString += (", real " + parameterName); } } } if (outputString != "") { outputString += ";"; } initString = "init { " + initString + " }"; modeString = "mode top = " + actor.getName() + "TopMode" + " ( " + modeParameterString + " ) ;"; codeString += (" " + actor.getName() + " ( " + parameterString + " )" + _endLine + "{" + _endLine + " " + outputString + _endLine + " " + inputString + _endLine + " " + initString + _endLine + " " + modeString + _endLine + "}" + _endLine); if (FSMDirector.class.isInstance(actor.getDirector())) { // mode code generation goes here. _modeCode += ("mode " + actor.getName() + "TopMode" + " ( " + typedModeParameterString + " )" + _endLine + "{" + _endLine + " " + outputString + _endLine + " " + inputString + _endLine); // notice the _fsmModeCode(actor) will modify the _modeCode with transitions code // and return the mode code for each sub mode.(refinement) String subModeString = _fsmModeCode( ((FSMDirector) actor.getDirector()).getController(), inputString, outputString); _modeCode += (" " + invariantString + _endLine + "}" + _endLine + subModeString); } else { flowString = _graphToText(actor); _modeCode += ("mode " + actor.getName() + "TopMode" + " ( " + typedModeParameterString + " )" + _endLine + "{" + _endLine + " " + outputString + _endLine + " " + inputString + _endLine + " " + flowString + _endLine + " " + invariantString + _endLine + "}" + _endLine); } return codeString; }
// 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; }