GeezerVison is showing github.com — fix · toolgood/ToolGood.Algorithm@77ecbbc
File treeExpand file treeCollapse file tree Open diff view settings ToolGood.Algorithm.Test/CalculationLogic ToolGood.Algorithm/CalculationLogic
java/toolgood.algorithm/src main/java/toolgood/algorithm/calculationlogic test/java/toolgood/algorithm/calculationlogic
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: JavaScript/src/CalculationLogic/CalculationLogicEngine.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { AlgorithmEngineEx } from '../AlgorithmEngineEx.js'; | 2 | 2 | import { AlgorithmEngineHelper } from '../AlgorithmEngineHelper.js'; | 3 | 3 | import { OperandType } from '../Enums/OperandType.js'; | | 4 | +import { MyDate } from '../Internals/MyDate.js'; | 4 | 5 | import { CalculationLogicType } from './CalculationLogicType.js'; | 5 | 6 | import { CalculationLogicInfo } from './CalculationLogicInfo.js'; | 6 | 7 |
| @@ -46,6 +47,7 @@ export class CalculationLogicEngine { | 46 | 47 | */ | 47 | 48 | InitValue(key, value, layer = 0, remark = null) { | 48 | 49 | if (typeof layer === 'string') { remark = layer; layer = 0; } | | 50 | + if (value instanceof Date) { value = new MyDate(value); } | 49 | 51 | this._engine.AddParameter(key, value); | 50 | 52 | if (this._useCalculationLogicInfo) { | 51 | 53 | this._initValueInfos.push(new CalculationLogicInfo({ | @@ -161,6 +163,7 @@ export class CalculationLogicEngine { | 161 | 163 | */ | 162 | 164 | SetValue(key, value, layer = 0, remark = null) { | 163 | 165 | if (typeof layer === 'string') { remark = layer; layer = 0; } | | 166 | + if (value instanceof Date) { value = new MyDate(value); } | 164 | 167 | this._engine.AddParameter(key, value); | 165 | 168 | if (this._useCalculationLogicInfo) { | 166 | 169 | this._calculationLogicInfos.push(new CalculationLogicInfo({ | @@ -209,12 +212,14 @@ export class CalculationLogicEngine { | 209 | 212 | _initExp(value) { | 210 | 213 | if (typeof value === 'string') return value; | 211 | 214 | if (typeof value === 'boolean') return value ? 'True' : 'False'; | | 215 | + if (value instanceof MyDate) return '"' + value.toString() + '"'; | 212 | 216 | return String(value); | 213 | 217 | } | 214 | 218 |
| 215 | 219 | _valueExp(value) { | 216 | 220 | if (typeof value === 'string') return '"' + value.replace(/"/g, '\\"') + '"'; | 217 | 221 | if (typeof value === 'boolean') return value ? 'True' : 'False'; | | 222 | + if (value instanceof MyDate) return '"' + value.toString() + '"'; | 218 | 223 | return String(value); | 219 | 224 | } | 220 | 225 |
| |
Collapse file Copy file name to clipboardExpand all lines: JavaScript/tests/CalculationLogicTest.js +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,27 @@ function testInitValue_CanBeUsedInCondition() { | 71 | 71 | assert.strictEqual(logic.CheckCondition('a>b'), false); | 72 | 72 | } | 73 | 73 |
| | 74 | +function testInitValue_DateTime() { | | 75 | + const logic = createEngine(); | | 76 | + logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0)); | | 77 | + assert.strictEqual(logic.ToInfoString(), '[初始] dt="2026-08-31 12:00:00";\n'); | | 78 | +} | | 79 | + | | 80 | +function testInitValue_DateTime_LayerRemark() { | | 81 | + // InitValue 的层级与备注不参与信息输出 | | 82 | + const logic = createEngine(); | | 83 | + logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0), 2, '备注'); | | 84 | + assert.strictEqual(logic.ToInfoString(), '[初始] dt="2026-08-31 12:00:00";\n'); | | 85 | +} | | 86 | + | | 87 | +function testInitValue_DateTime_CanBeUsedInCondition() { | | 88 | + const logic = createEngine(); | | 89 | + logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0)); | | 90 | + assert.strictEqual(logic.CheckCondition('YEAR(dt)=2026'), true); | | 91 | + assert.strictEqual(logic.CheckCondition('MONTH(dt)=8'), true); | | 92 | + assert.strictEqual(logic.CheckCondition("dt>DATEVALUE('2026-08-30')"), true); | | 93 | +} | | 94 | + | 74 | 95 | // #endregion | 75 | 96 |
| 76 | 97 | // #region CheckCondition 测试 | @@ -219,6 +240,26 @@ function testSetValue_LayerRemark() { | 219 | 240 | assert.strictEqual(logic.ToInfoString(), '[赋值] i = 100 // 备注\n'); | 220 | 241 | } | 221 | 242 |
| | 243 | +function testSetValue_DateTime() { | | 244 | + const logic = createEngine(); | | 245 | + logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0)); | | 246 | + assert.strictEqual(logic.ToInfoString(), '[赋值] dt = "2026-08-31 12:00:00"\n'); | | 247 | +} | | 248 | + | | 249 | +function testSetValue_DateTime_LayerRemark() { | | 250 | + const logic = createEngine(); | | 251 | + logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0), 1, '备注'); | | 252 | + assert.strictEqual(logic.ToInfoString(), '[赋值] dt = "2026-08-31 12:00:00" // 备注\n'); | | 253 | +} | | 254 | + | | 255 | +function testSetValue_DateTime_CanBeUsedInCondition() { | | 256 | + const logic = createEngine(); | | 257 | + logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0)); | | 258 | + assert.strictEqual(logic.CheckCondition('YEAR(dt)=2026'), true); | | 259 | + assert.strictEqual(logic.CheckCondition('MONTH(dt)=8'), true); | | 260 | + assert.strictEqual(logic.CheckCondition("dt>DATEVALUE('2026-08-30')"), true); | | 261 | +} | | 262 | + | 222 | 263 | // #endregion | 223 | 264 |
| 224 | 265 | // #region GetValue 测试 | @@ -267,6 +308,24 @@ function testGetValue_InitValue_Bool() { | 267 | 308 | assert.strictEqual(operand.BooleanValue, true); | 268 | 309 | } | 269 | 310 |
| | 311 | +function testGetValue_InitValue_DateTime() { | | 312 | + const logic = createEngine(); | | 313 | + logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0)); | | 314 | + const operand = logic.GetValue('dt'); | | 315 | + assert.strictEqual(operand.IsDate, true); | | 316 | + assert.strictEqual(operand.Type, OperandType.DATE); | | 317 | + assert.strictEqual(operand.DateValue.toString(), '2026-08-31 12:00:00'); | | 318 | +} | | 319 | + | | 320 | +function testGetValue_SetValue_DateTime() { | | 321 | + const logic = createEngine(); | | 322 | + logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0)); | | 323 | + const operand = logic.GetValue('dt'); | | 324 | + assert.strictEqual(operand.IsDate, true); | | 325 | + assert.strictEqual(operand.Type, OperandType.DATE); | | 326 | + assert.strictEqual(operand.DateValue.toString(), '2026-08-31 12:00:00'); | | 327 | +} | | 328 | + | 270 | 329 | function testGetValue_SetValue() { | 271 | 330 | const logic = createEngine(); | 272 | 331 | logic.SetValue('v', 'text'); | @@ -383,6 +442,9 @@ function runAllTests() { | 383 | 442 | testInitValue_Bool(); | 384 | 443 | testInitValue_LayerRemark(); | 385 | 444 | testInitValue_CanBeUsedInCondition(); | | 445 | + testInitValue_DateTime(); | | 446 | + testInitValue_DateTime_LayerRemark(); | | 447 | + testInitValue_DateTime_CanBeUsedInCondition(); | 386 | 448 | testCheckCondition_True(); | 387 | 449 | testCheckCondition_False(); | 388 | 450 | testCheckCondition_LayerRemark(); | @@ -399,11 +461,16 @@ function runAllTests() { | 399 | 461 | testSetValue_Double(); | 400 | 462 | testSetValue_Bool(); | 401 | 463 | testSetValue_LayerRemark(); | | 464 | + testSetValue_DateTime(); | | 465 | + testSetValue_DateTime_LayerRemark(); | | 466 | + testSetValue_DateTime_CanBeUsedInCondition(); | 402 | 467 | testGetValue_InitValue_Int(); | 403 | 468 | testGetValue_InitValue_String(); | 404 | 469 | testGetValue_InitValue_Decimal(); | 405 | 470 | testGetValue_InitValue_Double(); | 406 | 471 | testGetValue_InitValue_Bool(); | | 472 | + testGetValue_InitValue_DateTime(); | | 473 | + testGetValue_SetValue_DateTime(); | 407 | 474 | testGetValue_SetValue(); | 408 | 475 | testGetValue_SetFormula(); | 409 | 476 | testGetValue_AfterSetFormulaOverwrite(); | @@ -433,6 +500,9 @@ export { | 433 | 500 | testInitValue_Bool, | 434 | 501 | testInitValue_LayerRemark, | 435 | 502 | testInitValue_CanBeUsedInCondition, | | 503 | + testInitValue_DateTime, | | 504 | + testInitValue_DateTime_LayerRemark, | | 505 | + testInitValue_DateTime_CanBeUsedInCondition, | 436 | 506 | testCheckCondition_True, | 437 | 507 | testCheckCondition_False, | 438 | 508 | testCheckCondition_LayerRemark, | @@ -449,11 +519,16 @@ export { | 449 | 519 | testSetValue_Double, | 450 | 520 | testSetValue_Bool, | 451 | 521 | testSetValue_LayerRemark, | | 522 | + testSetValue_DateTime, | | 523 | + testSetValue_DateTime_LayerRemark, | | 524 | + testSetValue_DateTime_CanBeUsedInCondition, | 452 | 525 | testGetValue_InitValue_Int, | 453 | 526 | testGetValue_InitValue_String, | 454 | 527 | testGetValue_InitValue_Decimal, | 455 | 528 | testGetValue_InitValue_Double, | 456 | 529 | testGetValue_InitValue_Bool, | | 530 | + testGetValue_InitValue_DateTime, | | 531 | + testGetValue_SetValue_DateTime, | 457 | 532 | testGetValue_SetValue, | 458 | 533 | testGetValue_SetFormula, | 459 | 534 | testGetValue_AfterSetFormulaOverwrite, | |
Collapse file Copy file name to clipboardExpand all lines: csharp/ToolGood.Algorithm.Test/CalculationLogic/CalculationLogicTest.cs +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,6 +119,33 @@ public void InitValue_CanBeUsedInCondition_Test() | 119 | 119 | Assert.IsFalse(logic.CheckCondition("a>b")); | 120 | 120 | } | 121 | 121 |
| | 122 | + [Test] | | 123 | + public void InitValue_DateTime_Test() | | 124 | + { | | 125 | + var logic = CreateEngine(); | | 126 | + logic.InitValue("dt", new DateTime(2026, 8, 31, 12, 0, 0)); | | 127 | + Assert.AreEqual("[初始] dt=\"2026-08-31 12:00:00\";\n", NormalizeLine(logic.ToInfoString())); | | 128 | + } | | 129 | + | | 130 | + [Test] | | 131 | + public void InitValue_DateTime_LayerRemark_Test() | | 132 | + { | | 133 | + // InitValue 的层级与备注不参与信息输出 | | 134 | + var logic = CreateEngine(); | | 135 | + logic.InitValue("dt", new DateTime(2026, 8, 31, 12, 0, 0), 2, "备注"); | | 136 | + Assert.AreEqual("[初始] dt=\"2026-08-31 12:00:00\";\n", NormalizeLine(logic.ToInfoString())); | | 137 | + } | | 138 | + | | 139 | + [Test] | | 140 | + public void InitValue_DateTime_CanBeUsedInCondition_Test() | | 141 | + { | | 142 | + var logic = CreateEngine(); | | 143 | + logic.InitValue("dt", new DateTime(2026, 8, 31, 12, 0, 0)); | | 144 | + Assert.IsTrue(logic.CheckCondition("YEAR(dt)=2026")); | | 145 | + Assert.IsTrue(logic.CheckCondition("MONTH(dt)=8")); | | 146 | + Assert.IsTrue(logic.CheckCondition("dt>DATEVALUE('2026-08-30')")); | | 147 | + } | | 148 | + | 122 | 149 | #endregion | 123 | 150 |
| 124 | 151 | #region CheckCondition 测试 | @@ -280,6 +307,32 @@ public void SetValue_LayerRemark_Test() | 280 | 307 | Assert.AreEqual("[赋值] i = 100 // 备注\n", NormalizeLine(logic.ToInfoString())); | 281 | 308 | } | 282 | 309 |
| | 310 | + [Test] | | 311 | + public void SetValue_DateTime_Test() | | 312 | + { | | 313 | + var logic = CreateEngine(); | | 314 | + logic.SetValue("dt", new DateTime(2026, 8, 31, 12, 0, 0)); | | 315 | + Assert.AreEqual("[赋值] dt = \"2026-08-31 12:00:00\"\n", NormalizeLine(logic.ToInfoString())); | | 316 | + } | | 317 | + | | 318 | + [Test] | | 319 | + public void SetValue_DateTime_LayerRemark_Test() | | 320 | + { | | 321 | + var logic = CreateEngine(); | | 322 | + logic.SetValue("dt", new DateTime(2026, 8, 31, 12, 0, 0), 1, "备注"); | | 323 | + Assert.AreEqual("[赋值] dt = \"2026-08-31 12:00:00\" // 备注\n", NormalizeLine(logic.ToInfoString())); | | 324 | + } | | 325 | + | | 326 | + [Test] | | 327 | + public void SetValue_DateTime_CanBeUsedInCondition_Test() | | 328 | + { | | 329 | + var logic = CreateEngine(); | | 330 | + logic.SetValue("dt", new DateTime(2026, 8, 31, 12, 0, 0)); | | 331 | + Assert.IsTrue(logic.CheckCondition("YEAR(dt)=2026")); | | 332 | + Assert.IsTrue(logic.CheckCondition("MONTH(dt)=8")); | | 333 | + Assert.IsTrue(logic.CheckCondition("dt>DATEVALUE('2026-08-30')")); | | 334 | + } | | 335 | + | 283 | 336 | #endregion | 284 | 337 |
| 285 | 338 | #region GetValue 测试 | @@ -337,6 +390,28 @@ public void GetValue_InitValue_Bool_Test() | 337 | 390 | Assert.IsTrue(operand.BooleanValue); | 338 | 391 | } | 339 | 392 |
| | 393 | + [Test] | | 394 | + public void GetValue_InitValue_DateTime_Test() | | 395 | + { | | 396 | + var logic = CreateEngine(); | | 397 | + logic.InitValue("dt", new DateTime(2026, 8, 31, 12, 0, 0)); | | 398 | + var operand = logic.GetValue("dt"); | | 399 | + Assert.IsTrue(operand.IsDate); | | 400 | + Assert.AreEqual(OperandType.DATE, operand.Type); | | 401 | + Assert.AreEqual(new DateTime(2026, 8, 31, 12, 0, 0), operand.DateValue.ToDateTime()); | | 402 | + } | | 403 | + | | 404 | + [Test] | | 405 | + public void GetValue_SetValue_DateTime_Test() | | 406 | + { | | 407 | + var logic = CreateEngine(); | | 408 | + logic.SetValue("dt", new DateTime(2026, 8, 31, 12, 0, 0)); | | 409 | + var operand = logic.GetValue("dt"); | | 410 | + Assert.IsTrue(operand.IsDate); | | 411 | + Assert.AreEqual(OperandType.DATE, operand.Type); | | 412 | + Assert.AreEqual(new DateTime(2026, 8, 31, 12, 0, 0), operand.DateValue.ToDateTime()); | | 413 | + } | | 414 | + | 340 | 415 | [Test] | 341 | 416 | public void GetValue_SetValue_Test() | 342 | 417 | { | |
Collapse file Copy file name to clipboardExpand all lines: csharp/ToolGood.Algorithm/CalculationLogic/CalculationLogicEngine.cs +82Lines changed: 82 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -248,6 +248,47 @@ public void InitValue(string key, int value, int layer, string remark) | 248 | 248 | InitValueCore(key, value, () => value.ToString(), layer, remark); | 249 | 249 | } | 250 | 250 |
| | 251 | + /// <summary> | | 252 | + /// 初始化值 | | 253 | + /// </summary> | | 254 | + /// <param name="key"></param> | | 255 | + /// <param name="value"></param> | | 256 | + public void InitValue(string key, DateTime value) | | 257 | + { | | 258 | + InitValue(key, value, 0, null); | | 259 | + } | | 260 | + /// <summary> | | 261 | + /// 初始化值 | | 262 | + /// </summary> | | 263 | + /// <param name="key"></param> | | 264 | + /// <param name="value"></param> | | 265 | + /// <param name="layer"></param> | | 266 | + public void InitValue(string key, DateTime value, int layer) | | 267 | + { | | 268 | + InitValue(key, value, layer, null); | | 269 | + } | | 270 | + /// <summary> | | 271 | + /// 初始化值 | | 272 | + /// </summary> | | 273 | + /// <param name="key"></param> | | 274 | + /// <param name="value"></param> | | 275 | + /// <param name="remark"></param> | | 276 | + public void InitValue(string key, DateTime value, string remark) | | 277 | + { | | 278 | + InitValue(key, value, 0, remark); | | 279 | + } | | 280 | + /// <summary> | | 281 | + /// 初始化值 | | 282 | + /// </summary> | | 283 | + /// <param name="key"></param> | | 284 | + /// <param name="value"></param> | | 285 | + /// <param name="layer"></param> | | 286 | + /// <param name="remark"></param> | | 287 | + public void InitValue(string key, DateTime value, int layer, string remark) | | 288 | + { | | 289 | + InitValueCore(key, value, () => $"\"{value.ToString("yyyy-MM-dd HH:mm:ss")}\"", layer, remark); | | 290 | + } | | 291 | + | 251 | 292 | private void InitValueCore(string key, Operand value, Func<string> expFactory, int layer, string remark) | 252 | 293 | { | 253 | 294 | _engine.AddParameter(key, value); | @@ -578,6 +619,47 @@ public void SetValue(string key, int exp, int layer, string remark) | 578 | 619 | SetValueCore(key, exp, () => exp.ToString(), layer, remark); | 579 | 620 | } | 580 | 621 |
| | 622 | + /// <summary> | | 623 | + /// 设置值 | | 624 | + /// </summary> | | 625 | + /// <param name="key"></param> | | 626 | + /// <param name="exp"></param> | | 627 | + public void SetValue(string key, DateTime exp) | | 628 | + { | | 629 | + SetValue(key, exp, 0, null); | | 630 | + } | | 631 | + /// <summary> | | 632 | + /// 设置值 | | 633 | + /// </summary> | | 634 | + /// <param name="key"></param> | | 635 | + /// <param name="exp"></param> | | 636 | + /// <param name="layer"></param> | | 637 | + public void SetValue(string key, DateTime exp, int layer) | | 638 | + { | | 639 | + SetValue(key, exp, layer, null); | | 640 | + } | | 641 | + /// <summary> | | 642 | + /// 设置值 | | 643 | + /// </summary> | | 644 | + /// <param name="key"></param> | | 645 | + /// <param name="exp"></param> | | 646 | + /// <param name="remark"></param> | | 647 | + public void SetValue(string key, DateTime exp, string remark) | | 648 | + { | | 649 | + SetValue(key, exp, 0, remark); | | 650 | + } | | 651 | + /// <summary> | | 652 | + /// 设置值 | | 653 | + /// </summary> | | 654 | + /// <param name="key"></param> | | 655 | + /// <param name="exp"></param> | | 656 | + /// <param name="layer"></param> | | 657 | + /// <param name="remark"></param> | | 658 | + public void SetValue(string key, DateTime exp, int layer, string remark) | | 659 | + { | | 660 | + SetValueCore(key, exp, () => $"\"{exp.ToString("yyyy-MM-dd HH:mm:ss")}\"", layer, remark); | | 661 | + } | | 662 | + | 581 | 663 | private void SetValueCore(string key, Operand value, Func<string> expFactory, int layer, string remark) | 582 | 664 | { | 583 | 665 | _engine.AddParameter(key, value); | |
|