]> gerrit.simantics Code Review - simantics/platform.git/blob
2552192ef75dfa1f45d215c99855b87e91555a7d
[simantics/platform.git] /
1 package org.simantics.structural.synchronization.client;
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                 }
128             }
129         } catch (Exception e) {
130                 handler.reportProblem("Failed to serialize " + name + ": " + e.getMessage(), e);
131         }
132         
133         return null;
134         
135     }
136
137     Collection<SerializedVariable> mapProperties(SynchronizationEventHandler handler, Variable child) throws DatabaseException {
138         ArrayList<SerializedVariable> result = new ArrayList<SerializedVariable>();
139         for(Variable prop : child.getProperties(graph, StructuralResource2.URIs.SynchronizedRelation)) {
140                 SerializedVariable serialized = serialize(handler, prop, prop.getName(graph));
141                 if(serialized != null) result.add(serialized);
142         }
143         return result;
144     }
145
146     /**
147      * Assumes that the variable points to a composite.
148      */
149     public void fullSynchronization(Variable variable, SynchronizationEventHandler handler) throws DatabaseException {
150         long duration = 0;
151         if(TRACE) {
152             System.out.println("fullSynchronization " + variable.getURI(graph));
153             duration -= System.nanoTime();
154         }
155         SCLContext context = SCLContext.getCurrent();
156         Object oldGraph = context.put("graph", graph);
157         try {
158             handler.beginSynchronization();
159             synchronizationRec(variable, handler, null, 1.0);
160             handler.endSynchronization();
161         } finally {
162             context.put("graph", oldGraph);
163         }
164         if(TRACE) {
165             duration += System.nanoTime();
166             System.out.println("full sync in " + 1e-9*duration + "s.");
167         }
168     }
169     
170     /**
171      * Recursive implementation of partial and full synchronization. If {@code changeFlags}
172      * is null, this is full synchronization, otherwise partial synchronization.
173      */
174     private void synchronizationRec(Variable variable, SynchronizationEventHandler handler,
175             TObjectIntHashMap<Variable> changeFlags,
176             double proportionOfWork) throws DatabaseException {
177         String name = variable.getName(graph);
178         Resource type = variable.getPossibleType(graph, STR.Component);
179         if(type == null) {
180             // This same filtering is done separately in mapChildren when beginComponent has been called for the parent.
181             return;
182         }
183         Collection<Variable> children = variable.getChildren(graph);
184         if(graph.isInheritedFrom(type, STR.Composite) || graph.isInheritedFrom(type, STR.AbstractDefinedComponentType)) {
185             String typeId = graph.getPossibleURI(type);
186             if(typeId == null)
187                 throw new SynchronizationException("User component " + type + " does not have an URI.");
188             if(visitedTypes.add(typeId))
189                 visitType(typeId, type, handler);
190             boolean endComponentNeeded = false;
191             try {
192                 Collection<SerializedVariable> propertyMap = mapProperties(handler, variable);
193                 Collection<ChildInfo> childMap = mapChildren(handler, children);
194                 endComponentNeeded = true;
195                 handler.beginComponent(name, typeId, 
196                         propertyMap,
197                         Collections.<Connection>emptyList(), 
198                         childMap);
199             } catch(Exception e) {
200                 handler.reportProblem("Failed to synchronize " + name + ": " + e.getMessage(), e);
201                 if (endComponentNeeded)
202                     handler.endComponent();
203                 return;
204             }
205         } else {
206             String typeId = graph.getPossibleURI(type);
207             if(typeId == null)
208                 throw new SynchronizationException("User component " + type + " does not have an URI.");
209             if(visitedTypes.add(typeId))
210                 visitType(typeId, type, handler);
211             boolean endComponentNeeded = false;
212             try {
213                 Collection<SerializedVariable> propertyMap = mapProperties(handler, variable);
214                 Collection<Connection> connectionMap = mapConnections(handler, variable);
215                 Collection<ChildInfo> childMap = mapChildren(handler, children);
216                 endComponentNeeded = true;
217                 handler.beginComponent(name, 
218                         typeId,
219                         propertyMap,
220                         connectionMap,
221                         childMap);
222             } catch(Exception e) {
223                 handler.reportProblem("Failed to synchronize " + name + ": " + e.getMessage(), e);
224                 if (endComponentNeeded)
225                     handler.endComponent();
226                 return;
227             }
228         }
229         
230         if(changeFlags == null) {
231             // Full synchronization, synchronize all children
232             if(children.size() > 0) {
233                 double proportionOfWorkForChildren = proportionOfWork / children.size();
234                 for(Variable child : children)
235                     synchronizationRec(child, handler, null, proportionOfWorkForChildren);
236             }
237             else {
238                 didWork(proportionOfWork);
239                 // Full synchronization is cancelable
240                 if(monitor != null && monitor.isCanceled())
241                     throw new CancelTransactionException();
242             }
243         }
244         else {
245             // Partial synchronization, synchronize only children with positive changeFlag
246             int relevantChildCount = 0;
247             for(final Variable child : children) {
248                 int changeStatus = changeFlags.get(child);
249                 if(changeStatus != 0)
250                     ++relevantChildCount;
251             }
252             if(relevantChildCount > 0) {
253                 double proportionOfWorkForChildren = proportionOfWork / relevantChildCount;
254                 for(final Variable child : children) {
255                     int changeStatus = changeFlags.get(child);
256                     if(changeStatus == 0)
257                         continue;
258                     synchronizationRec(child, handler,
259                             // Apply full synchronization for subtree if changeStatus > 1
260                             changeStatus==1 ? changeFlags : null,
261                             proportionOfWorkForChildren
262                             );
263                 }
264             }
265             else {
266                 didWork(proportionOfWork);
267             }
268         }
269         handler.endComponent();
270     }
271     
272     public void partialSynchronization(Variable variable, SynchronizationEventHandler handler, TObjectIntHashMap<Variable> changeFlags) throws DatabaseException {
273         long duration = 0;
274         if(TRACE) {
275             System.out.println("partialSynchronization " + variable.getURI(graph));
276             duration -= System.nanoTime();
277         }
278         int changeStatus = changeFlags.get(variable);
279         if(changeStatus == 0) return;
280         SCLContext context = SCLContext.getCurrent();
281         Object oldGraph = context.put("graph", graph);
282         try {
283             handler.beginSynchronization();
284             synchronizationRec(variable, handler, changeStatus == 1 ? changeFlags : null, 1.0);
285             handler.endSynchronization();
286         } finally {
287             context.put("graph", oldGraph);
288         } 
289         if(TRACE) {
290             duration += System.nanoTime();
291             System.out.println("partial sync in " + 1e-9*duration + "s.");
292         }
293     }
294     
295     public void partialSynchronization(Variable variable, SynchronizationEventHandler handler, long fromRevision) throws DatabaseException {
296         TObjectIntHashMap<Variable> modifiedComponents = StructuralChangeFlattener.getModifiedComponents(graph, variable, fromRevision);
297         /*System.out.println("----------------");
298         modifiedComponents.forEachEntry(
299                 new TObjectIntProcedure<Variable>() {
300                     @Override
301                     public boolean execute(Variable a, int b) {
302                         try {
303                             System.out.println("Changed: " + a.getURI(graph) + " " + b);
304                         } catch (DatabaseException e) {
305                             e.printStackTrace();
306                         }
307                         return true;
308                     }
309                 });*/
310         partialSynchronization(variable, handler, modifiedComponents);
311     }
312     
313     void visitType(String typeId, Resource typeResource, SynchronizationEventHandler handler) throws DatabaseException {
314         Variable typeVariable = graph.syncRequest(new ResourceToPossibleVariable(typeResource), TransientCacheAsyncListener.<Variable>instance());
315         handler.beginType(typeId, mapProperties(handler, typeVariable));
316         handler.endType();
317     }
318
319     public long getHeadRevisionId() throws DatabaseException {
320         return graph.getService(ManagementSupport.class).getHeadRevisionId()+1;
321     }
322
323     
324 }