001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030/*
031 * @author xiaowen
032 */
033
034// To compile and test:
035// 1. Set your classpath to include all Ptolemy and SPA jars
036// 2. javac ActorInspector.java
037// 3. java org.sdm.spa.util.ActorInspector
038package org.sdm.spa.util;
039
040import java.lang.reflect.Constructor;
041import java.lang.reflect.InvocationTargetException;
042import java.util.Iterator;
043import java.util.List;
044import java.util.Vector;
045
046import ptolemy.actor.Manager;
047import ptolemy.actor.TypeConflictException;
048import ptolemy.actor.TypedAtomicActor;
049import ptolemy.actor.TypedCompositeActor;
050import ptolemy.actor.TypedIOPort;
051import ptolemy.actor.lib.Recorder;
052import ptolemy.data.expr.Parameter;
053import ptolemy.data.type.Type;
054import ptolemy.domains.pn.kernel.PNDirector;
055import ptolemy.kernel.CompositeEntity;
056import ptolemy.kernel.util.Attribute;
057import ptolemy.kernel.util.ConfigurableAttribute;
058import ptolemy.kernel.util.KernelException;
059
060public class ActorInspector {
061
062        TypedAtomicActor actor;
063
064        public static void main(String[] args) {
065                ActorInspector m;
066                try {
067                        m = new ActorInspector("org.sdm.spa.StringConst");
068                } catch (Exception e) {
069                        e.printStackTrace();
070                        return;
071                }
072
073                List inports = m.getInputPorts();
074                List outports = m.getOutputPorts();
075                List attrs = m.getAttributeList();
076
077                System.out.println("Input ports: " + inports);
078                System.out.println("Output ports: " + outports);
079                System.out.println("Attributes: " + attrs);
080        }
081
082        /**
083         * @param fullname
084         *            Name of the class to instantiate.
085         * @exception ClassCastException
086         *                If the class is not a TypedAtomicActor.
087         * @exception ClassNotFoundException
088         *                If the class cannot be found.
089         * @exception InstantiationException
090         *                If the class cannot be instantiated.
091         * @exception IllegalAccessException
092         *                If the class is not accessible.
093         * @exception InvocationTargetException
094         *                If the underlying constructor throws an exception.
095         * @exception KernelException
096         *                If the manager initializing a workflow containing this
097         *                actor throws it.
098         * @exception NoSuchMethodException
099         *                If expected constructor not found.
100         */
101        public ActorInspector(String fullname) throws ClassCastException,
102                        ClassNotFoundException, InstantiationException,
103                        IllegalAccessException, InvocationTargetException, KernelException,
104                        NoSuchMethodException {
105
106                TypedCompositeActor container;
107                Recorder rec;
108                PNDirector dir;
109                Manager manager;
110
111                try {
112                        container = new TypedCompositeActor();
113                        rec = new Recorder(container, "rec");
114                        dir = new PNDirector(container, "dir");
115                        manager = new Manager("manager");
116
117                        container.setManager(manager);
118                } catch (ptolemy.kernel.util.NameDuplicationException e) {
119                        e.printStackTrace();
120                        throw new RuntimeException("Internal Error!");
121                } catch (ptolemy.kernel.util.IllegalActionException e) {
122                        e.printStackTrace();
123                        throw new RuntimeException("Internal Error!");
124                }
125
126                Class actorclass = Class.forName(fullname);
127
128                // Check this is a TypedAtomicActor.
129                if (!TypedAtomicActor.class.isAssignableFrom(actorclass)) {
130                        throw new ClassCastException(fullname
131                                        + " is not a subclass of TypedAtomicActor.");
132                }
133
134                Constructor constructor = actorclass.getConstructor(new Class[] {
135                                CompositeEntity.class, String.class });
136                actor = (TypedAtomicActor) constructor.newInstance(new Object[] {
137                                container, "actor" });
138
139                try {
140                        manager.initialize();
141                } catch (TypeConflictException e) {
142                        System.err.println("TypeConflictException while processing "
143                                        + fullname + ": " + e);
144                        // The port is probably configured based on what it's linked to.
145                }
146        }
147
148        // ***** Attribute Class and Methods *****
149
150        public List getAttributeList() {
151                Vector vecAttributes = new java.util.Vector();
152
153                Iterator it = actor.attributeList().iterator();
154
155                while (it.hasNext()) {
156                        Attribute at = (Attribute) it.next();
157
158                        String name = at.getName();
159
160                        String value;
161                        if (at instanceof Parameter) {
162                                value = at.toString();
163                        } else if (at instanceof ConfigurableAttribute) {
164                                value = ((ConfigurableAttribute) at).getExpression();
165                        } else {
166                                value = "";
167                        }
168
169                        vecAttributes.add(new AttributeInfo(name, value));
170                }
171
172                return vecAttributes;
173        }
174
175        public class AttributeInfo {
176                public String name;
177                public String value;
178
179                public AttributeInfo(String name, String value) {
180                        this.name = name;
181                        this.value = value;
182                }
183
184                public String toString() {
185                        return "name: " + name + " value: " + value;
186                }
187        }
188
189        // ***** Port Class and Methods *****
190
191        public List getInputPorts() {
192                return getPorts(actor.inputPortList());
193        }
194
195        public List getOutputPorts() {
196                return getPorts(actor.outputPortList());
197        }
198
199        private List getPorts(List list) {
200                Vector vecPorts = new java.util.Vector();
201
202                Iterator it = list.iterator();
203
204                while (it.hasNext()) {
205                        TypedIOPort port = (TypedIOPort) it.next();
206
207                        // get properties
208                        String name = port.getName();
209                        Type type = port.getType();
210                        boolean isMultiport = port.isMultiport();
211
212                        // append to vector
213                        PortInfo pi = new PortInfo(name, type, isMultiport);
214                        vecPorts.addElement(pi);
215                }
216
217                return vecPorts;
218        }
219
220        public class PortInfo {
221                public String name;
222                public Type type;
223                public boolean isMultiport;
224
225                public PortInfo(String name, Type type, boolean isMultiport) {
226                        this.name = name;
227                        this.type = type;
228                        this.isMultiport = isMultiport;
229                }
230
231                public String toString() {
232                        return "name: " + name + " type: " + type + " isMultiport: "
233                                        + isMultiport;
234                }
235        }
236}