1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics;
\r
14 import java.io.File;
\r
15 import java.util.UUID;
\r
16 import java.util.concurrent.ScheduledFuture;
\r
17 import java.util.concurrent.TimeUnit;
\r
19 import org.eclipse.core.runtime.IProgressMonitor;
\r
20 import org.eclipse.core.runtime.NullProgressMonitor;
\r
21 import org.eclipse.core.runtime.Platform;
\r
22 import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy;
\r
23 import org.simantics.SimanticsPlatform.RecoveryPolicy;
\r
24 import org.simantics.application.arguments.IArguments;
\r
25 import org.simantics.application.arguments.SimanticsArguments;
\r
26 import org.simantics.db.ReadGraph;
\r
27 import org.simantics.db.Resource;
\r
28 import org.simantics.db.Session;
\r
29 import org.simantics.db.WriteGraph;
\r
30 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
\r
31 import org.simantics.db.exception.DatabaseException;
\r
32 import org.simantics.db.exception.RuntimeDatabaseException;
\r
33 import org.simantics.db.indexing.IndexUtils;
\r
34 import org.simantics.db.layer0.util.SimanticsClipboard;
\r
35 import org.simantics.db.layer0.util.SimanticsKeys;
\r
36 import org.simantics.db.layer0.variable.Variable;
\r
37 import org.simantics.db.layer0.variable.Variables;
\r
38 import org.simantics.db.management.ISessionContext;
\r
39 import org.simantics.db.management.ISessionContextProvider;
\r
40 import org.simantics.db.management.ISessionContextProviderSource;
\r
41 import org.simantics.db.management.SessionContextProvider;
\r
42 import org.simantics.db.management.SingleSessionContextProviderSource;
\r
43 import org.simantics.db.request.ReadInterface;
\r
44 import org.simantics.db.request.WriteInterface;
\r
45 import org.simantics.internal.FileServiceImpl;
\r
46 import org.simantics.layer0.Layer0;
\r
47 import org.simantics.project.IProject;
\r
48 import org.simantics.project.ProjectKeys;
\r
49 import org.simantics.scl.compiler.top.ValueNotFound;
\r
50 import org.simantics.scl.osgi.SCLOsgi;
\r
51 import org.simantics.scl.runtime.SCLContext;
\r
52 import org.simantics.scl.runtime.function.Function;
\r
53 import org.simantics.scl.runtime.function.Function1;
\r
54 import org.simantics.scl.runtime.function.Function2;
\r
55 import org.simantics.utils.FileService;
\r
56 import org.simantics.utils.FileUtils;
\r
57 import org.simantics.utils.TempFiles;
\r
58 import org.simantics.utils.threads.ThreadUtils;
\r
61 * A facade for accessing basic Simantics platform services. Usable without a
\r
62 * graphical UI, i.e. in headless contexts.
\r
64 * TODO: in time, move headless functionality of SimanticsUI into this class but
\r
65 * originals as delegates in SimanticsUI.
\r
67 * TODO: duplicate of org.simantics.db.layer0.util.Simantics, do something about this!!
\r
69 public class Simantics {
\r
72 * Default database driver ID
\r
74 private static final String DEFAULT_DATABASE_DRIVER_ID = "procore";
\r
76 private static String defaultDatabaseDriverId = DEFAULT_DATABASE_DRIVER_ID;
\r
78 private static ISessionContextProviderSource providerSource = null;
\r
79 private static volatile FileServiceImpl fileService = null;
\r
82 * Sets the database driver to be used by the platform. To have any effect,
\r
83 * this must be set before platform startup.
\r
85 * @param id driver id
\r
87 public static void setDefaultDatabaseDriver(String id) {
\r
88 defaultDatabaseDriverId = id;
\r
92 * Returns currently set default database driver id.
\r
94 public static String getDefaultDatabaseDriver() {
\r
95 return defaultDatabaseDriverId;
\r
102 * @throws PlatformException
\r
104 public static ISessionContext startUpHeadless(IArguments args, IProgressMonitor progress) throws PlatformException {
\r
105 if (SimanticsPlatform.INSTANCE.sessionContext != null) {
\r
106 throw new RuntimeDatabaseException("Simantics is already up and running.");
\r
109 RecoveryPolicy workspacePolicy = Platform.inDevelopmentMode() ? RecoveryPolicy.FixError : RecoveryPolicy.ThrowError;
\r
110 OntologyRecoveryPolicy ontologyPolicy = Platform.inDevelopmentMode() ? OntologyRecoveryPolicy.Merge : OntologyRecoveryPolicy.ThrowError;
\r
112 if (args.contains(SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS)) {
\r
113 workspacePolicy = RecoveryPolicy.FixError;
\r
114 ontologyPolicy = OntologyRecoveryPolicy.Merge;
\r
117 if (args.contains(SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL)) {
\r
118 ontologyPolicy = OntologyRecoveryPolicy.ReinstallDatabase;
\r
121 if (args.contains(SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL)) {
\r
122 ontologyPolicy = OntologyRecoveryPolicy.ReinstallDatabase;
\r
126 if (args.contains(SimanticsArguments.LOCAL_SERVER_PORT)) {
\r
128 localPort = args.get(SimanticsArguments.LOCAL_SERVER_PORT);
\r
129 } catch (IllegalArgumentException e) {
\r
130 throw new PlatformException("Failed to open database session", e);
\r
134 // ServerAddress remoteDatabase = null;
\r
135 // if (args.contains(SimanticsArguments.SERVER)) {
\r
136 // String serverAddress = args.get(SimanticsArguments.SERVER);
\r
138 // remoteDatabase = new ServerAddress(serverAddress);
\r
139 // } catch (IllegalArgumentException e) {
\r
140 // throw new PlatformException("Failed to open database session", e);
\r
144 return startUpHeadless(progress, workspacePolicy, ontologyPolicy, localPort /*, remoteDatabase*/);
\r
149 * @param workspacePolicy
\r
150 * @param ontologyPolicy
\r
152 * @param remoteDatabase
\r
154 * @throws PlatformException
\r
156 public static ISessionContext startUpHeadless(IProgressMonitor progress, RecoveryPolicy workspacePolicy, OntologyRecoveryPolicy ontologyPolicy, int localPort) throws PlatformException {
\r
157 if (SimanticsPlatform.INSTANCE.sessionContext != null) {
\r
158 throw new RuntimeDatabaseException("Simantics is already up and running.");
\r
161 // Set session context provider.
\r
162 final ISessionContextProvider provider = new SessionContextProvider(null);
\r
163 ISessionContextProviderSource source = new SingleSessionContextProviderSource(provider);
\r
164 setSessionContextProviderSource(source);
\r
165 org.simantics.db.layer0.internal.SimanticsInternal.setSessionContextProviderSource(source);
\r
167 if (progress == null)
\r
168 progress = new NullProgressMonitor();
\r
169 return SimanticsPlatform.INSTANCE.startUp(defaultDatabaseDriverId, progress, workspacePolicy, ontologyPolicy, true, new ConsoleUserAgent());
\r
174 * @throws PlatformException
\r
176 public static void shutdown(IProgressMonitor progress) throws PlatformException {
\r
177 SimanticsPlatform.INSTANCE.shutdown(progress);
\r
181 * Queue execution of a runnable.
\r
185 public static void async(Runnable runnable) {
\r
186 ThreadUtils.getBlockingWorkExecutor().execute(runnable);
\r
189 public static void async(Runnable runnable, int delay, TimeUnit unit) {
\r
190 ThreadUtils.getTimer().schedule(runnable, delay, unit);
\r
193 public static ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, int initialDelay, int period, TimeUnit unit) {
\r
194 return ThreadUtils.getTimer().scheduleAtFixedRate(runnable, initialDelay, period, unit);
\r
198 * Queue execution of a non-blocking runnable. Use this method with caution.
\r
199 * A non-blocking runnable nevers locks anything, No Locks, No semaphores,
\r
200 * No Object.wait(), No synchronized() {} blocks.
\r
202 * @param runnable a non-blocking runnable
\r
204 public static void asyncNonblocking(Runnable runnable) {
\r
205 ThreadUtils.getNonBlockingWorkExecutor().execute(runnable);
\r
209 * Schedule execution of a non-blocking runnable. Use this method with caution.
\r
210 * A non-blocking runnable never locks anything, No Locks, No semaphores,
\r
211 * No Object,wait(), No synchronized() {} blocks.
\r
213 * @param runnable a non-blocking runnable
\r
214 * @param initialDelay
\r
217 public static void asyncNonblocking(Runnable runnable, int initialDelay, int period) {
\r
218 ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);
\r
221 public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {
\r
222 return getSessionContextProvider().setSessionContext(ctx);
\r
225 public static void setSessionContextProviderSource(ISessionContextProviderSource source) {
\r
226 if (source == null)
\r
227 throw new IllegalArgumentException("null provider source");
\r
228 providerSource = source;
\r
231 public static ISessionContextProviderSource getProviderSource() {
\r
232 if (providerSource == null)
\r
233 throw new IllegalStateException(
\r
234 "providerSource must be initialized by the application before using class Simantics");
\r
235 return providerSource;
\r
238 public static ISessionContextProvider getSessionContextProvider() {
\r
239 return getProviderSource().getActive();
\r
243 * Returns the database session context associated with the currently active
\r
244 * context. This method should be used to retrieve session contexts only
\r
245 * when the client is sure that the correct context is active.
\r
247 * @return the session context associated with the currently active context
\r
248 * or <code>null</code> if the context has no session context
\r
250 public static ISessionContext getSessionContext() {
\r
251 ISessionContextProvider provider = getSessionContextProvider();
\r
252 return provider != null ? provider.getSessionContext() : null;
\r
256 * Returns the database Session bound to the currently active context.
\r
259 * The method always returns a non-null Session or produces an
\r
260 * IllegalStateException if a Session was not attainable.
\r
263 * @return the Session bound to the currently active workbench window
\r
264 * @throws IllegalStateException if no Session was available
\r
266 public static Session getSession() {
\r
267 ISessionContext ctx = getSessionContext();
\r
269 throw new IllegalStateException("Session unavailable, no database session open");
\r
270 return ctx.getSession();
\r
274 * Returns the database Session bound to the currently active context.
\r
275 * Differently from {@link #getSession()}, this method returns
\r
276 * <code>null</code> if there is no current Session available.
\r
278 * @see #getSession()
\r
279 * @return the Session bound to the currently active context or
\r
280 * <code>null</code>
\r
282 public static Session peekSession() {
\r
283 ISessionContext ctx = getSessionContext();
\r
284 return ctx == null ? null : ctx.peekSession();
\r
288 * @return the currently open and active project as a Resource
\r
289 * @throws IllegalStateException if there is no currently active database
\r
290 * session, which also means there is no active project at the
\r
293 public static Resource getProjectResource() {
\r
294 ISessionContext ctx = getSessionContext();
\r
296 throw new IllegalStateException("No current database session");
\r
297 Resource project = ctx.getHint(SimanticsKeys.KEY_PROJECT);
\r
298 if (project == null)
\r
299 throw new IllegalStateException("No current project resource in session context " + ctx);
\r
304 * @return the currently open and active project as a {@link Resource}
\r
306 public static Resource peekProjectResource() {
\r
307 ISessionContext ctx = getSessionContext();
\r
308 return ctx != null ? ctx.<Resource>getHint(SimanticsKeys.KEY_PROJECT) : null;
\r
312 * @return the currently open and active project as an {@link IProject}
\r
313 * @throws IllegalStateException if there is no currently active database
\r
314 * session, which also means there is no active project at the
\r
317 public static IProject getProject() {
\r
318 ISessionContext ctx = getSessionContext();
\r
320 throw new IllegalStateException("No current database session");
\r
321 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
\r
322 if (project == null)
\r
323 throw new IllegalStateException("No current project in session context " + ctx);
\r
328 * @return the currently open and active project as an {@link IProject} or
\r
329 * <code>null</code> if there is no active session or project
\r
331 public static IProject peekProject() {
\r
332 ISessionContext ctx = getSessionContext();
\r
333 return ctx == null ? null : ctx.<IProject>getHint(ProjectKeys.KEY_PROJECT);
\r
336 // FIXME: once org.simantics.db.layer0.util.Simantics is gone, re-enable this
\r
337 // private static SimanticsClipboard clipboard = SimanticsClipboard.EMPTY;
\r
342 public static void setClipboard(SimanticsClipboard content) {
\r
343 // FIXME: once org.simantics.db.layer0.util.Simantics is gone, re-enable this
\r
344 // if (content == null)
\r
345 // throw new NullPointerException("null clipboard content");
\r
346 // clipboard = content;
\r
347 org.simantics.db.layer0.internal.SimanticsInternal.setClipboard(content);
\r
350 public static SimanticsClipboard getClipboard() {
\r
351 // FIXME: once org.simantics.db.layer0.util.Simantics is gone, re-enable this
\r
352 //return clipboard;
\r
353 return org.simantics.db.layer0.internal.SimanticsInternal.getClipboard();
\r
356 public static Layer0 getLayer0() throws DatabaseException {
\r
357 return Layer0.getInstance(getSession());
\r
360 public static <T> T sync(ReadInterface<T> r) throws DatabaseException {
\r
361 return getSession().sync(r);
\r
364 public static <T> T sync(WriteInterface<T> r) throws DatabaseException {
\r
365 return getSession().sync(r);
\r
368 public static <T> void async(ReadInterface<T> r) {
\r
369 getSession().async(r, new ProcedureAdapter<T>());
\r
372 public static <T> void async(WriteInterface<T> r) {
\r
373 getSession().async(r);
\r
376 public static void clearTemporaryDirectory() {
\r
377 FileUtils.deleteDir(getTemporaryDirectory());
\r
380 public static File getTempfile(String directory, String suffix) {
\r
381 File dir = getTemporaryDirectory(directory);
\r
382 return new File(dir, UUID.randomUUID().toString() + "." + suffix);
\r
385 public static File getTemporaryDirectory(String directory) {
\r
386 File sub = new File(getTemporaryDirectory(), directory);
\r
391 public static File getTemporaryDirectory() {
\r
392 File workspace = Platform.getLocation().toFile();
\r
393 File temp = new File(workspace, "tempFiles");
\r
398 public static TempFiles getTempFiles() {
\r
402 private static class TempFilesImpl implements TempFiles {
\r
403 private final TempFilesImpl parent;
\r
404 private final String prefix;
\r
405 private final String fullPrefix;
\r
407 private TempFilesImpl(TempFilesImpl parent, String directory) {
\r
408 this.parent = parent;
\r
409 this.prefix = directory;
\r
410 this.fullPrefix = getPrefix(new StringBuilder()).toString();
\r
413 private StringBuilder getPrefix(StringBuilder sb) {
\r
414 if (parent != null)
\r
415 parent.getPrefix(sb);
\r
416 if (prefix != null && !prefix.isEmpty())
\r
417 sb.append(prefix).append(File.separatorChar);
\r
422 public File getRoot() {
\r
423 return Simantics.getTemporaryDirectory(fullPrefix);
\r
427 public File getTempfile(String directory, String suffix) {
\r
428 return Simantics.getTempfile(fullPrefix.isEmpty() ? directory : fullPrefix + directory, suffix);
\r
432 public TempFiles subdirectory(String directory) {
\r
433 return new TempFilesImpl(this, directory);
\r
437 public static TempFiles TEMP_FILES = new TempFilesImpl(null, null);
\r
439 public static void flushIndexCaches(IProgressMonitor progress, Session session) {
\r
441 IndexUtils.flushIndexCaches(progress, session);
\r
442 } catch (Exception e) {
\r
443 Logger.defaultLogError(e);
\r
448 @SuppressWarnings({ "unchecked", "rawtypes" })
\r
449 public static <T> T applySCL(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
\r
450 SCLContext sclContext = SCLContext.getCurrent();
\r
451 Object oldGraph = sclContext.put("graph", graph);
\r
453 T t = (T)((Function)SCLOsgi.MODULE_REPOSITORY.getValue(module, function)).applyArray(args);
\r
455 } catch (ValueNotFound e) {
\r
456 throw new DatabaseException("SCL Value not found: " + e.name);
\r
457 } catch (Throwable t) {
\r
458 throw new DatabaseException(t);
\r
460 sclContext.put("graph", oldGraph);
\r
465 @SuppressWarnings("unchecked")
\r
466 public static <T> T applySCLWrite(WriteGraph graph, @SuppressWarnings("rawtypes") Function f, Object ... args) throws DatabaseException {
\r
467 SCLContext sclContext = SCLContext.getCurrent();
\r
468 Object oldGraph = sclContext.put("graph", graph);
\r
470 return (T)f.applyArray(args);
\r
471 } catch (Throwable t) {
\r
472 throw new DatabaseException(t);
\r
474 sclContext.put("graph", oldGraph);
\r
478 @SuppressWarnings("unchecked")
\r
479 public static <T> T applySCLRead(ReadGraph graph, @SuppressWarnings("rawtypes") Function f, Object ... args) throws DatabaseException {
\r
480 SCLContext sclContext = SCLContext.getCurrent();
\r
481 Object oldGraph = sclContext.put("graph", graph);
\r
483 return (T)f.applyArray(args);
\r
484 } catch (Throwable t) {
\r
485 throw new DatabaseException(t);
\r
487 sclContext.put("graph", oldGraph);
\r
491 public static <P0,R> R applySCLWrite(WriteGraph graph, Function1<P0,R> function, P0 p0) throws DatabaseException {
\r
492 SCLContext sclContext = SCLContext.getCurrent();
\r
493 Object oldGraph = sclContext.put("graph", graph);
\r
495 return function.apply(p0);
\r
496 } catch (Throwable t) {
\r
497 throw new DatabaseException(t);
\r
499 sclContext.put("graph", oldGraph);
\r
503 public static <P0,R> R applySCLRead(ReadGraph graph, Function1<P0,R> function, P0 p0) throws DatabaseException {
\r
504 SCLContext sclContext = SCLContext.getCurrent();
\r
505 Object oldGraph = sclContext.put("graph", graph);
\r
507 return function.apply(p0);
\r
508 } catch (Throwable t) {
\r
509 throw new DatabaseException(t);
\r
511 sclContext.put("graph", oldGraph);
\r
515 public static <P0,P1,R> R applySCLRead(ReadGraph graph, Function2<P0,P1,R> function, P0 p0, P1 p1) throws DatabaseException {
\r
516 SCLContext sclContext = SCLContext.getCurrent();
\r
517 Object oldGraph = sclContext.put("graph", graph);
\r
519 return function.apply(p0, p1);
\r
520 } catch (Throwable t) {
\r
521 throw new DatabaseException(t);
\r
523 sclContext.put("graph", oldGraph);
\r
527 public static <P0,R> R invokeSCLWrite(WriteGraph graph, Variable property, P0 p0) throws DatabaseException {
\r
528 Function1<P0,R> fn = property.getPossibleValue(graph);
\r
529 if(fn == null) throw new DatabaseException("No function for " + property.getURI(graph));
\r
530 return Simantics.applySCLWrite(graph, fn, p0);
\r
533 public static <P0,R> R invokeSCL(ReadGraph graph, Variable property, P0 p0) throws DatabaseException {
\r
534 Function1<P0,R> fn = property.getPossibleValue(graph);
\r
535 if(fn == null) throw new DatabaseException("No function for " + property.getURI(graph));
\r
536 return Simantics.applySCLRead(graph, fn, p0);
\r
539 public static <P0,P1,R> R invokeSCL(ReadGraph graph, Variable property, P0 p0, P1 p1) throws DatabaseException {
\r
540 Function2<P0,P1,R> fn = property.getPossibleValue(graph);
\r
541 if(fn == null) throw new DatabaseException("No function for " + property.getURI(graph));
\r
542 return Simantics.applySCLRead(graph, fn, p0, p1);
\r
545 public static <P0,R> R invokeSCLWrite(WriteGraph graph, Resource entity, Resource property, P0 p0) throws DatabaseException {
\r
546 return invokeSCLWrite(graph, getProperty(graph, entity, property), p0);
\r
549 public static <P0,R> R invokeSCL(ReadGraph graph, Resource entity, Resource property, P0 p0) throws DatabaseException {
\r
550 return invokeSCL(graph, getProperty(graph, entity, property), p0);
\r
553 public static <P0,P1,R> R invokeSCL(ReadGraph graph, Resource entity, Resource property, P0 p0, P1 p1) throws DatabaseException {
\r
554 return invokeSCL(graph, getProperty(graph, entity, property), p0, p1);
\r
557 public static <P0,R> R tryInvokeSCL(ReadGraph graph, Resource entity, Resource property, P0 p0) throws DatabaseException {
\r
558 Variable p = Variables.tryGetProperty(graph, entity, property);
\r
561 return invokeSCL(graph, p, p0);
\r
564 public static <P0,P1,R> R tryInvokeSCL(ReadGraph graph, Resource entity, Resource property, P0 p0, P1 p1) throws DatabaseException {
\r
565 Variable p = Variables.tryGetProperty(graph, entity, property);
\r
568 return invokeSCL(graph, p, p0, p1);
\r
571 private static Variable getProperty(ReadGraph graph, Resource entity, Resource property) throws DatabaseException {
\r
572 return Variables.getVariable(graph, entity).getProperty(graph, property);
\r
575 public static boolean ensureMemoryBytes(long bytes) {
\r
577 Runtime runtime = Runtime.getRuntime();
\r
578 long consumedMemory = runtime.totalMemory() - runtime.freeMemory();
\r
579 long available = runtime.maxMemory() - consumedMemory;
\r
581 return available > bytes;
\r
585 public static long getDiskBytes() {
\r
587 File ws = new File(Platform.getInstanceLocation().getURL().getFile());
\r
588 return ws.getUsableSpace();
\r
593 * @return a service for dealing with recurring file system handling cases
\r
595 public static FileService getFileService() {
\r
596 FileService fs = fileService;
\r
598 synchronized (Simantics.class) {
\r
601 fs = fileService = new FileServiceImpl();
\r