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;
9 import java.util.HashSet;
10 import java.util.List;
12 import java.util.Optional;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.databoard.binding.Binding;
17 import org.simantics.databoard.binding.mutable.Variant;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.simulator.toolkit.StandardNodeManagerSupport;
20 import org.simantics.simulator.variable.exceptions.NodeManagerException;
21 import org.simantics.spreadsheet.graph.formula.SpreadsheetEvaluationEnvironment;
22 import org.simantics.spreadsheet.graph.synchronization.LineNodeUpdater;
23 import org.simantics.spreadsheet.graph.synchronization.LineUpdater;
24 import org.simantics.spreadsheet.graph.synchronization.NullUpdater;
25 import org.simantics.spreadsheet.graph.synchronization.SheetLineComponent;
26 import org.simantics.spreadsheet.graph.synchronization.StyleUpdater;
27 import org.simantics.structural.synchronization.base.ComponentFactory;
28 import org.simantics.structural.synchronization.base.MappingBase;
29 import org.simantics.structural.synchronization.base.ModuleUpdaterBase;
30 import org.simantics.structural.synchronization.base.ModuleUpdaterFactoryBase;
31 import org.simantics.structural.synchronization.base.Solver;
32 import org.simantics.structural.synchronization.base.SolverNameUtil;
34 import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
35 import it.unimi.dsi.fastutil.longs.AbstractLongList;
36 import it.unimi.dsi.fastutil.longs.AbstractLongSet;
37 import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
38 import it.unimi.dsi.fastutil.longs.LongArraySet;
39 import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
41 public class SpreadsheetBook implements StandardNodeManagerSupport<SheetNode>, SpreadsheetElement<SpreadsheetElement, SpreadsheetElement>, Serializable, SheetNode<SpreadsheetEngine, SheetNode>, Solver, SolverNameUtil, ComponentFactory<SheetLineComponent>, ModuleUpdaterFactoryBase<SheetLineComponent> {
43 private static final long serialVersionUID = 7417208688311691396L;
45 public Serializable NotAvailableError = new Serializable() {};
47 public Long2ObjectOpenHashMap<AbstractLongSet> referenceMap = new Long2ObjectOpenHashMap<>();
49 private Int2ObjectArrayMap<SpreadsheetStyle> styles = new Int2ObjectArrayMap<>();
50 // public ObjectArrayList<SpreadsheetStyle> styles = new ObjectArrayList<SpreadsheetStyle>();
51 public ArrayList<SpreadsheetEngine> sheets = new ArrayList<SpreadsheetEngine>();
53 private SpreadsheetMapping mapping;
56 private int idCounter = 1;
58 public SpreadsheetBook() {
60 mapping = new SpreadsheetMapping(new SheetLineComponent(""));
63 public Map<Integer, SpreadsheetElement> children = new HashMap<>();
65 private boolean iterationEnabled;
67 public int getNewId(SpreadsheetElement element) {
68 int result = idCounter++;
69 children.put(result, element);
73 public long getEngineIndex(SpreadsheetEngine engine) {
74 for(int i=0;i<sheets.size();i++)
75 if(sheets.get(i) == engine) return i;
76 throw new IllegalStateException("Did not find sheet " + engine.getName());
79 public void registerReferences(long cell, AbstractLongList refs) {
80 for(int i=0;i<refs.size();i++) {
81 long key = refs.getLong(i);
82 AbstractLongSet set = referenceMap.get(key);
84 set = new LongArraySet();
85 referenceMap.put(key, set);
88 AbstractLongSet newSet = new LongLinkedOpenHashSet();
91 referenceMap.put(key, set);
98 public Binding getEngineBinding(SheetNode node) throws NodeManagerException {
99 Object value = getEngineValue(node);
100 if(value instanceof Variant) return Bindings.VARIANT;
101 if(value instanceof String) return Bindings.STRING;
102 if(value instanceof Boolean) return Bindings.BOOLEAN;
103 else return Bindings.VOID;
108 public Object getEngineValue(SheetNode node) {
109 if(node instanceof SpreadsheetCellContent) {
111 SpreadsheetCellContent scc = (SpreadsheetCellContent)node;
112 Object content = scc.cell.evaluate(SpreadsheetEvaluationEnvironment.getInstance(this));
113 if(content == null) return Variant.ofInstance("");
114 if(content instanceof Variant) return content;
115 else return Variant.ofInstance(content);
116 } catch (Throwable t) {
118 return Variant.ofInstance(t.toString());
120 } else if (node instanceof SpreadsheetCellContentExpression) {
121 SpreadsheetCellContentExpression scce = (SpreadsheetCellContentExpression)node;
122 if (scce.cell.content instanceof SpreadsheetFormula) {
123 SpreadsheetFormula formula = (SpreadsheetFormula)scce.cell.content;
124 return formula.expression;
125 } else if (scce.cell.content instanceof SpreadsheetSCLConstant) {
126 SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) scce.cell.content;
127 return "=" + sclConstant.expression;
129 System.out.println("Broken SpreadsheetCellContentExpression possibly due to overwriting an existing expression with a constant or something else (current content is " + scce.cell.content + ")");
130 if (scce.cell.content instanceof Variant) {
131 return scce.cell.content;
133 return Variant.ofInstance(scce.cell.content);
136 } else if (node instanceof SpreadsheetTypeNode) {
137 SpreadsheetTypeNode stn = (SpreadsheetTypeNode)node;
139 } else if (node instanceof SpreadsheetCellStyle) {
140 int styleId = ((SpreadsheetCellStyle) node).cell.style;
141 SpreadsheetStyle style = getStyle(styleId);
143 style = SpreadsheetStyle.empty();
144 if (styleId != style.getStyleId())
145 new Exception("different style ids!" + styleId + " " + style.getStyleId()).printStackTrace();
149 } else if (node instanceof SpreadsheetCellEditable) {
150 boolean editable = ((SpreadsheetCellEditable) node).editable();
157 public void setEngineValue(SheetNode node, Object value) {
161 public String getName(SheetNode node) {
162 return node.getName();
166 public Map<String, SheetNode> getChildren(SheetNode node) {
167 return node.getChildren();
171 public Map<String, SheetNode> getProperties(SheetNode node) {
172 return node.getProperties();
176 public String getName() {
181 public Map<String, SpreadsheetEngine> getChildren() {
182 Map<String,SpreadsheetEngine> result = new HashMap<String,SpreadsheetEngine>();
183 for(SpreadsheetEngine engine : sheets)
184 result.put(engine.getName(), engine);
189 public Map<String, SheetNode> getProperties() {
190 return Collections.emptyMap();
193 public SpreadsheetCell get(String sheet, int row, int column) {
194 SpreadsheetEngine engine = getEngine(sheet);
195 if(engine == null) return null;
196 SpreadsheetLine line = engine.getLine(row);
197 if(line == null) return null;
198 if(line.cells.size() <= column) return null;
199 return line.cells.get(column);
202 public SpreadsheetCell get(SpreadsheetEngine engine, int row, int column) {
203 SpreadsheetLine line = engine.getLine(row);
204 if(line == null) return null;
205 if(line.cells.size() <= column) return null;
206 return line.cells.get(column);
209 public SpreadsheetEngine getEngine(String sheet) {
210 for(SpreadsheetEngine engine : sheets)
211 if(sheet.equals(engine.getName())) return engine;
216 public ModuleUpdaterBase<SheetLineComponent> createUpdater(String id) throws DatabaseException {
217 if("http://www.simantics.org/Spreadsheet-1.2/Line".equals(id))
218 return new LineUpdater(id);
219 else if("http://www.simantics.org/Spreadsheet-1.2/LineNode".equals(id))
220 return new LineNodeUpdater(id);
221 else if ("http://www.simantics.org/Spreadsheet-1.2/Style".equals(id))
222 return new StyleUpdater(id);
223 else if("http://www.simantics.org/Spreadsheet-1.2/Lines".equals(id))
224 return new NullUpdater(id);
225 else if("http://www.simantics.org/Spreadsheet-1.2/Spreadsheet".equals(id))
226 return new NullUpdater(id);
227 else if("http://www.simantics.org/Spreadsheet-1.2/Book".equals(id))
228 return new NullUpdater(id);
230 throw new IllegalStateException("createUpdater " + id);
234 public SheetLineComponent create(String uid) {
235 return new SheetLineComponent(uid);
239 public String getFreshName(String parentName, String name) {
240 return parentName + "/" + name;
244 public String ensureNameIsVariationOf(String parentName, int id, String name) {
245 if (parentName.isEmpty())
247 return parentName + "/" + name;
250 final static int COMP_ROOT_POS = "COMP_ROOT/".length();
253 public int getId(String name) {
255 if("COMP_ROOT".equals(name)) return 1;
257 String path = name.substring(COMP_ROOT_POS);
258 String[] parts = path.split("/");
259 Object o = resolve(parts, 0);
260 if(o instanceof SpreadsheetLines) {
261 return ((SpreadsheetLines)o).getId();
262 } else if(o instanceof SpreadsheetLine) {
263 return ((SpreadsheetLine)o).getId();
264 } else if(o instanceof SpreadsheetEngine) {
265 return ((SpreadsheetEngine)o).getId();
266 } else if (o instanceof SpreadsheetStyle) {
267 return ((SpreadsheetStyle) o).getId();
270 throw new IllegalStateException("Resolved object for parts " + Arrays.toString(parts) + " is not the right type! It is " + o);
274 Object resolve(String[] parts, int index) {
275 String part = parts[index];
276 if (part.startsWith("Style")) {
277 for (SpreadsheetStyle style : styles.values()) {
278 if (style.name.equals(part)) {
283 SpreadsheetEngine engine = getEngine(part);
284 if(engine == null) return 0;
285 if(index == parts.length-1) return engine;
286 else return engine.resolve(parts, index+1);
290 public String getName(int id) {
291 if(id == -2) return "http://www.simantics.org/Spreadsheet-1.2/Book";
292 else if(id == -3) return "http://www.simantics.org/Spreadsheet-1.2/Spreadsheet";
293 else if(id == -4) return "http://www.simantics.org/Spreadsheet-1.2/Lines";
295 return "http://www.simantics.org/Spreadsheet-1.2/LineNode";
297 return "http://www.simantics.org/Spreadsheet-1.2/Line";
299 return "http://www.simantics.org/Spreadsheet-1.2/Style";
304 public int getModuleType(int id) {
305 Serializable s = children.get(id);
306 if(s instanceof SpreadsheetBook) return -2;
307 else if(s instanceof SpreadsheetEngine) return -3;
308 else if(s instanceof SpreadsheetLines) {
309 if("Lines".equals(((SpreadsheetLines) s).getName()))
314 else if(s instanceof SpreadsheetLine)
316 else if (s instanceof SpreadsheetStyle)
318 else throw new IllegalStateException();
322 public void remove(int id) {
324 SpreadsheetElement child = children.get(id);
325 Optional<SpreadsheetElement> parent = child.getParent();
327 if (parent.isPresent()) {
328 parent.get().remove(child);
334 public void addSubprocess(String name, String subprocessType) {
335 ensureSubprocess(name);
338 public <T> T ensureSubprocess(String name) {
339 String[] parts = name.split("/");
340 if(parts.length == 2) {
341 SpreadsheetEngine engine = getEngine(parts[1]);
343 engine = new SpreadsheetEngine(this, parts[1]);
347 } else if (parts.length > 2) {
348 SpreadsheetEngine engine = getEngine(parts[1]);
349 return (T)engine.ensureSubprocess(parts, 2);
351 throw new IllegalStateException();
356 public void includeSubprocess(String parentName, String subprocessName) {
361 public <T> T getConcreteSolver() {
366 public void accept(SpreadsheetVisitor v) {
370 //Recursively find all SpreadsheetCells, invalidate them and return them all as a set
371 public Set<SpreadsheetCell> invalidate(SpreadsheetCell cell) {
372 Set<SpreadsheetCell> result = new HashSet<>();
375 long refKey = cell.makeReferenceKey();
376 AbstractLongSet refs = referenceMap.remove(refKey);
377 if(refs == null) return result;
378 for(long ref : refs) {
379 long sheet = ref >> 40;
380 long row = (ref >> 20) & 0xFFFFF;
381 long col = (ref) & 0xFFFFF;
382 SpreadsheetCell referer = get(sheets.get((int)sheet), (int)row, (int)col);
383 result.addAll(invalidate(referer));
389 public List<SpreadsheetCell> invalidateShallow(SpreadsheetCell cell) {
390 ArrayList<SpreadsheetCell> result = new ArrayList<>();
393 long refKey = cell.makeReferenceKey();
394 AbstractLongSet refs = referenceMap.remove(refKey);
395 if(refs == null) return result;
396 for(long ref : refs) {
397 long sheet = ref >> 40;
398 long row = (ref >> 20) & 0xFFFFF;
399 long col = (ref) & 0xFFFFF;
400 SpreadsheetCell referer = get(sheets.get((int)sheet), (int)row, (int)col);
407 public void addStyle(SpreadsheetStyle style) {
408 if (style.name == null) {
409 new Exception("Trying to add style to book without name!!").printStackTrace();
412 style.setSynchronizationId(getNewId(style));
413 styles.put(style.getStyleId(), style);
416 public SpreadsheetStyle getStyle(int styleId) {
417 return styles.get(styleId);
420 public MappingBase<SheetLineComponent> getMapping() {
425 public Optional<SpreadsheetElement> getParent() {
426 return Optional.empty();
430 public Collection<SpreadsheetElement> getSpreadsheetChildren() {
431 return children.values();
435 public void remove(SpreadsheetElement child) {
436 // TODO Auto-generated method stub
440 public void setIterationEnabled(boolean value) {
441 this.iterationEnabled = value;
444 public boolean isIterationEnabled() {
445 return iterationEnabled;