-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcalculate-formula.service.ts
More file actions
552 lines (436 loc) · 21.8 KB
/
Copy pathcalculate-formula.service.ts
File metadata and controls
552 lines (436 loc) · 21.8 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { IUnitRange } from '@univerjs/core';
import type { Observable } from 'rxjs';
import type {
IArrayFormulaRangeType,
IFeatureDirtyRangeType,
IFormulaDatasetConfig,
IFormulaExecuteResultItem,
IFormulaExecuteResultMap,
IFormulaStringMap,
IRuntimeUnitDataType,
IUnitExcludedCell,
IUnitRowData,
} from '../basics/common';
import type { IUniverEngineFormulaConfig } from '../config/config';
import type { LexerNode } from '../engine/analysis/lexer-node';
import type { IFormulaDependencyTreeFullJson, IFormulaDependencyTreeJson, IFormulaDependentsAndInRangeResults } from '../engine/dependency/dependency-tree';
import type { BaseReferenceObject, FunctionVariantType } from '../engine/reference-object/base-reference-object';
import type { ArrayValueObject } from '../engine/value-object/array-value-object';
import type { BaseValueObject } from '../engine/value-object/base-value-object';
import type { IAllRuntimeData, IExecutionInProgressParams } from './runtime.service';
import {
AsyncLock,
createIdentifier,
Disposable,
IConfigService,
Inject,
ObjectMatrix,
requestImmediateMacroTask,
} from '@univerjs/core';
import { Subject } from 'rxjs';
import { ErrorType } from '../basics/error-type';
import { CELL_INVERTED_INDEX_CACHE } from '../basics/inverted-index-cache';
import { DEFAULT_CYCLE_REFERENCE_COUNT, ENGINE_FORMULA_PLUGIN_CONFIG_KEY } from '../config/config';
import { Lexer } from '../engine/analysis/lexer';
import { AstTreeBuilder } from '../engine/analysis/parser';
import { IFormulaDependencyGenerator } from '../engine/dependency/formula-dependency';
import { Interpreter } from '../engine/interpreter/interpreter';
import { FORMULA_REF_TO_ARRAY_CACHE } from '../engine/reference-object/base-reference-object';
import { generateAstNode } from '../engine/utils/generate-ast-node';
import { ErrorValueObjectCache } from '../engine/value-object/base-value-object';
import { StringValueObjectCache } from '../engine/value-object/primitive-object';
import { IFormulaCurrentConfigService } from './current-data.service';
import { FormulaExecuteStageType, IFormulaRuntimeService } from './runtime.service';
export const DEFAULT_INTERVAL_COUNT = 500;
export const CYCLE_REFERENCE_COUNT = 'cycleReferenceCount';
export const EVERY_N_FUNCTION_EXECUTION_PAUSE = 100;
export interface ICalculateFormulaService {
readonly executionInProgressListener$: Observable<IExecutionInProgressParams>;
readonly executionCompleteListener$: Observable<IAllRuntimeData>;
setRuntimeFeatureCellData(featureId: string, featureData: IRuntimeUnitDataType): void;
setRuntimeFeatureRange(featureId: string, featureRange: IFeatureDirtyRangeType): void;
execute(formulaDatasetConfig: IFormulaDatasetConfig): Promise<void>;
stopFormulaExecution(): void;
calculate(formulaString: string, transformSuffix?: boolean): void;
executeFormulas(formulas: IFormulaStringMap, rowData?: IUnitRowData): Promise<IFormulaExecuteResultMap>;
getAllDependencyJson(rowData?: IUnitRowData): Promise<IFormulaDependencyTreeJson[]>;
getCellDependencyJson(unitId: string, sheetId: string, row: number, column: number, rowData?: IUnitRowData): Promise<IFormulaDependencyTreeFullJson | undefined>;
getRangeDependents(unitRanges: IUnitRange[]): Promise<IFormulaDependencyTreeJson[]>;
getInRangeFormulas(unitRanges: IUnitRange[]): Promise<IFormulaDependencyTreeJson[]>;
getDependentsAndInRangeFormulas(unitRanges: IUnitRange[]): Promise<IFormulaDependentsAndInRangeResults>;
}
export const ICalculateFormulaService = createIdentifier<ICalculateFormulaService>('engine-formula.calculate-formula.service');
export class CalculateFormulaService extends Disposable implements ICalculateFormulaService {
protected readonly _executionInProgressListener$ = new Subject<IExecutionInProgressParams>();
readonly executionInProgressListener$ = this._executionInProgressListener$.asObservable();
protected readonly _executionCompleteListener$ = new Subject<IAllRuntimeData>();
readonly executionCompleteListener$ = this._executionCompleteListener$.asObservable();
protected _executeLock = new AsyncLock();
protected _isCalculateTreeModel: boolean = false;
constructor(
@IConfigService protected readonly _configService: IConfigService,
@Inject(Lexer) protected readonly _lexer: Lexer,
@IFormulaCurrentConfigService protected readonly _currentConfigService: IFormulaCurrentConfigService,
@IFormulaRuntimeService protected readonly _runtimeService: IFormulaRuntimeService,
@IFormulaDependencyGenerator protected readonly _formulaDependencyGenerator: IFormulaDependencyGenerator,
@Inject(Interpreter) protected readonly _interpreter: Interpreter,
@Inject(AstTreeBuilder) protected readonly _astTreeBuilder: AstTreeBuilder
) {
super();
}
override dispose() {
super.dispose();
this._executionInProgressListener$.complete();
this._executionCompleteListener$.complete();
FORMULA_REF_TO_ARRAY_CACHE.clear();
CELL_INVERTED_INDEX_CACHE.clear();
ErrorValueObjectCache.clear();
StringValueObjectCache.clear();
}
/**
* Stop the execution of the formula.
*/
stopFormulaExecution() {
this._runtimeService.stopExecution();
}
/**
* When the feature is loading,
* the pre-calculated content needs to be input to the formula engine in advance,
* so that the formula can read the correct values.
* @param featureId
* @param featureData
*/
setRuntimeFeatureCellData(featureId: string, featureData: IRuntimeUnitDataType) {
this._runtimeService.setRuntimeFeatureCellData(featureId, featureData);
}
setRuntimeFeatureRange(featureId: string, featureRange: IFeatureDirtyRangeType) {
this._runtimeService.setRuntimeFeatureRange(featureId, featureRange);
}
async execute(formulaDatasetConfig: IFormulaDatasetConfig) {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.START);
this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
this._currentConfigService.load(formulaDatasetConfig);
this._runtimeService.reset();
const cycleReferenceCount = (formulaDatasetConfig.maxIteration || DEFAULT_CYCLE_REFERENCE_COUNT) as number;
this._isCalculateTreeModel = formulaDatasetConfig.isCalculateTreeModel || false;
this._executeLock.acquire('FORMULA_EXECUTION_LOCK', async () => {
for (let i = 0; i < cycleReferenceCount; i++) {
this._runtimeService.setFormulaCycleIndex(i);
await this._executeStep();
FORMULA_REF_TO_ARRAY_CACHE.clear();
const isCycleDependency = this._runtimeService.isCycleDependency();
if (!isCycleDependency) {
break;
}
}
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.CALCULATION_COMPLETED);
this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
this._executionCompleteListener$.next(this._runtimeService.getAllRuntimeData());
CELL_INVERTED_INDEX_CACHE.clear();
this._runtimeService.reset();
});
}
protected async _executeStep() {
const executeState = await this._apply();
if (executeState == null) {
return;
}
const { arrayFormulaRange, runtimeFeatureRange } = executeState;
const { dirtyRanges, excludedCell } = this._getArrayFormulaDirtyRangeAndExcludedRange(
arrayFormulaRange,
runtimeFeatureRange
);
if (dirtyRanges == null || dirtyRanges.length === 0) {
return true;
}
FORMULA_REF_TO_ARRAY_CACHE.clear();
this._currentConfigService.loadDirtyRangesAndExcludedCell(dirtyRanges, excludedCell);
await this._apply(true);
return true;
}
protected _getArrayFormulaDirtyRangeAndExcludedRange(
arrayFormulaRange: IArrayFormulaRangeType,
runtimeFeatureRange: { [featureId: string]: IFeatureDirtyRangeType }
) {
const dirtyRanges: IUnitRange[] = [];
const excludedCell: IUnitExcludedCell = {};
Object.keys(arrayFormulaRange).forEach((unitId) => {
const sheetArrayFormulaRange = arrayFormulaRange[unitId];
if (sheetArrayFormulaRange == null) {
return true;
}
Object.keys(sheetArrayFormulaRange).forEach((sheetId) => {
const cellValue = new ObjectMatrix(sheetArrayFormulaRange[sheetId]);
if (cellValue == null) {
return true;
}
const newCellData = new ObjectMatrix<boolean>();
cellValue.forValue((row, column, range) => {
newCellData.setValue(row, column, true);
dirtyRanges.push({ unitId, sheetId, range });
});
if (excludedCell[unitId] == null) {
excludedCell[unitId] = {};
}
excludedCell[unitId]![sheetId] = newCellData;
});
});
Object.keys(runtimeFeatureRange).forEach((featureId) => {
const arrayRange = runtimeFeatureRange[featureId];
Object.keys(arrayRange).forEach((unitId) => {
const sheetArrayFormulaRange = arrayRange[unitId];
if (sheetArrayFormulaRange == null) {
return true;
}
Object.keys(sheetArrayFormulaRange).forEach((sheetId) => {
const ranges = sheetArrayFormulaRange[sheetId];
if (ranges == null) {
return true;
}
for (const range of ranges) {
dirtyRanges.push({ unitId, sheetId, range });
}
});
});
});
return { dirtyRanges, excludedCell };
}
// eslint-disable-next-line max-lines-per-function
protected async _apply(isArrayFormulaState = false) {
if (isArrayFormulaState) {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.START_DEPENDENCY_ARRAY_FORMULA);
} else {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.START_DEPENDENCY);
}
this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
const treeList = await this._formulaDependencyGenerator.generate(this._isCalculateTreeModel);
const interpreter = this._interpreter;
if (isArrayFormulaState) {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.START_CALCULATION_ARRAY_FORMULA);
this._runtimeService.setTotalArrayFormulasToCalculate(treeList.length);
} else {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.START_CALCULATION);
this._runtimeService.setTotalFormulasToCalculate(treeList.length);
}
this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
let pendingTasks: (() => void)[] = [];
const config = this._configService.getConfig(ENGINE_FORMULA_PLUGIN_CONFIG_KEY) as IUniverEngineFormulaConfig;
const intervalCount = config?.intervalCount || DEFAULT_INTERVAL_COUNT;
let i = 0;
const treeCount = treeList.length;
while (treeList.length > 0) {
const tree = treeList.pop()!;
const node = generateAstNode(tree.unitId, tree.formula, this._lexer, this._astTreeBuilder, this._currentConfigService);
const nodeData = {
node,
refOffsetX: tree.refOffsetX,
refOffsetY: tree.refOffsetY,
};
const getDirtyData = tree.getDirtyData;
// Execute the await every 100 iterations
if (i % intervalCount === 0) {
/**
* For every functions, execute a setTimeout to wait for external command input.
*/
await new Promise((resolve) => {
const calCancelTask = requestImmediateMacroTask(resolve);
pendingTasks.push(calCancelTask);
});
if (isArrayFormulaState) {
this._runtimeService.setFormulaExecuteStage(
FormulaExecuteStageType.CURRENTLY_CALCULATING_ARRAY_FORMULA
);
this._runtimeService.setCompletedArrayFormulasCount(i + 1);
} else {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.CURRENTLY_CALCULATING);
this._runtimeService.setCompletedFormulasCount(i + 1);
}
this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
if (this._runtimeService.isStopExecution() || (nodeData == null && getDirtyData == null)) {
this._runtimeService.setFormulaExecuteStage(FormulaExecuteStageType.IDLE);
this._runtimeService.markedAsStopFunctionsExecuted();
this._executionCompleteListener$.next(this._runtimeService.getAllRuntimeData());
return;
}
}
this._runtimeService.setCurrent(
tree.row,
tree.column,
tree.rowCount,
tree.columnCount,
tree.subUnitId,
tree.unitId,
tree.type
);
let value: FunctionVariantType;
if (getDirtyData != null && tree.featureId != null) {
/**
* Execute the dependencies registered by the feature,
* and return the dirty area marked by the feature,
* so as to allow the formulas depending on the dirty area to continue the calculation.
*/
const { runtimeCellData, dirtyRanges } = getDirtyData(this._currentConfigService.getDirtyData(), this._runtimeService.getAllRuntimeData());
this._runtimeService.setRuntimeFeatureCellData(tree.featureId, runtimeCellData);
this._runtimeService.setRuntimeFeatureRange(tree.featureId, dirtyRanges);
} else if (nodeData != null) {
if (interpreter.checkAsyncNode(nodeData.node)) {
value = await interpreter.executeAsync(nodeData);
} else {
value = interpreter.execute(nodeData);
}
if (tree.formulaId != null) {
this._runtimeService.setRuntimeOtherData(tree.formulaId, tree.refOffsetX, tree.refOffsetY, value);
} else {
this._runtimeService.setRuntimeData(value);
}
}
node.resetCalculationState();
i++;
}
// clear all pending tasks
pendingTasks.forEach((cancel) => cancel());
pendingTasks = [];
if (treeCount > 0) {
this._runtimeService.markedAsSuccessfullyExecuted();
} else if (!isArrayFormulaState) {
this._runtimeService.markedAsNoFunctionsExecuted();
}
// treeList.length = 0;
return this._runtimeService.getAllRuntimeData();
}
async executeFormulas(formulas: IFormulaStringMap, rowData?: IUnitRowData) {
this._currentConfigService.loadDataLite(
rowData
);
const unitData = this._currentConfigService.getUnitData();
this._runtimeService.reset();
const result: IFormulaExecuteResultMap = {};
for (const unitId of Object.keys(formulas)) {
const sheetFormulas = formulas[unitId];
if (sheetFormulas == null) continue;
result[unitId] = {};
for (const sheetId of Object.keys(sheetFormulas)) {
const rowFormulas = sheetFormulas[sheetId];
if (rowFormulas == null) continue;
const sheetItem = unitData[unitId]?.[sheetId];
if (sheetItem == null) continue;
result[unitId]![sheetId] = {};
for (const rowString of Object.keys(rowFormulas)) {
const row = Number.parseInt(rowString);
result[unitId]![sheetId]![row] = {};
const cellData = rowFormulas[row];
if (!cellData) continue;
for (const columnString of Object.keys(cellData)) {
const column = Number.parseInt(columnString);
const formulaStrings = cellData[column];
if (!formulaStrings) continue;
const rowCount = sheetItem.rowCount || 0;
const columnCount = sheetItem.columnCount || 0;
this._runtimeService.setCurrent(
row,
column,
rowCount,
columnCount,
sheetId,
unitId
);
const item: IFormulaExecuteResultItem[] = [];
for (const formulaString of formulaStrings) {
let resultCell = await this.calculate(formulaString);
if (!resultCell) {
item.push({
value: null,
formula: formulaString,
});
continue;
}
if (resultCell.isReferenceObject()) {
resultCell = (resultCell as BaseReferenceObject).toArrayValueObject();
}
const resultRefObject = resultCell as BaseValueObject;
if (resultRefObject.isArray()) {
const resultRefArrayObject = resultRefObject as ArrayValueObject;
if (resultRefArrayObject.getRowCount() === 1 && resultRefArrayObject.getColumnCount() === 1) {
item.push({
value: resultRefArrayObject.getFirstCell().getValue(),
formula: formulaString,
});
continue;
}
item.push({
value: resultRefArrayObject.toValue(),
formula: formulaString,
});
continue;
}
item.push({
value: resultRefObject.getValue(),
formula: formulaString,
});
}
result[unitId]![sheetId]![row]![column] = item;
}
}
}
}
return result;
}
async calculate(formulaString: string) {
// TODO how to observe @alex
// this.getObserver('onBeforeFormulaCalculateObservable')?.notifyObservers(formulaString);
const lexerNode = this._lexer.treeBuilder(formulaString);
if (Object.values(ErrorType).includes(lexerNode as ErrorType)) {
return;
}
const astNode = this._astTreeBuilder.parse(lexerNode as LexerNode);
const interpreter = this._interpreter;
if (astNode == null) {
return;
}
const nodeData = {
node: astNode,
refOffsetX: 0,
refOffsetY: 0,
};
if (interpreter.checkAsyncNode(astNode)) {
return await interpreter.executeAsync(nodeData);
} else {
return interpreter.execute(nodeData);
}
}
async getAllDependencyJson(): Promise<IFormulaDependencyTreeJson[]> {
this._currentConfigService.loadDataLite();
return this._formulaDependencyGenerator.getAllDependencyJson();
}
async getCellDependencyJson(unitId: string, sheetId: string, row: number, column: number): Promise<IFormulaDependencyTreeFullJson | undefined> {
this._currentConfigService.loadDataLite();
return this._formulaDependencyGenerator.getCellDependencyJson(unitId, sheetId, row, column);
}
async getRangeDependents(unitRanges: IUnitRange[]): Promise<IFormulaDependencyTreeJson[]> {
this._currentConfigService.loadDataLite();
return this._formulaDependencyGenerator.getRangeDependents(unitRanges);
}
async getInRangeFormulas(unitRanges: IUnitRange[]): Promise<IFormulaDependencyTreeJson[]> {
this._currentConfigService.loadDataLite();
return this._formulaDependencyGenerator.getInRangeFormulas(unitRanges);
}
async getDependentsAndInRangeFormulas(unitRanges: IUnitRange[]): Promise<IFormulaDependentsAndInRangeResults> {
this._currentConfigService.loadDataLite();
return this._formulaDependencyGenerator.getRangeDependentsAndInRangeFormulas(unitRanges);
}
}