Added adapters for OrGate and NotGate
[Mograsim.git] / LogicUI / src / era / mi / gui / modeladapter / ViewLogicModelAdapter.java
1 package era.mi.gui.modeladapter;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.Set;
11 import java.util.function.Function;
12 import java.util.stream.Collectors;
13
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.components.GUINotGate;
18 import era.mi.gui.model.components.GUIOrGate;
19 import era.mi.gui.model.wires.GUIWire;
20 import era.mi.gui.model.wires.Pin;
21 import era.mi.gui.model.wires.WireCrossPoint;
22 import era.mi.gui.modeladapter.componentadapters.ComponentAdapter;
23 import era.mi.gui.modeladapter.componentadapters.SimpleGateAdapter;
24 import era.mi.logic.components.Component;
25 import era.mi.logic.components.gates.AndGate;
26 import era.mi.logic.components.gates.NotGate;
27 import era.mi.logic.components.gates.OrGate;
28 import era.mi.logic.timeline.Timeline;
29 import era.mi.logic.wires.Wire;
30 import era.mi.logic.wires.Wire.ReadEnd;
31
32 public class ViewLogicModelAdapter
33 {
34         private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
35         static
36         {
37                 Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashMap<>();
38                 componentAdaptersModifiable.put(GUIOrGate.class, new SimpleGateAdapter(OrGate::new));
39                 componentAdaptersModifiable.put(GUIAndGate.class, new SimpleGateAdapter(AndGate::new));
40                 componentAdaptersModifiable.put(GUINotGate.class, new SimpleGateAdapter((t, p, o, i) -> new NotGate(t, p, i[0], o)));
41                 // TODO list all "primitive" adapters here
42                 componentAdapters = Collections.unmodifiableMap(componentAdaptersModifiable);
43         }
44
45         public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
46         {
47                 // TODO replace Timeline with LogicModel as soon as it exists
48                 Timeline timeline = new Timeline(10);
49
50                 Map<Pin, Wire> logicWiresPerPin = convertWires(
51                                 viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet()),
52                                 viewModel.getWires(), params, timeline);
53                 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
54
55                 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
56                 for (GUIComponent guiComp : viewModel.getComponents())
57                 {
58                         // WireCrossPoints just vanish
59                         if (!(guiComp instanceof WireCrossPoint))
60                                 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
61                                                 componentAdapters.get(guiComp.getClass())));
62                 }
63
64                 // TODO handle complex components
65
66                 List<Component> logicComponents = new ArrayList<>();
67                 logicComponents.addAll(oneToOneComponents.values());
68
69                 return timeline;
70         }
71
72         private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
73         {
74                 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
75                 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
76                 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
77                 return logicWiresPerPin;
78         }
79
80         private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
81         {
82                 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
83                 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
84                 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
85                         logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
86                                         set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
87                 return logicWiresPerPin;
88         }
89
90         private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
91         {
92                 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
93                                 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
94                 for (GUIWire guiWire : wires)
95                         guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
96         }
97
98         private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
99         {
100                 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
101
102                 for (Pin p : allPins)
103                 {
104                         HashSet<Pin> connectedPins = new HashSet<>();
105                         connectedPins.add(p);
106                         connectedPinsPerPin.put(p, connectedPins);
107                 }
108
109                 wires.forEach(wire ->
110                 {
111                         Pin pin1 = wire.getPin1();
112                         Pin pin2 = wire.getPin2();
113
114                         Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
115                         Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
116
117                         pin1ConnectedPins.addAll(pin2ConnectedPins);
118                         pin1ConnectedPins.add(pin1);
119                         pin1ConnectedPins.add(pin2);
120
121                         pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
122                 });
123                 return connectedPinsPerPin;
124         }
125
126         @SuppressWarnings("unchecked")
127         private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
128                         GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
129         {
130                 if (adapter == null)
131                         throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
132                 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
133         }
134
135         private ViewLogicModelAdapter()
136         {
137                 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");
138         }
139 }