1 package org.simantics.modeling;
4 import java.text.SimpleDateFormat;
8 import org.eclipse.core.runtime.NullProgressMonitor;
9 import org.eclipse.core.runtime.Platform;
10 import org.eclipse.core.runtime.jobs.IJobManager;
11 import org.eclipse.core.runtime.jobs.Job;
12 import org.simantics.DatabaseJob;
13 import org.simantics.Simantics;
14 import org.simantics.SimanticsPlatform;
15 import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.request.external.EclipsePreferencePrimitiveRead;
22 import org.simantics.db.layer0.util.RemoverUtil;
23 import org.simantics.db.layer0.util.SimanticsClipboard;
24 import org.simantics.db.service.DebugSupport;
25 import org.simantics.db.service.ServiceActivityMonitor;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 private static final Logger LOGGER = LoggerFactory.getLogger(SCL.class);
32 public static void killPlatformWrite(WriteGraph graph) throws DatabaseException {
33 // Currently not supported.
34 // Would be relatively easy to support the desired functionality.
35 // To implement this I would recommend something like:
36 // SimanticsPlatform.INSTANCE.breakWrite();
37 // And it's implementation with db.management.breakWrite() method.
38 // To be clear, at the moment this method (breakWrite) does not exist.
40 public static void killPlatformRead(ReadGraph graph) throws DatabaseException {
43 public static void killPlatform() throws Exception {
44 SimanticsPlatform.INSTANCE.shutdown(null);
46 public static void shutdownPlatform() throws Exception {
47 SimanticsPlatform.INSTANCE.shutdown(null);
49 public static void reconnectPlatform() throws Exception {
50 SimanticsPlatform.INSTANCE.reconnect(Simantics.getDefaultDatabaseDriver());
52 public static void synchronizeOntologies() throws Exception {
53 SimanticsPlatform.INSTANCE.synchronizeOntologies(new NullProgressMonitor(), OntologyRecoveryPolicy.Merge, true);
54 Simantics.getSession().getService(DebugSupport.class).query(Simantics.getSession(), "exec QueryControl.flush");
57 public static void sync() throws DatabaseException {
58 // Multiple bugs here:
59 // -Model performs activation in separate write transactions because API does not support changing the virtual graph
60 // => activation & activation listener is delayed beyond this point
61 // -This should be fixed by the following code
62 // TransactionSupport ts = session.getService(TransactionSupport.class);
63 // ts.waitCompletion();
64 // but unfortunately this does not work either...
65 // so we synchronize by a familiar write transaction
66 // And then wait still some more
67 // for(int i=0;i<3;i++) {
68 Simantics.getSession().syncRequest(new WriteRequest() {
70 public void perform(WriteGraph graph) throws DatabaseException {
74 public String toString() {
79 // // And then wait still some more
80 // Simantics.getSession().syncRequest(new ReadRequest() {
82 // public void run(ReadGraph graph) throws DatabaseException {
88 public static void syncGraph() throws Exception {
90 Simantics.getSession().getService(ServiceActivityMonitor.class).waitForCompletion();
91 } catch (InterruptedException e) {
92 throw new DatabaseException(e);
95 // OK, now the experiment activate job should be scheduled
96 // Wait for the job to finish
97 IJobManager job = Job.getJobManager();
98 Job[] jobs = job.find(null);
100 if(j instanceof DatabaseJob) j.join();
105 public static boolean deleteMBNode(List<Resource> resources) throws DatabaseException {
106 boolean value = false;
108 value = RemoverUtil.tryCollectionRemover(resources);
109 } catch (DatabaseException e){
115 public static void sleep(int ms) {
118 } catch (InterruptedException e) {
119 LOGGER.warn("Sleep was interrupted.", e);
123 public static boolean hasSomethingToPaste(ReadGraph graph, Resource resource) throws DatabaseException {
125 SimanticsClipboard clipboard = Simantics.getClipboard();
126 return !clipboard.getContents().isEmpty();
130 public static boolean canDelete(ReadGraph graph, Resource resource) throws DatabaseException {
132 return RemoverUtil.canRemove(graph, resource);
136 public static boolean canRename(ReadGraph graph, Resource resource) throws DatabaseException {
140 public static String currentDate(String format) {
142 SimpleDateFormat sdf = new SimpleDateFormat(format);
143 return sdf.format(new Date(System.currentTimeMillis()));
147 public static File workspaceDirectory() {
148 return new File(Platform.getInstanceLocation().getURL().getFile());
151 public static String queryPreference(ReadGraph graph, String pluginId, String preferenceKey) throws DatabaseException {
152 String result = graph.syncRequest(new EclipsePreferencePrimitiveRead(pluginId, preferenceKey));
153 if(result == null) return "";