1 package org.simantics.spreadsheet.graph;
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
11 import java.util.Optional;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.mutable.Variant;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.simulator.toolkit.StandardNodeManagerSupport;
18 import org.simantics.simulator.variable.exceptions.NodeManagerException;
19 import org.simantics.spreadsheet.graph.formula.SpreadsheetEvaluationEnvironment;
20 import org.simantics.spreadsheet.graph.synchronization.LineNodeUpdater;
21 import org.simantics.spreadsheet.graph.synchronization.LineUpdater;
22 import org.simantics.spreadsheet.graph.synchronization.NullUpdater;
23 import org.simantics.spreadsheet.graph.synchronization.SheetLineComponent;
24 import org.simantics.spreadsheet.graph.synchronization.StyleUpdater;
25 import org.simantics.structural.synchronization.base.ComponentFactory;
26 import org.simantics.structural.synchronization.base.MappingBase;
27 import org.simantics.structural.synchronization.base.ModuleUpdaterBase;
28 import org.simantics.structural.synchronization.base.ModuleUpdaterFactoryBase;
29 import org.simantics.structural.synchronization.base.Solver;
30 import org.simantics.structural.synchronization.base.SolverNameUtil;
32 import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
33 import it.unimi.dsi.fastutil.longs.AbstractLongList;
34 import it.unimi.dsi.fastutil.longs.AbstractLongSet;
35 import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
36 import it.unimi.dsi.fastutil.longs.LongArraySet;
37 import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
39 public class SpreadsheetBook implements StandardNodeManagerSupport<SheetNode>, SpreadsheetElement<SpreadsheetElement, SpreadsheetElement>, Serializable, SheetNode<SpreadsheetEngine, SheetNode>, Solver, SolverNameUtil, ComponentFactory<SheetLineComponent>, ModuleUpdaterFactoryBase<SheetLineComponent> {
41 private static final long serialVersionUID = 7417208688311691396L;
43 public Serializable NotAvailableError = new Serializable() {};
45 public Long2ObjectOpenHashMap<AbstractLongSet> referenceMap = new Long2ObjectOpenHashMap<>();
47 private Int2ObjectArrayMap<SpreadsheetStyle> styles = new Int2ObjectArrayMap<>();
48 // public ObjectArrayList<SpreadsheetStyle> styles = new ObjectArrayList<SpreadsheetStyle>();
49 public ArrayList<SpreadsheetEngine> sheets = new ArrayList<SpreadsheetEngine>();
51 private SpreadsheetMapping mapping;
54 private int idCounter = 1;
56 public SpreadsheetBook() {
58 mapping = new SpreadsheetMapping(new SheetLineComponent(""));
61 public Map<Integer, SpreadsheetElement> children = new HashMap<>();
63 private boolean iterationEnabled;
65 public int getNewId(SpreadsheetElement element) {
66 int result = idCounter++;
67 children.put(result, element);
71 public long getEngineIndex(SpreadsheetEngine engine) {
72 for(int i=0;i<sheets.size();i++)
73 if(sheets.get(i) == engine) return i;
74 throw new IllegalStateException("Did not find sheet " + engine.getName());
77 public void registerReferences(long cell, AbstractLongList refs) {
78 for(int i=0;i<refs.size();i++) {
79 long key = refs.getLong(i);
80 AbstractLongSet set = referenceMap.get(key);
82 set = new LongArraySet();
83 referenceMap.put(key, set);
86 AbstractLongSet newSet = new LongLinkedOpenHashSet();
89 referenceMap.put(key, set);
96 public Binding getEngineBinding(SheetNode node) throws NodeManagerException {
97 Object value = getEngineValue(node);
98 if(value instanceof Variant) return Bindings.VARIANT;
99 if(value instanceof String) return Bindings.STRING;
100 if(value instanceof Boolean) return Bindings.BOOLEAN;
101 else return Bindings.VOID;
106 public Object getEngineValue(SheetNode node) {
107 if(node instanceof SpreadsheetCellContent) {
109 SpreadsheetCellContent scc = (SpreadsheetCellContent)node;
110 Object content = scc.cell.evaluate(SpreadsheetEvaluationEnvironment.getInstance(this));
111 if(content == null) return Variant.ofInstance("");
112 if(content instanceof Variant) return content;
113 else return Variant.ofInstance(content);
114 } catch (Throwable t) {
116 return Variant.ofInstance(t.toString());
118 } else if (node instanceof SpreadsheetCellContentExpression) {
119 SpreadsheetCellContentExpression scce = (SpreadsheetCellContentExpression)node;
120 if (scce.cell.content instanceof SpreadsheetFormula) {
121 SpreadsheetFormula formula = (SpreadsheetFormula)scce.cell.content;
122 return formula.expression;
123 } else if (scce.cell.content instanceof SpreadsheetSCLConstant) {
124 SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) scce.cell.content;
125 return "=" + sclConstant.expression;
127 System.out.println("Broken SpreadsheetCellContentExpression possibly due to overwriting an existing expression with a constant or something else (current content is " + scce.cell.content + ")");
128 if (scce.cell.content instanceof Variant) {
129 return scce.cell.content;
131 return Variant.ofInstance(scce.cell.content);
134 } else if (node instanceof SpreadsheetTypeNode) {
135 SpreadsheetTypeNode stn = (SpreadsheetTypeNode)node;
137 } else if (node instanceof SpreadsheetCellStyle) {
138 int styleId = ((SpreadsheetCellStyle) node).cell.style;
139 SpreadsheetStyle style = getStyle(styleId);
141 style = SpreadsheetStyle.empty();
142 if (styleId != style.getStyleId())
143 new Exception("different style ids!" + styleId + " " + style.getStyleId()).printStackTrace();
147 } else if (node instanceof SpreadsheetCellEditable) {
148 boolean editable = ((SpreadsheetCellEditable) node).editable();
155 public void setEngineValue(SheetNode node, Object value) {
159 public String getName(SheetNode node) {
160 return node.getName();
164 public Map<String, SheetNode> getChildren(SheetNode node) {
165 return node.getChildren();
169 public Map<String, SheetNode> getProperties(SheetNode node) {
170 return node.getProperties();
174 public String getName() {
179 public Map<String, SpreadsheetEngine> getChildren() {
180 Map<String,SpreadsheetEngine> result = new HashMap<String,SpreadsheetEngine>();
181 for(SpreadsheetEngine engine : sheets)
182 result.put(engine.getName(), engine);
187 public Map<String, SheetNode> getProperties() {
188 return Collections.emptyMap();
191 public SpreadsheetCell get(String sheet, int row, int column) {
192 SpreadsheetEngine engine = getEngine(sheet);
193 if(engine == null) return null;
194 SpreadsheetLine line = engine.getLine(row);
195 if(line == null) return null;
196 if(line.cells.size() <= column) return null;
197 return line.cells.get(column);
200 public SpreadsheetCell get(SpreadsheetEngine engine, int row, int column) {
201 SpreadsheetLine line = engine.getLine(row);
202 if(line == null) return null;
203 if(line.cells.size() <= column) return null;
204 return line.cells.get(column);
207 public SpreadsheetEngine getEngine(String sheet) {
208 for(SpreadsheetEngine engine : sheets)
209 if(sheet.equals(engine.getName())) return engine;
214 public ModuleUpdaterBase<SheetLineComponent> createUpdater(String id) throws DatabaseException {
215 if("http://www.simantics.org/Spreadsheet-1.2/Line".equals(id))
216 return new LineUpdater(id);
217 else if("http://www.simantics.org/Spreadsheet-1.2/LineNode".equals(id))
218 return new LineNodeUpdater(id);
219 else if ("http://www.simantics.org/Spreadsheet-1.2/Style".equals(id))
220 return new StyleUpdater(id);
221 else if("http://www.simantics.org/Spreadsheet-1.2/Lines".equals(id))
222 return new NullUpdater(id);
223 else if("http://www.simantics.org/Spreadsheet-1.2/Spreadsheet".equals(id))
224 return new NullUpdater(id);
225 else if("http://www.simantics.org/Spreadsheet-1.2/Book".equals(id))
226 return new NullUpdater(id);
228 throw new IllegalStateException("createUpdater " + id);
232 public SheetLineComponent create(String uid) {
233 return new SheetLineComponent(uid);
237 public String getFreshName(String parentName, String name) {
238 return parentName + "/" + name;
242 public String ensureNameIsVariationOf(String parentName, int id, String name) {
243 if (parentName.isEmpty())
245 return parentName + "/" + name;
248 final static int COMP_ROOT_POS = "COMP_ROOT/".length();
251 public int getId(String name) {
253 if("COMP_ROOT".equals(name)) return 1;
255 String path = name.substring(COMP_ROOT_POS);
256 String[] parts = path.split("/");
257 Object o = resolve(parts, 0);
258 if(o instanceof SpreadsheetLines) {
259 return ((SpreadsheetLines)o).getId();
260 } else if(o instanceof SpreadsheetLine) {
261 return ((SpreadsheetLine)o).getId();
262 } else if(o instanceof SpreadsheetEngine) {
263 return ((SpreadsheetEngine)o).getId();
264 } else if (o instanceof SpreadsheetStyle) {
265 return ((SpreadsheetStyle) o).getId();
268 throw new IllegalStateException("Resolved object for parts " + Arrays.toString(parts) + " is not the right type! It is " + o);
272 Object resolve(String[] parts, int index) {
273 String part = parts[index];
274 if (part.startsWith("Style")) {
275 for (SpreadsheetStyle style : styles.values()) {
276 if (style.name.equals(part)) {
281 SpreadsheetEngine engine = getEngine(part);
282 if(engine == null) return 0;
283 if(index == parts.length-1) return engine;
284 else return engine.resolve(parts, index+1);
288 public String getName(int id) {
289 if(id == -2) return "http://www.simantics.org/Spreadsheet-1.2/Book";
290 else if(id == -3) return "http://www.simantics.org/Spreadsheet-1.2/Spreadsheet";
291 else if(id == -4) return "http://www.simantics.org/Spreadsheet-1.2/Lines";
293 return "http://www.simantics.org/Spreadsheet-1.2/LineNode";
295 return "http://www.simantics.org/Spreadsheet-1.2/Line";
297 return "http://www.simantics.org/Spreadsheet-1.2/Style";
302 public int getModuleType(int id) {
303 Serializable s = children.get(id);
304 if(s instanceof SpreadsheetBook) return -2;
305 else if(s instanceof SpreadsheetEngine) return -3;
306 else if(s instanceof SpreadsheetLines) {
307 if("Lines".equals(((SpreadsheetLines) s).getName()))
312 else if(s instanceof SpreadsheetLine)
314 else if (s instanceof SpreadsheetStyle)
316 else throw new IllegalStateException();
320 public void remove(int id) {
322 SpreadsheetElement child = children.get(id);
323 Optional<SpreadsheetElement> parent = child.getParent();
325 if (parent.isPresent()) {
326 parent.get().remove(child);
332 public void addSubprocess(String name, String subprocessType) {
333 ensureSubprocess(name);
336 public <T> T ensureSubprocess(String name) {
337 String[] parts = name.split("/");
338 if(parts.length == 2) {
339 SpreadsheetEngine engine = getEngine(parts[1]);
341 engine = new SpreadsheetEngine(this, parts[1]);
345 } else if (parts.length > 2) {
346 SpreadsheetEngine engine = getEngine(parts[1]);
347 return (T)engine.ensureSubprocess(parts, 2);
349 throw new IllegalStateException();
354 public void includeSubprocess(String parentName, String subprocessName) {
359 public <T> T getConcreteSolver() {
364 public void accept(SpreadsheetVisitor v) {
368 public List<SpreadsheetCell> invalidate(SpreadsheetCell cell) {
369 ArrayList<SpreadsheetCell> result = new ArrayList<>();
372 long refKey = cell.makeReferenceKey();
373 AbstractLongSet refs = referenceMap.remove(refKey);
374 if(refs == null) return result;
375 for(long ref : refs) {
376 long sheet = ref >> 40;
377 long row = (ref >> 20) & 0xFFFFF;
378 long col = (ref) & 0xFFFFF;
379 SpreadsheetCell referer = get(sheets.get((int)sheet), (int)row, (int)col);
386 public void addStyle(SpreadsheetStyle style) {
387 if (style.name == null) {
388 new Exception("Trying to add style to book without name!!").printStackTrace();
391 style.setSynchronizationId(getNewId(style));
392 styles.put(style.getStyleId(), style);
395 public SpreadsheetStyle getStyle(int styleId) {
396 return styles.get(styleId);
399 public MappingBase<SheetLineComponent> getMapping() {
404 public Optional<SpreadsheetElement> getParent() {
405 return Optional.empty();
409 public Collection<SpreadsheetElement> getSpreadsheetChildren() {
410 return children.values();
414 public void remove(SpreadsheetElement child) {
415 // TODO Auto-generated method stub
419 public void setIterationEnabled(boolean value) {
420 this.iterationEnabled = value;
423 public boolean isIterationEnabled() {
424 return iterationEnabled;