Repository: workcraft/workcraft
Issue: Signal type consistency across waveforms

When creating a new signal in a waveform that coincides with an existing signal in another waveform, the type might be inconsistent. This also happens when renaming a signal into a previously existing signal.

Additionally, renaming a signal should rename any guards that reference it.

Changed files:
WtgPlugin/src/org/workcraft/plugins/wtg/supervisors/SignalTypeConsistencySupervisor.java
WtgPlugin/src/org/workcraft/plugins/wtg/utils/VerificationUtils.java
WtgPlugin/src/org/workcraft/plugins/wtg/properties/SignalNamePropertyDescriptor.java
WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgReachabilityVerificationCommand.java
WtgPlugin/src/org/workcraft/plugins/wtg/utils/WtgUtils.java
WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgInputPropernessVerificationCommand.java

diff --git a/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgInputPropernessVerificationCommand.java b/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgInputPropernessVerificationCommand.java
index 80ec9f4c4..cac82011b 100644
--- a/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgInputPropernessVerificationCommand.java
+++ b/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgInputPropernessVerificationCommand.java
@@ -24,11 +24,11 @@ public class WtgInputPropernessVerificationCommand extends AbstractVerificationC
     @Override
     public Boolean execute(WorkspaceEntry we) {
         final Wtg wtg = WorkspaceUtils.getAs(we, Wtg.class);
-        if (VerificationUtils.checkInputProperness(wtg)) {
+        boolean result = VerificationUtils.checkInputProperness(wtg);
+        if (result) {
             DialogUtils.showInfo("The model is input proper.", TITLE);
-            return true;
         }
-        return false;
+        return result;
     }
 
 }
\\ No newline at end of file
diff --git a/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgReachabilityVerificationCommand.java b/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgReachabilityVerificationCommand.java
index 477f30c8d..70b2a393b 100644
--- a/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgReachabilityVerificationCommand.java
+++ b/WtgPlugin/src/org/workcraft/plugins/wtg/commands/WtgReachabilityVerificationCommand.java
@@ -23,11 +23,10 @@ public class WtgReachabilityVerificationCommand extends AbstractVerificationComm
     @Override
     public Boolean execute(WorkspaceEntry we) {
         final Wtg wtg = WorkspaceUtils.getAs(we, Wtg.class);
-        if (!VerificationUtils.checkReachability(wtg)) {
-            DialogUtils.showInfo("The model has unreachable nodes/transitions.", TITLE);
-            return false;
+        boolean result = VerificationUtils.checkReachability(wtg);
+        if (result) {
+            DialogUtils.showInfo("All nodes and transitions are reachable.", TITLE);
         }
-        DialogUtils.showInfo("All nodes and transitions are reachable.", TITLE);
-        return true;
+        return result;
     }
 }
diff --git a/WtgPlugin/src/org/workcraft/plugins/wtg/properties/SignalNamePropertyDescriptor.java b/WtgPlugin/src/org/workcraft/plugins/wtg/properties/SignalNamePropertyDescriptor.java
index 966e3fd30..86c017caf 100644
--- a/WtgPlugin/src/org/workcraft/plugins/wtg/properties/SignalNamePropertyDescriptor.java
+++ b/WtgPlugin/src/org/workcraft/plugins/wtg/properties/SignalNamePropertyDescriptor.java
@@ -3,11 +3,14 @@ package org.workcraft.plugins.wtg.properties;
 import org.workcraft.gui.propertyeditor.PropertyDescriptor;
 import org.workcraft.observation.PropertyChangedEvent;
 import org.workcraft.plugins.dtd.Signal;
+import org.workcraft.plugins.wtg.Guard;
+import org.workcraft.plugins.wtg.Waveform;
 import org.workcraft.plugins.wtg.Wtg;
 
 import java.util.Map;
 
 public class SignalNamePropertyDescriptor implements PropertyDescriptor {
+
     private final Wtg wtg;
     private final String signalName;
 
@@ -49,7 +52,16 @@ public class SignalNamePropertyDescriptor implements PropertyDescriptor {
                 wtg.setName(signal, (String) value);
                 signal.sendNotification(new PropertyChangedEvent(signal, Signal.PROPERTY_NAME));
             }
-
+            for (Waveform waveform : wtg.getWaveforms()) {
+                Guard guard = waveform.getGuard();
+                if (!guard.containsKey(signalName)) continue;
+                Guard newGuard = new Guard();
+                for (Map.Entry<String, Boolean> entry : guard.entrySet()) {
+                    String key = signalName.equals(entry.getKey()) ? (String) value : entry.getKey();
+                    newGuard.put(key, entry.getValue());
+                }
+                waveform.setGuard(newGuard);
+            }
         }
     }
 
diff --git a/WtgPlugin/src/org/workcraft/plugins/wtg/supervisors/SignalTypeConsistencySupervisor.java b/WtgPlugin/src/org/workcraft/plugins/wtg/supervisors/SignalTypeConsistencySupervisor.java
index 814d3beca..40b9f9127 100644
--- a/WtgPlugin/src/org/workcraft/plugins/wtg/supervisors/SignalTypeConsistencySupervisor.java
+++ b/WtgPlugin/src/org/workcraft/plugins/wtg/supervisors/SignalTypeConsistencySupervisor.java
@@ -7,6 +7,7 @@ import org.workcraft.plugins.dtd.Signal;
 import org.workcraft.plugins.wtg.Wtg;
 
 public class SignalTypeConsistencySupervisor extends StateSupervisor {
+
     private final Wtg wtg;
 
     public SignalTypeConsistencySupervisor(Wtg wtg) {
@@ -18,13 +19,30 @@ public class SignalTypeConsistencySupervisor extends StateSupervisor {
         if (e instanceof PropertyChangedEvent) {
             PropertyChangedEvent pce = (PropertyChangedEvent) e;
             String propertyName = pce.getPropertyName();
+            if (propertyName.equals(Signal.PROPERTY_NAME)) {
+                updateThisSignalType((Signal) e.getSender());
+            }
             if (propertyName.equals(Signal.PROPERTY_TYPE)) {
-                updateSignalType((Signal) e.getSender());
+                updateOtherSignalType((Signal) e.getSender());
+            }
+        }
+    }
+
+    private void updateThisSignalType(Signal signal) {
+        String signalName = wtg.getName(signal);
+        if (signalName == null) {
+            return;
+        }
+        for (Signal otherSignal : wtg.getSignals()) {
+            if (signal == otherSignal) continue;
+            if (signalName.equals(wtg.getName(otherSignal))) {
+                signal.setType(otherSignal.getType());
+                break;
             }
         }
     }
 
-    private void updateSignalType(Signal signal) {
+    private void updateOtherSignalType(Signal signal) {
         String signalName = wtg.getName(signal);
         if (signalName == null) {
             return;
diff --git a/WtgPlugin/src/org/workcraft/plugins/wtg/utils/VerificationUtils.java b/WtgPlugin/src/org/workcraft/plugins/wtg/utils/VerificationUtils.java
index 8485119dc..c94862149 100644
--- a/WtgPlugin/src/org/workcraft/plugins/wtg/utils/VerificationUtils.java
+++ b/WtgPlugin/src/org/workcraft/plugins/wtg/utils/VerificationUtils.java
@@ -8,11 +8,12 @@ import org.workcraft.plugins.wtg.State;
 import org.workcraft.plugins.wtg.Waveform;
 import org.workcraft.plugins.wtg.Wtg;
 import org.workcraft.util.DialogUtils;
+import org.workcraft.util.LogUtils;
 
 import java.util.*;
 
-import static org.workcraft.plugins.wtg.utils.WtgUtils.*;
 import static org.workcraft.plugins.wtg.converter.WtgToStgConverter.*;
+import static org.workcraft.plugins.wtg.utils.WtgUtils.*;
 
 public class VerificationUtils {
 
@@ -526,7 +527,6 @@ public class VerificationUtils {
     public static boolean checkNodeReachability(Wtg wtg) {
         Set<Node> reachableNodes = new HashSet<>();
         State initialState = wtg.getInitialState();
-        int nonReachableNodes = wtg.getWaveforms().size() + wtg.getStates().size() - 1;
         Queue<Node> nodesToVisit = new LinkedList<>();
         nodesToVisit.add(initialState);
         reachableNodes.add(initialState);
@@ -537,25 +537,21 @@ public class VerificationUtils {
                 if (!reachableNodes.contains(n)) {
                     reachableNodes.add(n);
                     nodesToVisit.add(n);
-                    nonReachableNodes = nonReachableNodes - 1;
                 }
             }
         }
-
-        if (nonReachableNodes > 0) {
-            //Error handling
-            String msg = "The following nodes are unreachable:\\n";
-            for (Waveform waveform : wtg.getWaveforms()) {
-                if (!reachableNodes.contains(waveform)) {
-                    msg = msg.concat("    " + wtg.getName(waveform) + "\\n");
-                }
-            }
-            for (State state : wtg.getStates()) {
-                if (!reachableNodes.contains(state)) {
-                    msg = msg.concat("    " + wtg.getName(state) + "\\n");
-                }
-            }
-            DialogUtils.showError(msg);
+        //Error handling
+        List<String> unreachableNodeNames = new ArrayList<>();
+        for (Waveform waveform : wtg.getWaveforms()) {
+            if (reachableNodes.contains(waveform)) continue;
+            unreachableNodeNames.add(wtg.getName(waveform));
+        }
+        for (State state : wtg.getStates()) {
+            if (reachableNodes.contains(state)) continue;
+            unreachableNodeNames.add(wtg.getName(state));
+        }
+        if (!unreachableNodeNames.isEmpty()) {
+            DialogUtils.showError(LogUtils.getTextWithRefs("Unreachable node", unreachableNodeNames));
             return false;
         }
         return true;
diff --git a/WtgPlugin/src/org/workcraft/plugins/wtg/utils/WtgUtils.java b/WtgPlugin/src/org/workcraft/plugins/wtg/utils/WtgUtils.java
index f077ac119..0fffe1171 100644
--- a/WtgPlugin/src/org/workcraft/plugins/wtg/utils/WtgUtils.java
+++ b/WtgPlugin/src/org/workcraft/plugins/wtg/utils/WtgUtils.java
@@ -138,4 +138,5 @@ public class WtgUtils {
         }
         return null;
     }
+
 }