001/* 002 * Copyright (c) 2003-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.ecoinformatics.seek.util; 031 032import java.util.Hashtable; 033 034import ptolemy.actor.TypedAtomicActor; 035import ptolemy.actor.TypedIOPort; 036import ptolemy.data.StringToken; 037import ptolemy.data.Token; 038import ptolemy.data.type.BaseType; 039import ptolemy.kernel.CompositeEntity; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042 043public class FieldChooser extends TypedAtomicActor { 044 public TypedIOPort input = new TypedIOPort(this, "input", true, false); // the 045 // record 046 // input 047 public TypedIOPort inputChoice = new TypedIOPort(this, "inputChoice", true, 048 false); // the field in the record to choose 049 public TypedIOPort output = new TypedIOPort(this, "output", false, true); // output 050 // the 051 // users 052 // choice 053 054 /** 055 * Construct an actor with the given container and name. 056 * 057 * @param container 058 * The container. 059 * @param name 060 * The name of this actor. 061 * @exception IllegalActionException 062 * If the actor cannot be contained by the proposed 063 * container. 064 * @exception NameDuplicationException 065 * If the container already has an actor with this name. 066 */ 067 public FieldChooser(CompositeEntity container, String name) 068 throws NameDuplicationException, IllegalActionException { 069 super(container, name); 070 input.setTypeEquals(BaseType.GENERAL); 071 inputChoice.setTypeEquals(BaseType.STRING); 072 output.setTypeEquals(BaseType.GENERAL); 073 } 074 075 /** 076 * Send a random number with a uniform distribution to the output. This 077 * number is only changed in the prefire() method, so it will remain 078 * constant throughout an iteration. 079 * 080 * @exception IllegalActionException 081 * If there is no director. 082 */ 083 public void fire() throws IllegalActionException { 084 super.fire(); 085 int inputChoiceWidth = inputChoice.getWidth(); 086 if (inputChoiceWidth > 1) { 087 throw new IllegalActionException( 088 "The inputChoice port can only accept " 089 + "one channel of data."); 090 } 091 092 Token inputChoiceToken = inputChoice.get(0); 093 String inputChoiceVal = inputChoiceToken.toString(); 094 // take the quotes off the string 095 inputChoiceVal = inputChoiceVal.substring(1, 096 inputChoiceVal.length() - 1); 097 098 int width = input.getWidth(); 099 for (int i = 0; i < width; i++) { // get the records and output the 100 // correct field 101 if (input.hasToken(i)) { 102 Token token = input.get(i); 103 String value = token.toString(); 104 Hashtable fields = new Hashtable(); 105 value = value.substring(1, value.length() - 1); // chop the 106 // braces 107 while (value.length() != 0) { 108 String val; 109 if (value.indexOf(",") == -1) { // at the end of the string 110 val = value.substring(0, value.length()); 111 value = ""; 112 } else { 113 val = value.substring(0, value.indexOf(",")); 114 } 115 String hashname = val.substring(0, val.indexOf("=")); 116 String hashval = val.substring(val.indexOf("=") + 1, val 117 .length()); 118 fields.put(hashname.trim(), hashval.trim()); 119 if (val.length() + 1 < value.length()) { 120 value = value.substring(val.length() + 1, value 121 .length()); 122 } 123 } 124 125 if (!fields.containsKey(inputChoiceVal)) { 126 throw new IllegalActionException("No such key '" 127 + inputChoiceVal + "'...fields is " 128 + fields.toString()); 129 } 130 131 try { 132 sendToken(fields.get(inputChoiceVal)); 133 } catch (ClassCastException cce) { 134 throw new IllegalActionException( 135 "Error casting the output token '" 136 + fields.get(inputChoiceVal).toString() 137 + "' to the appropriate output port type: " 138 + cce.getMessage()); 139 } 140 } 141 } 142 } 143 144 /** 145 * sends the token. this can be overwritten to send different types of 146 * tokens besides strings. 147 */ 148 protected void sendToken(Object o) throws ClassCastException, 149 IllegalActionException { 150 output.send(0, new StringToken((String) o)); 151 // getToken will always return a stringToken here. 152 } 153 154 /** 155 * 156 */ 157 public boolean prefire() throws IllegalActionException { 158 return super.prefire(); 159 } 160}