[Solved] Reading numerical values with a script works ok but no text

qwertzqwertz Member Posts: 130 Contributor II
edited November 2018 in Help
Dear all,

I wonder why I cannot read out any text with the "execute script" operator. It's possible for values and it's possible to write text but not to read it... The following example code is supposed to simply copy the text from column "str" to "str2".

Please advice!

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="6.1.000">
 <operator activated="true" class="process" compatibility="6.1.000" expanded="true" name="Process">
   <process expanded="true">
     <operator activated="true" class="generate_data" compatibility="6.1.000" expanded="true" height="60" name="Generate Data" width="90" x="45" y="30">
       <parameter key="number_examples" value="5"/>
       <parameter key="number_of_attributes" value="1"/>
     <operator activated="true" class="generate_attributes" compatibility="6.1.000" expanded="true" height="76" name="Generate Attributes" width="90" x="179" y="30">
       <list key="function_descriptions">
         <parameter key="str" value="&quot;text&quot;"/>
         <parameter key="str2" value="&quot;&quot;"/>
     <operator activated="true" class="execute_script" compatibility="6.1.000" expanded="true" height="76" name="Execute Script" width="90" x="380" y="30">
       <parameter key="script" value="import javax.swing.JOptionPane;&#10;&#10;ExampleSet exampleSet = input[ 0 ];&#10;&#10;Attribute att = exampleSet.getAttributes().get( &quot;str&quot; );&#10;Attribute att2 = exampleSet.getAttributes().get( &quot;str2&quot; );&#10;for (Example example : exampleSet )&#10;{&#10;String s = example.getValue( att );&#10;example.setValue( att2, s );&#10;}&#10;&#10;return exampleSet;"/>
     <connect from_op="Generate Data" from_port="output" to_op="Generate Attributes" to_port="example set input"/>
     <connect from_op="Generate Attributes" from_port="example set output" to_op="Execute Script" to_port="input 1"/>
     <connect from_op="Execute Script" from_port="output 1" to_port="result 1"/>
     <portSpacing port="source_input 1" spacing="0"/>
     <portSpacing port="sink_result 1" spacing="0"/>
     <portSpacing port="sink_result 2" spacing="0"/>

Kind regards


  • Options
    Marco_BoeckMarco_Boeck Administrator, Moderator, Employee, Member, University Professor Posts: 1,996 RM Engineering

    Strings are stored in a nominal mapping which exists for each attribute. This is basically a map from an int key to a String. So the actual data contains the double (in this case an int) key instead of the String.
    If you want to get Strings, you will have to call example.getValueAsString() if it's nominal. To write, you have to first store the value in the mapping via attribute.getMapping().mapString(value) and then store the returned int (as double) in the example.

  • Options
    qwertzqwertz Member Posts: 130 Contributor II
    Thank you very much for your quick response. The way you described it, it worked perfectly.
    Now I understand why RM always returned numbers instead of the string :)

Sign In or Register to comment.