001/* An actor that reads expressions from a text file and outputs them as tokens. 002 003 @Copyright (c) 2002-2014 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the 009 above copyright notice and the following two paragraphs appear in all 010 copies of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION 2 026 COPYRIGHTENDKEY 027 */ 028package ptolemy.actor.lib.io; 029 030import ptolemy.data.BooleanToken; 031import ptolemy.data.expr.Variable; 032import ptolemy.kernel.CompositeEntity; 033import ptolemy.kernel.util.IllegalActionException; 034import ptolemy.kernel.util.NameDuplicationException; 035import ptolemy.kernel.util.Workspace; 036 037/////////////////////////////////////////////////////////////////// 038//// ExpressionReader 039 040/** 041 This actor reads a file or URL, one line at a time, evaluates each 042 line as an expression, and outputs the token resulting from the 043 evaluation. The first line in the file determines the data type 044 of the output. All other lines must contain expressions that 045 evaluate to the same type or a subtype, or a run-time type error will occur. 046 The file or URL is specified using any form acceptable 047 to FileParameter. If an end of file is reached, then prefire() and 048 postfire() will both return false. 049 050 @see ExpressionWriter 051 @see ptolemy.data.expr.FileParameter 052 @author Edward A. Lee 053 @version $Id$ 054 @since Ptolemy II 2.2 055 @deprecated Use LineReader and ExpressionToToken. 056 @Pt.ProposedRating Yellow (eal) 057 @Pt.AcceptedRating Red (liuj) 058 */ 059@Deprecated 060public class ExpressionReader extends LineReader { 061 /** Construct an actor with the given container and name. 062 * @param container The container. 063 * @param name The name of this actor. 064 * @exception IllegalActionException If the actor cannot be contained 065 * by the proposed container. 066 * @exception NameDuplicationException If the container already has an 067 * actor with this name. 068 */ 069 public ExpressionReader(CompositeEntity container, String name) 070 throws IllegalActionException, NameDuplicationException { 071 super(container, name); 072 073 _expressionEvaluator = new Variable(this, "_expressionEvaluator"); 074 } 075 076 /////////////////////////////////////////////////////////////////// 077 //// public methods //// 078 079 /** Clone the actor into the specified workspace. 080 * @param workspace The workspace for the new object. 081 * @return A new actor. 082 * @exception CloneNotSupportedException If a derived class contains 083 * an attribute that cannot be cloned. 084 */ 085 @Override 086 public Object clone(Workspace workspace) throws CloneNotSupportedException { 087 ExpressionReader newObject = (ExpressionReader) super.clone(workspace); 088 089 newObject._expressionEvaluator = (Variable) newObject 090 .getAttribute("_expressionEvaluator"); 091 return newObject; 092 } 093 094 /** Output the data read in the preinitialize() or postfire() if 095 * there is any. 096 * @exception IllegalActionException If there's no director, or 097 * if the expression read from the file cannot be parsed. 098 */ 099 @Override 100 public void fire() throws IllegalActionException { 101 // NOTE: Since we don't call super.fire(), we have to do what 102 // is done in the Source base class. 103 for (int i = 0; i < trigger.getWidth(); i++) { 104 if (trigger.hasToken(i)) { 105 trigger.get(i); 106 } 107 } 108 // Initialize method in superclass closes the file. 109 // We need to reopen it and reread it. 110 if (_firstFiring) { 111 _openAndReadFirstTwoLines(); 112 _firstFiring = false; 113 } 114 115 if (_currentLine != null) { 116 _expressionEvaluator.setExpression(_currentLine); 117 output.broadcast(_expressionEvaluator.getToken()); 118 } 119 if (_nextLine == null) { 120 endOfFile.broadcast(BooleanToken.TRUE); 121 } else { 122 endOfFile.broadcast(BooleanToken.FALSE); 123 } 124 } 125 126 /** Open the file or URL and read the first line, and use the 127 * first line to set the type of the output. 128 * @exception IllegalActionException If the file or URL cannot be 129 * opened, or if the first line cannot be read. 130 */ 131 @Override 132 public void preinitialize() throws IllegalActionException { 133 super.preinitialize(); 134 135 // Read the first two lines so that we can use the first 136 // line to set the type of the output. The file will be 137 // closed and re-opened in initialize(). 138 _openAndReadFirstTwoLines(); 139 140 // Set the output type. 141 _expressionEvaluator.setExpression(_currentLine); 142 output.setTypeEquals(_expressionEvaluator.getType()); 143 } 144 145 /** Override the base class to clear memory of any 146 * possibly erroneous expression. 147 */ 148 @Override 149 public void wrapup() throws IllegalActionException { 150 super.wrapup(); 151 // This is necessary because if there is an 152 // invalid expression here, then validate() will 153 // fail when the model is next run. 154 // Thanks to Adriana Ricchiuti for diagnosing this. 155 _expressionEvaluator.setExpression(""); 156 } 157 158 /////////////////////////////////////////////////////////////////// 159 //// private members //// 160 161 /** Variable used to evaluate expressions. */ 162 private Variable _expressionEvaluator; 163}