\#include "stdafx.h" \#include "pch.h" \#include "${pyName}.h" \#include \#include "pyutils.h" \#include "jni.h" \#include "Python.h" \#include "PyGILThreadLock.h" \#include "convertutils.h" using namespace std; #{foreach} ($entity in $modelMethods) jobject ${pyName}_${entity.methodName}(JNIEnv* env, #{foreach} ($column in [1..$entity.dataLength])jobjectArray data${column}, #{end}#{if}($entity.model==1)jobject model_path_, #{end}jobject settings) { try { PyGILThreadLock lock; jclass hashmapClass = env->FindClass("java/util/HashMap"); jmethodID getMID = env->GetMethodID(hashmapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); jstring keyJString = env->NewStringUTF("pyFile"); jobject javaValueObj = env->CallObjectMethod(settings, getMID, keyJString); const char* strValue = env->GetStringUTFChars((jstring)javaValueObj, NULL); PyObject* pModule = create_py_module(strValue); /*PyObject* pModule = create_py_module("${pyModule}.${pyName}");*/ if (pModule == NULL) { cout << "model error" << endl; throw "模块调用失败!"; } //*************导入函数****************** PyObject* pFunc = PyObject_GetAttrString(pModule, "${entity.methodName}"); if (pFunc == NULL) { cout << "func error" << endl; throw "函数调用失败!"; } #{foreach} ($column in [1..$entity.dataLength]) PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column}); #{end} #{if}($entity.model==1) PyObject* model_path = ConvertJhmapToPydict(env, model_path_); #{end} PyObject* settings_1 = ConvertJhmapToPydict(env, settings); //************构造参数数据 ******************** #{if}($entity.model==1) #set($a=$entity.dataLength + 2) PyObject* pArg = PyTuple_New(${a}); #{else} #set($a=$entity.dataLength + 1) PyObject* pArg = PyTuple_New(${a}); #{end} #{foreach} ($column in [1..$entity.dataLength]) #set($a=$column - 1) PyTuple_SetItem(pArg, ${a}, data_${column}); #{end} #{if}($entity.model==1) PyTuple_SetItem(pArg, ${entity.dataLength}, model_path); #set($a=$entity.dataLength + 1) PyTuple_SetItem(pArg, ${a}, settings_1); #{else} PyTuple_SetItem(pArg, ${entity.dataLength}, settings_1); #{end} /*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/ //********************调用python方法计算***************** PyObject* pReturn = PyEval_CallObject(pFunc, pArg); if (pReturn == NULL) { cout << "return error" << endl; throw "函数返回值异常!"; } //*******************返回数据封装************************ // 开辟返回数据内存空间,转换数据,拆分数据 /*jobject result = jhmappydict_(env, pReturn);*/ jobject result = ConvertPydictToJhmap(env, pReturn); if (result == NULL) { cout << "ConvertPydictToJhmap error" << endl; throw "函数返回值异常!"; } /*delete pModule; pModule = nullptr; delete pModule; delete pFunc; pFunc = nullptr; delete pFunc; #{foreach} ($column in [1..$entity.dataLength]) delete[] data_${column}; data_${column} = nullptr; delete data_${column}; #{end} #{if}($entity.model==1) delete model_path; model_path = nullptr; delete model_path; #{end} delete settings_1; settings_1 = nullptr; delete settings_1; delete[] pArg; pArg = nullptr; delete pArg; Py_DECREF(pReturn); Py_DECREF(pFunc); Py_DECREF(pModule); Py_CLEAR(pModule);*/ return result; } catch (const char* msg) { cout << "${entity.methodName} error" << endl; return NULL; } } #{end}