]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/SCL.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCL.java
1 package org.simantics.modeling;
2
3 import java.io.File;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.List;
7
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;
28
29 public class SCL {
30     private static final Logger LOGGER = LoggerFactory.getLogger(SCL.class);
31
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.
39     }
40     public static void killPlatformRead(ReadGraph graph) throws DatabaseException {
41         // See above.
42     }
43     public static void killPlatform() throws Exception {
44         SimanticsPlatform.INSTANCE.shutdown(null);
45     }
46     public static void shutdownPlatform() throws Exception {
47         SimanticsPlatform.INSTANCE.shutdown(null);
48     }
49     public static void reconnectPlatform() throws Exception {
50         SimanticsPlatform.INSTANCE.reconnect(Simantics.getDefaultDatabaseDriver());
51     }
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");
55     }
56
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() {
69             @Override
70             public void perform(WriteGraph graph) throws DatabaseException {
71             }
72
73                 @Override
74                 public String toString() {
75                         return "Utils sync";
76                 }
77        });
78 //
79 //             // And then wait still some more
80 //             Simantics.getSession().syncRequest(new ReadRequest() {
81 //                 @Override
82 //                 public void run(ReadGraph graph) throws DatabaseException {
83 //                 }
84 //            });
85 //              }
86     }
87
88     public static void syncGraph() throws Exception {
89         try {
90             Simantics.getSession().getService(ServiceActivityMonitor.class).waitForCompletion();
91         } catch (InterruptedException e) {
92             throw new DatabaseException(e);
93         }
94
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);
99         for (Job j : jobs) {
100             if(j instanceof DatabaseJob) j.join();
101         }
102         sync();
103         }
104
105     public static boolean deleteMBNode(List<Resource> resources) throws DatabaseException {
106         boolean value = false;
107                 try {
108                 value = RemoverUtil.tryCollectionRemover(resources);
109         } catch (DatabaseException e){
110                 return value;
111         }
112         return value;
113     }
114
115     public static void sleep(int ms) {
116         try {
117                         Thread.sleep(ms);
118                 } catch (InterruptedException e) {
119             LOGGER.warn("Sleep was interrupted.", e);
120                 }
121     }
122
123     public static boolean hasSomethingToPaste(ReadGraph graph, Resource resource) throws DatabaseException {
124
125         SimanticsClipboard clipboard = Simantics.getClipboard();
126         return !clipboard.getContents().isEmpty();
127
128     }
129     
130     public static boolean canDelete(ReadGraph graph, Resource resource) throws DatabaseException {
131
132         return RemoverUtil.canRemove(graph, resource);
133
134     }
135
136     public static boolean canRename(ReadGraph graph, Resource resource) throws DatabaseException {
137         return true;
138     }
139
140     public static String currentDate(String format) {
141
142         SimpleDateFormat sdf = new SimpleDateFormat(format);
143         return sdf.format(new Date(System.currentTimeMillis()));
144
145     }
146
147     public static File workspaceDirectory() {
148         return new File(Platform.getInstanceLocation().getURL().getFile());
149     }
150     
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 "";
154         return result;
155     }
156
157     
158 }