--- /dev/null
+package org.simantics.spreadsheet.graph.request;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.LinkedList;\r
+import java.util.Stack;\r
+\r
+import org.simantics.basicexpression.analysis.DepthFirstAdapter;\r
+import org.simantics.basicexpression.node.AAddressValue;\r
+import org.simantics.basicexpression.node.AConstantValue;\r
+import org.simantics.basicexpression.node.AFunctionPrimary;\r
+import org.simantics.basicexpression.node.AMultMultiplicative;\r
+import org.simantics.basicexpression.node.APlusExpression;\r
+import org.simantics.basicexpression.node.ARangeValue;\r
+import org.simantics.basicexpression.node.ARviValue;\r
+import org.simantics.basicexpression.node.ASequenceArgList;\r
+import org.simantics.basicexpression.node.ASingleArgList;\r
+import org.simantics.basicexpression.node.ASingleRange;\r
+import org.simantics.basicexpression.node.AStringValue;\r
+import org.simantics.basicexpression.node.AVariablePrimary;\r
+import org.simantics.basicexpression.node.PArgList;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Function;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.spreadsheet.Range;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+\r
+public class SpreadsheetExpressionVisitor extends DepthFirstAdapter {\r
+\r
+ public static final boolean DEBUG_APPLICATION = false;\r
+ public static final boolean DEBUG = false;\r
+ \r
+ public static class ApplicationException extends Exception {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ public ApplicationException(String message) {\r
+ super(message);\r
+ }\r
+\r
+ }\r
+\r
+ final ReadGraph graph;\r
+ final Variable cellVariable;\r
+ final Resource model;\r
+ final Resource sheet;\r
+ final int row;\r
+ final int column;\r
+ // final EvaluationEnvironmentImpl env;\r
+ Stack<Object> stack = new Stack<Object>();\r
+\r
+ HashMap<String, Function> builtins = new HashMap<String, Function>();\r
+\r
+ // class EvaluationEnvironmentImpl implements EvaluationEnvironment {\r
+ // \r
+ // private boolean ready = false;\r
+ // private boolean valid = true;\r
+ // public HashSet<Range> deps = new HashSet<Range>();\r
+ // \r
+ //\r
+ // @Override\r
+ // public int getColumn() {\r
+ // return column;\r
+ // }\r
+ //\r
+ // @Override\r
+ // public Model getModel() {\r
+ // return model;\r
+ // }\r
+ //\r
+ // @Override\r
+ // public int getRow() {\r
+ // return row;\r
+ // }\r
+ // \r
+ // public void depend(Range range) {\r
+ // deps.add(range);\r
+ // }\r
+ // \r
+ // public void listen() {\r
+ // ready = true;\r
+ // }\r
+ // \r
+ // @Override\r
+ // public void invalidate() {\r
+ // // DependencyHandler handler = model.getAdapter(DependencyHandler.class);\r
+ // // handler.invalidate(row, column);\r
+ // if(!ready) return;\r
+ // valid = false;\r
+ // ThreadUtils.getNonBlockingWorkExecutor().execute(new Runnable() {\r
+ //\r
+ // @Override\r
+ // public void run() {\r
+ //\r
+ // Cell cell = model.get(row, column);\r
+ // UpdateHandler handler = cell.getAdapter(UpdateHandler.class);\r
+ // if(handler != null) handler.update();\r
+ // \r
+ // }\r
+ // \r
+ // });\r
+ // \r
+ // }\r
+ // \r
+ // @Override\r
+ // public boolean isDisposed() {\r
+ // return !valid;\r
+ // }\r
+ // \r
+ // }\r
+\r
+ public SpreadsheetExpressionVisitor(ReadGraph graph, Variable cellVariable, int row, int column) throws DatabaseException {\r
+\r
+ // assert(model != null);\r
+ // // assert(graph != null);\r
+ // // assert(sheet != null);\r
+ this.graph = graph;\r
+ this.cellVariable = cellVariable;\r
+ this.model = Variables.getModel(graph, cellVariable);\r
+ Resource cell = cellVariable.getPossiblePropertyValue(graph, Variables.RESOURCE); \r
+ this.sheet = graph.getPossibleObject(cell, Layer0.getInstance(graph).PartOf);\r
+// this.sheet = sheet;\r
+// this.model = model;\r
+ this.row = row;\r
+ this.column = column;\r
+ // \r
+ // builtins.put("Sequence", new Sequence());\r
+ // builtins.put("Set", new Set());\r
+ // builtins.put("SetBlock", new SetBlock());\r
+ // builtins.put("Sum", new Sum());\r
+ // builtins.put("Naturals", new Naturals());\r
+ // \r
+ // env = new EvaluationEnvironmentImpl();\r
+ // \r
+\r
+ // SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+ // \r
+ // builtins.put("SUBSCRIPT", new Runnable() {\r
+ //\r
+ // @Override\r
+ // public void run() {\r
+ // \r
+ // try {\r
+ // \r
+ // int column = ((Double)stack.pop()).intValue();\r
+ // int row = ((Double)stack.pop()).intValue();\r
+ // Object value = stack.pop();\r
+ // \r
+ // //System.out.println("subscript(value=" + value + " row=" + row +\r
+ // " column=" + column + ")");\r
+ // \r
+ // if(value instanceof Collection) {\r
+ // stack.push(((Collection)value).toArray()[row]);\r
+ // }\r
+ // \r
+ // } catch (Throwable t) {\r
+ // t.printStackTrace();\r
+ // }\r
+ // \r
+ // }\r
+ // \r
+ // });\r
+\r
+ }\r
+\r
+ public Object getResult() {\r
+ return stack.pop();\r
+ }\r
+\r
+ public void outAConstantValue(AConstantValue node) {\r
+ if(DEBUG) System.out.println("outAConstantValue " + node);\r
+ stack.push(Double.valueOf(node.toString()));\r
+ }\r
+\r
+ public void outAStringValue(AStringValue node) {\r
+ if(DEBUG) System.out.println("outAStringValue " + node);\r
+ String value = node.toString();\r
+ stack.push(value.substring(1, value.length() - 2).trim());\r
+ }\r
+\r
+ public void outAAddressValue(AAddressValue node) {\r
+ if(DEBUG) System.out.println("outAAddressValue " + node);\r
+ stack.push('&' + node.getRange().toString());\r
+ }\r
+\r
+ @Override\r
+ public void outASingleRange(ASingleRange node) {\r
+ \r
+ if(DEBUG) System.out.println("outASingleRange " + node);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void outARviValue(ARviValue node) {\r
+ \r
+ if(DEBUG) System.out.println("outARviValue " + node);\r
+ \r
+ String rvi = node.toString().trim();\r
+ \r
+ try {\r
+ System.out.println("browsing at " + cellVariable.getURI(graph));\r
+ Variable var = cellVariable.browse(graph, rvi);\r
+ stack.push(var);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void outAVariablePrimary(AVariablePrimary node) {\r
+ \r
+ if(DEBUG) System.out.println("outAVariablePrimary " + node);\r
+ \r
+ String identifier = node.toString().trim();\r
+\r
+ Range range = SpreadsheetUtils.decodeRange(identifier, row, column);\r
+ if (range.size() != 1) {\r
+ ArrayList<Object> value = new ArrayList<Object>();\r
+ for (int c = range.startColumn; c <= range.endColumn; c++) {\r
+ for (int r = range.startRow; r <= range.endRow; r++) {\r
+ try {\r
+ String location = SpreadsheetUtils.cellName(r, c);\r
+ Variable cell = cellVariable.getChild(graph, location);\r
+ System.out.println("cell=" + cell.getURI(graph));\r
+ String label = cell.getPossiblePropertyValue(graph, "Label");\r
+ System.out.println("lavel=" + label);\r
+ value.add(label);\r
+ } catch (DatabaseException e) {\r
+ value.add(null);\r
+ }\r
+ }\r
+ }\r
+ stack.push(value);\r
+ // System.out.println("pushing " + value);\r
+ return;\r
+ }\r
+\r
+// try {\r
+// String location = SpreadsheetUtils.cellName(range.startRow,\r
+// range.startColumn);\r
+// stack.push(graph.syncRequest(new CellResult(sheet, model, location)));\r
+// } catch (DatabaseException e) {\r
+// stack.push(null);\r
+// }\r
+ \r
+ }\r
+ \r
+ public void outARangeValue(ARangeValue node) {\r
+\r
+ if(DEBUG) System.out.println("outARangeValue " + node);\r
+\r
+ String identifier = node.getRange().toString().trim();\r
+ // try {\r
+\r
+// Range range = SpreadsheetUtils.decodeRange(identifier, row, column);\r
+// // env.depend(range);\r
+// if (range.size() != 1) {\r
+// ArrayList<Object> value = new ArrayList<Object>();\r
+// for (int c = range.startColumn; c <= range.endColumn; c++) {\r
+// for (int r = range.startRow; r <= range.endRow; r++) {\r
+// try {\r
+// String location = SpreadsheetUtils.cellName(r, c);\r
+// \r
+// value.add(graph\r
+// .syncRequest(new CellResult(sheet, model, location)));\r
+// } catch (DatabaseException e) {\r
+// value.add(null);\r
+// }\r
+// }\r
+// }\r
+// stack.push(value);\r
+// // System.out.println("pushing " + value);\r
+// return;\r
+// }\r
+//\r
+// try {\r
+// String location = SpreadsheetUtils.cellName(range.startRow,\r
+// range.startColumn);\r
+// stack.push(graph.syncRequest(new CellResult(sheet, model, location)));\r
+// } catch (DatabaseException e) {\r
+// stack.push(null);\r
+// }\r
+\r
+ }\r
+\r
+ private double extractValue(Object o) {\r
+ if (o instanceof Number) {\r
+ return ((Number) o).doubleValue();\r
+ } else if (o instanceof String) {\r
+ return Double.valueOf((String) o);\r
+ } else {\r
+ return Double.NaN;\r
+ }\r
+ }\r
+\r
+ public void outAPlusExpression(APlusExpression node) {\r
+ \r
+ if(DEBUG) System.out.println("outAPlusExpression " + node);\r
+ \r
+ Object o1 = stack.pop();\r
+ Object o2 = stack.pop();\r
+ double d1 = extractValue(o1);\r
+ double d2 = extractValue(o2);\r
+ stack.push(d1 + d2);\r
+ // System.out.println("plus " + d1 + " " + d2);\r
+ }\r
+\r
+ public void outAMultMultiplicative(AMultMultiplicative node) {\r
+ \r
+ if(DEBUG) System.out.println("outAMultiplicative " + node);\r
+ \r
+ Object o1 = stack.pop();\r
+ Object o2 = stack.pop();\r
+ double d1 = extractValue(o1);\r
+ double d2 = extractValue(o2);\r
+ stack.push(d1 * d2);\r
+ // System.out.println("mult " + d1 + " " + d2);\r
+ }\r
+\r
+ int countArguments(PArgList args) {\r
+ if (args == null)\r
+ return 0;\r
+ if (args instanceof ASingleArgList)\r
+ return 1;\r
+ ASequenceArgList seq = (ASequenceArgList) args;\r
+ return 1 + countArguments(seq.getArgList());\r
+ }\r
+\r
+ public void outAFunctionPrimary(AFunctionPrimary node) {\r
+\r
+ if(DEBUG) System.out.println("outAFunctionPrimary " + node);\r
+\r
+ try {\r
+\r
+ String functionName = node.getFunc().getText().replace("(", "");\r
+\r
+ if (DEBUG_APPLICATION)\r
+ System.out.println("function apply " + functionName);\r
+\r
+ Function function = builtins.get(functionName);\r
+ if (function != null) {\r
+\r
+ LinkedList<Object> args = new LinkedList<Object>();\r
+ int argc = countArguments(node.getArgList());\r
+ for (int i = 0; i < argc; i++) {\r
+ args.addFirst(stack.pop());\r
+ }\r
+ args.addFirst(sheet);\r
+ args.addFirst(model);\r
+\r
+ Object result = function.apply(graph, args);\r
+ stack.push(result);\r
+\r
+ } else {\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Instances instances = graph.adapt(L0.Function, Instances.class);\r
+ Collection<Resource> functions = instances.find(graph, model,\r
+ "Name:" + functionName);\r
+ if (DEBUG_APPLICATION)\r
+ System.out.println("Found " + functions.size() + " matches.");\r
+ if (functions != null && functions.size() == 1) {\r
+\r
+ Resource functionResource = functions.iterator().next();\r
+ function = graph.adapt(functionResource, Function.class);\r
+ LinkedList<Object> args = new LinkedList<Object>();\r
+ int argc = countArguments(node.getArgList());\r
+ for (int i = 0; i < argc; i++) {\r
+ args.addFirst(stack.pop());\r
+ }\r
+ args.addFirst(cellVariable);\r
+ //args.addFirst(model);\r
+ // System.out.println("args=" +\r
+ // Arrays.toString(args.toArray()));\r
+ Object result = function.apply(graph, args.toArray());\r
+ stack.push(result);\r
+\r
+ } else {\r
+\r
+ stack.push(null);\r
+\r
+ }\r
+\r
+ }\r
+\r
+ } catch (DatabaseException e) {\r
+\r
+ stack.push(null);\r
+\r
+ }\r
+\r
+ }\r
+\r
+}\r