The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here

Create macro with Java - Execute Script Operator

cs_diablocs_diablo Member Posts: 1 Learner II
edited November 2018 in Help

Hi,

 

I wish to create macro starting from an object in java in Execute Script operator. In my example, I recover the date of a file and I wish to use it thereafter as macro in my RapidMiner process. I don't know how to exit this object of the Execute Script operator in the form of macro.

 

LastDate = Files.getLastModifiedTime(Paths.get("%{Way}")) ;

// and after ?

If not I had found an alternative by creating a table and while inserting the date inside. But I did not succeed in inserting this date in table.

(I am to help with this post)

 

LastDate = Files.getLastModifiedTime(Paths.get("%{Way}")) 

// create a table
MemoryExampleTable table = new MemoryExampleTable();

// create and add attributes
nominalAttr = AttributeFactory.createAttribute("File", Ontology.NOMINAL);
dateAttr = AttributeFactory.createAttribute("Last_Modify",Ontology.DATE_TIME);

table.addAttribute(nominalAttr);
table.addAttribute(dateAttr);

// create a data row
DataRow row = new DoubleSparseArrayDataRow();

row.set(nominalAttr, nominalAttr.getMapping().mapString("%{file_name}"));
row.set(dateAttr, LastDate);

// add the row to the table
table.addDataRow(row);

// create an ExampleSet from the underlying table
ExampleSet exampleSet = table.createExampleSet();

return exampleSet;

 

I am really thankful for every help from you

Tagged:

Best Answer

  • pschlunderpschlunder Employee-RapidMiner, RapidMiner Certified Analyst, RapidMiner Certified Expert, RMResearcher, Member Posts: 96 RM Research
    Solution Accepted

    Hi,

    sorry for the late reply.

     

    This should do the trick:

    operator.getProcess().getMacroHandler().addMacro("Key", "Value");

     

    Best regards,

    Philipp

Answers

  • KanikaAg15KanikaAg15 Member Posts: 19 Learner II
    @pschlunder the function you have specified is failing in the static scope of a script in java. Do we need to import any library or change the data type of the function. 
  • jwpfaujwpfau Employee-RapidMiner, Member Posts: 303 RM Engineering
    Hi Kanika,

    here is a full example process which is using the Code Snippet:

    <?xml version="1.0" encoding="UTF-8"?><process version="9.10.012">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="9.10.012" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" class="retrieve" compatibility="9.10.012" expanded="true" height="68" name="Retrieve Deals" width="90" x="45" y="34">
            <parameter key="repository_entry" value="//Samples/data/Deals"/>
          </operator>
          <operator activated="true" class="execute_script" compatibility="9.10.012" expanded="true" height="82" name="Execute Script" width="90" x="179" y="34">
            <parameter key="script" value="/* &#10; * You can use both Java and Groovy syntax in this script.&#10; * &#10; * Note that you have access to the following two predefined variables:&#10; * 1) input (an array of all input data)&#10; * 2) operator (the operator instance which is running this script)&#10; */&#10;&#10;// Take first input data and treat it as generic IOObject&#10;// Alternatively, you could treat it as an ExampleSet if it is one:&#10;// ExampleSet inputData = input[0];&#10;IOObject inputData = input[0];&#10;&#10;// You can add any code here&#10;operator.getProcess().getMacroHandler().addMacro(&quot;Macro&quot;, &quot;Lazy Example&quot;);&#10;&#10;&#10;// This line returns the first input as the first output&#10;return inputData;"/>
            <parameter key="standard_imports" value="true"/>
          </operator>
          <operator activated="true" class="generate_attributes" compatibility="9.10.012" expanded="true" height="82" name="Generate Attributes" width="90" x="313" y="34">
            <list key="function_descriptions">
              <parameter key="Macro" value="%{Macro}"/>
            </list>
            <parameter key="keep_all" value="true"/>
          </operator>
          <connect from_op="Retrieve Deals" from_port="output" to_op="Execute Script" to_port="input 1"/>
          <connect from_op="Execute Script" from_port="output 1" to_op="Generate Attributes" to_port="example set input"/>
          <connect from_op="Generate Attributes" from_port="example set output" 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>
    

    Greetings,
    Jonas
Sign In or Register to comment.