"Very basic use in Java question"

Jacksonhi
Jacksonhi New Altair Community Member
edited November 5 in Community Q&A
I have basically implemented this below in the next code section. It is working, passes all the unit tests I wrote. Thanks for the advice which I will try and add since this solution is not perfect, but working.
<?xml version="1.0" encoding="UTF-8"?>
<process version="4.0beta2">

 <operator name="Root" class="Process">
     <description text="hope this will work"/>
     <operator name="TestExampleSource" class="ExampleSource">
         <parameter key="attributes"   value="2011-05_34.aml"/>
     </operator>
     <operator name="ModelLoader" class="ModelLoader">
         <parameter key="model_file"   value="2011-04_34_DecisionTree.mod"/>
     </operator>
`
     <operator name="ModelApplier" class="ModelApplier">
         <list key="application_parameters">
         </list>
     </operator>
     <operator name="ExampleSetWriter" class="ExampleSetWriter">
         <parameter key="example_set_file"     value="2011-05_34_DecisionTree.out"/>
         <parameter key="format"       value="special_format"/>
         <parameter key="special_format"       value="$l $p"/>
     </operator>
 </operator>
   
</process>


I have fixed this so it is working. Not quite what I wanted but it passes all the unit tests I wrote. Good enough to move on. It expects one pattern, if it gets more it uses the last pattern. It deletes the resultset from the container and keeps the models which need to be loaded one time.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dataMine;

import com.rapidminer.RapidMiner;
import com.rapidminer.example.Example;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorCreationException;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.tools.OperatorService;
import java.io.File;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author ron
*/
public class rapidUtil {
   
   public static final String database_url = "jdbc:mysql://192.168.0.15:3306/mng";
   public static final String username = "ibt";
   public static final String password = "88bitz";

   HashMap<String, IOContainer> container = new HashMap<String, IOContainer>();
   

   public void rapidUtil() {
       String pluginDirString = new File("/home/ats/machineLearning/rapidminer-4.6/lib/plugins").getAbsolutePath();
       System.setProperty(RapidMiner.PROPERTY_RAPIDMINER_INIT_PLUGINS_LOCATION, pluginDirString);
       // set properties - rapid miner home
       String path = "/home/ats/machineLearning/rapidminer-4.6/";
       System.setProperty("rapidminer.home", path);
       RapidMiner.init(false, false, true, false);
   }

   public void AddModel(String containerName, String modelPath) {
       try {
           
           if (!container.containsKey(containerName)) {
               container.put(containerName, new IOContainer());
           }

           Operator modelLoader = OperatorService.createOperator("ModelLoader");
           modelLoader.setParameter("model_file", new File(modelPath).getAbsolutePath());
           container.put(containerName, modelLoader.apply(container.get(containerName)));

       } catch (OperatorException ex) {
           Logger.getLogger(rapidUtil.class.getName()).log(Level.SEVERE, null, ex);
       } catch (OperatorCreationException ex) {
           Logger.getLogger(rapidUtil.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
   
   public int ApplyModel(String containerName, String sqlQuery) {    
       int result = 999;
       
       try {
       Operator exampleSource;
           exampleSource = OperatorService.createOperator("DatabaseExampleSource");
           exampleSource.setParameter("database_url", database_url);
           exampleSource.setParameter("username", username);
           exampleSource.setParameter("password", password);
           exampleSource.setParameter("query", sqlQuery);

           container.put(containerName,exampleSource.apply(container.get(containerName)));
     
           Operator modelApplier = OperatorService.createOperator("ModelApplier");
           modelApplier.setParameter("keep_model", "true");
           container.put(containerName, modelApplier.apply(container.get(containerName)));
           
           ExampleSet resultSet = container.get(containerName).get(ExampleSet.class);
                     
           Example e = resultSet.getExample(resultSet.size() -1);
           result = (int) e.getPredictedLabel();
           
           container.get(containerName).remove(ExampleSet.class);

       } catch (OperatorException ex) {
           Logger.getLogger(rapidUtil.class.getName()).log(Level.SEVERE, null, ex);
       } catch (OperatorCreationException ex) {
           Logger.getLogger(rapidUtil.class.getName()).log(Level.SEVERE, null, ex);
       }
       return result;
   }

   
}
Tagged:

Answers

  • Marco_Boeck
    Marco_Boeck New Altair Community Member
    Hi,

    please note that we have a Development forum for these kind of questions, so I'm moving the thread there :)
    There are also examples in there which pretty much cover your question, but I've summed them up anyway.

    To make the java approach as simple as possible, it is advisable to create all processes you need with the appropriate settings in RapidMiner, and then call these processes via java.
    Basically you will need something along these lines:

    // this initializes RapidMiner with your repositories available
    RapidMiner.setExecutionMode(ExecutionMode.COMMAND_LINE);
    RapidMiner.init();

    // loads the process from the repository
    RepositoryLocation pLoc = new RepositoryLocation("//LocalRepository/folder/as/needed/yourProcessName"));
    ProcessEntry pEntry = (ProcessEntry) pLoc.locateEntry();
    String processXML = pEntry.retrieveXML();
    Process myProcess = new Process(processXML);

    // if need be, you can give the process IOObjects as parameter (this would be the case if you used the process input ports)
    RepositoryLocation loc = new RepositoryLocation("//LocalRepository/folder/as/needed/yourData"));
    IOObjectEntry entry = (IOObjectEntry) loc.locateEntry();
    myIOObject= entry.retrieveData(null);

    // execute the process and get the resulting objects
    IOContainer ioInput = new IOContainer(new IOObject[] {myIOObject});
    // just use myProcess.run() if you don't use the input ports for your process
    IOContainer ioResult = myProcess.run(ioInput);

    // use the result(s) as needed, for example if your process just returns one ExampleSet, use this:
    if (ioResult.getElementAt(0) instanceof ExampleSet) {
    ExampleSet resultSet = (ExampleSet)ioResult.getElementAt(0);
    }
    Regards,
    Marco