1 package org.simantics.structural.synchronization;
3 import gnu.trove.map.hash.TObjectIntHashMap;
4 import gnu.trove.set.hash.THashSet;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Collections;
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;
32 public class Synchronizer {
34 public static boolean TRACE = false;
38 StructuralResource2 STR;
39 THashSet<String> visitedTypes = new THashSet<String>();
40 IProgressMonitor monitor;
42 double workDone = 0.0;
46 public Synchronizer(ReadGraph graph) {
48 L0 = Layer0.getInstance(graph);
49 STR = StructuralResource2.getInstance(graph);
52 public void setMonitor(IProgressMonitor monitor, int maxWork) {
53 this.monitor = monitor;
54 this.maxWork = maxWork;
55 this.workDoneInteger = 0;
58 private void didWork(double amount) {
60 //System.out.println(workDone);
62 int t = (int)(workDone * maxWork);
63 if(t > workDoneInteger) {
64 monitor.worked(t-workDoneInteger);
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());
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)
81 result.add(mapChild(child));
82 } catch(Exception e) {
83 handler.reportProblem("Failed to get ChildInfo for " + child + ".", e);
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);
103 result.add(new Connection(name, cps));
111 private SerializedVariable serialize(SynchronizationEventHandler handler, Variable var, String name) throws DatabaseException {
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);
120 } catch (MissingVariableValueException e) {
121 handler.reportProblem("Failed to read " + name + ". " + e.getMessage());
124 while((cur = cur.getCause()) != null) {
125 if(!(cur instanceof MissingVariableValueException)) {
126 handler.reportProblem(cur.getMessage());
129 } catch (Exception e) {
130 handler.reportProblem("Failed to serialize " + name + ": " + e.getMessage(), e);
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);
147 * Assumes that the variable points to a composite.
149 public void fullSynchronization(Variable variable, SynchronizationEventHandler handler) throws DatabaseException {
152 System.out.println("fullSynchronization " + variable.getURI(graph));
153 duration -= System.nanoTime();
155 SCLContext context = SCLContext.getCurrent();
156 Object oldGraph = context.put("graph", graph);
158 handler.beginSynchronization();
159 synchronizationRec(variable, handler, null, 1.0);
160 handler.endSynchronization();
162 context.put("graph", oldGraph);
165 duration += System.nanoTime();
166 System.out.println("full sync in " + 1e-9*duration + "s.");
171 * Recursive implementation of partial and full synchronization. If {@code changeFlags}
172 * is null, this is full synchronization, otherwise partial synchronization.
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);
180 // This same filtering is done separately in mapChildren when beginComponent has been called for the parent.
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);
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;
192 Collection<SerializedVariable> propertyMap = mapProperties(handler, variable);
193 Collection<ChildInfo> childMap = mapChildren(handler, children);
194 endComponentNeeded = true;
195 handler.beginComponent(name, typeId,
197 Collections.<Connection>emptyList(),
199 } catch(Exception e) {
200 handler.reportProblem("Failed to synchronize " + name + ": " + e.getMessage(), e);
201 if (endComponentNeeded)
202 handler.endComponent();
206 String typeId = graph.getPossibleURI(type);
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;
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,
222 } catch(Exception e) {
223 handler.reportProblem("Failed to synchronize " + name + ": " + e.getMessage(), e);
224 if (endComponentNeeded)
225 handler.endComponent();
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);
238 didWork(proportionOfWork);
239 // Full synchronization is cancelable
240 if(monitor != null && monitor.isCanceled())
241 throw new CancelTransactionException();
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;
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)
258 synchronizationRec(child, handler,
259 // Apply full synchronization for subtree if changeStatus > 1
260 changeStatus==1 ? changeFlags : null,
261 proportionOfWorkForChildren
266 didWork(proportionOfWork);
269 handler.endComponent();
272 public void partialSynchronization(Variable variable, SynchronizationEventHandler handler, TObjectIntHashMap<Variable> changeFlags) throws DatabaseException {
275 System.out.println("partialSynchronization " + variable.getURI(graph));
276 duration -= System.nanoTime();
278 int changeStatus = changeFlags.get(variable);
279 if(changeStatus == 0) return;
280 SCLContext context = SCLContext.getCurrent();
281 Object oldGraph = context.put("graph", graph);
283 handler.beginSynchronization();
284 synchronizationRec(variable, handler, changeStatus == 1 ? changeFlags : null, 1.0);
285 handler.endSynchronization();
287 context.put("graph", oldGraph);
290 duration += System.nanoTime();
291 System.out.println("partial sync in " + 1e-9*duration + "s.");
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>() {
301 public boolean execute(Variable a, int b) {
303 System.out.println("Changed: " + a.getURI(graph) + " " + b);
304 } catch (DatabaseException e) {
310 partialSynchronization(variable, handler, modifiedComponents);
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));
319 public long getHeadRevisionId() throws DatabaseException {
320 return graph.getService(ManagementSupport.class).getHeadRevisionId()+1;