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.HashMap; 033 034import ptolemy.actor.TypedAtomicActor; 035import ptolemy.actor.TypedIOPort; 036import ptolemy.data.RecordToken; 037import ptolemy.data.StringToken; 038import ptolemy.data.expr.StringParameter; 039import ptolemy.data.type.BaseType; 040import ptolemy.kernel.CompositeEntity; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.NameDuplicationException; 043 044////////////////////////////////////////////////////////////////////////// 045//// StrigsToRecord class name 046/** 047 * This actor converts a set of labels and values to a record. Similar to 048 * RecordAssembler except that the label names are read from an input port 049 * instead of a parameter. 050 * 051 * @author Daniel Crawl 052 * @version $Id: StringsToRecord.java 24234 2010-05-06 05:21:26Z welker $ 053 */ 054 055public class StringsToRecord extends TypedAtomicActor { 056 057 /** 058 * Construct a StringsToRecord source with the given container and name. 059 * 060 * @param name 061 * The name of this actor. 062 * @exception IllegalActionException 063 * If the entity cannot be contained by the proposed 064 * container. 065 * @exception NameDuplicationException 066 * If the container already has an actor with this name. 067 */ 068 public StringsToRecord(CompositeEntity container, String name) 069 throws NameDuplicationException, IllegalActionException { 070 super(container, name); 071 072 labels = new TypedIOPort(this, "labels", true, false); 073 labels.setTypeEquals(BaseType.STRING); 074 075 values = new TypedIOPort(this, "values", true, false); 076 values.setTypeEquals(BaseType.STRING); 077 078 output = new TypedIOPort(this, "output", false, true); 079 values.setTypeEquals(BaseType.GENERAL); 080 081 delim = new StringParameter(this, "Field delimeter"); 082 083 _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" " 084 + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n" 085 + "</svg>\n"); 086 } 087 088 // ///////////////////////////////////////////////////////////////// 089 // // ports and parameters //// 090 091 public TypedIOPort labels = null; 092 public TypedIOPort values = null; 093 094 public TypedIOPort output = null; 095 096 // the delimeter 097 public StringParameter delim = null; 098 099 // ///////////////////////////////////////////////////////////////// 100 // // public methods //// 101 102 /** 103 * Send the token in the value parameter to the output. 104 * 105 * @exception IllegalActionException 106 * If it is thrown by the send() method sending out the 107 * token. 108 */ 109 public void fire() throws IllegalActionException { 110 super.fire(); 111 112 String l = ((StringToken) labels.get(0)).stringValue(); 113 String v = ((StringToken) values.get(0)).stringValue(); 114 115 String delimStr = delim.stringValue(); 116 117 String[] labelsStr = l.split(delimStr); 118 String[] valuesStr = v.split(delimStr); 119 120 // sanity check 121 if (labelsStr.length != valuesStr.length) { 122 String msg = "number of labels (" + l + ")"; 123 msg += " and values (" + v + ") do not equal."; 124 throw new IllegalActionException(msg); 125 } 126 127 HashMap map = new HashMap(); 128 for (int i = 0; i < labelsStr.length; i++) { 129 map.put(labelsStr[i].trim(), new StringToken(valuesStr[i].trim())); 130 } 131 132 RecordToken out = new RecordToken(map); 133 output.broadcast(out); 134 } 135}