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