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