Due to recent updates, all users are required to create an Altair One account to login to the RapidMiner community. Click the Register button to create your account using the same email that you have previously used to login to the RapidMiner community. This will ensure that any previously created content will be synced to your Altair One account. Once you login, you will be asked to provide a username that identifies you to other Community users. Email us at Community with questions.

[solved] ConcurrentModificationException when iterating over attributes (script)

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

having recently started with writing scripts, I have an idea what causes this error but unfortunately no idea how to solve it  :-\

This piece of code is supposed to create a new attribute with a prefix in the name for each attribute in the example set.
(e.g. "att1" leads to "prefix_att1" and "att2" leads to "prefix_att2")

I suppose the error occurs because the for loop shall iterate over each attribute in the example set but while doing so the code creates new attributes and adds them to the example set.

Additionally, the examples of all newly created attributes - and only the newly created - shall be edited (e.g. set to "123").

PS: I forgot to say: When I run this routine a second time (in the same script right after the first loop) it would like to create new attributes again (let's say prefix2_att1) but only for the original attributes an not those that have been recently created during the first loop.

Does anyone have a hint how to proceed?

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="6.1.000">
 <context>
   <input/>
   <output/>
   <macros/>
 </context>
 <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"/>
     <operator activated="true" class="execute_script" compatibility="6.1.000" expanded="true" height="76" name="Execute Script" width="90" x="179" y="30">
       <parameter key="script" value="import com.rapidminer.tools.Ontology;&#10;&#10;ExampleSet es = input [0];&#10;&#10;for ( Attribute att : es.getAttributes() )&#10;{&#10;&#9;String att_name = &quot;prefix&quot; + att.getName();&#10;&#9;Attribute newAttribute = AttributeFactory.createAttribute( att_name, Ontology.ATTRIBUTE_VALUE_TYPE.NUMERICAL );&#10;&#9;es.getExampleTable().addAttribute( newAttribute );&#10;&#9;es.getAttributes().addRegular( newAttribute );&#10;&#9;es.getExample( 0 ).setValue( att, 123 );&#10;}&#10;&#10;return input;"/>
     </operator>
     <connect from_op="Generate Data" from_port="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"/>
   </process>
 </operator>
</process>

Kind regards
Sachs

Answers

  • qwertzqwertz Member Posts: 130 Contributor II

    YEAY! I made it :D So happy  8)

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="6.1.000">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <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"/>
          <operator activated="true" class="execute_script" compatibility="6.1.000" expanded="true" height="76" name="Execute Script (2)" width="90" x="179" y="30">
            <parameter key="script" value="import com.rapidminer.tools.Ontology;&#10;&#10;ExampleSet es = input[ 0 ];&#10;List&lt;Attribute&gt; listOfAtts = new LinkedList&lt;&gt;();&#10;&#10;for ( Attribute att : es.getAttributes().allAttributes() )&#10; {&#10;&#9;listOfAtts.add( att );&#10; }&#10;&#10;for ( Attribute att : listOfAtts )&#10; {&#10; &#9;String name = &quot;prefix_&quot; + att.getName();&#10; &#9;Attribute newAttribute = AttributeFactory.createAttribute(name, Ontology.ATTRIBUTE_VALUE_TYPE.NUMERICAL);&#10;&#9;es.getExampleTable().addAttribute( newAttribute );&#10;&#9;es.getAttributes().addRegular( newAttribute );&#10; }&#10;&#10;for ( Attribute att : listOfAtts )&#10; {&#10; &#9;String name = &quot;prefix2_&quot; + att.getName();&#10; &#9;Attribute newAttribute = AttributeFactory.createAttribute(name, Ontology.ATTRIBUTE_VALUE_TYPE.NUMERICAL);&#10;&#9;es.getExampleTable().addAttribute( newAttribute );&#10;&#9;es.getAttributes().addRegular( newAttribute );&#10; }&#10;&#10;return input;"/>
          </operator>
          <connect from_op="Generate Data" from_port="output" to_op="Execute Script (2)" to_port="input 1"/>
          <connect from_op="Execute Script (2)" 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"/>
        </process>
      </operator>
    </process>

    Cheers
    Sachs
Sign In or Register to comment.