]> gerrit.simantics Code Review - simantics/platform.git/blob
9c7428416531a2da02c33d9ac10c895736a4797c
[simantics/platform.git] /
1 package org.simantics.structural.synchronization;
2
3 import gnu.trove.map.hash.TObjectIntHashMap;
4 import gnu.trove.set.hash.THashSet;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Collections;
9 import java.util.List;
10
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
15 import org.simantics.db.exception.CancelTransactionException;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.exception.MissingVariableValueException;
18 import org.simantics.db.layer0.request.ResourceToPossibleVariable;
19 import org.simantics.db.layer0.variable.RVI;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.service.ManagementSupport;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.scl.runtime.SCLContext;
24 import org.simantics.structural.stubs.StructuralResource2;
25 import org.simantics.structural.synchronization.protocol.ChildInfo;
26 import org.simantics.structural.synchronization.protocol.Connection;
27 import org.simantics.structural.synchronization.protocol.SerializedVariable;
28 import org.simantics.structural.synchronization.protocol.SynchronizationEventHandler;
29 import org.simantics.structural.synchronization.protocol.SynchronizationException;
30 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
31
32 public class Synchronizer {
33     
34     public static boolean TRACE = false;
35     
36     ReadGraph graph;
37     Layer0 L0;
38     StructuralResource2 STR;
39     THashSet<String> visitedTypes = new THashSet<String>();
40     IProgressMonitor monitor;
41     
42     double workDone = 0.0;
43     int workDoneInteger;
44     int maxWork;
45     
46     public Synchronizer(ReadGraph graph) {
47         this.graph = graph;
48         L0 = Layer0.getInstance(graph);
49         STR = StructuralResource2.getInstance(graph);
50     }
51     
52     public void setMonitor(IProgressMonitor monitor, int maxWork) {
53         this.monitor = monitor;
54         this.maxWork = maxWork;
55         this.workDoneInteger = 0;
56     }
57     
58     private void didWork(double amount) {
59         workDone += amount;
60         //System.out.println(workDone);
61         if(monitor != null) {
62             int t = (int)(workDone * maxWork);
63             if(t > workDoneInteger) {
64                 monitor.worked(t-workDoneInteger);
65                 workDoneInteger = t;
66             }
67         }
68     }
69
70     ChildInfo mapChild(Variable child) throws DatabaseException {
71         String name = child.getName(graph);
72         RVI rvi = child.getRVI(graph);
73         return new ChildInfo(name, rvi.toString());
74     }
75
76     Collection<ChildInfo> mapChildren(SynchronizationEventHandler handler, Collection<Variable> children) throws DatabaseException {
77         ArrayList<ChildInfo> result = new ArrayList<ChildInfo>(children.size());
78         for(Variable child : children) {
79             if(child.getPossibleType(graph, STR.Component) != null)
80                 try {
81                     result.add(mapChild(child));
82                 } catch(Exception e) {
83                     handler.reportProblem("Failed to get ChildInfo for " + child + ".", e);
84                 }
85         }
86         return result;
87     }
88
89     Collection<Connection> mapConnections(SynchronizationEventHandler handler, Variable child) throws DatabaseException {
90         ArrayList<Connection> result = new ArrayList<Connection>();
91         for(Variable conn : child.getProperties(graph, StructuralResource2.URIs.SynchronizedConnectionRelation)) {
92             String name = conn.getName(graph);
93             org.simantics.structural2.variables.Connection vc = conn.getValue(graph);
94             Collection<VariableConnectionPointDescriptor> connectionPoints = vc.getConnectionPointDescriptors(graph, null);
95             List<String> cps = new ArrayList<String>(connectionPoints.size());
96             for(VariableConnectionPointDescriptor desc : connectionPoints) {
97                 if(desc.isFlattenedFrom(graph, conn)) continue;
98                 if(!desc.hasClassification(graph, StructuralResource2.URIs.ProvidingConnectionRelation)) continue;
99                 String cpRef = desc.getRelativeRVI(graph, child);
100                 cps.add(cpRef);
101             }
102             
103             result.add(new Connection(name, cps));
104             
105         }
106         
107         return result;
108         
109     }
110     
111     private SerializedVariable serialize(SynchronizationEventHandler handler, Variable var, String name) throws DatabaseException {
112         try {
113                 SerializedVariable result = new SerializedVariable(name, var.getVariantValue(graph));
114                 for(Variable prop : var.getProperties(graph, StructuralResource2.URIs.SynchronizedRelation)) {
115                         String pName = prop.getName(graph);
116                         SerializedVariable v = serialize(handler, prop, pName);
117                         if(v != null) result.addProperty(pName, v);
118                 }
119                 return result;
120         } catch (MissingVariableValueException e) {
121             handler.reportProblem("Failed to read " + name + ". " + e.getMessage());
122             
123             Throwable cur = e;
124             while((cur = cur.getCause()) != null) {
125                 if(!(cur instanceof MissingVariableValueException)) {
126                     handler.reportProblem(cur.getMessage());
127                     break;
128                 }
129             }
130         } catch (Exception e) {
131                 handler.reportProblem("Failed to serialize " + name + ": " + e.getMessage(), e);
132         }
133         
134         return null;
135         
136     }
137
138     Collection<SerializedVariable> mapProperties(SynchronizationEventHandler handler, Variable child) throws DatabaseException {
139         ArrayList<SerializedVariable> result = new ArrayList<SerializedVariable>();
140         for(Variable prop : child.getProperties(graph, StructuralResource2.URIs.SynchronizedRelation)) {
141                 SerializedVariable serialized = serialize(handler, prop, prop.getName(graph));
142                 if(serialized != null) result.add(serialized);
143         }
144         return result;
145     }
146
147     /**
148      * Assumes that the variable points to a composite.
149      */
150     public void fullSynchronization(Variable variable, SynchronizationEventHandler handler) throws DatabaseException {
151         long duration = 0;
152         if(TRACE) {
153             System.out.println("fullSynchronization " + variable.getURI(graph));
154             duration -= System.nanoTime();
155         }
156         SCLContext context = SCLContext.getCurrent();
157         Object oldGraph = context.put("graph", graph);
158         try {
159             handler.beginSynchronization();
160             synchronizationRec(variable, handler, null, 1.0);
161             handler.endSynchronization();
162         } finally {
163             context.put("graph", oldGraph);
164         }
165         if(TRACE) {
166             duration += System.nanoTime();
167             System.out.println("full sync in " + 1e-9*duration + "s.");
168         }
169     }
170     
171     /**
172      * Recursive implementation of partial and full synchronization. If {@code changeFlags}
173      * is null, this is full synchronization, otherwise partial synchronization.
174      */
175     private void synchronizationRec(Variable variable, SynchronizationEventHandler handler,
176             TObjectIntHashMap<Variable> changeFlags,
177             double proportionOfWork) throws DatabaseException {
178         String name = variable.getName(graph);
179         Resource type = variable.getPossibleType(graph, STR.Component);
180         if(type == null) {
181             // This same filtering is done separately in mapChildren when beginComponent has been called for the parent.
182             return;
183         }
184         Collection<Variable> children = variable.getChildren(graph);
185         if(graph.isInheritedFrom(type, STR.Composite) || graph.isInheritedFrom(type, STR.AbstractDefinedComponentType)) {
186             String typeId = graph.getPossibleURI(type);
187             if(typeId == null)
188                 throw new SynchronizationException("User component " + type + " does not have an URI.");
189             if(visitedTypes.add(typeId))
190                 visitType(typeId, type, handler);
191             boolean endComponentNeeded = false;
192             try {
193                 Collection<SerializedVariable> propertyMap = mapProperties(handler, variable);
194                 Collection<ChildInfo> childMap = mapChildren(handler, children);
195                 endComponentNeeded = true;
196                 handler.beginComponent(name, typeId, 
197                         propertyMap,
198                         Collections.<Connection>emptyList(), 
199                         childMap);
200             } catch(Exception e) {
201                 handler.reportProblem("Failed to synchronize " + name + ": " + e.getMessage(), e);
202                 if (endComponentNeeded)
203                     handler.endComponent();
204                 return;
205             }
206         } else {
207             String typeId = graph.getPossibleURI(type);
208             if(typeId == null)
209                 throw new SynchronizationException("User component " + type + " does not have an URI.");
210             if(visitedTypes.add(typeId))
211                 visitType(typeId, type, handler);
212             boolean endComponentNeeded = false;
213             try {
214                 Collection<SerializedVariable> propertyMap = mapProperties(handler, variable);
215                 Collection<Connection> connectionMap = mapConnections(handler, variable);
216                 Collection<ChildInfo> childMap = mapChildren(handler, children);
217                 endComponentNeeded = true;
218                 handler.beginComponent(name, 
219                         typeId,
220                         propertyMap,
221                         connectionMap,
222                         childMap);
223             } catch(Exception e) {
224                 handler.reportProblem("Failed to synchronize " + name + ": " + e.getMessage(), e);
225                 if (endComponentNeeded)
226                     handler.endComponent();
227                 return;
228             }
229         }
230         
231         if(changeFlags == null) {
232             // Full synchronization, synchronize all children
233             if(children.size() > 0) {
234                 double proportionOfWorkForChildren = proportionOfWork / children.size();
235                 for(Variable child : children)
236                     synchronizationRec(child, handler, null, proportionOfWorkForChildren);
237             }
238             else {
239                 didWork(proportionOfWork);
240                 // Full synchronization is cancelable
241                 if(monitor != null && monitor.isCanceled())
242                     throw new CancelTransactionException();
243             }
244         }
245         else {
246             // Partial synchronization, synchronize only children with positive changeFlag
247             int relevantChildCount = 0;
248             for(final Variable child : children) {
249                 int changeStatus = changeFlags.get(child);
250                 if(changeStatus != 0)
251                     ++relevantChildCount;
252             }
253             if(relevantChildCount > 0) {
254                 double proportionOfWorkForChildren = proportionOfWork / relevantChildCount;
255                 for(final Variable child : children) {
256                     int changeStatus = changeFlags.get(child);
257                     if(changeStatus == 0)
258                         continue;
259                     synchronizationRec(child, handler,
260                             // Apply full synchronization for subtree if changeStatus > 1
261                             changeStatus==1 ? changeFlags : null,
262                             proportionOfWorkForChildren
263                             );
264                 }
265             }
266             else {
267                 didWork(proportionOfWork);
268             }
269         }
270         handler.endComponent();
271     }
272     
273     public void partialSynchronization(Variable variable, SynchronizationEventHandler handler, TObjectIntHashMap<Variable> changeFlags) throws DatabaseException {
274         long duration = 0;
275         if(TRACE) {
276             System.out.println("partialSynchronization " + variable.getURI(graph));
277             duration -= System.nanoTime();
278         }
279         int changeStatus = changeFlags.get(variable);
280         if(changeStatus == 0) return;
281         SCLContext context = SCLContext.getCurrent();
282         Object oldGraph = context.put("graph", graph);
283         try {
284             handler.beginSynchronization();
285             synchronizationRec(variable, handler, changeStatus == 1 ? changeFlags : null, 1.0);
286             handler.endSynchronization();
287         } finally {
288             context.put("graph", oldGraph);
289         } 
290         if(TRACE) {
291             duration += System.nanoTime();
292             System.out.println("partial sync in " + 1e-9*duration + "s.");
293         }
294     }
295     
296     public void partialSynchronization(Variable variable, SynchronizationEventHandler handler, long fromRevision) throws DatabaseException {
297         TObjectIntHashMap<Variable> modifiedComponents = StructuralChangeFlattener.getModifiedComponents(graph, variable, fromRevision);
298         /*System.out.println("----------------");
299         modifiedComponents.forEachEntry(
300                 new TObjectIntProcedure<Variable>() {
301                     @Override
302                     public boolean execute(Variable a, int b) {
303                         try {
304                             System.out.println("Changed: " + a.getURI(graph) + " " + b);
305                         } catch (DatabaseException e) {
306                             e.printStackTrace();
307                         }
308                         return true;
309                     }
310                 });*/
311         partialSynchronization(variable, handler, modifiedComponents);
312     }
313     
314     void visitType(String typeId, Resource typeResource, SynchronizationEventHandler handler) throws DatabaseException {
315         Variable typeVariable = graph.syncRequest(new ResourceToPossibleVariable(typeResource), TransientCacheAsyncListener.<Variable>instance());
316         handler.beginType(typeId, mapProperties(handler, typeVariable));
317         handler.endType();
318     }
319
320     public long getHeadRevisionId() throws DatabaseException {
321         return graph.getService(ManagementSupport.class).getHeadRevisionId()+1;
322     }
323
324     
325 }