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.formula.SpreadsheetEvaluationEnvironment;
24 import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler;
25 import org.simantics.spreadsheet.resource.SpreadsheetResource;
26 import org.simantics.structural.synchronization.Synchronizer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class SpreadsheetSessionManager extends StandardVariableSessionManager<SheetNode, SpreadsheetBook> {
32 private static final Logger LOGGER = LoggerFactory.getLogger(SpreadsheetSessionManager.class);
34 private static SpreadsheetSessionManager INSTANCE;
36 public static SpreadsheetSessionManager getInstance() {
37 if(INSTANCE == null) {
38 INSTANCE = new SpreadsheetSessionManager();
43 public class ClassLoaderObjectInputStream extends ObjectInputStream{
45 private ClassLoader classLoader;
47 public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
49 this.classLoader = classLoader;
53 protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{
56 String name = desc.getName();
57 return Class.forName(name, false, classLoader);
59 catch(ClassNotFoundException e){
60 return super.resolveClass(desc);
65 private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException {
66 SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);
67 Collection<Resource> ics = graph.getObjects(context, SR.HasInitialCondition);
69 Resource found = null;
70 Set<Resource> founds = new HashSet<>();
71 Set<Resource> foundDefaults = new HashSet<>();
73 for (Resource initialCondition : ics) {
74 if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) {
75 foundDefaults.add(initialCondition);
77 if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) {
78 founds.add(initialCondition);
81 if (foundDefaults.size() == 1) {
82 found = foundDefaults.iterator().next();
83 } else if (foundDefaults.size() == 0 && founds.size() == 1) {
84 found = founds.iterator().next();
86 System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book));
87 System.err.println("foundDefaults : " + foundDefaults.size());
88 if (!foundDefaults.isEmpty()) {
89 for (Resource foundDefault : foundDefaults) {
90 System.err.println(graph.getPossibleURI(foundDefault));
93 System.err.println("founds : " + founds.size());
94 if (!founds.isEmpty()) {
95 for (Resource foun : founds) {
96 System.err.println(graph.getPossibleURI(foun));
103 File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found);
104 System.err.println("Extracting IC from " + tmp.getAbsolutePath());
105 InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp));
106 ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn);
107 SpreadsheetBook srBook = (SpreadsheetBook) in.readObject();
110 } catch (IOException e) {
111 throw new DatabaseException(e);
112 } catch (ClassNotFoundException e) {
113 throw new DatabaseException(e);
120 protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException {
122 Variable run = Variables.getVariable(graph, id);
123 Variable context = ProxyVariables.proxyVariableInput(graph, run);
124 if (context != null) {
125 Variable base = ProxyVariables.proxyVariableBase(graph, run);
126 Resource bookResource = base.getRepresents(graph);
127 Resource contextResource = context.getRepresents(graph);
128 if (contextResource != null) {
129 SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource);
134 SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource);
139 SpreadsheetBook book = new SpreadsheetBook();
141 Variable base = ProxyVariables.proxyVariableBase(graph, run);
142 Resource bookResource = base.getRepresents(graph);
143 Variable configuration = Variables.getVariable(graph, bookResource);
145 SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book);
146 Synchronizer synchronizer = new Synchronizer(graph);
147 synchronizer.fullSynchronization(configuration, handler);
154 protected StandardRealm<SheetNode, SpreadsheetBook> createRealm(SpreadsheetBook engine, String id) {
155 return new SpreadsheetRealm(engine, id);
159 public void removeRealm(WriteGraph graph, String id) throws DatabaseException {
160 StandardRealm<SheetNode, SpreadsheetBook> realm = getOrCreateRealm(graph, id);
161 SpreadsheetEvaluationEnvironment.removeInstance(realm.getEngine());
162 super.removeRealm(graph, id);
165 // Utility function for SCL, this should maybe be replaced with something better in the future
166 public static void removeSpreadsheetSession(WriteGraph graph, Variable runVariable) throws DatabaseException {
167 String uri = runVariable.getParent(graph).getURI(graph);
168 getInstance().removeRealm(graph, uri);