1 package org.simantics.spreadsheet.graph;
3 import org.simantics.databoard.binding.mutable.Variant;
4 import org.simantics.spreadsheet.Range;
5 import org.simantics.spreadsheet.graph.formula.FormulaError2;
6 import org.simantics.spreadsheet.graph.formula.SpreadsheetEvaluationEnvironment;
7 import org.simantics.spreadsheet.graph.parser.ast.AstApply;
8 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
9 import org.simantics.spreadsheet.graph.parser.ast.AstArithmeticExpression;
10 import org.simantics.spreadsheet.graph.parser.ast.AstArray;
11 import org.simantics.spreadsheet.graph.parser.ast.AstArrayFormulaReference;
12 import org.simantics.spreadsheet.graph.parser.ast.AstBoolean;
13 import org.simantics.spreadsheet.graph.parser.ast.AstDouble;
14 import org.simantics.spreadsheet.graph.parser.ast.AstFactor;
15 import org.simantics.spreadsheet.graph.parser.ast.AstIdentifier;
16 import org.simantics.spreadsheet.graph.parser.ast.AstInteger;
17 import org.simantics.spreadsheet.graph.parser.ast.AstNothing;
18 import org.simantics.spreadsheet.graph.parser.ast.AstNull;
19 import org.simantics.spreadsheet.graph.parser.ast.AstRange;
20 import org.simantics.spreadsheet.graph.parser.ast.AstRelation;
21 import org.simantics.spreadsheet.graph.parser.ast.AstString;
22 import org.simantics.spreadsheet.graph.parser.ast.AstTerm;
23 import org.simantics.spreadsheet.graph.parser.ast.AstValue;
24 import org.simantics.spreadsheet.graph.parser.ast.AstValueVisitor;
25 import org.simantics.spreadsheet.util.SpreadsheetUtils;
27 import it.unimi.dsi.fastutil.longs.AbstractLongList;
28 import it.unimi.dsi.fastutil.longs.LongArrayList;
30 public class CellValueVisitor implements AstValueVisitor<Object> {
32 final public SpreadsheetBook book;
33 final private SpreadsheetEvaluationEnvironment env;
34 final private SpreadsheetCell thisCell;
35 final private LongArrayList references = new LongArrayList();
37 public CellValueVisitor(SpreadsheetEvaluationEnvironment env, SpreadsheetCell thisCell) {
38 this.book = env.getBook();
40 this.thisCell = thisCell;
43 public void addReference(long ref) {
47 public AbstractLongList getReferences() {
52 public Object visit(AstBoolean astBoolean) {
53 return astBoolean.value;
57 public Object visit(AstDouble astFloat) {
58 return astFloat.value;
62 public Object visit(AstInteger astInteger) {
63 return astInteger.value;
67 public Object visit(AstNull astNull) {
68 throw new IllegalStateException();
72 public Object visit(AstString astString) {
73 return astString.value;
77 public Object visit(AstRange astRange) {
80 return FormulaError2.REF.getString();
83 if(astRange.isCell()) {
84 String ref = astRange.first;
85 Range r = SpreadsheetUtils.decodeCell(ref, 0, 0);
86 String sheetName = astRange.sheetName != null ? astRange.sheetName : thisCell.getEngine().getName();
87 SpreadsheetCell cell = thisCell.getBook().get(sheetName, r.startRow, r.startColumn);
89 SpreadsheetEngine eng = thisCell.getBook().getEngine(sheetName);
90 SpreadsheetLine line = eng.getLine(r.startRow);
93 line = new SpreadsheetLine(eng.lines, r.startRow);
94 eng.lines.lines.put(-r.startRow, line);
96 cell = SpreadsheetCell.empty(line, r.startColumn);
98 return cell.evaluate(env, this);
102 Object cached = thisCell.getEngine().getCachedRange(astRange);
105 Range r_ = SpreadsheetUtils.decodeRange(astRange.first + ":" + astRange.second);
106 String sheetName = astRange.sheetName != null ? astRange.sheetName : thisCell.getEngine().getName();
107 SpreadsheetEngine eng = thisCell.getBook().getEngine(sheetName);
108 Range r = eng.actualRange(r_);
109 for(int row=0; row < r.height();row++) {
110 SpreadsheetLine line = eng.getLine(r.startRow + row);
112 for(int column=0; column < r.width();column++) {
113 int col = r.startColumn + column;
114 if(line.cells.size() > col) {
115 SpreadsheetCell cell = line.cells.get(r.startColumn + column);
116 //Add references, but do not evaluate if there exists a cached range.
117 addReference(cell.makeReferenceKey());
125 Range r_ = SpreadsheetUtils.decodeRange(astRange.first + ":" + astRange.second);
126 String sheetName = astRange.sheetName != null ? astRange.sheetName : thisCell.getEngine().getName();
127 SpreadsheetEngine eng = thisCell.getBook().getEngine(sheetName);
128 // Full ranges are resolved here
129 Range r = eng.actualRange(r_);
130 SpreadsheetMatrix result = new SpreadsheetMatrix(r.width(), r.height());
131 for(int row=0; row < r.height();row++) {
132 SpreadsheetLine line = eng.getLine(r.startRow + row);
134 for(int column=0; column < r.width();column++) {
135 int col = r.startColumn + column;
136 if(line.cells.size() > col) {
137 SpreadsheetCell cell = line.cells.get(r.startColumn + column);
138 result.values[r.width()*row + column] = cell.evaluate(env, this);
143 thisCell.getEngine().cacheRange(astRange, result);
149 public Object visit(AstArgList astArgList) {
150 throw new IllegalStateException();
154 public Object visit(AstApply astApply) {
155 CellFormulaFunction<?> fn = env.getFunction(astApply.value);
157 return fn.evaluate(this, astApply.args);
159 return FormulaError2.NAME.getString();
164 public Object visit(AstRelation astRelation) {
166 Object leftResult = astRelation.left.accept(this);
167 Object rightResult = astRelation.right.accept(this);
169 FormulaError2 err = FormulaError2.forObject(leftResult);
170 if(err!=null) return err.getString();
171 FormulaError2 err2 = FormulaError2.forObject(rightResult);
172 if(err2!=null) return err2.getString();
174 if(leftResult instanceof Variant){
175 Object leftTemp = ((Variant)leftResult).getValue();
176 Double leftVal = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(leftTemp);
177 if(leftVal==null) leftResult = leftTemp.toString();
178 else leftResult = leftVal;
180 if(rightResult instanceof Variant){
181 Object rightTemp = ((Variant)rightResult).getValue();
182 Double rightVal = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(rightTemp);
183 if(rightVal==null) rightResult = rightTemp.toString();
184 else rightResult = rightVal;
186 if ((leftResult instanceof String) && (rightResult instanceof String)) {
187 String leftString = (leftResult.toString()).toLowerCase();
188 String rightString = (rightResult.toString()).toLowerCase();
189 if("<".equals(astRelation.op.trim())) return leftString.compareTo(rightString) < 0;
190 else if(">".equals(astRelation.op.trim())) return leftString.compareTo(rightString) > 0;
191 else if("=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) == 0;
192 else if("<>".equals(astRelation.op.trim())) return leftString.compareTo(rightString) != 0 ;
193 else if("<=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) <= 0 ;
194 else if(">=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) >= 0 ;
195 else throw new IllegalStateException();
197 Number leftNumber = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(leftResult);
198 Number rightNumber = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(rightResult);
199 if(leftNumber==null || rightNumber==null) return false;
200 if("<".equals(astRelation.op.trim())) return leftNumber.doubleValue() < rightNumber.doubleValue();
201 else if(">".equals(astRelation.op.trim())) return leftNumber.doubleValue() > rightNumber.doubleValue();
202 else if("=".equals(astRelation.op.trim())) return leftNumber.doubleValue() == rightNumber.doubleValue();
203 else if("<>".equals(astRelation.op.trim())) return leftNumber.doubleValue() != rightNumber.doubleValue();
204 else if("<=".equals(astRelation.op.trim())) return leftNumber.doubleValue() <= rightNumber.doubleValue();
205 else if(">=".equals(astRelation.op.trim())) return leftNumber.doubleValue() >= rightNumber.doubleValue();
206 else throw new IllegalStateException();
210 Object leftValueWithPrefix(Object result, AstValue value, String prefix, boolean forceNumber) {
212 Object obj = value.accept(this);
213 FormulaError2 err = FormulaError2.forObject(obj);
214 if(err!=null) return err.getString();
216 if("-".equals(prefix)) {
217 result = SpreadsheetGraphUtils.asNumber(obj);
218 return -((Number)result).doubleValue();
220 if(forceNumber) return SpreadsheetGraphUtils.asNumber(obj);
225 return (Number)Double.parseDouble(result.toString());
226 } catch (NumberFormatException e){
232 public Object visit(AstArithmeticExpression exp) {
233 Object result = null;
235 AstValue value = null;
236 Object acceptedValue = null;
238 for(int i=0;i<exp.rightCount();i++) {
239 String op = exp.rightOp(i);
240 value = exp.rightValue(i);
241 acceptedValue = value.accept(this);
243 result = leftValueWithPrefix(result, exp.left, exp.prefix, false);
245 if(!(result instanceof Number)) {
246 FormulaError2 err = FormulaError2.forObject(result);
247 if(err!=null) return err.getString();
249 if(result instanceof String && !((String) result).isEmpty()){
250 Number num = SpreadsheetGraphUtils.asValidNumber(result);
252 return FormulaError2.VALUE.getString();
257 else if(result instanceof Variant){
258 Object val = ((Variant)result).getValue();
259 if(val instanceof String && (val.toString().isEmpty())){
262 Number resNum = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(val);
263 if(resNum==null) return FormulaError2.VALUE.getString();
264 else result = resNum;
271 FormulaError2 err2 = FormulaError2.forObject(acceptedValue);
272 if(err2!=null) return err2.getString();
274 other = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(acceptedValue);
276 return FormulaError2.handleErrorCall(acceptedValue);
278 result = ((Number)result).doubleValue() + ((Number)other).doubleValue();
280 } else if("-".equals(op)) {
281 result = leftValueWithPrefix(result, exp.left, exp.prefix, false);
283 if(!(result instanceof Number)) {
284 FormulaError2 err = FormulaError2.forObject(result);
285 if(err!=null) return err.getString();
287 if(result instanceof String && !((String) result).isEmpty()){
288 Number num = SpreadsheetGraphUtils.asValidNumber(result);
290 return FormulaError2.VALUE.getString();
295 else if(result instanceof Variant){
296 Object val = ((Variant)result).getValue();
297 if(val instanceof String && (val.toString().isEmpty())){
300 Number resNum = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(val);
302 return FormulaError2.VALUE.getString();
303 else result = resNum;
309 FormulaError2 err2 = FormulaError2.forObject(acceptedValue);
310 if(err2!=null) return err2.getString();
312 other = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(acceptedValue);
314 return FormulaError2.handleErrorCall(acceptedValue);
316 result = ((Number)result).doubleValue() - ((Number)other).doubleValue();
318 } else if("&".equals(op)) {
319 result = leftValueWithPrefix(result, exp.left, exp.prefix, false);
320 FormulaError2 err = FormulaError2.forObject(result);
321 if(err!=null) return err.getString();
322 FormulaError2 err2 = FormulaError2.forObject(acceptedValue);
323 if(err2!=null) return err2.getString();
325 result = SpreadsheetGraphUtils.asString(result);
326 other = SpreadsheetGraphUtils.asString(acceptedValue);
328 result += (String)other;
331 return leftValueWithPrefix(result, exp.left, exp.prefix, false);
336 public Object visit(AstTerm exp) {
337 Number result = null;
338 for(int i=0;i<exp.rightCount();i++) {
339 String op = exp.rightOp(i);
340 AstValue value = exp.rightValue(i);
341 Object leftValue = exp.left.accept(this);
342 Object rightValue = value.accept(this);
345 result = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(leftValue);
347 return FormulaError2.handleErrorCall(leftValue);
349 Number other = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(rightValue);
350 if(other==null) return FormulaError2.handleErrorCall(rightValue);
352 result = new Double(result.doubleValue() * other.doubleValue());
354 } else if("/".equals(op)) {
356 result = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(leftValue);
358 return FormulaError2.handleErrorCall(leftValue);
360 Number other = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(rightValue);
361 if(other==null) return FormulaError2.handleErrorCall(rightValue);
362 if(other.doubleValue()==0.0) return FormulaError2.DIV0.getString();
364 result = new Double(result.doubleValue() / other.doubleValue());
367 if(result == null) result = SpreadsheetGraphUtils.asNumber(exp.left.accept(this));
372 public Object visit(AstFactor exp) {
373 Object result = null;
374 for(int i=0;i<exp.rightCount();i++) {
375 String op = exp.rightOp(i);
376 AstValue value = exp.rightValue(i);
379 Object leftValue = exp.left.accept(this);
381 FormulaError2 err = FormulaError2.forObject(leftValue);
382 if(err!=null) return err.getString();
384 if(leftValue instanceof Variant){
385 Object leftTemp = ((Variant)leftValue).getValue();
386 Double leftV = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(leftTemp);
387 if(leftV==null) leftValue = leftTemp.toString();
388 else leftValue = leftV;
391 if(leftValue instanceof String){
392 if((leftValue.toString()).isEmpty())
395 return FormulaError2.VALUE.getString();
397 else if(leftValue instanceof SpreadsheetMatrix)
400 result = SpreadsheetGraphUtils.asNumber(leftValue);
402 Object otherValue = value.accept(this);
404 FormulaError2 err2 = FormulaError2.forObject(otherValue);
405 if(err2!=null) return err2.getString();
407 if(otherValue instanceof Variant){
408 Object otherTemp = ((Variant)otherValue).getValue();
409 Double otherV = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(otherTemp);
410 if(otherV==null) otherValue = otherTemp.toString();
411 else otherValue = otherV;
414 if(otherValue instanceof String){
415 if((otherValue.toString()).isEmpty())
418 return FormulaError2.VALUE.getString();
421 if(result instanceof SpreadsheetMatrix) {
422 result = ((SpreadsheetMatrix)result).pow(otherValue);
424 if(otherValue instanceof SpreadsheetMatrix) {
425 throw new IllegalStateException();
427 Double base = ((Number)result).doubleValue();
428 Double exponent = SpreadsheetGraphUtils.asNumber(otherValue);
429 if(exponent==0 && base==0)
430 return FormulaError2.NUM.getString();
431 if(exponent<0 && base==0)
432 return FormulaError2.DIV0.getString();
433 result = Math.pow(base, exponent);
434 if(result instanceof Double && Double.isInfinite((Double)result)){
435 return FormulaError2.NUM.getString();
441 if(result == null) result = SpreadsheetGraphUtils.asNumber(exp.left.accept(this));
446 public Object visit(AstIdentifier id) {
447 return FormulaError2.NAME.getString();
448 //throw new IllegalStateException();
452 public Object visit(AstArray array) {
453 SpreadsheetMatrix m = new SpreadsheetMatrix(array.values.size(), 1);
454 for(int i=0;i<array.values.size();i++) {
455 m.values[i] = array.values.get(i).accept(this);
461 public Object visit(AstNothing array) {
462 return AstNothing.NOTHING;
466 public Object visit(AstArrayFormulaReference ref) {
468 Range thisRange = SpreadsheetUtils.decodeRange(thisCell.getName());
469 Range arrayRange = SpreadsheetUtils.decodeRange(ref.range);
470 int x = thisRange.startColumn - arrayRange.startColumn;
471 int y = thisRange.startRow - arrayRange.startRow;
473 SpreadsheetMatrix m = (SpreadsheetMatrix)ref.value.accept(this);