How to use Model built in RapidMiner (GUI) in JAVA Application

marjaw
marjaw New Altair Community Member
edited November 5 in Community Q&A
Hello, 

I want to write a java program that gets input data and uses a model, which was built with RapidMiner, to produce an output. 

I have this code: 
Caused by: com.rapidminer.operator.UserError: Cannot retrieve repository data from entry '//repository/models/cross_validation_model_neural_net9'. Reason: Cannot load data from 'C:\Users\wahl\price-calculator\src\main\resources\repository\models\cross_validation_model_neural_net9.ioo': java.lang.ClassCastException: cannot assign instance of java.util.ArrayList to field com.rapidminer.example.table.AbstractAttribute.transformations of type java.util.LinkedList in instance of com.rapidminer.example.table.NumericalAttribute.
	at com.rapidminer.Process.loadInitialData(Process.java:761)
	at com.rapidminer.Process.run(Process.java:933)
	at com.rapidminer.Process.run(Process.java:873)
	at com.rapidminer.Process.run(Process.java:832)
	at com.rapidminer.Process.run(Process.java:827)
	at com.rapidminer.Process.run(Process.java:817)
	at net.infosim.dilink.Calculator.calculatePrice(Calculator.java:79)
	at net.infosim.dilink.PriceCalculatorService.calculate(PriceCalculatorService.java:29)
	at net.infosim.dilink.PriceCalculatorService_ClientProxy.calculate(PriceCalculatorService_ClientProxy.zig:186)
	at net.infosim.dilink.PriceCalculatorResource.performCalculate(PriceCalculatorResource.java:46)
	at net.infosim.dilink.PriceCalculatorResource_ClientProxy.performCalculate(PriceCalculatorResource_ClientProxy.zig:186)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
	at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
	at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:621)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:487)
	at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:437)
	at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:362)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:439)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:400)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:374)
	at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:67)
	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:488)
	... 20 more
Caused by: com.rapidminer.repository.RepositoryException: Cannot load data from 'C:\Users\wahl\price-calculator\src\main\resources\repository\models\cross_validation_model_neural_net9.ioo': java.lang.ClassCastException: cannot assign instance of java.util.ArrayList to field com.rapidminer.example.table.AbstractAttribute.transformations of type java.util.LinkedList in instance of com.rapidminer.example.table.NumericalAttribute
	at com.rapidminer.repository.local.SimpleIOObjectEntry.retrieveData(SimpleIOObjectEntry.java:91)
	at com.rapidminer.Process.loadInitialData(Process.java:752)
	... 45 more
Caused by: java.lang.ClassCastException: cannot assign instance of java.util.ArrayList to field com.rapidminer.example.table.AbstractAttribute.transformations of type java.util.LinkedList in instance of com.rapidminer.example.table.NumericalAttribute
	at java.base/java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2281)
	at java.base/java.io.ObjectStreamClass$FieldReflector.checkObjectFieldValueTypes(ObjectStreamClass.java:2244)
	at java.base/java.io.ObjectStreamClass.checkObjFieldValueTypes(ObjectStreamClass.java:1463)
	at java.base/java.io.ObjectInputStream.defaultCheckFieldValues(ObjectInputStream.java:2536)
	at java.base/java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:655)
	at com.rapidminer.example.table.AbstractAttribute.readObject(AbstractAttribute.java:114)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at java.base/java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1216)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2381)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2520)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2414)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:517)
	at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:475)
	at java.base/java.util.ArrayList.readObject(ArrayList.java:899)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at java.base/java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1216)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2381)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2520)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2414)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2520)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2414)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2520)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2414)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2520)
	at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2414)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2215)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1707)
	at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:517)
	at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:475)
	at com.rapidminer.operator.tools.JavaBinaryBodySerializer.deserialize(SerializationType.java:46)
	at com.rapidminer.operator.tools.IOObjectSerializer.deserialize(IOObjectSerializer.java:105)
	at com.rapidminer.repository.local.SimpleIOObjectEntry.retrieveData(SimpleIOObjectEntry.java:89)
	... 46 more</code><pre>public String calculatePrice(Material materialToMatch) throws OperatorException, RepositoryException, IOException, XMLException {<br>        RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);<br>        URL path = Calculator.class.getClassLoader().getResource("repository");<br>        System.setProperty("rapidminer.home", path.toString());<br>        RapidMiner.init();<br><br>        RepositoryManager repositoryManager = RepositoryManager.getInstance(null);<br><br>        String absolutePath = null;<br>        if (repositoryManager.getRepositories().get(2) instanceof LocalRepository) {<br>            LocalRepository localRepository = (LocalRepository) repositoryManager.getRepositories().get(2);<br>            File location = localRepository.getFile();<br>            absolutePath = location.getAbsolutePath().replace("\\", "/");<br>        }
<br>        assert absolutePath != null;<br>        RepositoryLocation pLoc = new RepositoryLocation("//Repository/processes/model_neuralnet");<br>        ProcessEntry pEntry = (ProcessEntry) pLoc.locateEntry();<br>        String processXML = pEntry.retrieveXML();<br>        Process process = new Process(processXML);<br>        process.setProcessLocation(new RepositoryProcessLocation(pLoc));<br><br>        Operator read_csv = process.getOperator("Read CSV");<br>        read_csv.setParameter(PARAMETER_CSV_FILE, absolutePath + "/data/input.csv");<br>        Operator retrieve = process.getOperator("Retrieve");<br>        retrieve.setParameter(PARAMETER_REPOSITORY_ENTRY, "/models/cross_validation_model_neural_net9");<br><br>        IOContainer ioResult = process.run();<br><br>        ExampleSet resultSet = null;<br>        if (ioResult.getElementAt(0) instanceof ExampleSet) {<br>            resultSet = (ExampleSet) ioResult.getElementAt(0);<br>        }<br>        if (resultSet == null) {<br>            throw new IllegalArgumentException("Error while calculating the price");<br>        }<br>        return resultSet.toString();<br>    }</pre></pre></pre>My repository-folder is structured like this:<br>repository&nbsp;<br>&nbsp;-data<br>&nbsp; &nbsp; &nbsp; &nbsp;-input.csv<br>&nbsp;-models<br>&nbsp; &nbsp; &nbsp; &nbsp;-cross_validation_model_neural_net9.ioo<br>&nbsp;-processes<br>&nbsp; &nbsp; &nbsp; &nbsp;-model_neuralnet.rmp<br><br>I built the model and process file with the RapidMiner GUI. Then, I copied the files and pasted them into my java-project. This leads to following error:<br><br><pre class="CodeBlock"><code>

My process file: (it is a simple process, a Retrieve- and Read-CSV-file-Operator and one Apply-Model-Operator)
<?xml version="1.0" encoding="UTF-8"?><process version="9.7.002">
<context>
<input>
<location>//Repository/models/cross_validation_model_neural_net9</location>
</input>
<output/>
<macros/>
</context>
<operator activated="true" class="process" compatibility="9.7.002" 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="read_csv" compatibility="9.7.002" expanded="true" height="68" name="Read CSV" width="90" x="112" y="136">
<parameter key="csv_file" value="//Repository/data/input.csv"/>
<parameter key="column_separators" value=";"/>
<parameter key="trim_lines" value="false"/>
<parameter key="use_quotes" value="true"/>
<parameter key="quotes_character" value="&quot;"/>
<parameter key="escape_character" value="\"/>
<parameter key="skip_comments" value="false"/>
<parameter key="comment_characters" value="#"/>
<parameter key="starting_row" value="1"/>
<parameter key="parse_numbers" value="true"/>
<parameter key="decimal_character" value="."/>
<parameter key="grouped_digits" value="false"/>
<parameter key="grouping_character" value=","/>
<parameter key="infinity_representation" value=""/>
<parameter key="date_format" value=""/>
<parameter key="first_row_as_names" value="true"/>
<list key="annotations"/>
<parameter key="time_zone" value="SYSTEM"/>
<parameter key="locale" value="English (United States)"/>
<parameter key="encoding" value="SYSTEM"/>
<parameter key="read_all_values_as_polynominal" value="false"/>
<list key="data_set_meta_data_information"/>
<parameter key="read_not_matching_values_as_missings" value="true"/>
<parameter key="datamanagement" value="double_array"/>
<parameter key="data_management" value="auto"/>
</operator>
<operator activated="true" class="retrieve" compatibility="9.7.002" expanded="true" height="68" name="Retrieve" width="90" x="112" y="34">
<parameter key="repository_entry" value="//Repository/models/cross_validation_model_neural_net9.ioo"/>
</operator>
<operator activated="true" class="apply_model" compatibility="9.7.002" expanded="true" height="82" name="Apply Model" width="90" x="447" y="34">
<list key="application_parameters"/>
<parameter key="create_view" value="false"/>
</operator>
<connect from_op="Read CSV" from_port="output" to_op="Apply Model" to_port="unlabelled data"/>
<connect from_op="Retrieve" from_port="output" to_op="Apply Model" to_port="model"/>
<connect from_op="Apply Model" from_port="labelled data" 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>

Answers

  • marjaw
    marjaw New Altair Community Member
    I updated my RapidMiner dependency to the current version (9.7.0) and optimized my code:
            RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
    String path = Calculator.class.getClassLoader().getResource("repository").getPath(); //path to folder repository
    System.setProperty("rapidminer.home", path);
    RapidMiner.init();
    RepositoryManager repositoryManager = RepositoryManager.getInstance(null);
    String repositoryName = "repository";

    try {
    Optional<Repository> repoOpt = repositoryManager.getRepositories()
    .stream()
    .filter(repo -> Objects.equals(repo.getName(), repositoryName))
    .findFirst();
    if (repoOpt.isEmpty()) {
    repositoryManager.addRepository(new LocalRepository(repositoryName, new File(path)));
    }

    } catch (RepositoryException e1) {
    e1.printStackTrace();
    }

    // loads process from file
    Repository repo = repositoryManager.getRepository("repository");
    RepositoryProcessLocation processLocation = new RepositoryProcessLocation(new RepositoryLocation(repo.getLocation(), "model_neuralnet"));
    Process process = processLocation.load(null);

    // declare operators
    Operator read_csv = process.getOperator("Read CSV");
    read_csv.setParameter(com.rapidminer.operator.nio.CSVExampleSource.PARAMETER_CSV_FILE, path + "input.csv");
    Operator retrieve = process.getOperator("Retrieve");
    retrieve.setParameter(RepositorySource.PARAMETER_REPOSITORY_ENTRY, "cross_validation_model_neural_net9");

    // start process
    IOContainer ioResult = process.run();

    ExampleSet resultSet = null;
    if (ioResult.getElementAt(0) instanceof ExampleSet) {
    resultSet = (ExampleSet) ioResult.getElementAt(0);
    }
    if (resultSet == null) {
    throw new IllegalArgumentException("Error while calculating the price");
    }
    return resultSet.toString();
    }
    I still can't get any output due to this error:

    Caused by: com.rapidminer.operator.UserError: Cannot retrieve repository data from entry 'cross_validation_model_neural_net9'. Reason: Cannot load data from 'C:\Users\wahl\price-calculator\target\classes\repository\cross_validation_model_neural_net9.ioo': java.lang.ClassCastException: class com.rapidminer.operator.learner.functions.neuralnet.ImprovedNeuralNetModel cannot be cast to class com.rapidminer.operator.IOObject (com.rapidminer.operator.learner.functions.neuralnet.ImprovedNeuralNetModel is in unnamed module of loader 'app'; com.rapidminer.operator.IOObject is in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @17b016ac).
    	at com.rapidminer.operator.io.RepositorySource.read(RepositorySource.java:187)
    	at com.rapidminer.operator.io.AbstractReader.doWork(AbstractReader.java:271)
    	at com.rapidminer.operator.Operator.execute(Operator.java:1022)
    	at com.rapidminer.operator.execution.SimpleUnitExecutor.execute(SimpleUnitExecutor.java:77)
    	at com.rapidminer.operator.ExecutionUnit$2.run(ExecutionUnit.java:812)
    	at com.rapidminer.operator.ExecutionUnit$2.run(ExecutionUnit.java:807)
    	at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
    	at com.rapidminer.operator.ExecutionUnit.execute(ExecutionUnit.java:807)
    	at com.rapidminer.operator.OperatorChain.doWork(OperatorChain.java:423)
    	at com.rapidminer.operator.Operator.execute(Operator.java:1022)
    	at com.rapidminer.Process.executeRoot(Process.java:1464)
    	at com.rapidminer.Process.lambda$executeRootInPool$5(Process.java:1443)
    	at com.rapidminer.studio.concurrency.internal.AbstractConcurrencyContext$AdaptedCallable.exec(AbstractConcurrencyContext.java:328)
    	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
    	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016)
    	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665)
    	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598)
    	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
    Caused by: com.rapidminer.repository.RepositoryException: Cannot load data from 'C:\Users\wahl\price-calculator\target\classes\repository\cross_validation_model_neural_net9.ioo': java.lang.ClassCastException: class com.rapidminer.operator.learner.functions.neuralnet.ImprovedNeuralNetModel cannot be cast to class com.rapidminer.operator.IOObject (com.rapidminer.operator.learner.functions.neuralnet.ImprovedNeuralNetModel is in unnamed module of loader 'app'; com.rapidminer.operator.IOObject is in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @17b016ac)
    	at com.rapidminer.repository.local.SimpleIOObjectEntry.retrieveData(SimpleIOObjectEntry.java:103)
    	at com.rapidminer.operator.io.RepositorySource.read(RepositorySource.java:182)
    	... 17 more
    Caused by: java.lang.ClassCastException: class com.rapidminer.operator.learner.functions.neuralnet.ImprovedNeuralNetModel cannot be cast to class com.rapidminer.operator.IOObject (com.rapidminer.operator.learner.functions.neuralnet.ImprovedNeuralNetModel is in unnamed module of loader 'app'; com.rapidminer.operator.IOObject is in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @17b016ac)
    	at com.rapidminer.repository.local.SimpleIOObjectEntry.readDataFromFile(SimpleIOObjectEntry.java:119)
    	at com.rapidminer.repository.local.SimpleIOObjectEntry.retrieveData(SimpleIOObjectEntry.java:101)
    	... 18 more

  • MartinLiebig
    MartinLiebig
    Altair Employee
    Hi,
    why not just use an RTS for that?
    Best,
    Martin