潘志宝
9 天以前 9d5be382e52f9ac57199d5ef75cc23f925a4cdb0
提交 | 用户 | 时间
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
27         PyObject* pModule = create_py_module(strValue);
28         /*PyObject* pModule = create_py_module("${pyModule}.${pyName}");*/
449017 29         if (pModule == NULL)
D 30         {
31             cout << "model error" << endl;
32             throw "模块调用失败!";
33         }
34
35         //*************导入函数******************
36         PyObject* pFunc = PyObject_GetAttrString(pModule, "${entity.methodName}");
37         if (pFunc == NULL)
38         {
39             cout << "func error" << endl;
40             throw "函数调用失败!";
41         }
42 #{foreach} ($column in [1..$entity.dataLength])
43         PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column});
44 #{end}
45 #{if}($entity.model==1)
46         PyObject* model_path = ConvertJhmapToPydict(env, model_path_);
47 #{end}
48         PyObject* settings_1 = ConvertJhmapToPydict(env, settings);
49
50
51         //************构造参数数据 ********************
52 #{if}($entity.model==1)
53 #set($a=$entity.dataLength + 2)
54         PyObject* pArg = PyTuple_New(${a});
55 #{else}
56 #set($a=$entity.dataLength + 1)
57         PyObject* pArg = PyTuple_New(${a});
58 #{end}
59 #{foreach} ($column in [1..$entity.dataLength])
60 #set($a=$column - 1)
61         PyTuple_SetItem(pArg, ${a}, data_${column});
62 #{end}
63 #{if}($entity.model==1)
64         PyTuple_SetItem(pArg, ${entity.dataLength}, model_path);
65 #set($a=$entity.dataLength + 1)
66         PyTuple_SetItem(pArg, ${a}, settings_1);
67 #{else}
68         PyTuple_SetItem(pArg, ${entity.dataLength}, settings_1);
69 #{end}
70
71         /*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/
72
73
74         //********************调用python方法计算*****************
75         PyObject* pReturn = PyEval_CallObject(pFunc, pArg);
76         if (pReturn == NULL)
77         {
78             cout << "return error" << endl;
79             throw "函数返回值异常!";
80         }
81         //*******************返回数据封装************************
82        // 开辟返回数据内存空间,转换数据,拆分数据
83
84         /*jobject result = jhmappydict_(env, pReturn);*/
85         jobject result = ConvertPydictToJhmap(env, pReturn);
86         if (result == NULL)
87         {
88             cout << "ConvertPydictToJhmap error" << endl;
89             throw "函数返回值异常!";
90         }
c7009e 91         /*delete pModule;
449017 92         pModule = nullptr;
D 93         delete pModule;
94
95         delete pFunc;
96         pFunc = nullptr;
97         delete pFunc;
98
99 #{foreach} ($column in [1..$entity.dataLength])
100         delete[] data_${column};
101         data_${column} = nullptr;
102         delete data_${column};
103
104 #{end}
105 #{if}($entity.model==1)
106         delete model_path;
107         model_path = nullptr;
108         delete model_path;
109 #{end}
110
111         delete settings_1;
112         settings_1 = nullptr;
113         delete settings_1;
114
115         delete[] pArg;
116         pArg = nullptr;
207486 117         delete pArg;
D 118
119         Py_DECREF(pReturn);
120         Py_DECREF(pFunc);
121         Py_DECREF(pModule);
122         Py_CLEAR(pModule);*/
449017 123
D 124         return result;
125     }
126     catch (const char* msg)
127     {
128         cout << "${entity.methodName} error" << endl;
129         return NULL;
130     }
131 }
132
133 #{end}