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