No, Kepler does not have 'StringToDouble' actor. So i do some tests.
First, i try to define type of the output port as BaseType.DOUBLE_MATRIX
but it failed. After that i define it as,
output.setTypeEquals(new ArrayType(BaseType.DOUBLE));
and at the end of the code (when sending output),
output.send(0, new ArrayToken(BaseType.DOUBLE, dataIntTokens));
like StringToIntArray actor in Ptolemy. But it fails also and i get
following error message,
"Conversion is not supported from ptolemy.data.StringToken "...." to the
type double becuse the type of the token is higher or incomperable with
the given type."
so, i am working on it and if i solve my problem, i will let you know.
Thanks for all,
Ufuk
Edward A. Lee wrote:
> Type casting from string to a numeric type won't work.
> Type casts are only supported when conversion is guaranteed
> to be lossless, and this isn't true for string to numeric.
> So the conversion has to be done in Java code. Do we have
> a StringToDouble conversion actor? If not, we should...
>
> Edward
>
> At 03:48 AM 8/10/2007, [BE] Ufuk Utku Turuncoglu wrote:
>
>> Hi,
>>
>> In the previous postings to the list, i had a problem reading ASCII file in
>> Kepler but after getting some idea from list it solved. Now i write a new
>> actor that substract the specified column of the multi column ASCII file
>> after reading it. The code is attached to mail as 'ArrayColumn.java'. It
>> gets two parameter; first one is number of column in ASCII file and second
>> one is index of the first data (must be less than number of column !!!). So
>> using theses values it ouputs the defined column of the array.
>>
>> My problem is controlling data type of the output port. It gets the input
>> from 'String Splitter' actor (it is used after reading ASCII file with \s
>> regular expression) and results is the same data type with input
>> (ArrayType(String)). So, i need to convert them into float or other numeric
>> data types to calculate the maximum, minimum or average. How can i control
>> the output port data type automatically? Is it neccecary to fix it inside of
>> the code such as type casting? Any idea must be very helpful.
>>
>> Best regards,
>>
>> Ufuk Utku Turuncoglu
>>
>>
>> package edu.tutorial.turuncu;
>>
>> //import ptolemy.actor.TypedAtomicActor;
>> //import ptolemy.actor.TypedIOPort;
>> import ptolemy.actor.lib.Transformer;
>> import ptolemy.actor.parameters.PortParameter;
>> import ptolemy.data.ArrayToken;
>> import ptolemy.data.IntToken;
>> import ptolemy.data.Token;
>> import ptolemy.data.StringToken;
>> import ptolemy.data.type.Type;
>> //import ptolemy.data.expr.Parameter;
>> import ptolemy.data.type.ArrayType;
>> import ptolemy.data.type.BaseType;
>> import ptolemy.kernel.CompositeEntity;
>> import ptolemy.kernel.util.IllegalActionException;
>> import ptolemy.kernel.util.InternalErrorException;
>> import ptolemy.kernel.util.NameDuplicationException;
>> import ptolemy.kernel.util.Workspace;
>>
>> //public class ArrayColumn extends TypedAtomicActor {
>> public class ArrayColumn extends Transformer {
>>
>> public ArrayColumn (CompositeEntity container, String name)
>> throws NameDuplicationException, IllegalActionException {
>> super(container, name);
>>
>> //output.setTypeAtLeast(ArrayType.elementType(input));
>> output.setTypeAtLeast(input);
>> output.setTypeAtLeast(ArrayType.ARRAY_UNSIZED_BOTTOM);
>>
>> //output = new TypedIOPort(this, "array", false, true);
>> //output.setTypeEquals(new ArrayType(BaseType.INT));
>>
>> //input = new TypedIOPort(this, "array", true, false);
>> //input.setTypeEquals(new ArrayType(BaseType.STRING));
>>
>> //ind = new Parameter(this, "column #", new IntToken(2));
>> ndims = new PortParameter(this, "# dimension");
>> ndims.setTypeEquals(BaseType.INT);
>> ndims.setExpression("1");
>>
>> index = new PortParameter(this, "first index");
>> index.setTypeEquals(BaseType.INT);
>> index.setExpression("0");
>>
>> // icon
>> //_attachText("_iconDescription", "<svg>\n"
>> // + "<polygon points=\"-30,-20 30,-4 30,4 -30,20\" "
>> // + "style=\"fill:white\"/>\n"
>> // + "</svg>\n");
>> }
>>
>> ///////////////////////////////////////////////////////////////////
>> //// ports and parameters ////
>>
>> /** The input port, which contains the string.
>> */
>> //public TypedIOPort output = null;
>> /** The output port, which contains the array.
>> */
>> //public TypedIOPort input = null;
>> /** The parameter, which specifies the regular expression.
>> */
>> //public Parameter ind;
>> public PortParameter ndims;
>> public PortParameter index;
>>
>> ///////////////////////////////////////////////////////////////////
>> //// public methods ////
>>
>> public Object clone(Workspace workspace) throws
>> CloneNotSupportedException {
>> ArrayColumn newObject = (ArrayColumn) super.clone(workspace);
>> try {
>> newObject.output.setTypeAtLeast(ArrayType
>> .elementType(newObject.input));
>> } catch (IllegalActionException e) {
>> // Should have been caught before.
>> throw new InternalErrorException(e);
>> }
>> return newObject;
>> }
>>
>> public void fire() throws IllegalActionException {
>> super.fire();
>> // Get number of dimension
>> ndims.update();
>> int dimsCount = ((IntToken) ndims.getToken()).intValue();
>>
>> // Get dimension index
>> index.update();
>> int indexValue = ((IntToken) index.getToken()).intValue();
>>
>> // Get specified column data
>> if (input.hasToken(0)) {
>> ArrayToken inputValue = (ArrayToken) input.get(0);
>> Type inputElementType = inputValue.getElementType();
>>
>> Token[] outputArray = new Token[inputValue.length()/dimsCount];
>>
>> int j=0;
>> for (int i = indexValue; i < inputValue.length(); i+=dimsCount) {
>> outputArray[j] = Float.parseFloat(inputValue.getElement(i));
>> j=j+1;
>> }
>>
>> output.send(0, new ArrayToken(inputElementType, outputArray));
>> }
>> }
>> }
>>
>> _______________________________________________
>> Kepler-users mailing list
>> Kepler-users at ecoinformatics.org
>> http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/kepler-users
>>
>
> ------------
> Edward A. Lee
> Chair of EECS and Robert S. Pepper Distinguished Professor
> 231 Cory Hall, UC Berkeley, Berkeley, CA 94720-1770
> phone: 510-642-0253, fax: 510-642-2845
> eal at eecs.Berkeley.EDU, http://ptolemy.eecs.berkeley.edu/~eal
>