1 package org.simantics.spreadsheet.graph;
3 import java.io.BufferedInputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectStreamClass;
10 import java.util.Collection;
11 import java.util.HashSet;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.variable.ProxyVariables;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.simulator.toolkit.StandardRealm;
22 import org.simantics.simulator.toolkit.db.StandardVariableSessionManager;
23 import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler;
24 import org.simantics.spreadsheet.resource.SpreadsheetResource;
25 import org.simantics.spreadsheet.solver.SheetNode;
26 import org.simantics.spreadsheet.solver.SpreadsheetBook;
27 import org.simantics.spreadsheet.solver.formula.SpreadsheetEvaluationEnvironment;
28 import org.simantics.structural.synchronization.client.Synchronizer;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class SpreadsheetSessionManager extends StandardVariableSessionManager<SheetNode, SpreadsheetBook> {
34 private static final Logger LOGGER = LoggerFactory.getLogger(SpreadsheetSessionManager.class);
36 private static SpreadsheetSessionManager INSTANCE;
38 public static SpreadsheetSessionManager getInstance() {
39 if(INSTANCE == null) {
40 INSTANCE = new SpreadsheetSessionManager();
45 public class ClassLoaderObjectInputStream extends ObjectInputStream{
47 private ClassLoader classLoader;
49 public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
51 this.classLoader = classLoader;
55 protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{
58 String name = desc.getName();
59 return Class.forName(name, false, classLoader);
61 catch(ClassNotFoundException e){
62 return super.resolveClass(desc);
67 private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException {
68 SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);
69 Collection<Resource> ics = graph.getObjects(context, SR.HasInitialCondition);
71 Resource found = null;
72 Set<Resource> founds = new HashSet<>();
73 Set<Resource> foundDefaults = new HashSet<>();
75 for (Resource initialCondition : ics) {
76 if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) {
77 foundDefaults.add(initialCondition);
79 if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) {
80 founds.add(initialCondition);
83 if (foundDefaults.size() == 1) {
84 found = foundDefaults.iterator().next();
85 } else if (foundDefaults.size() == 0 && founds.size() == 1) {
86 found = founds.iterator().next();
88 System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book));
89 System.err.println("foundDefaults : " + foundDefaults.size());
90 if (!foundDefaults.isEmpty()) {
91 for (Resource foundDefault : foundDefaults) {
92 System.err.println(graph.getPossibleURI(foundDefault));
95 System.err.println("founds : " + founds.size());
96 if (!founds.isEmpty()) {
97 for (Resource foun : founds) {
98 System.err.println(graph.getPossibleURI(foun));
105 File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found);
106 System.err.println("Extracting IC from " + tmp.getAbsolutePath());
107 InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp));
108 ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn);
109 SpreadsheetBook srBook = (SpreadsheetBook) in.readObject();
112 } catch (IOException e) {
113 throw new DatabaseException(e);
114 } catch (ClassNotFoundException e) {
115 throw new DatabaseException(e);
122 protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException {
124 Variable run = Variables.getVariable(graph, id);
125 Variable context = ProxyVariables.proxyVariableInput(graph, run);
126 if (context != null) {
127 Variable base = ProxyVariables.proxyVariableBase(graph, run);
128 Resource bookResource = base.getRepresents(graph);
129 Resource contextResource = context.getRepresents(graph);
130 if (contextResource != null) {
131 SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource);
136 SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource);
141 SpreadsheetBook book = new SpreadsheetBook(context.getURI(graph));
143 Variable base = ProxyVariables.proxyVariableBase(graph, run);
144 Resource bookResource = base.getRepresents(graph);
145 Variable configuration = Variables.getVariable(graph, bookResource);
147 SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book);
148 Synchronizer synchronizer = new Synchronizer(graph);
149 synchronizer.fullSynchronization(configuration, handler);
156 protected StandardRealm<SheetNode, SpreadsheetBook> createRealm(SpreadsheetBook engine, String id) {
157 return new SpreadsheetRealm(engine, id);
161 public void removeRealm(WriteGraph graph, String id) throws DatabaseException {
162 StandardRealm<SheetNode, SpreadsheetBook> realm = getOrCreateRealm(graph, id);
163 SpreadsheetEvaluationEnvironment.removeInstance(realm.getEngine());
164 super.removeRealm(graph, id);
167 // Utility function for SCL, this should maybe be replaced with something better in the future
168 public static void removeSpreadsheetSession(WriteGraph graph, Variable runVariable) throws DatabaseException {
169 String uri = runVariable.getParent(graph).getURI(graph);
170 getInstance().removeRealm(graph, uri);