\#include "stdafx.h" \#include "pch.h" \#include \#include "pyutils.h" \#include "jni.h" \#include "Python.h" \#include "PyGILThreadLock.h" \#include "convertutils.h" using namespace std; #{foreach} ($entity in $entities) jobject ${entity.pyName}_train(JNIEnv* env, jobjectArray data_train, jobject settings) { try { PyGILThreadLock lock; PyObject* pModule = create_py_module("${entity.pyModule}.${entity.pyName}"); if (pModule == NULL) { cout << "pModule error" << endl; throw "模块调用失败!"; } //*************导入函数****************** PyObject* pFunc = PyObject_GetAttrString(pModule, "train"); if (pFunc == NULL) { cout << "pfunc error" << endl; throw "函数调用失败!"; } PyObject* data_1 = ConvertJdarrToPydarr(env, data_train); PyObject* settings_ = ConvertJhmapToPydict(env, settings); //************构造参数数据 ******************** PyObject* pArg = PyTuple_New(2); PyTuple_SetItem(pArg, 0, data_1); PyTuple_SetItem(pArg, 1, settings_); /*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/ //********************调用python方法计算***************** PyObject* pReturn = PyEval_CallObject(pFunc, pArg); if (pReturn == NULL) { cout << "pReturn error" << endl; throw "函数返回值异常!"; } //*******************返回数据封装************************ // 开辟返回数据内存空间,转换数据,拆分数据 /*jobject result = jhmappydict_(env, pReturn);*/ jobject result = ConvertPydictToJhmap(env, pReturn); if (result == NULL) { cout << "ConvertPydictToJhmap error" << endl; throw "函数返回值异常!"; } delete pModule, pFunc, data_1, settings_, pArg; return result; } catch (const char* msg) { cout << "train error" << endl; return NULL; } } jobject ${entity.pyName}_predict(JNIEnv* env, #{foreach} ($column in [1..$entity.dataLength])jobjectArray data${column}, #{end}jobject model_path_,jobject settings) { try { PyGILThreadLock lock; PyObject* pModule = create_py_module("${entity.pyModule}.${entity.pyName}"); if (pModule == NULL) { cout << "model error" << endl; throw "模块调用失败!"; } //*************导入函数****************** PyObject* pFunc = PyObject_GetAttrString(pModule, "predict"); if (pFunc == NULL) { cout << "func error" << endl; throw "函数调用失败!"; } #{foreach} ($column in [1..$entity.dataLength]) PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column}); #{end} PyObject* model_path = ConvertJhmapToPydict(env, model_path_); PyObject* settings_1 = ConvertJhmapToPydict(env, settings); //************构造参数数据 ******************** #set($a=$entity.dataLength + 2) PyObject* pArg = PyTuple_New(${a}); #{foreach} ($column in [1..$entity.dataLength]) #set($a=$column - 1) PyTuple_SetItem(pArg, ${a}, data_${column}); #{end} PyTuple_SetItem(pArg, ${entity.dataLength}, model_path); #set($a=$entity.dataLength + 1) PyTuple_SetItem(pArg, ${a}, settings_1); /*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} delete model_path; model_path = nullptr; delete model_path; delete settings_1; settings_1 = nullptr; delete settings_1; delete[] pArg; pArg = nullptr; delete pArg; return result; } catch (const char* msg) { cout << "predict error" << endl; return NULL; } } #{end}