Pins are now accessed via name, not index
[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.Collections;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Map.Entry;
9 import java.util.Set;
10 import java.util.function.Function;
11 import java.util.stream.Collectors;
12
13 import net.mograsim.logic.core.components.gates.AndGate;
14 import net.mograsim.logic.core.components.gates.NandGate;
15 import net.mograsim.logic.core.components.gates.NotGate;
16 import net.mograsim.logic.core.components.gates.OrGate;
17 import net.mograsim.logic.core.timeline.Timeline;
18 import net.mograsim.logic.core.wires.Wire;
19 import net.mograsim.logic.core.wires.Wire.ReadEnd;
20 import net.mograsim.logic.ui.model.ViewModel;
21 import net.mograsim.logic.ui.model.components.GUIAndGate;
22 import net.mograsim.logic.ui.model.components.GUIComponent;
23 import net.mograsim.logic.ui.model.components.GUINandGate;
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.components.SubmodelComponent;
27 import net.mograsim.logic.ui.model.components.SubmodelInterface;
28 import net.mograsim.logic.ui.model.wires.GUIWire;
29 import net.mograsim.logic.ui.model.wires.Pin;
30 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
31 import net.mograsim.logic.ui.modeladapter.componentadapters.AtomicAm2901NANDBasedAdapter;
32 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
33 import net.mograsim.logic.ui.modeladapter.componentadapters.ComponentAdapter;
34 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
35 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
36
37 public class ViewLogicModelAdapter
38 {
39         private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
40         static
41         {
42                 Set<ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashSet<>();
43                 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
44                 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
45                 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
46                 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINandGate.class, NandGate::new));
47                 componentAdaptersModifiable.add(new ManualSwitchAdapter());
48                 componentAdaptersModifiable.add(new BitDisplayAdapter());
49                 componentAdaptersModifiable.add(new AtomicAm2901NANDBasedAdapter());
50                 // TODO list all adapters here
51                 componentAdapters = Collections.unmodifiableMap(
52                                 componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
53         }
54
55         public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
56         {
57                 // TODO replace Timeline with LogicModel as soon as it exists
58                 Timeline timeline = new Timeline(10);
59
60                 convert(viewModel, params, timeline, Map.of());
61
62                 return timeline;
63         }
64
65         private static void convert(ViewModel viewModel, LogicModelParameters params, Timeline timeline, Map<Pin, Wire> externalWires)
66         {
67                 Map<Pin, Wire> logicWiresPerPin = convertWires(getAllPins(viewModel), viewModel.getWires(), externalWires, params, timeline);
68                 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
69
70                 for (GUIComponent guiComp : viewModel.getComponents())
71                 {
72                         if (guiComp instanceof SubmodelComponent)
73                         {
74                                 SubmodelComponent guiCompCasted = (SubmodelComponent) guiComp;
75                                 Map<String, Pin> supermodelPins = guiCompCasted.getSupermodelPins();
76                                 Map<Pin, Wire> externalWiresForSubmodel = supermodelPins.entrySet().stream()
77                                                 .collect(Collectors.toMap(e -> guiCompCasted.getSubmodelPin(e.getKey()), e -> logicWiresPerPin.get(e.getValue())));
78                                 convert(guiCompCasted.submodel, params, timeline, externalWiresForSubmodel);
79                         } else if (guiComp instanceof WireCrossPoint)
80                         {
81                                 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
82                                 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
83                         } else if (!(guiComp instanceof SubmodelInterface))// nothing to do for SubmodelInterfaces
84                                 createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable, componentAdapters.get(guiComp.getClass()));
85                 }
86         }
87
88         private static Set<Pin> getAllPins(ViewModel viewModel)
89         {
90                 return viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet());
91         }
92
93         private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, Map<Pin, Wire> externalWires,
94                         LogicModelParameters params, Timeline timeline)
95         {
96                 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
97                 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups, externalWires);
98                 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
99                 return logicWiresPerPin;
100         }
101
102         private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
103                         Map<Pin, Wire> externalWires)
104         {
105                 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
106                 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
107                 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
108                         logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(), set ->
109                         {
110                                 Wire externalWire = null;
111                                 for (Pin p : set)
112                                 {
113                                         Wire externalWireCandidate = externalWires.get(p);
114                                         if (externalWireCandidate != null)
115                                                 if (externalWire == null)
116                                                         externalWire = externalWireCandidate;
117                                                 else
118                                                         throw new IllegalArgumentException("Two pins to external wires can't be connected directly");
119                                 }
120                                 return externalWire == null ? new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
121                         }));
122                 return logicWiresPerPin;
123         }
124
125         private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
126         {
127                 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
128                                 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
129                 for (GUIWire guiWire : wires)
130                         guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
131         }
132
133         private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
134         {
135                 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
136
137                 for (Pin p : allPins)
138                 {
139                         HashSet<Pin> connectedPins = new HashSet<>();
140                         connectedPins.add(p);
141                         connectedPinsPerPin.put(p, connectedPins);
142                 }
143
144                 wires.forEach(wire ->
145                 {
146                         Pin pin1 = wire.getPin1();
147                         Pin pin2 = wire.getPin2();
148
149                         Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
150                         Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
151
152                         pin1ConnectedPins.addAll(pin2ConnectedPins);
153                         pin1ConnectedPins.add(pin1);
154                         pin1ConnectedPins.add(pin2);
155
156                         pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
157                 });
158                 return connectedPinsPerPin;
159         }
160
161         @SuppressWarnings("unchecked")
162         private static <G extends GUIComponent> void createAndLinkComponent(Timeline timeline, LogicModelParameters params,
163                         GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
164         {
165                 if (adapter == null)
166                         throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
167                 adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
168         }
169
170         private ViewLogicModelAdapter()
171         {
172                 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");
173         }
174 }