001/* Actor that reads the causality interface of its container and produces a
002 * string describing it.
003
004 Copyright (c) 2003-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.actor.lib;
030
031import java.util.Collection;
032import java.util.Collections;
033import java.util.LinkedList;
034import java.util.List;
035
036import ptolemy.actor.Actor;
037import ptolemy.actor.IOPort;
038import ptolemy.actor.TypedIOPort;
039import ptolemy.actor.util.CausalityInterface;
040import ptolemy.data.StringToken;
041import ptolemy.data.expr.StringParameter;
042import ptolemy.data.type.BaseType;
043import ptolemy.kernel.CompositeEntity;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046
047///////////////////////////////////////////////////////////////////
048//// GetCausalityInterface
049
050/**
051 Actor that reads the causality interface of its container or an
052 actor inside the container and produces a
053 string describing it. This actor is meant mainly for testing.
054
055 @author Edward A. Lee
056 @version $Id$
057 @since Ptolemy II 8.0
058 @Pt.ProposedRating Yellow (eal)
059 @Pt.AcceptedRating Red (eal)
060 */
061public class GetCausalityInterface extends Source {
062
063    /** Construct an actor with the given container and name.
064     *  The output and trigger ports are also constructed.
065     *  @param container The container.
066     *  @param name The name of this actor.
067     *  @exception IllegalActionException If the entity cannot be contained
068     *   by the proposed container.
069     *  @exception NameDuplicationException If the container already has an
070     *   actor with this name.
071     */
072    public GetCausalityInterface(CompositeEntity container, String name)
073            throws IllegalActionException, NameDuplicationException {
074        super(container, name);
075        output.setTypeEquals(BaseType.STRING);
076
077        dependents = new TypedIOPort(this, "dependents", false, true);
078        dependents.setTypeEquals(BaseType.STRING);
079
080        equivalences = new TypedIOPort(this, "equivalences", false, true);
081        equivalences.setTypeEquals(BaseType.STRING);
082
083        actorName = new StringParameter(this, "actorName");
084        actorName.setExpression("");
085    }
086
087    ///////////////////////////////////////////////////////////////////
088    ////                  ports and parameters                     ////
089
090    /** Name of the actor to get the causality interface of. If this
091     *  is the empty string (the default), then the container is used.
092     */
093    public StringParameter actorName;
094
095    /** Output port on which to put the description of the dependent ports. */
096    public TypedIOPort dependents;
097
098    /** Output port on which to put the description of the equivalence classes. */
099    public TypedIOPort equivalences;
100
101    ///////////////////////////////////////////////////////////////////
102    ////                         public methods                    ////
103
104    /** Read the causality interface from the container and produce a
105     *  description of it as an output.
106     *  @exception IllegalActionException Not thrown in this base class.
107     */
108    @Override
109    public void fire() throws IllegalActionException {
110        super.fire();
111        Actor container = (Actor) getContainer();
112        Actor target = container;
113        String targetName = actorName.stringValue();
114        if (targetName != null && !targetName.trim().equals("")) {
115            target = (Actor) ((CompositeEntity) container)
116                    .getEntity(targetName);
117            if (target == null) {
118                throw new IllegalActionException(this,
119                        "No actor named " + targetName);
120            }
121        }
122        CausalityInterface causalityInterface = target.getCausalityInterface();
123        output.send(0, new StringToken(causalityInterface.toString()));
124
125        StringBuffer dependentsResult = new StringBuffer();
126        List<IOPort> inputs = target.inputPortList();
127        for (IOPort input : inputs) {
128            Collection<IOPort> outputs = causalityInterface
129                    .dependentPorts(input);
130            if (outputs.size() > 0) {
131                dependentsResult.append(input.getName());
132                dependentsResult.append(" has output port dependencies:\n");
133                List<String> outputList = new LinkedList<String>();
134                for (IOPort output : outputs) {
135                    outputList.add(output.getName());
136                }
137                // To get deterministic results for testing, sort alphabetically.
138                Collections.sort(outputList);
139                for (String name : outputList) {
140                    dependentsResult.append("  ");
141                    dependentsResult.append(name);
142                    dependentsResult.append("\n");
143                }
144            }
145        }
146        List<IOPort> outputs = target.outputPortList();
147        for (IOPort output : outputs) {
148            Collection<IOPort> ports = causalityInterface
149                    .dependentPorts(output);
150            if (ports.size() > 0) {
151                dependentsResult.append(output.getName());
152                dependentsResult.append(" has input port dependencies:\n");
153                List<String> inputList = new LinkedList<String>();
154                for (IOPort input : ports) {
155                    inputList.add(input.getName());
156                }
157                // To get deterministic results for testing, sort alphabetically.
158                Collections.sort(inputList);
159                for (String name : inputList) {
160                    dependentsResult.append("  ");
161                    dependentsResult.append(name);
162                    dependentsResult.append("\n");
163                }
164            }
165        }
166        dependents.send(0, new StringToken(dependentsResult.toString()));
167
168        StringBuffer equivalencesResult = new StringBuffer();
169        for (IOPort input : inputs) {
170            Collection<IOPort> equivalents = causalityInterface
171                    .equivalentPorts(input);
172            if (equivalents.size() > 0) {
173                equivalencesResult.append(input.getName());
174                equivalencesResult.append(" has equivalent input ports:\n");
175                List<String> equivalentsList = new LinkedList<String>();
176                for (IOPort port : equivalents) {
177                    equivalentsList.add(port.getName());
178                }
179                // To get deterministic results for testing, sort alphabetically.
180                Collections.sort(equivalentsList);
181                for (String name : equivalentsList) {
182                    equivalencesResult.append("  ");
183                    equivalencesResult.append(name);
184                    equivalencesResult.append("\n");
185                }
186            }
187        }
188        equivalences.send(0, new StringToken(equivalencesResult.toString()));
189    }
190}