[kepler-dev] Re: [kepler-cvs] kepler/src/util FileReader.java
Chad Berkley
berkley at nceas.ucsb.edu
Wed May 5 09:33:58 PDT 2004
Hey Christopher,
Thanks for pointing that stuff out. I actually had the name of the
actor the same as the variable name, but I was getting a parser error
when vergil tried to add the actor to the actor library. It was
complaining about a duplicate entity named "input" so that's why I
changed it. I didn't think it should be doing that, but I'll mess
around with it this morning and try to figure out why that happened.
I have another question for you though about port params. When you drag
a port param onto the workspace, why does the icon have two parts? Is
that just for aesthetics, or does it serve a purpose?
thanks,
chad
Christopher Hylands Brooks wrote:
> BTW - One can use a PortParameter here so that you can get the
> file name from either a port or a parameter.
> See actor.lib.Ramp, actor.lib.Exec, actor.lib.security.KeyStoreActor
> and the actors in actor.lib.string
>
> Really, we should modify the actor.lib.io.FileReader actor so that
> it uses some sort of FilePortParameter instead of a FileParameter
>
> Also, you have
> output = new TypedIOPort(this, "stroutput", false, true);
> Usually, the coding standard is to have the name in the constructor
> be the same as the variable name:
> output = new TypedIOPort(this, "output", false, true);
> There is a test in configs/test or vergil/test that reports issues
> like this.
>
> No big deal on this, just a btw.
>
> -Christopher
>
> --------
>
> berkley 04/05/04 16:27:25
>
> Added: src/util FileReader.java
> Log:
> a file reader that can take the filename through a port instead of an att
> ribute
>
> Revision Changes Path
> 1.1 kepler/src/util/FileReader.java
>
> Index: FileReader.java
> ===================================================================
> /**
> * '$RCSfile: FileReader.java,v $'
> *
> * '$Author: berkley $'
> * '$Date: 2004/05/04 23:27:25 $'
> * '$Revision: 1.1 $'
> *
> * For Details: http://kepler.ecoinformatics.org
> *
> * Copyright (c) 2004 The Regents of the University of California.
> * All rights reserved.
> *
> * Permission is hereby granted, without written agreement and without
> * license or royalty fees, to use, copy, modify, and distribute this
> * software and its documentation for any purpose, provided that the
> * above copyright notice and the following two paragraphs appear in
> * all copies of this software.
> *
> * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
> * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
> * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
> * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
> * OF SUCH DAMAGE.
> *
> * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
> * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
> * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
> * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
> * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
> * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
> */
>
> package util;
>
> import ptolemy.actor.TypedIOPort;
> import ptolemy.actor.TypedAtomicActor;
> import ptolemy.actor.lib.Source;
> import ptolemy.kernel.attributes.FileAttribute;
> import ptolemy.kernel.util.IllegalActionException;
> import ptolemy.kernel.util.Workspace;
> import ptolemy.kernel.util.NameDuplicationException;
> import ptolemy.kernel.CompositeEntity;
> import ptolemy.data.expr.Parameter;
> import ptolemy.data.StringToken;
> import ptolemy.data.Token;
> import ptolemy.data.type.BaseType;
>
> import java.io.IOException;
> import java.io.BufferedReader;
>
> /**
> * class to extend the FileReader so that you can input the name of the
> * file to be read from an input port.
> * NOTE:
> * Originally written by Yang Zhao of the Ptolemy project.
> */
> public class FileReader extends TypedAtomicActor
> {
> public TypedIOPort filename, output;
> private String filenameStr;
>
> /**
> *@param workspace
> *@exception IllegalActionException
> *@exception NameDuplicationException
> */
> public FileReader(Workspace workspace)
> throws IllegalActionException, NameDuplicationException
> {
> super(workspace);
> filename = new TypedIOPort(this, "filename", true, false);
> output = new TypedIOPort(this, "stroutput", false, true);
> output.setTypeEquals(BaseType.STRING);
> }
>
> /**
> *@param container The container.
> *@param name The name of this actor.
> *@exception IllegalActionException If the container is incompatibl
> e
> * with this actor.
> *@exception NameDuplicationException If the name coincides with
> * an actor already in the container.
> */
> public FileReader(CompositeEntity container, String name)
> throws IllegalActionException, NameDuplicationException
> {
> super(container, name);
> filename = new TypedIOPort(this, "filename", true, false);
> output = new TypedIOPort(this, "stroutput", false, true);
> output.setTypeEquals(BaseType.STRING);
> }
>
> /**
> * Output the data read from the file or URL as a string.
> *
> *@exception IllegalActionException If there is no director.
> */
> public void fire()
> throws IllegalActionException
> {
> try
> {
> BufferedReader reader;
> StringToken st = (StringToken)filename.get(0);
> filenameStr = st.toString();
> if(filenameStr.indexOf("\"") != -1)
> { //trim the quotes
> filenameStr = filenameStr.substring(1, filenameStr.length() - 1);
> }
> reader = new BufferedReader(new java.io.FileReader(filenameStr));
>
> StringBuffer momlBuffer = new StringBuffer();
> String newline = System.getProperty("line.separator");
>
> while(true)
> {
> String line = reader.readLine();
>
> if(line == null)
> {
> break;
> }
> momlBuffer = momlBuffer.append(line);
> momlBuffer = momlBuffer.append(newline);
> }
>
> String momlString = momlBuffer.toString();
>
> output.broadcast(new StringToken(momlString));
> }
> catch(Exception ex)
> {
> throw new IllegalActionException(this, ex.getMessage());
> }
> }
>
> /**
> * Return true if there is token at the <i>trigger<i> input.
> * Otherwise, return false.
> *
> *@return
> *@exception IllegalActionException If the superclass throws it.
> */
> public boolean prefire()
> throws IllegalActionException
> {
> if(filename.getWidth() > 0)
> {
> return true;
> }
> return false;
> }
>
> }
>
>
>
>
> _______________________________________________
> kepler-cvs mailing list
> kepler-cvs at ecoinformatics.org
> http://www.ecoinformatics.org/mailman/listinfo/kepler-cvs
> --------
--
-----------------------
Chad Berkley
National Center for
Ecological Analysis
and Synthesis (NCEAS)
berkley at nceas.ucsb.edu
-----------------------
More information about the Kepler-dev
mailing list