潘志宝
2024-12-10 a440ec3bfaa1363f5841100b8948d852971a2eb1
提交 | 用户 | 时间
449017 1 \#include "stdafx.h"
D 2 \#include "pch.h"
3 \#include "${pyName}.h"
4 \#include <string>
5 \#include "pyutils.h"
6 \#include "jni.h"
7 \#include "Python.h"
8 \#include "PyGILThreadLock.h"
9 \#include "convertutils.h"
10
11 using namespace std;
12
13 #{foreach} ($entity in $modelMethods)
14 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)
15 {
16     try
17     {
18
19         PyGILThreadLock lock;
0425a3 20
D 21         jclass hashmapClass = env->FindClass("java/util/HashMap");
22         jmethodID getMID = env->GetMethodID(hashmapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
23         jstring keyJString = env->NewStringUTF("pyFile");
24         jobject javaValueObj = env->CallObjectMethod(settings, getMID, keyJString);
25         const char* strValue = env->GetStringUTFChars((jstring)javaValueObj, NULL);
26         cout << strValue << endl;
27
28         PyObject* pModule = create_py_module(strValue);
29         /*PyObject* pModule = create_py_module("${pyModule}.${pyName}");*/
449017 30         if (pModule == NULL)
D 31         {
32             cout << "model error" << endl;
33             throw "模块调用失败!";
34         }
35
36         //*************导入函数******************
37         PyObject* pFunc = PyObject_GetAttrString(pModule, "${entity.methodName}");
38         if (pFunc == NULL)
39         {
40             cout << "func error" << endl;
41             throw "函数调用失败!";
42         }
43 #{foreach} ($column in [1..$entity.dataLength])
44         PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column});
45 #{end}
46 #{if}($entity.model==1)
47         PyObject* model_path = ConvertJhmapToPydict(env, model_path_);
48 #{end}
49         PyObject* settings_1 = ConvertJhmapToPydict(env, settings);
50
51
52         //************构造参数数据 ********************
53 #{if}($entity.model==1)
54 #set($a=$entity.dataLength + 2)
55         PyObject* pArg = PyTuple_New(${a});
56 #{else}
57 #set($a=$entity.dataLength + 1)
58         PyObject* pArg = PyTuple_New(${a});
59 #{end}
60 #{foreach} ($column in [1..$entity.dataLength])
61 #set($a=$column - 1)
62         PyTuple_SetItem(pArg, ${a}, data_${column});
63 #{end}
64 #{if}($entity.model==1)
65         PyTuple_SetItem(pArg, ${entity.dataLength}, model_path);
66 #set($a=$entity.dataLength + 1)
67         PyTuple_SetItem(pArg, ${a}, settings_1);
68 #{else}
69         PyTuple_SetItem(pArg, ${entity.dataLength}, settings_1);
70 #{end}
71
72         /*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/
73
74
75         //********************调用python方法计算*****************
76         PyObject* pReturn = PyEval_CallObject(pFunc, pArg);
77         if (pReturn == NULL)
78         {
79             cout << "return error" << endl;
80             throw "函数返回值异常!";
81         }
82         //*******************返回数据封装************************
83        // 开辟返回数据内存空间,转换数据,拆分数据
84
85         /*jobject result = jhmappydict_(env, pReturn);*/
86         jobject result = ConvertPydictToJhmap(env, pReturn);
87         if (result == NULL)
88         {
89             cout << "ConvertPydictToJhmap error" << endl;
90             throw "函数返回值异常!";
91         }
c7009e 92         /*delete pModule;
449017 93         pModule = nullptr;
D 94         delete pModule;
95
96         delete pFunc;
97         pFunc = nullptr;
98         delete pFunc;
99
100 #{foreach} ($column in [1..$entity.dataLength])
101         delete[] data_${column};
102         data_${column} = nullptr;
103         delete data_${column};
104
105 #{end}
106 #{if}($entity.model==1)
107         delete model_path;
108         model_path = nullptr;
109         delete model_path;
110 #{end}
111
112         delete settings_1;
113         settings_1 = nullptr;
114         delete settings_1;
115
116         delete[] pArg;
117         pArg = nullptr;
207486 118         delete pArg;
D 119
120         Py_DECREF(pReturn);
121         Py_DECREF(pFunc);
122         Py_DECREF(pModule);
123         Py_CLEAR(pModule);*/
449017 124
D 125         return result;
126     }
127     catch (const char* msg)
128     {
129         cout << "${entity.methodName} error" << endl;
130         return NULL;
131     }
132 }
133
134 #{end}