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.exception.DatabaseException;
17 import org.simantics.db.layer0.StandardRealm;
18 import org.simantics.db.layer0.StandardSessionManager;
19 import org.simantics.db.layer0.variable.ProxyVariables;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.layer0.variable.Variables;
22 import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler;
23 import org.simantics.spreadsheet.resource.SpreadsheetResource;
24 import org.simantics.structural.synchronization.Synchronizer;
26 public class SpreadsheetSessionManager extends StandardSessionManager<SheetNode, SpreadsheetBook> {
28 private static SpreadsheetSessionManager INSTANCE;
30 public static SpreadsheetSessionManager getInstance() {
31 if(INSTANCE == null) {
32 INSTANCE = new SpreadsheetSessionManager();
37 public class ClassLoaderObjectInputStream extends ObjectInputStream{
39 private ClassLoader classLoader;
41 public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
43 this.classLoader = classLoader;
47 protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{
50 String name = desc.getName();
51 return Class.forName(name, false, classLoader);
53 catch(ClassNotFoundException e){
54 return super.resolveClass(desc);
59 private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException {
60 SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);
61 Collection<Resource> ics = graph.getObjects(context, SR.HasInitialCondition);
63 Resource found = null;
64 Set<Resource> founds = new HashSet<>();
65 Set<Resource> foundDefaults = new HashSet<>();
67 for (Resource initialCondition : ics) {
68 if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) {
69 foundDefaults.add(initialCondition);
71 if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) {
72 founds.add(initialCondition);
75 if (foundDefaults.size() == 1) {
76 found = foundDefaults.iterator().next();
77 } else if (foundDefaults.size() == 0 && founds.size() == 1) {
78 found = founds.iterator().next();
80 System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book));
81 System.err.println("foundDefaults : " + foundDefaults.size());
82 if (!foundDefaults.isEmpty()) {
83 for (Resource foundDefault : foundDefaults) {
84 System.err.println(graph.getPossibleURI(foundDefault));
87 System.err.println("founds : " + founds.size());
88 if (!founds.isEmpty()) {
89 for (Resource foun : founds) {
90 System.err.println(graph.getPossibleURI(foun));
97 File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found);
98 System.err.println("Extracting IC from " + tmp.getAbsolutePath());
99 InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp));
100 ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn);
101 SpreadsheetBook srBook = (SpreadsheetBook) in.readObject();
104 } catch (IOException e) {
105 throw new DatabaseException(e);
106 } catch (ClassNotFoundException e) {
107 throw new DatabaseException(e);
114 protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException {
116 Variable run = Variables.getVariable(graph, id);
117 Variable context = ProxyVariables.proxyVariableInput(graph, run);
118 if (context != null) {
119 Variable base = ProxyVariables.proxyVariableBase(graph, run);
120 Resource bookResource = base.getRepresents(graph);
121 Resource contextResource = context.getRepresents(graph);
122 if (contextResource != null) {
123 SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource);
128 SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource);
133 SpreadsheetBook book = new SpreadsheetBook();
135 Variable base = ProxyVariables.proxyVariableBase(graph, run);
136 Resource bookResource = base.getRepresents(graph);
137 Variable configuration = Variables.getVariable(graph, bookResource);
139 SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book);
140 Synchronizer synchronizer = new Synchronizer(graph);
141 synchronizer.fullSynchronization(configuration, handler);
148 protected StandardRealm<SheetNode, SpreadsheetBook> createRealm(SpreadsheetBook engine, String id) {
149 return new SpreadsheetRealm(engine, id);
152 // static ConcurrentHashMap<String, SpreadsheetRealm> REALMS =
153 // new ConcurrentHashMap<String, SpreadsheetRealm>();
155 // static ConcurrentHashMap<String, NodeSupport<String>> SUPPORTS =
156 // new ConcurrentHashMap<String, NodeSupport<String>>();
158 // public static SpreadsheetRealm sclRealmById(String id) {
159 // // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here
160 // return REALMS.get(id);
163 // public static NodeSupport<String> getOrCreateNodeSupport(String id) {
164 // synchronized(SUPPORTS) {
165 // NodeSupport<String> result = SUPPORTS.get(id);
166 // if(result == null) {
167 // SpreadsheetRealm realm = getOrCreateSCLRealm(id);
168 // result = new NodeSupport<String>(realm.getNodeManager());
169 // SUPPORTS.put(id, result);
175 // public static SpreadsheetRealm createRealm() {
176 // synchronized(REALMS) {
177 // String id = UUID.randomUUID().toString();
178 // return createRealm(id);
182 // public static SpreadsheetRealm getOrCreateSCLRealm(String id) {
183 // synchronized(REALMS) {
184 // SpreadsheetRealm session = sclRealmById(id);
185 // if(session == null)
186 // return createRealm(id);
192 // private static SpreadsheetRealm createRealm(String id) {
193 // SpreadsheetBook book = new SpreadsheetBook();
194 // SpreadsheetRealm realm = new SpreadsheetRealm(book, id);
195 // REALMS.put(id, realm);
199 // public static void removeRealm(String id) {
200 // REALMS.remove(id);