1 package era.mi.gui.modeladapter;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
9 import java.util.Map.Entry;
11 import java.util.function.Function;
12 import java.util.stream.Collectors;
14 import era.mi.gui.model.ViewModel;
15 import era.mi.gui.model.components.GUIAndGate;
16 import era.mi.gui.model.components.GUIComponent;
17 import era.mi.gui.model.wires.GUIWire;
18 import era.mi.gui.model.wires.Pin;
19 import era.mi.gui.model.wires.WireCrossPoint;
20 import era.mi.gui.modeladapter.componentadapters.AndGateAdapter;
21 import era.mi.gui.modeladapter.componentadapters.ComponentAdapter;
22 import era.mi.logic.components.Component;
23 import era.mi.logic.timeline.Timeline;
24 import era.mi.logic.wires.Wire;
25 import era.mi.logic.wires.Wire.ReadEnd;
27 public class ViewLogicModelAdapter
29 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
32 Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashMap<>();
33 componentAdaptersModifiable.put(GUIAndGate.class, new AndGateAdapter());
34 // TODO list all "primitive" adapters here
35 componentAdapters = Collections.unmodifiableMap(componentAdaptersModifiable);
38 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
40 // TODO replace Timeline with LogicModel as soon as it exists
41 Timeline timeline = new Timeline(10);
43 Map<Pin, Wire> logicWiresPerPin = convertWires(viewModel.getWires(), params, timeline);
44 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
46 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
47 for (GUIComponent guiComp : viewModel.getComponents())
49 // WireCrossPoints just vanish
50 if (!(guiComp instanceof WireCrossPoint))
51 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
52 componentAdapters.get(guiComp.getClass())));
55 // TODO handle complex components
57 List<Component> logicComponents = new ArrayList<>();
58 logicComponents.addAll(oneToOneComponents.values());
63 private static Map<Pin, Wire> convertWires(List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
65 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(wires);
66 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
67 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
68 return logicWiresPerPin;
71 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
73 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
74 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
75 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
76 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
77 set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
78 return logicWiresPerPin;
81 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
83 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream()
84 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
85 for (GUIWire guiWire : wires)
86 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
89 private static Map<Pin, Set<Pin>> getConnectedPinGroups(List<GUIWire> wires)
91 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
94 Pin pin1 = wire.getPin1();
95 Pin pin2 = wire.getPin2();
97 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.putIfAbsent(pin1, new HashSet<>());
98 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.putIfAbsent(pin2, new HashSet<>());
100 pin1ConnectedPins.addAll(pin2ConnectedPins);
101 pin1ConnectedPins.add(pin1);
102 pin1ConnectedPins.add(pin2);
104 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
106 return connectedPinsPerPin;
109 @SuppressWarnings("unchecked")
110 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
111 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
113 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
116 private ViewLogicModelAdapter()
118 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");