]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/LifeCycleProcesses.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / LifeCycleProcesses.java
1 package org.simantics.modeling;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.util.Layer0Utils;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.project.exception.ProjectException;
15 import org.simantics.project.features.AbstractProjectFeature;
16 import org.simantics.scl.runtime.function.Function1;
17 import org.simantics.scl.runtime.tuple.Tuple0;
18
19 public class LifeCycleProcesses extends AbstractProjectFeature {
20         
21         Set<LifeCycleContext> contexts = new HashSet<LifeCycleContext>();
22
23         @Override
24         public void configure() throws ProjectException {
25
26                 try {
27
28                         Simantics.getSession().syncRequest(new ReadRequest() {
29
30                                 @Override
31                                 public void run(ReadGraph graph) throws DatabaseException {
32
33                                         ModelingResources MOD = ModelingResources.getInstance(graph);
34                                         for(Resource indexRoot : Layer0Utils.listIndexRoots(graph)) {
35                                                 for(Resource lcp : ModelingUtils.searchByTypeShallow(graph, indexRoot, MOD.LifeCycleProcess)) {
36                                                         
37 //                                                      System.err.println("Loading life cycle process " + graph.getURI(lcp));
38                                                         Function1<LifeCycleContext,Tuple0> load = null; 
39                                                         Function1<LifeCycleContext,Tuple0> unload = null;
40                                                 Variable loadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_load);
41                                                 if (loadProperty != null) {
42                                                     load = loadProperty.getPossibleValue(graph);
43                                                 }
44                                                 Variable unloadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_unload);
45                                                 if (unloadProperty != null) {
46                                                     unload = unloadProperty.getPossibleValue(graph);
47                                                 }
48
49                                                         LifeCycleContext lcc = new LifeCycleContext(lcp, load, unload);
50                                                         contexts.add(lcc);
51                                                         
52                                                 }
53                                         }
54                                         
55                                 }
56                                 
57                         });
58                         
59                 } catch (DatabaseException e) {
60                         
61                         throw new ProjectException(e);
62                         
63                 }
64                 
65                 for(LifeCycleContext context : contexts) {
66                         context.load();
67                 }
68                 
69         }
70
71         @Override
72         public void deconfigure() throws ProjectException {
73
74                 for(LifeCycleContext context : contexts) {
75                         context.unload();
76                 }
77
78         }
79         
80 }