package org.workcraft.plugins.fst.converters;

import java.util.Map;

import org.workcraft.dom.math.MathNode;
import org.workcraft.dom.visual.connections.VisualConnection;
import org.workcraft.gui.graph.converters.DefaultModelConverter;
import org.workcraft.plugins.fsm.Event;
import org.workcraft.plugins.fsm.Fsm;
import org.workcraft.plugins.fsm.State;
import org.workcraft.plugins.fsm.Symbol;
import org.workcraft.plugins.fsm.VisualEvent;
import org.workcraft.plugins.fsm.VisualFsm;
import org.workcraft.plugins.fsm.VisualState;
import org.workcraft.plugins.fst.Fst;
import org.workcraft.plugins.fst.Signal;
import org.workcraft.plugins.fst.SignalEvent;
import org.workcraft.plugins.fst.VisualFst;
import org.workcraft.plugins.fst.VisualSignalEvent;

public class FsmToFstConverter extends DefaultModelConverter<VisualFsm, VisualFst> {

    public FsmToFstConverter(VisualFsm srcModel, VisualFst dstModel) {
        super(srcModel, dstModel);
    }

    @Override
    public Map<Class<? extends MathNode>, Class<? extends MathNode>> getComponentClassMap() {
        Map<Class<? extends MathNode>, Class<? extends MathNode>> result = super.getComponentClassMap();
        result.put(State.class, State.class);
        return result;
    }

    @Override
    public VisualConnection convertConnection(VisualConnection srcConnection) {
        VisualConnection dstConnection = super.convertConnection(srcConnection);
        if ((srcConnection instanceof VisualEvent) && (dstConnection instanceof VisualSignalEvent)) {
            Event srcEvent = (Event) srcConnection.getReferencedConnection();
            Symbol srcSymbol = srcEvent.getSymbol();
            Fsm fsm = (Fsm) getSrcModel().getMathModel();
            String name = fsm.getName(srcSymbol);
            Fst fst = (Fst) getDstModel().getMathModel();
            Signal dstSignal = fst.getOrCreateSignal(name, Signal.Type.DUMMY);
            SignalEvent dstSignalEvent = (SignalEvent) dstConnection.getReferencedConnection();
            dstSignalEvent.setSymbol(dstSignal);
        }
        return dstConnection;
    }

    @Override
    public void postprocessing() {
        VisualFsm fsm = getSrcModel();
        for (VisualState srcState: fsm.getVisualStates()) {
            if (srcState.getReferencedState().isInitial()) {
                VisualState dstState = (VisualState) getSrcToDstNode(srcState);
                if (dstState != null) {
                    dstState.getReferencedState().setInitial(true);
                }
            }
        }
    }

}

@@ -44,31 +44,8 @@
 s;%0A%0A
-import java.util.Map;%0A%0A
 impo
@@ -80,16 +80,16 @@
 thNode;%0A
+
 import o
@@ -244,306 +244,9 @@
 fsm.
-Event;%0Aimport org.workcraft.plugins.fsm.Fsm;%0Aimport org.workcraft.plugins.fsm.State;%0Aimport org.workcraft.plugins.fsm.Symbol;%0Aimport org.workcraft.plugins.fsm.VisualEvent;%0Aimport org.workcraft.plugins.fsm.VisualFsm;%0Aimport org.workcraft.plugins.fsm.VisualState;%0Aimport org.workcraft.plugins.fst.Fst
+*
 ;%0Aim
@@ -374,16 +374,16 @@
 ualFst;%0A
-
 import o
@@ -427,16 +427,39 @@
 Event;%0A%0A
+import java.util.Map;%0A%0A
 public c
@@ -1362,87 +1362,93 @@
     
-Fsm fsm = (Fsm) getSrcModel().getMathModel();%0A            String name = fsm.get
+String name = (srcSymbol == null) ? Fsm.EPSILON_SERIALISATION : getSrcModel().getMath
 Name
@@ -1476,23 +1476,26 @@
     
-Fst fst = (Fst)
+Signal dstSignal =
  get
@@ -1523,44 +1523,8 @@
 el()
-;%0A            Signal dstSignal = fst
 .get

Fix for FSM to FST conversion for epsilon symbol