]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java
Adopt spreadsheet changes made in Balas development
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / SpreadsheetSessionManager.java
1 package org.simantics.spreadsheet.graph;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
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;
12 import java.util.Set;
13
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;
31
32 public class SpreadsheetSessionManager extends StandardVariableSessionManager<SheetNode, SpreadsheetBook> {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(SpreadsheetSessionManager.class);
35     
36         private static SpreadsheetSessionManager INSTANCE;
37         
38         public static SpreadsheetSessionManager getInstance() {
39                 if(INSTANCE == null) {
40                         INSTANCE = new SpreadsheetSessionManager();
41                 }
42                 return INSTANCE;
43         }
44         
45         public class ClassLoaderObjectInputStream extends ObjectInputStream{
46
47              private ClassLoader classLoader;
48
49              public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
50                   super(in);
51                   this.classLoader = classLoader;
52              }
53              
54              @Override
55              protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{
56              
57                   try{
58                        String name = desc.getName();
59                        return Class.forName(name, false, classLoader);
60                   }
61                   catch(ClassNotFoundException e){
62                        return super.resolveClass(desc);
63                   }
64              }
65         }
66         
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);
70         
71         Resource found = null;
72         Set<Resource> founds = new HashSet<>();
73         Set<Resource> foundDefaults = new HashSet<>();
74
75         for (Resource initialCondition : ics) {
76             if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) {
77                 foundDefaults.add(initialCondition);
78             }
79             if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) {
80                 founds.add(initialCondition);
81             }
82         }
83         if (foundDefaults.size() == 1) {
84             found = foundDefaults.iterator().next();
85         } else if (foundDefaults.size() == 0 && founds.size() == 1) {
86             found = founds.iterator().next();
87         } else {
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));
93                 }
94             }
95             System.err.println("founds : " + founds.size());
96             if (!founds.isEmpty()) {
97                 for (Resource foun : founds) {
98                     System.err.println(graph.getPossibleURI(foun));
99                 }
100             }
101         }
102
103         if (found != null) {
104             try {
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();
110                 in.close();
111                 return srBook;
112             } catch (IOException e) {
113                 throw new DatabaseException(e);
114             } catch (ClassNotFoundException e) {
115                 throw new DatabaseException(e);
116             }
117         }
118         return null;
119     }
120         
121     @Override
122     protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException {
123
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);
132                 if (ic != null)
133                     return ic;
134             }
135
136             SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource);
137             if (ic != null)
138                 return ic;
139         }
140
141         SpreadsheetBook book = new SpreadsheetBook(context.getURI(graph));
142         
143         Variable base = ProxyVariables.proxyVariableBase(graph, run);
144         Resource bookResource = base.getRepresents(graph);
145         Variable configuration = Variables.getVariable(graph, bookResource);
146         
147         SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book);
148         Synchronizer synchronizer = new Synchronizer(graph);
149         synchronizer.fullSynchronization(configuration, handler);
150         
151         return book;
152
153     }
154
155         @Override
156         protected StandardRealm<SheetNode, SpreadsheetBook> createRealm(SpreadsheetBook engine, String id) {
157                 return new SpreadsheetRealm(engine, id);
158         }
159
160         @Override
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);
165     }
166     
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);
171     }
172 }
173