[JAVA] Invoking evaluate Method from PMML Jar

Hi guys,

 

I am new to Knime and its forum and i am not sure if this is the correct section for my request, if not please move. :)

 

I need to invoke a method from a jar file that has been created by the "Compiled Model to Jar Writer"-node. The problem now is just I dont know the correct signature of this method. Is there any (hidden) documentation available for this node? I need to know the exact signature of the MainModel.evaluate-Method.

Concerning the Dokumentation I found (Within the Analytics Platform), the signature should be something like this:

Object[] MainModel.evaluate(Object[] InputFields)

But if I try to invoke this Method with this Parameter Eclipse comes up with a IllegalArgumentException: wrong number of arguments.

//the relevant code...

//sortedInputs contains the 5 column names of the example dataset "iris.csv"
Object[] sortedInputs = new Object[inputFieldUnsorted.length];

	int index = 0;
//this loop sorts the input parameters 
	for(int i= 0; i<inputFieldUnsorted.length;i++){
		Method mEva2 = cls.getMethod("getInputFieldIndex",params);
		index = (int) mEva2.invoke(MainModelObj, inputFieldUnsorted[i]);
		sortedInputs[index] = inputFieldUnsorted[i];
	}	

Method mEvaluate= cls.getMethod("evaluate", Object[].class);
	Object[] evalOutputs = new Object[dim];
	evalOutputs = (Object[]) mEvaluate.invoke(sortedInputs);
	cl.close();

I debugged the code above, and it seems to work, so the only problem for me is to figure out what Parameters the method needs. The documentation is not that helpful :(

 

Thanks for your help in advance.

 

Greetings

Have you seen this documentation (from here)? It tells you, you need an object to call the method on, which you forget to add to the invoke method. (I have not checked, but the evaluate method might be static, which case the first parameter (before sortedInputs) can be null, but should be present.)

Cheers, gabor

Hi Gabor,

thanks for your reply. Yes, indeed this parameter is missing, thanks for this advice. I added the required Object parameter now but it still doesnt work. :(

//reading the jar file from directory
	File file  = new File("C:\\Knime\\ClusterJar");
	try{
	URL url = file.toURI().toURL();
	URL[] urls = new URL[]{url};
	URLClassLoader cl = new URLClassLoader(urls);
//instantiate the classfile 
	Class<?> cls = cl.loadClass("MainModel");
	
	Constructor<?> constructor = cls.getConstructor();
	Object MainModelObj = constructor.newInstance();
/*invoking the evaluate method. concerning the documentation the method u need to call
in order to use the model inside the jarfile*/
	Method mEvaluate= cls.getMethod("evaluate", Object[].class);
/*allocating an array for the outputs of the evaluate method*/
	Object[] evalOutputs = new Object[dim];
	evalOutputs = (Object[]) mEvaluate.invoke(MainModelObj, sortedInputs);

It still throws the same Exception and the same line. (In line 17, where I invoke the mEvaluate instance)

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at mayato.MyJarImport.main(MyJarImport.java:62)

EDIT:

I had a closer look now to the method jdoc concerning the IllegalArgumentException and I think I can eliminate the first two bullet points... But I am not exactly sure how I can check the last two in my program ...

IllegalArgumentException -

if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof);

if the number of actual and formal parameters differ;

if an unwrapping conversion for primitive arguments fails;

or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.

Still hoping for some help. :(

 

greetings

*push it* come on guys please :(