You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EvaluationTracker.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import java.util.ArrayList;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import org.apache.poi.ss.formula.eval.BlankEval;
  21. import org.apache.poi.ss.formula.eval.ErrorEval;
  22. import org.apache.poi.ss.formula.eval.ValueEval;
  23. /**
  24. * Instances of this class keep track of multiple dependent cell evaluations due
  25. * to recursive calls to {@link WorkbookEvaluator#evaluate(EvaluationCell)}}
  26. * The main purpose of this class is to detect an attempt to evaluate a cell
  27. * that is already being evaluated. In other words, it detects circular
  28. * references in spreadsheet formulas.
  29. *
  30. * @author Josh Micich
  31. */
  32. final class EvaluationTracker {
  33. // TODO - consider deleting this class and letting CellEvaluationFrame take care of itself
  34. private final List<CellEvaluationFrame> _evaluationFrames;
  35. private final Set<FormulaCellCacheEntry> _currentlyEvaluatingCells;
  36. private final EvaluationCache _cache;
  37. public EvaluationTracker(EvaluationCache cache) {
  38. _cache = cache;
  39. _evaluationFrames = new ArrayList<>();
  40. _currentlyEvaluatingCells = new HashSet<>();
  41. }
  42. /**
  43. * Notifies this evaluation tracker that evaluation of the specified cell is
  44. * about to start.<br>
  45. *
  46. * In the case of a <code>true</code> return code, the caller should
  47. * continue evaluation of the specified cell, and also be sure to call
  48. * <tt>endEvaluate()</tt> when complete.<br>
  49. *
  50. * In the case of a <code>null</code> return code, the caller should
  51. * return an evaluation result of
  52. * <tt>ErrorEval.CIRCULAR_REF_ERROR<tt>, and not call <tt>endEvaluate()</tt>.
  53. * <br>
  54. * @return <code>false</code> if the specified cell is already being evaluated
  55. */
  56. public boolean startEvaluate(FormulaCellCacheEntry cce) {
  57. if (cce == null) {
  58. throw new IllegalArgumentException("cellLoc must not be null");
  59. }
  60. if (_currentlyEvaluatingCells.contains(cce)) {
  61. return false;
  62. }
  63. _currentlyEvaluatingCells.add(cce);
  64. _evaluationFrames.add(new CellEvaluationFrame(cce));
  65. return true;
  66. }
  67. public void updateCacheResult(ValueEval result) {
  68. int nFrames = _evaluationFrames.size();
  69. if (nFrames < 1) {
  70. throw new IllegalStateException("Call to endEvaluate without matching call to startEvaluate");
  71. }
  72. CellEvaluationFrame frame = _evaluationFrames.get(nFrames-1);
  73. if (result == ErrorEval.CIRCULAR_REF_ERROR && nFrames > 1) {
  74. // Don't cache a circular ref error result if this cell is not the top evaluated cell.
  75. // A true circular ref error will propagate all the way around the loop. However, it's
  76. // possible to have parts of the formula tree (/ parts of the loop) to evaluate to
  77. // CIRCULAR_REF_ERROR, and that value not get used in the final cell result (see the
  78. // unit tests for a simple example). Thus, the only CIRCULAR_REF_ERROR result that can
  79. // safely be cached is that of the top evaluated cell.
  80. return;
  81. }
  82. frame.updateFormulaResult(result);
  83. }
  84. /**
  85. * Notifies this evaluation tracker that the evaluation of the specified cell is complete. <p>
  86. *
  87. * Every successful call to <tt>startEvaluate</tt> must be followed by a call to <tt>endEvaluate</tt> (recommended in a finally block) to enable
  88. * proper tracking of which cells are being evaluated at any point in time.<p>
  89. *
  90. * Assuming a well behaved client, parameters to this method would not be
  91. * required. However, they have been included to assert correct behaviour,
  92. * and form more meaningful error messages.
  93. */
  94. public void endEvaluate(CellCacheEntry cce) {
  95. int nFrames = _evaluationFrames.size();
  96. if (nFrames < 1) {
  97. throw new IllegalStateException("Call to endEvaluate without matching call to startEvaluate");
  98. }
  99. nFrames--;
  100. CellEvaluationFrame frame = _evaluationFrames.get(nFrames);
  101. if (cce != frame.getCCE()) {
  102. throw new IllegalStateException("Wrong cell specified. ");
  103. }
  104. // else - no problems so pop current frame
  105. _evaluationFrames.remove(nFrames);
  106. _currentlyEvaluatingCells.remove(cce);
  107. }
  108. public void acceptFormulaDependency(CellCacheEntry cce) {
  109. // Tell the currently evaluating cell frame that it has a dependency on the specified
  110. int prevFrameIndex = _evaluationFrames.size()-1;
  111. if (prevFrameIndex < 0) {
  112. // Top level frame, there is no 'cell' above this frame that is using the current cell
  113. } else {
  114. CellEvaluationFrame consumingFrame = _evaluationFrames.get(prevFrameIndex);
  115. consumingFrame.addSensitiveInputCell(cce);
  116. }
  117. }
  118. public void acceptPlainValueDependency(EvaluationWorkbook evalWorkbook, int bookIndex, int sheetIndex,
  119. int rowIndex, int columnIndex, ValueEval value) {
  120. // Tell the currently evaluating cell frame that it has a dependency on the specified
  121. int prevFrameIndex = _evaluationFrames.size() - 1;
  122. if (prevFrameIndex < 0) {
  123. // Top level frame, there is no 'cell' above this frame that is using the current cell
  124. } else {
  125. CellEvaluationFrame consumingFrame = _evaluationFrames.get(prevFrameIndex);
  126. if (value == BlankEval.instance) {
  127. consumingFrame.addUsedBlankCell(evalWorkbook, bookIndex, sheetIndex, rowIndex, columnIndex);
  128. } else {
  129. PlainValueCellCacheEntry cce = _cache.getPlainValueEntry(bookIndex, sheetIndex,
  130. rowIndex, columnIndex, value);
  131. consumingFrame.addSensitiveInputCell(cce);
  132. }
  133. }
  134. }
  135. }