RapidMiner 9.8 Beta is now available

Be one of the first to get your hands on the new features. More details and downloads here:

GET RAPIDMINER 9.8 BETA

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

marjawmarjaw Member Posts: 2 Learner I
edited September 21 in Help
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: 
public String calculatePrice(Material materialToMatch) throws OperatorException, RepositoryException, IOException, XMLException {
RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
URL path = Calculator.class.getClassLoader().getResource("repository");
System.setProperty("rapidminer.home", path.toString());
RapidMiner.init();

RepositoryManager repositoryManager = RepositoryManager.getInstance(null);

String absolutePath = null;
if (repositoryManager.getRepositories().get(2) instanceof LocalRepository) {
LocalRepository localRepository = (LocalRepository) repositoryManager.getRepositories().get(2);
File location = localRepository.getFile();
absolutePath = location.getAbsolutePath().replace("\\", "/");
}
assert absolutePath != null;
RepositoryLocation pLoc = new RepositoryLocation("//Repository/processes/model_neuralnet");
ProcessEntry pEntry = (ProcessEntry) pLoc.locateEntry();
String processXML = pEntry.retrieveXML();
Process process = new Process(processXML);
process.setProcessLocation(new RepositoryProcessLocation(pLoc));

Operator read_csv = process.getOperator("Read CSV");
read_csv.setParameter(PARAMETER_CSV_FILE, absolutePath + "/data/input.csv");
Operator retrieve = process.getOperator("Retrieve");
retrieve.setParameter(PARAMETER_REPOSITORY_ENTRY, "/models/cross_validation_model_neural_net9");

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();
}
My repository-folder is structured like this:
repository 
 -data
       -input.csv
 -models
       -cross_validation_model_neural_net9.ioo
 -processes
       -model_neuralnet.rmp

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:

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

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>
Tagged:

Answers

  • marjawmarjaw Member Posts: 2 Learner I
    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

  • mschmitzmschmitz Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 2,636  RM Data Scientist
    Hi,
    why not just use an RTS for that?
    Best,
    Martin
    - Head of Data Science Services at RapidMiner -
    Dortmund, Germany
Sign In or Register to comment.