潘志宝
2024-09-30 b6ab595569e66c1410c598129440e7761d161784
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
118
119
120
\#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;
        PyObject* pModule = create_py_module("${pyModule}.${pyName}");
        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 "函数返回值异常!";
        }
        /*delete pModule;
        pModule = nullptr;
        delete pModule;
 
        delete pFunc;
        pFunc = nullptr;
        delete pFunc;
 
#{foreach} ($column in [1..$entity.dataLength])
        delete[] data_${column};
        data_${column} = nullptr;
        delete data_${column};
 
#{end}
#{if}($entity.model==1)
        delete model_path;
        model_path = nullptr;
        delete model_path;
#{end}
 
        delete settings_1;
        settings_1 = nullptr;
        delete settings_1;
 
        delete[] pArg;
        pArg = nullptr;
        delete pArg;*/
 
        return result;
    }
    catch (const char* msg)
    {
        cout << "${entity.methodName} error" << endl;
        return NULL;
    }
}
 
#{end}