001/*
002 * Copyright (c) 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
030package org.ROADnet;
031
032import ptolemy.actor.lib.Transformer;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039/**
040 * Ptolemy actor to use a RecordToken to fill in the fields of a newly
041 * instantiated Object and then output this object encapsulated within an
042 * ObjectToken. This operation is, of course, rather hairy, but it seemed like a
043 * logical thing to do after implementing ObjectToRecord. <b>NOT IMPLEMENTED
044 * YET.</b>
045 * 
046 * @author Tobin Fricke, University of California
047 * @version $Id: RecordToObject.java 24234 2010-05-06 05:21:26Z welker $
048 * @Pt.ProposedRating Red (tobin)
049 */
050
051public class RecordToObject extends Transformer {
052
053        /**
054         * @param container
055         *            The container.
056         * @param name
057         *            The name of this actor.
058         * @exception IllegalActionException
059         *                If the actor cannot be contained by the proposed
060         *                container.
061         * @exception NameDuplicationException
062         *                If the container already has an actor with this name.
063         */
064
065        public RecordToObject(CompositeEntity container, String name)
066                        throws NameDuplicationException, IllegalActionException {
067                super(container, name);
068
069                input.setMultiport(false);
070                input.setTypeEquals(BaseType.OBJECT);
071                output.setTypeEquals(BaseType.OBJECT);
072
073                classname = new Parameter(this, "orbname");
074                classname.setTypeEquals(BaseType.STRING);
075        }
076
077        public boolean prefire() throws IllegalActionException {
078                return (input.hasToken(0) && super.prefire());
079        }
080
081        /**
082         * Accept an ObjectToken from the input and produce a corresponding
083         * RecordToken on the output.
084         */
085
086        public void fire() throws IllegalActionException {
087                super.fire();
088                /*
089                 * Class c = Class.forName(---, false, null); Object o =
090                 * c.newInstance();
091                 * 
092                 * // Now we will make lists of the fields in the class and the //
093                 * fields in the record, so that we can verify their bijection.
094                 * 
095                 * Field[] classFields = c.getFields(); int nClassFields =
096                 * Array.getLength(fields);
097                 * 
098                 * if (nClassFields != nRecordFields) { _debug("Class has " +
099                 * nClassFields + " fields while record has " + nRecordFields +
100                 * "fields."); }
101                 * 
102                 * String[] labels = new String[nFields]; ObjectToken[] values = new
103                 * ObjectToken[nFields];
104                 * 
105                 * for (int i = 0; i < nFields; i++) { Field f =
106                 * (Field)(Array.get(fields, i)); labels[i] = f.getName();
107                 * 
108                 * // use class.isPrimitive if necessary }
109                 * 
110                 * Token r = new RecordToken(labels, values); output.broadcast(r);
111                 */
112        }
113
114        public Parameter classname;
115}