]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
9f242b28c8efade8dc8d606d4c6409bd94b86a69
[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 && graph instanceof WriteGraph) {
258             return (T)f.apply(Tuple0.INSTANCE);
259         } else {
260             if (graph != null) {
261                 LOGGER.error(
262                         "SCLContext {} for current thread {} contains an existing graph object but it is not WriteGraph - Somewhere is a function that forgets to remove the graph from the context!!",
263                         context, Thread.currentThread());
264             }
265             final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
266             return Simantics.getSession().syncRequest(new WriteResultRequest<T>() {
267                 @Override
268                 public T perform(WriteGraph graph) throws DatabaseException {
269                     SCLContext.push(context);
270                     SCLReportingHandler oldPrinter = (SCLReportingHandler)context.put(SCLReportingHandler.REPORTING_HANDLER, printer);
271                     ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
272                     try {
273                         return (T)f.apply(args);
274                     } finally {
275                         context.put(GRAPH, oldGraph);
276                         context.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
277                         SCLContext.pop();
278                     }
279                 }
280             });
281         }
282     }
283     
284     public static <T> T delayedSyncWrite(final Function f) throws DatabaseException {
285         final SCLContext context = SCLContext.getCurrent();
286         final DataContainer<T> dc = new DataContainer<T>(null);
287
288         DelayedWriteRequest request = new DelayedWriteRequest() {
289             @Override
290             public void perform(WriteGraph graph) throws DatabaseException {
291                 final SCLContext context = SCLContext.getCurrent();
292                 SCLContext.push(context);
293                 ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
294                 try {
295                     dc.set((T)f.apply(Tuple0.INSTANCE));
296                 } finally {
297                     context.put(GRAPH, oldGraph);
298                     SCLContext.pop();
299                 }
300             }
301         };
302     
303         Object graph = context.get(GRAPH);
304         if (graph != null) {
305             if (graph instanceof WriteGraph) {
306                 ((WriteGraph)graph).syncRequest(request);
307             } else {
308                 throw new DatabaseException("Caller is inside a read transaction.");
309             }
310         } else {
311             Simantics.getSession().syncRequest(request);
312         }
313         return dc.get();
314     }
315
316     public static <T> T virtualSyncWriteMem(WriteGraph graph, String virtualGraphId, final Function f) throws DatabaseException {
317         final SCLContext context = SCLContext.getCurrent();
318         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
319         VirtualGraph vg = vgs.getMemoryPersistent(virtualGraphId);
320         return graph.syncRequest(new WriteResultRequest<T>(vg) {
321             @Override
322             public T perform(WriteGraph graph) throws DatabaseException {
323                 SCLContext.push(context);
324                 ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
325                 try {
326                     return (T)f.apply(Tuple0.INSTANCE);
327                 } finally {
328                     context.put(GRAPH, oldGraph);
329                     SCLContext.pop();
330                 }
331             }
332         });
333     }
334     
335     public static <T> T virtualSyncWriteWS(WriteGraph graph, String virtualGraphId, final Function f) throws DatabaseException {
336         final SCLContext context = SCLContext.getCurrent();
337         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
338         VirtualGraph vg = vgs.getWorkspacePersistent(virtualGraphId);
339         return graph.syncRequest(new WriteResultRequest<T>(vg) {
340             @Override
341             public T perform(WriteGraph graph) throws DatabaseException {
342                 SCLContext.push(context);
343                 ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
344                 try {
345                     return (T)f.apply(Tuple0.INSTANCE);
346                 } finally {
347                     context.put(GRAPH, oldGraph);
348                     SCLContext.pop();
349                 }
350             }
351         });
352     }
353     
354     public static <T> T readValue(final String uri) throws DatabaseException {
355         return Simantics.getSession().syncRequest(new Read<T>() {
356             @Override
357             public T perform(ReadGraph graph) throws DatabaseException {
358                 return Variables.getVariable(graph, uri).getValue(graph);
359             }
360         });
361     }
362     
363     public static <T> void writeValue(final String uri, final T value) throws DatabaseException {
364         Simantics.getSession().syncRequest(new WriteRequest() {
365             @Override
366             public void perform(WriteGraph graph) throws DatabaseException {
367                 Variables.getVariable(graph, uri).setValue(graph, value);
368             }
369         });
370     }
371     
372     public static void activateOnce(Resource r) {
373         Simantics.getSession().getService(IActivationManager.class).activateOnce(r);
374     }
375     
376     public static void syncActivateOnce(WriteGraph graph, Resource r) throws DatabaseException {
377         graph.getService(IActivationManager.class).activateOnce(graph, r);
378     }
379
380     public static Resource resourceFromId(ReadGraph graph, long id) throws DatabaseException, IOException {
381         SerialisationSupport ss = graph.getService(SerialisationSupport.class);
382         return ss.getResource(id);
383     }
384     
385     public static void disableDependencies(WriteGraph graph) {
386         Layer0Utils.setDependenciesIndexingDisabled(graph, true);       
387     }
388     
389     public static void enableDependencies(WriteGraph graph) {
390         Layer0Utils.setDependenciesIndexingDisabled(graph, false);       
391     }
392     
393     public static void collectClusters() {
394         Simantics.getSession().getService(ClusterControl.class).collectClusters(Integer.MAX_VALUE);
395     }
396     
397     public static class SCLUnaryRead extends BinaryRead<Function1<Object,Object>, Object, Object> {
398
399         public SCLUnaryRead(Function1<Object, Object> parameter1, Object parameter2) {
400              super(parameter1, parameter2);
401         }
402
403         @Override
404         public Object perform(ReadGraph graph) throws DatabaseException {
405             return Simantics.applySCLRead(graph, parameter, parameter2);
406         }
407
408     }
409     
410     public static Object unaryQuery(ReadGraph graph, Function1<Object,Object> fn, Object value) throws DatabaseException {
411         return graph.syncRequest(new SCLUnaryRead(fn, value));
412     }
413
414     public static Object unaryQueryCached(ReadGraph graph, Function1<Object,Object> fn, Object value) throws DatabaseException {
415         return graph.syncRequest(new SCLUnaryRead(fn, value), TransientCacheAsyncListener.<Object>instance());
416     }
417     
418
419     private static class Subquery extends UnaryRead<Function, Object> {
420
421         public Subquery(Function q) {
422             super(q);
423         }
424
425         @Override
426         public Object perform(ReadGraph graph) throws DatabaseException {
427             return Simantics.applySCLRead(graph, parameter, Tuple0.INSTANCE);
428         }
429
430     }
431
432     public static Object subquery(ReadGraph graph, Function q) throws DatabaseException {
433         return graph.syncRequest(new Subquery(q));
434     }
435
436     public static Object subqueryC(ReadGraph graph, Function q) throws DatabaseException {
437         return graph.syncRequest(new Subquery(q), TransientCacheAsyncListener.<Object>instance());
438     }
439     
440     public static void subqueryL(ReadGraph graph, Function query, Function executeCallback, Function1<Throwable, Tuple> exceptionCallback, Function1<Tuple0, Boolean> isDisposedCallback) throws DatabaseException {
441         graph.syncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
442             @Override
443             public void execute(ReadGraph graph, Object result) throws DatabaseException {
444                 Simantics.applySCLRead(graph, executeCallback, result);
445             }
446             
447             @Override
448             public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
449                 Simantics.applySCLRead(graph, exceptionCallback, t);
450             }
451             
452             @Override
453             public boolean isDisposed() {
454                 return isDisposedCallback.apply(Tuple0.INSTANCE);
455             }
456         });
457     }
458
459     public static Object possibleFromDynamic(Type expectedType, String moduleName, Object value) {
460     
461         try {
462
463         
464             Failable<Module> failable = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
465             Module module = failable.getResult();
466             
467             RuntimeEnvironment env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
468             EnvironmentSpecification.of(moduleName, ""), module.getParentClassLoader());
469
470             JavaTypeTranslator tr = new JavaTypeTranslator(env.getEnvironment());
471             TypeDesc desc = tr.toTypeDesc(expectedType);
472             String className = desc.getFullName();
473             Class<?> clazz = env.getMutableClassLoader().loadClass(className);
474             if (!clazz.isAssignableFrom(value.getClass()))
475                 return null;
476             
477         } catch (ImportFailureException | ClassNotFoundException e) {
478         }
479         return value;
480     }
481
482     public static void restrictQueries(ReadGraph graph, int amount, int step, int maxTimeInMs) {
483
484         QueryControl qc = graph.getService(QueryControl.class);
485         long start = System.currentTimeMillis();
486         while(true) {
487             int current = qc.count();
488             if(current < amount) return;
489             qc.gc(graph, step);
490             long duration = System.currentTimeMillis() - start;
491             if(duration > maxTimeInMs) return;
492         }
493
494     }
495
496     public static int countQueries(ReadGraph graph) {
497
498         QueryControl qc = graph.getService(QueryControl.class);
499         return qc.count();
500
501     }
502     
503 }