X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.structural.synchronization.client%2Fsrc%2Forg%2Fsimantics%2Fstructural%2Fsynchronization%2Fbase%2FModuleUpdaterBase.java;h=45ea7400fbbf8682d0da92ce463cc008fdbe62d1;hb=b825d7a7aff95002f355878ec2767352411b5a43;hp=c8347f7e22b25d2234353c48f6a7100a46e9ff6b;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/ModuleUpdaterBase.java b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/ModuleUpdaterBase.java index c8347f7e2..45ea7400f 100644 --- a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/ModuleUpdaterBase.java +++ b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/ModuleUpdaterBase.java @@ -1,111 +1,113 @@ -package org.simantics.structural.synchronization.base; - -import gnu.trove.map.hash.THashMap; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.structural.synchronization.protocol.Connection; -import org.simantics.structural.synchronization.protocol.SerializedVariable; - -abstract public class ModuleUpdaterBase> { - - public String moduleType; - public THashMap> propertyUpdateRules = - new THashMap>(); - public THashMap> connectionUpdateRules = - new THashMap>(); - public boolean isUserComponent; - public boolean isComposite; - - public ModuleUpdaterBase(String moduleType) { - this.moduleType = moduleType; - } - - public void addPropertyUpdateRule(PropertyUpdateRule rule) { - propertyUpdateRules.put(rule.getPropertyName(), rule); - } - - public void addConnectionUpdateRule(ConnectionUpdateRule rule) { - connectionUpdateRules.put(rule.getConnectionPointName(), rule); - } - - public void create(ModuleUpdateContext context, Collection properties, Collection connections) { - context.command = createAddCommandBuilder(context.getModuleName()); - applyRules(context, true, properties, connections); - } - - abstract public CommandBuilder createAddCommandBuilder(String name); - - public void update(ModuleUpdateContext context, Collection properties, Collection connections) { - // Check that the module type matches - int moduleTypeId = context.getSolver().getModuleType(context.getModuleId()); - String moduleTypeName = context.getSolver().getName(moduleTypeId); - if(!moduleTypeName.equals(moduleType)) { - context.getSolver().remove(context.getModuleId()); - context.component.componentId = -1; - context.setModuleId(-1); - create(context, properties, connections); - } - - // Update - else { - context.command = createUpdateCommandBuilder(context.getModuleName()); - applyRules(context, false, properties, connections); - } - } - - abstract public CommandBuilder createUpdateCommandBuilder(String name); - - private void applyRules(ModuleUpdateContext context, boolean inCreate, - Collection properties, Collection connections) { - THashMap propertyMap = new THashMap(properties.size()); - Map> connectionMap = connections.isEmpty() - ? Collections.>emptyMap() - : new THashMap>(connections.size()); - for(SerializedVariable property : properties) - propertyMap.put(property.name, property.value); - for(Connection connection : connections) - connectionMap.put(connection.relation, connection.connectionPoints); - - context.incPendingCount(); // To prevent premature execution of the command - for(SerializedVariable property : properties) { - PropertyUpdateRule rule = propertyUpdateRules.get(property.name); - if(rule != null) - rule.apply(context, inCreate, propertyMap, connectionMap, property.value); - else if(property.name.equals("IsAttached")) - ; - else - if(SynchronizationEventHandlerBase.TRACE_EVENTS) - System.out.println(" skipped property " + property.name + " " + property.toString()); - } - if(inCreate) { - for(Connection connection : connections) { - ConnectionUpdateRule rule = connectionUpdateRules.get(connection.relation); - if(rule != null) - rule.apply(context, propertyMap, connection.connectionPoints); - else - if(SynchronizationEventHandlerBase.TRACE_EVENTS) - System.out.println(" skipped connection " + connection.relation + " " + connection.connectionPoints); - } - } - else { - THashMap> unusedConnectionUpdateRules = - new THashMap>(connectionUpdateRules); - for(Connection connection : connections) { - ConnectionUpdateRule rule = unusedConnectionUpdateRules.remove(connection.relation); - if(rule != null) - rule.apply(context, propertyMap, connection.connectionPoints); - else - if(SynchronizationEventHandlerBase.TRACE_EVENTS) - System.out.println(" skipped connection " + connection.relation + " " + connection.connectionPoints); - } - for(ConnectionUpdateRule rule : unusedConnectionUpdateRules.values()) - rule.apply(context, propertyMap, Collections.emptyList()); - } - context.decPendingCount(); - } - -} +package org.simantics.structural.synchronization.base; + +import gnu.trove.map.hash.THashMap; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.structural.synchronization.protocol.Connection; +import org.simantics.structural.synchronization.protocol.SerializedVariable; +import org.simantics.structural.synchronization.utils.ComponentBase; + +abstract public class ModuleUpdaterBase> { + + public String moduleType; + public THashMap> propertyUpdateRules = + new THashMap>(); + public THashMap> connectionUpdateRules = + new THashMap>(); + public boolean isUserComponent; + public boolean isComposite; + public String subprocessType; + + public ModuleUpdaterBase(String moduleType) { + this.moduleType = moduleType; + } + + public void addPropertyUpdateRule(PropertyUpdateRule rule) { + propertyUpdateRules.put(rule.getPropertyName(), rule); + } + + public void addConnectionUpdateRule(ConnectionUpdateRule rule) { + connectionUpdateRules.put(rule.getConnectionPointName(), rule); + } + + public void create(ModuleUpdateContext context, Collection properties, Collection connections) { + context.command = createAddCommandBuilder(context.getModuleName()); + applyRules(context, true, properties, connections); + } + + abstract public CommandBuilder createAddCommandBuilder(String name); + + public void update(ModuleUpdateContext context, Collection properties, Collection connections) { + // Check that the module type matches + int moduleTypeId = context.getSolver().getModuleType(context.getModuleId()); + String moduleTypeName = context.getSolver().getName(moduleTypeId); + if(!moduleTypeName.equals(moduleType)) { + context.getSolver().remove(context.getModuleId()); + context.component.componentId = -1; + context.setModuleId(-1); + create(context, properties, connections); + } + + // Update + else { + context.command = createUpdateCommandBuilder(context.getModuleName()); + applyRules(context, false, properties, connections); + } + } + + abstract public CommandBuilder createUpdateCommandBuilder(String name); + + private void applyRules(ModuleUpdateContext context, boolean inCreate, + Collection properties, Collection connections) { + THashMap propertyMap = new THashMap(properties.size()); + Map> connectionMap = connections.isEmpty() + ? Collections.>emptyMap() + : new THashMap>(connections.size()); + for(SerializedVariable property : properties) + propertyMap.put(property.name, property.value); + for(Connection connection : connections) + connectionMap.put(connection.relation, connection.connectionPoints); + + context.incPendingCount(); // To prevent premature execution of the command + for(SerializedVariable property : properties) { + PropertyUpdateRule rule = propertyUpdateRules.get(property.name); + if(rule != null) + rule.apply(context, inCreate, propertyMap, connectionMap, property.value); + else if(property.name.equals("IsAttached")) + ; + else + if(SynchronizationEventHandlerBase.TRACE_EVENTS) + System.out.println(" skipped property " + property.name + " " + property.toString()); + } + if(inCreate) { + for(Connection connection : connections) { + ConnectionUpdateRule rule = connectionUpdateRules.get(connection.relation); + if(rule != null) + rule.apply(context, propertyMap, connection.connectionPoints); + else + if(SynchronizationEventHandlerBase.TRACE_EVENTS) + System.out.println(" skipped connection " + connection.relation + " " + connection.connectionPoints); + } + } + else { + THashMap> unusedConnectionUpdateRules = + new THashMap>(connectionUpdateRules); + for(Connection connection : connections) { + ConnectionUpdateRule rule = unusedConnectionUpdateRules.remove(connection.relation); + if(rule != null) + rule.apply(context, propertyMap, connection.connectionPoints); + else + if(SynchronizationEventHandlerBase.TRACE_EVENTS) + System.out.println(" skipped connection " + connection.relation + " " + connection.connectionPoints); + } + for(ConnectionUpdateRule rule : unusedConnectionUpdateRules.values()) + rule.apply(context, propertyMap, Collections.emptyList()); + } + context.decPendingCount(); + } + +}