+package org.simantics.structural.synchronization.base;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.structural.synchronization.protocol.Connection;\r
+import org.simantics.structural.synchronization.protocol.SerializedVariable;\r
+\r
+abstract public class ModuleUpdaterBase<T extends ComponentBase<T>> {\r
+\r
+ public String moduleType;\r
+ public THashMap<String, PropertyUpdateRule<T>> propertyUpdateRules =\r
+ new THashMap<String, PropertyUpdateRule<T>>();\r
+ public THashMap<String, ConnectionUpdateRule<T>> connectionUpdateRules =\r
+ new THashMap<String, ConnectionUpdateRule<T>>();\r
+ public boolean isUserComponent;\r
+ public boolean isComposite;\r
+ \r
+ public ModuleUpdaterBase(String moduleType) {\r
+ this.moduleType = moduleType;\r
+ }\r
+ \r
+ public void addPropertyUpdateRule(PropertyUpdateRule<T> rule) {\r
+ propertyUpdateRules.put(rule.getPropertyName(), rule);\r
+ }\r
+ \r
+ public void addConnectionUpdateRule(ConnectionUpdateRule<T> rule) {\r
+ connectionUpdateRules.put(rule.getConnectionPointName(), rule);\r
+ }\r
+\r
+ public void create(ModuleUpdateContext<T> context, Collection<SerializedVariable> properties, Collection<Connection> connections) {\r
+ context.command = createAddCommandBuilder(context.getModuleName()); \r
+ applyRules(context, true, properties, connections);\r
+ }\r
+ \r
+ abstract public CommandBuilder createAddCommandBuilder(String name);\r
+\r
+ public void update(ModuleUpdateContext<T> context, Collection<SerializedVariable> properties, Collection<Connection> connections) {\r
+ // Check that the module type matches\r
+ int moduleTypeId = context.getSolver().getModuleType(context.getModuleId());\r
+ String moduleTypeName = context.getSolver().getName(moduleTypeId);\r
+ if(!moduleTypeName.equals(moduleType)) {\r
+ context.getSolver().remove(context.getModuleId());\r
+ context.component.componentId = -1;\r
+ context.setModuleId(-1);\r
+ create(context, properties, connections);\r
+ }\r
+ \r
+ // Update\r
+ else {\r
+ context.command = createUpdateCommandBuilder(context.getModuleName());\r
+ applyRules(context, false, properties, connections);\r
+ }\r
+ }\r
+ \r
+ abstract public CommandBuilder createUpdateCommandBuilder(String name);\r
+ \r
+ private void applyRules(ModuleUpdateContext<T> context, boolean inCreate,\r
+ Collection<SerializedVariable> properties, Collection<Connection> connections) {\r
+ THashMap<String, Variant> propertyMap = new THashMap<String, Variant>(properties.size());\r
+ Map<String, Collection<String>> connectionMap = connections.isEmpty()\r
+ ? Collections.<String, Collection<String>>emptyMap()\r
+ : new THashMap<String, Collection<String>>(connections.size());\r
+ for(SerializedVariable property : properties)\r
+ propertyMap.put(property.name, property.value);\r
+ for(Connection connection : connections)\r
+ connectionMap.put(connection.relation, connection.connectionPoints);\r
+\r
+ context.incPendingCount(); // To prevent premature execution of the command\r
+ for(SerializedVariable property : properties) {\r
+ PropertyUpdateRule<T> rule = propertyUpdateRules.get(property.name);\r
+ if(rule != null)\r
+ rule.apply(context, inCreate, propertyMap, connectionMap, property.value);\r
+ else if(property.name.equals("IsAttached"))\r
+ ;\r
+ else\r
+ if(SynchronizationEventHandlerBase.TRACE_EVENTS)\r
+ System.out.println(" skipped property " + property.name + " " + property.toString());\r
+ }\r
+ if(inCreate) {\r
+ for(Connection connection : connections) {\r
+ ConnectionUpdateRule<T> rule = connectionUpdateRules.get(connection.relation);\r
+ if(rule != null)\r
+ rule.apply(context, propertyMap, connection.connectionPoints);\r
+ else\r
+ if(SynchronizationEventHandlerBase.TRACE_EVENTS)\r
+ System.out.println(" skipped connection " + connection.relation + " " + connection.connectionPoints);\r
+ }\r
+ }\r
+ else {\r
+ THashMap<String, ConnectionUpdateRule<T>> unusedConnectionUpdateRules =\r
+ new THashMap<String, ConnectionUpdateRule<T>>(connectionUpdateRules);\r
+ for(Connection connection : connections) {\r
+ ConnectionUpdateRule<T> rule = unusedConnectionUpdateRules.remove(connection.relation);\r
+ if(rule != null)\r
+ rule.apply(context, propertyMap, connection.connectionPoints);\r
+ else\r
+ if(SynchronizationEventHandlerBase.TRACE_EVENTS)\r
+ System.out.println(" skipped connection " + connection.relation + " " + connection.connectionPoints);\r
+ }\r
+ for(ConnectionUpdateRule<T> rule : unusedConnectionUpdateRules.values())\r
+ rule.apply(context, propertyMap, Collections.<String>emptyList());\r
+ }\r
+ context.decPendingCount();\r
+ }\r
+\r
+}\r