3f05964eb140d38ee8c09ada39b3c1393b24761c
[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.Objects;
11 import java.util.Set;
12 import java.util.function.Function;
13 import java.util.stream.Collectors;
14
15 import net.mograsim.logic.core.components.Component;
16 import net.mograsim.logic.core.components.gates.AndGate;
17 import net.mograsim.logic.core.components.gates.NotGate;
18 import net.mograsim.logic.core.components.gates.OrGate;
19 import net.mograsim.logic.core.timeline.Timeline;
20 import net.mograsim.logic.core.wires.Wire;
21 import net.mograsim.logic.core.wires.Wire.ReadEnd;
22 import net.mograsim.logic.ui.model.ViewModel;
23 import net.mograsim.logic.ui.model.components.GUIAndGate;
24 import net.mograsim.logic.ui.model.components.GUIComponent;
25 import net.mograsim.logic.ui.model.components.GUINotGate;
26 import net.mograsim.logic.ui.model.components.GUIOrGate;
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 "primitive" 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                 Map<Pin, Wire> logicWiresPerPin = convertWires(
59                                 viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet()),
60                                 viewModel.getWires(), params, timeline);
61                 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
62
63                 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
64                 for (GUIComponent guiComp : viewModel.getComponents())
65                 {
66                         if (!(guiComp instanceof WireCrossPoint))
67                                 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
68                                                 componentAdapters.get(guiComp.getClass())));
69                         else
70                         {
71                                 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
72                                 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
73                         }
74                 }
75
76                 // TODO handle complex components
77
78                 List<Component> logicComponents = new ArrayList<>();
79                 // null means "no one to one mapping"
80                 oneToOneComponents.values().stream().filter(Objects::nonNull).forEach(logicComponents::add);
81
82                 return timeline;
83         }
84
85         private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
86         {
87                 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
88                 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
89                 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
90                 return logicWiresPerPin;
91         }
92
93         private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
94         {
95                 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
96                 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
97                 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
98                         logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
99                                         set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
100                 return logicWiresPerPin;
101         }
102
103         private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
104         {
105                 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
106                                 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
107                 for (GUIWire guiWire : wires)
108                         guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
109         }
110
111         private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
112         {
113                 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
114
115                 for (Pin p : allPins)
116                 {
117                         HashSet<Pin> connectedPins = new HashSet<>();
118                         connectedPins.add(p);
119                         connectedPinsPerPin.put(p, connectedPins);
120                 }
121
122                 wires.forEach(wire ->
123                 {
124                         Pin pin1 = wire.getPin1();
125                         Pin pin2 = wire.getPin2();
126
127                         Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
128                         Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
129
130                         pin1ConnectedPins.addAll(pin2ConnectedPins);
131                         pin1ConnectedPins.add(pin1);
132                         pin1ConnectedPins.add(pin2);
133
134                         pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
135                 });
136                 return connectedPinsPerPin;
137         }
138
139         @SuppressWarnings("unchecked")
140         private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
141                         GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
142         {
143                 if (adapter == null)
144                         throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
145                 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
146         }
147
148         private ViewLogicModelAdapter()
149         {
150                 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");
151         }
152 }