X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FSCL.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FSCL.java;h=691d82961e892389eb91b5842c03c36cfa49926c;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java new file mode 100644 index 000000000..691d82961 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java @@ -0,0 +1,151 @@ +package org.simantics.modeling; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.jobs.IJobManager; +import org.eclipse.core.runtime.jobs.Job; +import org.simantics.DatabaseJob; +import org.simantics.Logger; +import org.simantics.Simantics; +import org.simantics.SimanticsPlatform; +import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.external.EclipsePreferencePrimitiveRead; +import org.simantics.db.layer0.util.RemoverUtil; +import org.simantics.db.layer0.util.SimanticsClipboard; +import org.simantics.db.service.DebugSupport; + +public class SCL { + public static void killPlatformWrite(WriteGraph graph) throws DatabaseException { + // Currently not supported. + // Would be relatively easy to support the desired functionality. + // To implement this I would recommend something like: + // SimanticsPlatform.INSTANCE.breakWrite(); + // And it's implementation with db.management.breakWrite() method. + // To be clear, at the moment this method (breakWrite) does not exist. + } + public static void killPlatformRead(ReadGraph graph) throws DatabaseException { + // See above. + } + public static void killPlatform() throws Exception { + SimanticsPlatform.INSTANCE.shutdown(null); + } + public static void shutdownPlatform() throws Exception { + SimanticsPlatform.INSTANCE.shutdown(null); + } + public static void reconnectPlatform() throws Exception { + SimanticsPlatform.INSTANCE.reconnect(null); + } + public static void synchronizeOntologies() throws Exception { + SimanticsPlatform.INSTANCE.synchronizeOntologies(new NullProgressMonitor(), OntologyRecoveryPolicy.Merge, true); + Simantics.getSession().getService(DebugSupport.class).query(Simantics.getSession(), "exec QueryControl.flush"); + } + + public static void sync() throws DatabaseException { + // Multiple bugs here: + // -Model performs activation in separate write transactions because API does not support changing the virtual graph + // => activation & activation listener is delayed beyond this point + // -This should be fixed by the following code + // TransactionSupport ts = session.getService(TransactionSupport.class); + // ts.waitCompletion(); + // but unfortunately this does not work either... + // so we synchronize by a familiar write transaction + // And then wait still some more + for(int i=0;i<3;i++) { + Simantics.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + } + + @Override + public String toString() { + return "Utils sync"; + } + }); + + // And then wait still some more + Simantics.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + } + }); + } + } + + public static void syncGraph() throws Exception { + sync(); + + // OK, now the experiment activate job should be scheduled + // Wait for the job to finish + IJobManager job = Job.getJobManager(); + Job[] jobs = job.find(null); + for (Job j : jobs) { + if(j instanceof DatabaseJob) j.join(); + } + sync(); + } + + public static boolean deleteMBNode(List resources) throws DatabaseException { + boolean value = false; + try { + value = RemoverUtil.tryCollectionRemover(resources); + } catch (DatabaseException e){ + return value; + } + return value; + } + + public static void sleep(int ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + Logger.defaultLogError(e); + } + } + + public static boolean hasSomethingToPaste(ReadGraph graph, Resource resource) throws DatabaseException { + + SimanticsClipboard clipboard = Simantics.getClipboard(); + return !clipboard.getContents().isEmpty(); + + } + + public static boolean canDelete(ReadGraph graph, Resource resource) throws DatabaseException { + + return RemoverUtil.canRemove(graph, resource); + + } + + public static boolean canRename(ReadGraph graph, Resource resource) throws DatabaseException { + return true; + } + + public static String currentDate(String format) { + + SimpleDateFormat sdf = new SimpleDateFormat(format); + return sdf.format(new Date(System.currentTimeMillis())); + + } + + public static File workspaceDirectory() { + return new File(Platform.getInstanceLocation().getURL().getFile()); + } + + public static String queryPreference(ReadGraph graph, String pluginId, String preferenceKey) throws DatabaseException { + String result = graph.syncRequest(new EclipsePreferencePrimitiveRead(pluginId, preferenceKey)); + if(result == null) return ""; + return result; + } + + +}