X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FSpreadsheetSessionManager.java;h=d90fe3106613fbad5907920a5711f00492c4bc51;hp=ff247b73a76f30b51d78d79aa6760a0a706d7e54;hb=82ed7c74dbd83a2a557e781b8674b3262b52da54;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java index ff247b73a..d90fe3106 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java @@ -1,203 +1,171 @@ -package org.simantics.spreadsheet.graph; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.io.ObjectStreamClass; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.StandardRealm; -import org.simantics.db.layer0.StandardSessionManager; -import org.simantics.db.layer0.variable.ProxyVariables; -import org.simantics.db.layer0.variable.Variable; -import org.simantics.db.layer0.variable.Variables; -import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler; -import org.simantics.spreadsheet.resource.SpreadsheetResource; -import org.simantics.structural.synchronization.Synchronizer; - -public class SpreadsheetSessionManager extends StandardSessionManager { - - private static SpreadsheetSessionManager INSTANCE; - - public static SpreadsheetSessionManager getInstance() { - if(INSTANCE == null) { - INSTANCE = new SpreadsheetSessionManager(); - } - return INSTANCE; - } - - public class ClassLoaderObjectInputStream extends ObjectInputStream{ - - private ClassLoader classLoader; - - public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException { - super(in); - this.classLoader = classLoader; - } - - @Override - protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{ - - try{ - String name = desc.getName(); - return Class.forName(name, false, classLoader); - } - catch(ClassNotFoundException e){ - return super.resolveClass(desc); - } - } - } - - private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException { - SpreadsheetResource SR = SpreadsheetResource.getInstance(graph); - Collection ics = graph.getObjects(context, SR.HasInitialCondition); - - Resource found = null; - Set founds = new HashSet<>(); - Set foundDefaults = new HashSet<>(); - - for (Resource initialCondition : ics) { - if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) { - foundDefaults.add(initialCondition); - } - if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) { - founds.add(initialCondition); - } - } - if (foundDefaults.size() == 1) { - found = foundDefaults.iterator().next(); - } else if (foundDefaults.size() == 0 && founds.size() == 1) { - found = founds.iterator().next(); - } else { - System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book)); - System.err.println("foundDefaults : " + foundDefaults.size()); - if (!foundDefaults.isEmpty()) { - for (Resource foundDefault : foundDefaults) { - System.err.println(graph.getPossibleURI(foundDefault)); - } - } - System.err.println("founds : " + founds.size()); - if (!founds.isEmpty()) { - for (Resource foun : founds) { - System.err.println(graph.getPossibleURI(foun)); - } - } - } - - if (found != null) { - try { - File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found); - System.err.println("Extracting IC from " + tmp.getAbsolutePath()); - InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp)); - ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn); - SpreadsheetBook srBook = (SpreadsheetBook) in.readObject(); - in.close(); - return srBook; - } catch (IOException e) { - throw new DatabaseException(e); - } catch (ClassNotFoundException e) { - throw new DatabaseException(e); - } - } - return null; - } - - @Override - protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException { - - Variable run = Variables.getVariable(graph, id); - Variable context = ProxyVariables.proxyVariableInput(graph, run); - if (context != null) { - Variable base = ProxyVariables.proxyVariableBase(graph, run); - Resource bookResource = base.getRepresents(graph); - Resource contextResource = context.getRepresents(graph); - if (contextResource != null) { - SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource); - if (ic != null) - return ic; - } - - SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource); - if (ic != null) - return ic; - } - - SpreadsheetBook book = new SpreadsheetBook(); - - Variable base = ProxyVariables.proxyVariableBase(graph, run); - Resource bookResource = base.getRepresents(graph); - Variable configuration = Variables.getVariable(graph, bookResource); - - SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book); - Synchronizer synchronizer = new Synchronizer(graph); - synchronizer.fullSynchronization(configuration, handler); - - return book; - - } - - @Override - protected StandardRealm createRealm(SpreadsheetBook engine, String id) { - return new SpreadsheetRealm(engine, id); - } - -// static ConcurrentHashMap REALMS = -// new ConcurrentHashMap(); -// -// static ConcurrentHashMap> SUPPORTS = -// new ConcurrentHashMap>(); -// -// public static SpreadsheetRealm sclRealmById(String id) { -// // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here -// return REALMS.get(id); -// } -// -// public static NodeSupport getOrCreateNodeSupport(String id) { -// synchronized(SUPPORTS) { -// NodeSupport result = SUPPORTS.get(id); -// if(result == null) { -// SpreadsheetRealm realm = getOrCreateSCLRealm(id); -// result = new NodeSupport(realm.getNodeManager()); -// SUPPORTS.put(id, result); -// } -// return result; -// } -// } -// -// public static SpreadsheetRealm createRealm() { -// synchronized(REALMS) { -// String id = UUID.randomUUID().toString(); -// return createRealm(id); -// } -// } -// -// public static SpreadsheetRealm getOrCreateSCLRealm(String id) { -// synchronized(REALMS) { -// SpreadsheetRealm session = sclRealmById(id); -// if(session == null) -// return createRealm(id); -// else -// return session; -// } -// } -// -// private static SpreadsheetRealm createRealm(String id) { -// SpreadsheetBook book = new SpreadsheetBook(); -// SpreadsheetRealm realm = new SpreadsheetRealm(book, id); -// REALMS.put(id, realm); -// return realm; -// } -// -// public static void removeRealm(String id) { -// REALMS.remove(id); -// } - -} +package org.simantics.spreadsheet.graph; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamClass; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.StandardRealm; +import org.simantics.db.layer0.StandardSessionManager; +import org.simantics.db.layer0.variable.ProxyVariables; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.spreadsheet.graph.formula.SpreadsheetEvaluationEnvironment; +import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler; +import org.simantics.spreadsheet.resource.SpreadsheetResource; +import org.simantics.structural.synchronization.Synchronizer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SpreadsheetSessionManager extends StandardSessionManager { + + private static final Logger LOGGER = LoggerFactory.getLogger(SpreadsheetSessionManager.class); + + private static SpreadsheetSessionManager INSTANCE; + + public static SpreadsheetSessionManager getInstance() { + if(INSTANCE == null) { + INSTANCE = new SpreadsheetSessionManager(); + } + return INSTANCE; + } + + public class ClassLoaderObjectInputStream extends ObjectInputStream{ + + private ClassLoader classLoader; + + public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException { + super(in); + this.classLoader = classLoader; + } + + @Override + protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{ + + try{ + String name = desc.getName(); + return Class.forName(name, false, classLoader); + } + catch(ClassNotFoundException e){ + return super.resolveClass(desc); + } + } + } + + private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException { + SpreadsheetResource SR = SpreadsheetResource.getInstance(graph); + Collection ics = graph.getObjects(context, SR.HasInitialCondition); + + Resource found = null; + Set founds = new HashSet<>(); + Set foundDefaults = new HashSet<>(); + + for (Resource initialCondition : ics) { + if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) { + foundDefaults.add(initialCondition); + } + if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) { + founds.add(initialCondition); + } + } + if (foundDefaults.size() == 1) { + found = foundDefaults.iterator().next(); + } else if (foundDefaults.size() == 0 && founds.size() == 1) { + found = founds.iterator().next(); + } else { + System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book)); + System.err.println("foundDefaults : " + foundDefaults.size()); + if (!foundDefaults.isEmpty()) { + for (Resource foundDefault : foundDefaults) { + System.err.println(graph.getPossibleURI(foundDefault)); + } + } + System.err.println("founds : " + founds.size()); + if (!founds.isEmpty()) { + for (Resource foun : founds) { + System.err.println(graph.getPossibleURI(foun)); + } + } + } + + if (found != null) { + try { + File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found); + System.err.println("Extracting IC from " + tmp.getAbsolutePath()); + InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp)); + ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn); + SpreadsheetBook srBook = (SpreadsheetBook) in.readObject(); + in.close(); + return srBook; + } catch (IOException e) { + throw new DatabaseException(e); + } catch (ClassNotFoundException e) { + throw new DatabaseException(e); + } + } + return null; + } + + @Override + protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException { + + Variable run = Variables.getVariable(graph, id); + Variable context = ProxyVariables.proxyVariableInput(graph, run); + if (context != null) { + Variable base = ProxyVariables.proxyVariableBase(graph, run); + Resource bookResource = base.getRepresents(graph); + Resource contextResource = context.getRepresents(graph); + if (contextResource != null) { + SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource); + if (ic != null) + return ic; + } + + SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource); + if (ic != null) + return ic; + } + + SpreadsheetBook book = new SpreadsheetBook(); + + Variable base = ProxyVariables.proxyVariableBase(graph, run); + Resource bookResource = base.getRepresents(graph); + Variable configuration = Variables.getVariable(graph, bookResource); + + SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book); + Synchronizer synchronizer = new Synchronizer(graph); + synchronizer.fullSynchronization(configuration, handler); + + return book; + + } + + @Override + protected StandardRealm createRealm(SpreadsheetBook engine, String id) { + return new SpreadsheetRealm(engine, id); + } + + @Override + public void removeRealm(WriteGraph graph, String id) throws DatabaseException { + StandardRealm realm = getOrCreateRealm(graph, id); + SpreadsheetEvaluationEnvironment.removeInstance(realm.getEngine()); + super.removeRealm(graph, id); + } + + // Utility function for SCL, this should maybe be replaced with something better in the future + public static void removeSpreadsheetSession(WriteGraph graph, Variable runVariable) throws DatabaseException { + String uri = runVariable.getParent(graph).getURI(graph); + getInstance().removeRealm(graph, uri); + } +} +