001/* 002 * Copyright (c) 2005-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: berkley $' 006 * '$Date: 2010-04-27 17:12:36 -0700 (Tue, 27 Apr 2010) $' 007 * '$Revision: 24000 $' 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.kepler.actor; 031 032import java.text.DecimalFormat; 033 034import ptolemy.actor.TypedAtomicActor; 035import ptolemy.actor.TypedIOPort; 036import ptolemy.actor.parameters.PortParameter; 037import ptolemy.data.StringToken; 038import ptolemy.data.type.BaseType; 039import ptolemy.kernel.CompositeEntity; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042 043////////////////////////////////////////////////////////////////////////// 044//// DecimalFormatConverter 045 046/** 047 * This actor will generate a number based on the specified format. 048 * 049 * @author Jianwu Wang 050 * @version $Id: DecimalFormatConverter.java 24000 2010-04-28 00:12:36Z berkley $ 051 */ 052public class DecimalFormatConverter extends TypedAtomicActor { 053 054 /** 055 * Construct a DecimalFormatConverter 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 entity 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 DecimalFormatConverter(CompositeEntity container, String name) 068 throws NameDuplicationException, IllegalActionException { 069 super(container, name); 070 071 input = new TypedIOPort(this, "input", true, false); 072 073 output = new TypedIOPort(this, "output", false, true); 074 output.setTypeEquals(BaseType.STRING); 075 076 myformat = new PortParameter(this, "decimal format"); 077 myformat.setStringMode(true); 078 myformat.setExpression("" + 0); 079 myformat.getPort().setTypeEquals(BaseType.STRING); 080 081 _attachText("_iconDescription", "<svg>\n" 082 + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" " 083 + "style=\"fill:white\"/>\n" 084 + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10" 085 + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n" 086 + "</svg>\n"); 087 } 088 089 // ///////////////////////////////////////////////////////////////// 090 // // ports and parameters //// 091 092 /** 093 * The input port, which is a trigger. 094 */ 095 public TypedIOPort input = null; 096 /** 097 * The output port, which contains the new directory path. 098 */ 099 public TypedIOPort output = null; 100 /** 101 * The parameter, which is a string for the designed format. 102 */ 103 public PortParameter myformat = null; 104 105 // ///////////////////////////////////////////////////////////////// 106 // // public methods //// 107 108 /** 109 * output the number according to designed format. 110 * 111 * @exception IllegalActionException 112 * 113 */ 114 public void fire() throws IllegalActionException { 115 super.fire(); 116 117 String _inputString = input.get(0).toString(); 118 _input = (new Double(_inputString)).doubleValue(); 119 120 myformat.update(); 121 _myformat = ((StringToken)myformat.getToken()).stringValue(); 122 DecimalFormat df = new DecimalFormat(_myformat); 123 124 125 _output = df.format(_input); 126 127 output.send(0, new StringToken(_output)); 128 } 129 130 // ///////////////////////////////////////////////////////////////// 131 // // private members //// 132 133 private String _myformat; 134 private double _input; 135 private String _output; 136}