001/*
002 * Copyright (c) 1998-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.surge;
031
032import java.util.Iterator;
033import java.util.Set;
034
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.TypedIOPort;
037import ptolemy.data.RecordToken;
038import ptolemy.data.StringToken;
039import ptolemy.data.expr.StringParameter;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.Attribute;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045import util.StringUtil;
046
047//////////////////////////////////////////////////////////////////////////
048//// RecordToStrings class name
049/**
050 * This actor converts a record into a string of labels and a string of values.
051 * Similar to RecordDisassembler except that the label names are output as a
052 * string instead of a parameter.
053 * 
054 * @author Daniel Crawl
055 * @version $Id: RecordToStrings.java 24234 2010-05-06 05:21:26Z welker $
056 */
057
058public class RecordToStrings extends TypedAtomicActor {
059
060        /**
061         * Construct a RecordToStrings source with the given container and name.
062         * 
063         * @param name
064         *            The name of this actor.
065         * @exception IllegalActionException
066         *                If the entity cannot be contained by the proposed
067         *                container.
068         * @exception NameDuplicationException
069         *                If the container already has an actor with this name.
070         */
071        public RecordToStrings(CompositeEntity container, String name)
072                        throws NameDuplicationException, IllegalActionException {
073                super(container, name);
074
075                input = new TypedIOPort(this, "input", true, false);
076
077                labels = new TypedIOPort(this, "labels", false, true);
078                labels.setTypeEquals(BaseType.STRING);
079
080                values = new TypedIOPort(this, "values", false, true);
081                values.setTypeEquals(BaseType.STRING);
082
083                delim = new StringParameter(this, "Field separator");
084
085                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
086                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
087                                + "</svg>\n");
088        }
089
090        // /////////////////////////////////////////////////////////////////
091        // // ports and parameters ////
092
093        public TypedIOPort input = null;
094
095        public TypedIOPort labels = null;
096        public TypedIOPort values = null;
097
098        public StringParameter delim = null;
099
100        // /////////////////////////////////////////////////////////////////
101        // // public methods ////
102
103        /**
104         * Send the token in the value parameter to the output.
105         * 
106         * @exception IllegalActionException
107         *                If it is thrown by the send() method sending out the
108         *                token.
109         */
110        public void fire() throws IllegalActionException {
111                super.fire();
112
113                String delimStr = delim.stringValue();
114
115                for (int i = 0; i < input.getWidth(); i++) {
116                        RecordToken rec = (RecordToken) input.get(i);
117                        Set ls = rec.labelSet();
118                        Iterator iter = ls.iterator();
119
120                        String[] labelsStr = new String[ls.size()];
121                        String[] valsStr = new String[ls.size()];
122                        int j = 0;
123
124                        while (iter.hasNext()) {
125                                labelsStr[j] = (String) iter.next();
126                                valsStr[j] = ((StringToken) rec.get(labelsStr[j]))
127                                                .stringValue();
128                                j++;
129                        }
130
131                        labels.broadcast(new StringToken(StringUtil.join(labelsStr,
132                                        delimStr)));
133                        values
134                                        .broadcast(new StringToken(StringUtil.join(valsStr,
135                                                        delimStr)));
136                }
137        }
138
139        public void attributeChanged(Attribute attribute)
140                        throws IllegalActionException {
141                System.out.println("attr changed: " + attribute);
142
143                super.attributeChanged(attribute);
144        }
145}