潘志宝
3 天以前 292674a4bd5146a816ad59e711f3661dab7502fd
提交 | 用户 | 时间
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         }
074eb5 97         cout << "clear_py_memory" << endl;
D 98         Py_XDECREF(pFunc);
449017 99 #{foreach} ($column in [1..$entity.dataLength])
074eb5 100         Py_XDECREF(data_${column});
449017 101 #{end}
D 102 #{if}($entity.model==1)
074eb5 103         Py_XDECREF(model_path);
449017 104 #{end}
074eb5 105         Py_XDECREF(settings_1);
D 106         Py_XDECREF(pArg);
107         Py_XDECREF(pReturn);
449017 108         return result;
D 109     }
110     catch (const char* msg)
111     {
112         cout << "${entity.methodName} error" << endl;
113         return NULL;
114     }
115 }
116
117 #{end}