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