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