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