Repository: workcraft/workcraft
Issue: Trace conversion to DTD misses first level if initial state is high

To reproduce, create a simulation trace "a-" and convert it to DTD -- the resultant waveform misses the initial high level.

Changed files:
DtdPlugin/src/org/workcraft/plugins/dtd/VisualDtd.java, StgPlugin/src/org/workcraft/plugins/stg/converters/StgToDtdConverter.java, GraphPlugin/src/org/workcraft/plugins/graph/Graph.java, CircuitPlugin/src/org/workcraft/plugins/circuit/interop/VerilogImporter.java

diff --git a/CircuitPlugin/src/org/workcraft/plugins/circuit/interop/VerilogImporter.java b/CircuitPlugin/src/org/workcraft/plugins/circuit/interop/VerilogImporter.java
index ede94d41e..8db61a816 100644
--- a/CircuitPlugin/src/org/workcraft/plugins/circuit/interop/VerilogImporter.java
+++ b/CircuitPlugin/src/org/workcraft/plugins/circuit/interop/VerilogImporter.java
@@ -818,7 +818,8 @@ public class VerilogImporter implements Importer {
                 try {
                     component.setIsZeroDelay(true);
                 } catch (ArgumentException e) {
-                    LogUtils.logWarning("Component '" + verilogInstance.name + "': " + e.getMessage());
+                    LogUtils.logWarning(e.getMessage()
+                            + " Zero delay attribute is ignored for component '" + verilogInstance.name + "'.");
                 }
             }
         }
diff --git a/DtdPlugin/src/org/workcraft/plugins/dtd/VisualDtd.java b/DtdPlugin/src/org/workcraft/plugins/dtd/VisualDtd.java
index fbffe2452..b01b48317 100644
--- a/DtdPlugin/src/org/workcraft/plugins/dtd/VisualDtd.java
+++ b/DtdPlugin/src/org/workcraft/plugins/dtd/VisualDtd.java
@@ -395,9 +395,6 @@ public class VisualDtd extends AbstractVisualModel {
         VisualConnection beforeLevel = null;
         try {
             beforeLevel = connect(event, edge);
-            if (connection instanceof VisualLevelConnection) {
-                color = ((VisualLevelConnection) connection).getColor();
-            }
             beforeLevel.setColor(color);
         } catch (InvalidConnectionException e) {
         }
diff --git a/GraphPlugin/src/org/workcraft/plugins/graph/Graph.java b/GraphPlugin/src/org/workcraft/plugins/graph/Graph.java
index 88af073f7..6e677169b 100644
--- a/GraphPlugin/src/org/workcraft/plugins/graph/Graph.java
+++ b/GraphPlugin/src/org/workcraft/plugins/graph/Graph.java
@@ -58,7 +58,7 @@ public class Graph extends AbstractMathModel {
         if (srcModel == null) {
             srcModel = this;
         }
-        HierarchyReferenceManager refManager = (HierarchyReferenceManager) getReferenceManager();
+        HierarchyReferenceManager refManager = getReferenceManager();
         NameManager nameManagerer = refManager.getNameManager(null);
         for (MathNode srcNode: srcChildren) {
             if (srcNode instanceof Vertex) {
diff --git a/StgPlugin/src/org/workcraft/plugins/stg/converters/StgToDtdConverter.java b/StgPlugin/src/org/workcraft/plugins/stg/converters/StgToDtdConverter.java
index e010c77d5..9efaeb760 100644
--- a/StgPlugin/src/org/workcraft/plugins/stg/converters/StgToDtdConverter.java
+++ b/StgPlugin/src/org/workcraft/plugins/stg/converters/StgToDtdConverter.java
@@ -7,6 +7,7 @@ import org.workcraft.exceptions.InvalidConnectionException;
 import org.workcraft.gui.tools.Trace;
 import org.workcraft.plugins.dtd.*;
 import org.workcraft.plugins.dtd.VisualDtd.SignalEvent;
+import org.workcraft.plugins.dtd.utils.DtdUtils;
 import org.workcraft.plugins.stg.SignalTransition;
 import org.workcraft.plugins.stg.Stg;
 import org.workcraft.types.Pair;
@@ -16,6 +17,7 @@ import java.awt.geom.Point2D;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
+import java.util.Set;
 
 public class StgToDtdConverter {
     private static final double SIGNAL_OFFSET = 1.0;
@@ -34,6 +36,7 @@ public class StgToDtdConverter {
         this.stg = stg;
         this.dtd = (dtd == null) ? new VisualDtd(new Dtd()) : dtd;
         signalMap = createSignals(signals);
+        setInitialState(trace);
         eventMap = ctreateEvents(trace);
     }
 
@@ -84,12 +87,30 @@ public class StgToDtdConverter {
         return visualSignal;
     }
 
+    private void setInitialState(Trace trace) {
+        Set<String> visitedSignals = new HashSet<>();
+        for (String transitionRef: trace) {
+            MathNode node = stg.getNodeByReference(transitionRef);
+            if (node instanceof SignalTransition) {
+                SignalTransition transition = (SignalTransition) node;
+                String signalRef = stg.getSignalReference(transition);
+                if (visitedSignals.contains(signalRef)) continue;
+                visitedSignals.add(signalRef);
+                if (signalMap.containsKey(signalRef)) {
+                    VisualSignal signal = signalMap.get(signalRef);
+                    TransitionEvent.Direction direction = getDirection(transition.getDirection());
+                    signal.setInitialState(DtdUtils.getPreviousState(direction));
+                }
+            }
+        }
+    }
+
     private HashMap<SignalEvent, SignalTransition> ctreateEvents(Trace trace) {
         HashMap<SignalEvent, SignalTransition> result = new HashMap<>();
         HashMap<Node, HashSet<SignalEvent>> causeMap = new HashMap<>();
         double x = EVENT_OFFSET;
         for (String transitionRef: trace) {
-            MathNode node = (MathNode) stg.getNodeByReference(transitionRef);
+            MathNode node = stg.getNodeByReference(transitionRef);
             if (node == null) continue;
             boolean skip = true;
             if (node instanceof SignalTransition) {