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