]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
09022d543c1aba3e9101028c88ed939b72759963
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLFunctions.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scl.db;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.cojen.classfile.TypeDesc;
19 import org.simantics.Simantics;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.VirtualGraph;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
25 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
26 import org.simantics.db.common.request.BinaryRead;
27 import org.simantics.db.common.request.DelayedWriteRequest;
28 import org.simantics.db.common.request.ReadRequest;
29 import org.simantics.db.common.request.UnaryRead;
30 import org.simantics.db.common.request.WriteRequest;
31 import org.simantics.db.common.request.WriteResultRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.util.Layer0Utils;
34 import org.simantics.db.layer0.variable.Variables;
35 import org.simantics.db.request.Read;
36 import org.simantics.db.service.ClusterControl;
37 import org.simantics.db.service.QueryControl;
38 import org.simantics.db.service.SerialisationSupport;
39 import org.simantics.db.service.VirtualGraphSupport;
40 import org.simantics.layer0.utils.triggers.IActivationManager;
41 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
42 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
43 import org.simantics.scl.compiler.errors.DoesNotExist;
44 import org.simantics.scl.compiler.errors.Failable;
45 import org.simantics.scl.compiler.errors.Failure;
46 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
47 import org.simantics.scl.compiler.module.Module;
48 import org.simantics.scl.compiler.module.repository.ImportFailureException;
49 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
50 import org.simantics.scl.compiler.runtime.RuntimeModule;
51 import org.simantics.scl.compiler.top.ValueNotFound;
52 import org.simantics.scl.compiler.types.TCon;
53 import org.simantics.scl.compiler.types.Type;
54 import org.simantics.scl.compiler.types.Types;
55 import org.simantics.scl.compiler.types.exceptions.MatchException;
56 import org.simantics.scl.compiler.types.util.MultiFunction;
57 import org.simantics.scl.osgi.SCLOsgi;
58 import org.simantics.scl.reflection.ValueNotFoundException;
59 import org.simantics.scl.runtime.SCLContext;
60 import org.simantics.scl.runtime.function.Function;
61 import org.simantics.scl.runtime.function.Function1;
62 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
63 import org.simantics.scl.runtime.tuple.Tuple;
64 import org.simantics.scl.runtime.tuple.Tuple0;
65 import org.simantics.utils.DataContainer;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 @SuppressWarnings({"rawtypes", "unchecked"})
70 public class SCLFunctions {
71
72     private static final Logger LOGGER = LoggerFactory.getLogger(SCLFunctions.class);
73
74     public static final String GRAPH = "graph";
75
76     public static <T> T safeExec(final Function f) {
77         try {
78             return (T)f.apply(Tuple0.INSTANCE);
79         } catch (Throwable t) {
80             LOGGER.error("safeExec caught exception", t);
81             return null;
82         }
83     }
84
85     public static Function resolveFunction(RuntimeModule rm, String function) throws ValueNotFound {
86         return (Function)rm.getValue(function);
87     }
88     
89     private static SCLValue resolveSCLValue(RuntimeModule rm, String function) throws ValueNotFound {
90         return rm.getModule().getValue(function);
91     }
92     
93     private static RuntimeModule resolveRuntimeModule(String module) throws ValueNotFound {
94         Failable<RuntimeModule> f = SCLOsgi.MODULE_REPOSITORY.getRuntimeModule(module);
95         if(f.didSucceed())
96             return f.getResult();
97         else if(f == DoesNotExist.INSTANCE)
98             throw new ValueNotFound("Didn't find module " + module);
99         else
100             throw new ValueNotFound(((Failure)f).toString());
101     }
102     
103     private static List<TCon> getEffects(SCLValue value) throws ValueNotFoundException, ValueNotFound, MatchException {
104     
105         Type type = value.getType();
106         MultiFunction mfun = Types.matchFunction(type, 1);
107         ArrayList<TCon> concreteEffects = new ArrayList<>();
108         mfun.effect.collectConcreteEffects(concreteEffects);
109         return concreteEffects;
110         
111     }
112
113     public static List<TCon> getEffects(RuntimeModule rm, String function) throws ValueNotFoundException, ValueNotFound, MatchException {
114         return getEffects(resolveSCLValue(rm, function));
115     }
116
117     public static List<TCon> getEffects(String module, String function) throws ValueNotFoundException, ValueNotFound, MatchException {
118         return getEffects(resolveSCLValue(resolveRuntimeModule(module), function));
119     }
120     
121     private static <T> T evaluate(Function function, Object ... args) {
122         return (T)function.applyArray(args);
123     }
124
125     private static <T> T evaluate(RuntimeModule rm, String function, Object ... args) throws ValueNotFound {
126         return evaluate(resolveFunction(rm, function), args);
127     }
128
129     public static <T> T evaluate(String module, String function, Object ... args) throws ValueNotFound {
130         return evaluate(resolveRuntimeModule(module), function, args);
131     }
132
133     public static <T> T evaluateDB(String module, String function, Object ... args) throws DatabaseException {
134         try {
135             RuntimeModule rm = resolveRuntimeModule(module);
136             List<TCon> effects = getEffects(resolveSCLValue(rm, function));
137             Function f = resolveFunction(rm, function);
138             if(effects.contains(Types.WRITE_GRAPH)) {
139                 return syncWrite(f, args);
140             } else if(effects.contains(Types.READ_GRAPH)) {
141                 return syncRead(f, args);
142             } else {
143                 return evaluate(f, args);
144             }
145         } catch (ValueNotFound e) {
146             throw new DatabaseException("SCL Value not found: " + e.name);
147         } catch (Throwable t) {
148             if (t instanceof DatabaseException)
149                 throw (DatabaseException) t;
150             throw new DatabaseException(t);
151         }
152     }
153     
154     public static <T> T evaluateGraph(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
155         final SCLContext context = SCLContext.getCurrent();
156         SCLContext.push(context);
157         Object oldGraph = context.put(GRAPH, graph);
158         try {
159             return evaluateDB(module, function, args);
160         } finally {
161             context.put(GRAPH, oldGraph);
162             SCLContext.pop();
163         }
164     }
165
166     public static void runWithGraph(ReadGraph graph, Runnable r) {
167         final SCLContext context = SCLContext.getCurrent();
168         SCLContext.push(context);
169         Object oldGraph = context.put(GRAPH, graph);
170         try {
171             r.run();
172         } finally {
173             context.put(GRAPH, oldGraph);
174             SCLContext.pop();
175         }
176     }
177
178     private static Object[] NO_ARGS = new Object[] { Tuple0.INSTANCE };
179
180     public static <T> void asyncRead(final Function f) throws DatabaseException {    
181         asyncRead(f, NO_ARGS);
182     }
183
184     public static void asyncRead(final Function f, final Object ... args) throws DatabaseException {
185         final SCLContext context = SCLContext.createDerivedContext();
186         Simantics.getSession().asyncRequest(new ReadRequest() {
187             @Override
188             public void run(ReadGraph graph) throws DatabaseException {
189                 SCLContext.push(context);
190                 context.put(GRAPH, graph);
191                 try {
192                     f.apply(Tuple0.INSTANCE);
193                 } finally {
194                     SCLContext.pop();
195                 }
196             }
197         });
198     }
199     
200     public static <T> T syncRead(final Function f) throws DatabaseException {
201         return syncRead(f, NO_ARGS);
202     }
203     
204     public static <T> T syncRead(final Function f, final Object ... args) throws DatabaseException {
205         final SCLContext context = SCLContext.getCurrent();
206         Object graph = context.get(GRAPH);
207         if (graph != null) {
208             return (T)f.applyArray(args);
209         } else {
210             return Simantics.getSession().syncRequest(new Read<T>() {
211                 @Override
212                 public T perform(ReadGraph graph) throws DatabaseException {
213                     SCLContext.push(context);
214                     ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
215                     try {
216                         return (T)f.apply(Tuple0.INSTANCE);
217                     } finally {
218                         context.put(GRAPH, oldGraph);
219                         SCLContext.pop();
220                     }
221                 }
222             });
223         }
224     }
225
226     public static void asyncWrite(final Function f) throws DatabaseException {
227         asyncWrite(f, NO_ARGS);
228     }
229
230     public static void asyncWrite(final Function f, final Object ... args) throws DatabaseException {
231         SCLContext context = SCLContext.createDerivedContext();
232         if (Simantics.peekSession() != null) {
233             Simantics.getSession().asyncRequest(new WriteRequest() {
234                 @Override
235                 public void perform(WriteGraph graph) throws DatabaseException {
236                     SCLContext.push(context);
237                     context.put(GRAPH, graph);
238                     try {
239                         f.apply(args);
240                     } finally {
241                         SCLContext.pop();
242                     }
243                 }
244             });
245         } else {
246             LOGGER.warn("No session available for asynchronous write requests");
247         }
248     }
249     
250     public static <T> T syncWrite(final Function f) throws DatabaseException {
251         return syncWrite(f, NO_ARGS);
252     }
253
254     public static <T> T syncWrite(final Function f, final Object ... args) throws DatabaseException {
255         final SCLContext context = SCLContext.getCurrent();
256         Object graph = context.get(GRAPH);
257         if (graph != null) {
258             return (T)f.apply(Tuple0.INSTANCE);
259         } else {
260             final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
261             return Simantics.getSession().syncRequest(new WriteResultRequest<T>() {
262                 @Override
263                 public T perform(WriteGraph graph) throws DatabaseException {
264                     SCLContext.push(context);
265                     SCLReportingHandler oldPrinter = (SCLReportingHandler)context.put(SCLReportingHandler.REPORTING_HANDLER, printer);
266                     ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
267                     try {
268                         return (T)f.apply(args);
269                     } finally {
270                         context.put(GRAPH, oldGraph);
271                         context.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
272                         SCLContext.pop();
273                     }
274                 }
275             });
276         }
277     }
278     
279     public static <T> T delayedSyncWrite(final Function f) throws DatabaseException {
280         final SCLContext context = SCLContext.getCurrent();
281         final DataContainer<T> dc = new DataContainer<T>(null);
282
283         DelayedWriteRequest request = new DelayedWriteRequest() {
284             @Override
285             public void perform(WriteGraph graph) throws DatabaseException {
286                 final SCLContext context = SCLContext.getCurrent();
287                 SCLContext.push(context);
288                 ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
289                 try {
290                     dc.set((T)f.apply(Tuple0.INSTANCE));
291                 } finally {
292                     context.put(GRAPH, oldGraph);
293                     SCLContext.pop();
294                 }
295             }
296         };
297     
298         Object graph = context.get(GRAPH);
299         if (graph != null) {
300             if (graph instanceof WriteGraph) {
301                 ((WriteGraph)graph).syncRequest(request);
302             } else {
303                 throw new DatabaseException("Caller is inside a read transaction.");
304             }
305         } else {
306             Simantics.getSession().syncRequest(request);
307         }
308         return dc.get();
309     }
310
311     public static <T> T virtualSyncWriteMem(WriteGraph graph, String virtualGraphId, final Function f) throws DatabaseException {
312         final SCLContext context = SCLContext.getCurrent();
313         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
314         VirtualGraph vg = vgs.getMemoryPersistent(virtualGraphId);
315         return graph.syncRequest(new WriteResultRequest<T>(vg) {
316             @Override
317             public T perform(WriteGraph graph) throws DatabaseException {
318                 SCLContext.push(context);
319                 ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
320                 try {
321                     return (T)f.apply(Tuple0.INSTANCE);
322                 } finally {
323                     context.put(GRAPH, oldGraph);
324                     SCLContext.pop();
325                 }
326             }
327         });
328     }
329     
330     public static <T> T virtualSyncWriteWS(WriteGraph graph, String virtualGraphId, final Function f) throws DatabaseException {
331         final SCLContext context = SCLContext.getCurrent();
332         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
333         VirtualGraph vg = vgs.getWorkspacePersistent(virtualGraphId);
334         return graph.syncRequest(new WriteResultRequest<T>(vg) {
335             @Override
336             public T perform(WriteGraph graph) throws DatabaseException {
337                 SCLContext.push(context);
338                 ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
339                 try {
340                     return (T)f.apply(Tuple0.INSTANCE);
341                 } finally {
342                     context.put(GRAPH, oldGraph);
343                     SCLContext.pop();
344                 }
345             }
346         });
347     }
348     
349     public static <T> T readValue(final String uri) throws DatabaseException {
350         return Simantics.getSession().syncRequest(new Read<T>() {
351             @Override
352             public T perform(ReadGraph graph) throws DatabaseException {
353                 return Variables.getVariable(graph, uri).getValue(graph);
354             }
355         });
356     }
357     
358     public static <T> void writeValue(final String uri, final T value) throws DatabaseException {
359         Simantics.getSession().syncRequest(new WriteRequest() {
360             @Override
361             public void perform(WriteGraph graph) throws DatabaseException {
362                 Variables.getVariable(graph, uri).setValue(graph, value);
363             }
364         });
365     }
366     
367     public static void activateOnce(Resource r) {
368         Simantics.getSession().getService(IActivationManager.class).activateOnce(r);
369     }
370     
371     public static void syncActivateOnce(WriteGraph graph, Resource r) throws DatabaseException {
372         graph.getService(IActivationManager.class).activateOnce(graph, r);
373     }
374
375     public static Resource resourceFromId(ReadGraph graph, long id) throws DatabaseException, IOException {
376         SerialisationSupport ss = graph.getService(SerialisationSupport.class);
377         return ss.getResource(id);
378     }
379     
380     public static void disableDependencies(WriteGraph graph) {
381         Layer0Utils.setDependenciesIndexingDisabled(graph, true);       
382     }
383     
384     public static void enableDependencies(WriteGraph graph) {
385         Layer0Utils.setDependenciesIndexingDisabled(graph, false);       
386     }
387     
388     public static void collectClusters() {
389         Simantics.getSession().getService(ClusterControl.class).collectClusters(Integer.MAX_VALUE);
390     }
391     
392     public static class SCLUnaryRead extends BinaryRead<Function1<Object,Object>, Object, Object> {
393
394         public SCLUnaryRead(Function1<Object, Object> parameter1, Object parameter2) {
395              super(parameter1, parameter2);
396         }
397
398         @Override
399         public Object perform(ReadGraph graph) throws DatabaseException {
400             return Simantics.applySCLRead(graph, parameter, parameter2);
401         }
402
403     }
404     
405     public static Object unaryQuery(ReadGraph graph, Function1<Object,Object> fn, Object value) throws DatabaseException {
406         return graph.syncRequest(new SCLUnaryRead(fn, value));
407     }
408
409     public static Object unaryQueryCached(ReadGraph graph, Function1<Object,Object> fn, Object value) throws DatabaseException {
410         return graph.syncRequest(new SCLUnaryRead(fn, value), TransientCacheAsyncListener.<Object>instance());
411     }
412     
413
414     private static class Subquery extends UnaryRead<Function, Object> {
415
416         public Subquery(Function q) {
417             super(q);
418         }
419
420         @Override
421         public Object perform(ReadGraph graph) throws DatabaseException {
422             return Simantics.applySCLRead(graph, parameter, Tuple0.INSTANCE);
423         }
424
425     }
426
427     public static Object subquery(ReadGraph graph, Function q) throws DatabaseException {
428         return graph.syncRequest(new Subquery(q));
429     }
430
431     public static Object subqueryC(ReadGraph graph, Function q) throws DatabaseException {
432         return graph.syncRequest(new Subquery(q), TransientCacheAsyncListener.<Object>instance());
433     }
434     
435     public static void subqueryL(ReadGraph graph, Function query, Function executeCallback, Function1<Throwable, Tuple> exceptionCallback, Function1<Tuple0, Boolean> isDisposedCallback) throws DatabaseException {
436         graph.syncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
437             @Override
438             public void execute(ReadGraph graph, Object result) throws DatabaseException {
439                 Simantics.applySCLRead(graph, executeCallback, result);
440             }
441             
442             @Override
443             public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
444                 Simantics.applySCLRead(graph, exceptionCallback, t);
445             }
446             
447             @Override
448             public boolean isDisposed() {
449                 return isDisposedCallback.apply(Tuple0.INSTANCE);
450             }
451         });
452     }
453
454     public static Object possibleFromDynamic(Type expectedType, String moduleName, Object value) {
455     
456         try {
457
458         
459             Failable<Module> failable = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
460             Module module = failable.getResult();
461             
462             RuntimeEnvironment env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
463             EnvironmentSpecification.of(moduleName, ""), module.getParentClassLoader());
464
465             JavaTypeTranslator tr = new JavaTypeTranslator(env.getEnvironment());
466             TypeDesc desc = tr.toTypeDesc(expectedType);
467             String className = desc.getFullName();
468             Class<?> clazz = env.getMutableClassLoader().loadClass(className);
469             if (!clazz.isAssignableFrom(value.getClass()))
470                 return null;
471             
472         } catch (ImportFailureException | ClassNotFoundException e) {
473         }
474         return value;
475     }
476
477     public static void restrictQueries(ReadGraph graph, int amount, int step, int maxTimeInMs) {
478
479         QueryControl qc = graph.getService(QueryControl.class);
480         long start = System.currentTimeMillis();
481         while(true) {
482             int current = qc.count();
483             if(current < amount) return;
484             qc.gc(graph, step);
485             long duration = System.currentTimeMillis() - start;
486             if(duration > maxTimeInMs) return;
487         }
488
489     }
490
491     public static int countQueries(ReadGraph graph) {
492
493         QueryControl qc = graph.getService(QueryControl.class);
494         return qc.count();
495
496     }
497     
498 }