-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsrule.cpp
More file actions
184 lines (167 loc) · 5.09 KB
/
jsrule.cpp
File metadata and controls
184 lines (167 loc) · 5.09 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @file classrecord.h
* @brief 班级记录类
* @author howdy213
* @date 2026-3-1
* @version 1.3.0
*
* Copyright (C) 2025-2026 howdy213
*
* This file is part of QuantifyPlugin.
*
* QuantifyPlugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QuantifyPlugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include "jsrule.h"
///
/// \brief JSRule::JSRule
/// \param parent
///
JSRule::JSRule(QObject *parent) : QObject(parent) {
m_engine = new QJSEngine(this);
}
///
/// \brief JSRule::~JSRule
///
JSRule::~JSRule() {}
///
/// \brief JSRule::loadFromFile
/// \param content
/// \return
///
bool JSRule::loadFromFile(const QString &content) {
QJSValue obj = m_engine->evaluate(content);
if (!obj.isObject()) {
qWarning() << "JS rule must return an object";
return false;
}
QJSValue reasonVal = obj.property("reason");
if (!reasonVal.isString()) {
qWarning() << "Missing 'reason' property";
return false;
}
m_reason = reasonVal.toString();
QJSValue chVal = obj.property("reason_ch");
m_reason_ch = chVal.isString() ? chVal.toString() : m_reason;
m_dailyFunc = obj.property("daily");
m_weeklyFunc = obj.property("weekly");
m_termlyFunc = obj.property("termly");
if (!m_dailyFunc.isCallable() && !m_weeklyFunc.isCallable() &&
!m_termlyFunc.isCallable()) {
qWarning() << "No callable function found";
return false;
}
return true;
}
///
/// \brief JSRule::result
/// \param rt
/// \param rec
/// \return
///
Record JSRule::result(RecordType rt, const Record &rec) const {
QJSValue func;
switch (rt) {
case DAILY:
func = m_dailyFunc;
break;
case WEEKLY:
func = m_weeklyFunc;
break;
case TERMLY:
func = m_termlyFunc;
break;
default:
return rec;
}
if (!func.isCallable())
return rec;
QJSValue jsRec = recordToJS(rec);
QJSValue res = func.call({jsRec});
if (res.isError()) {
qWarning() << "JS error:" << res.toString();
return rec;
}
return recordFromJS(res.isObject() ? res : jsRec);
}
///
/// \brief JSRule::recordToJS
/// \param rec
/// \return
///
QJSValue JSRule::recordToJS(const Record &rec) const {
QJSValue obj = m_engine->newObject();
obj.setProperty("t1", rec.t1);
obj.setProperty("t2", rec.t2);
obj.setProperty("t3", rec.t3);
obj.setProperty("t", 0);
obj.setProperty("s1", rec.s1);
obj.setProperty("s2", rec.s2);
obj.setProperty("s3", rec.s3);
obj.setProperty("s", 0);
return obj;
}
///
/// \brief JSRule::recordFromJS
/// \param obj
/// \return
///
Record JSRule::recordFromJS(const QJSValue &obj) const {
Record r;
r.t1 = obj.property("t1").toInt();
r.t2 = obj.property("t2").toInt();
r.t3 = obj.property("t3").toInt();
r.s1 = obj.property("s1").toNumber();
r.s2 = obj.property("s2").toNumber();
r.s3 = obj.property("s3").toNumber();
r = setVariable(r, "t", obj.property("t").toInt());
r = setVariable(r, "s", obj.property("s").toNumber());
return r;
}
///
/// \brief JSRule::isRuleValid
/// \param rule
/// \return
///
CheckResult JSRule::isRuleValid(const QString &rule) {
QJSEngine engine;
QJSValue result = engine.evaluate(rule);
if (result.isError()) {
QString errorMsg = result.toString();
int line = -1;
// 错误对象通常包含 lineNumber 和 columnNumber 属性
if (result.hasProperty("lineNumber")) {
line = result.property("lineNumber").toInt();
return {
false,
QString("JavaScript 语法错误 (行 %1): %2").arg(line).arg(errorMsg)};
} else {
return {false, QString("JavaScript 语法错误: %1").arg(errorMsg)};
}
}
if (!result.isObject())
return {false, "规则必须返回一个对象 (使用 ({ ... }) 包裹)"};
QJSValue reason = result.property("reason");
if (!reason.isString())
return {false, "缺少必需的字符串属性 'reason' 或类型错误"};
QJSValue reasonCh = result.property("reason_ch");
if (!reasonCh.isUndefined() && !reasonCh.isString())
return {false, "属性 'reason_ch' 如果存在,必须是字符串"};
QJSValue daily = result.property("daily");
QJSValue weekly = result.property("weekly");
QJSValue termly = result.property("termly");
if (!daily.isCallable() && !weekly.isCallable() && !termly.isCallable())
return {false, "至少需要提供一个可调用的函数:daily、weekly 或 termly"};
return {true, "JavaScript 规则有效"};
}