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