1 package net.mograsim.logic.model.modeladapter;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
8 import java.util.Map.Entry;
10 import java.util.function.Function;
11 import java.util.stream.Collectors;
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;
25 public class ViewLogicModelAdapter
27 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters = new HashMap<>();
29 public static void addComponentAdapter(ComponentAdapter<? extends GUIComponent> componentAdapter)
31 componentAdapters.put(componentAdapter.getSupportedClass(), componentAdapter);
34 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
36 // TODO replace Timeline with LogicModel as soon as it exists
37 Timeline timeline = new Timeline(10);
39 convert(viewModel, params, timeline, Map.of());
44 private static void convert(ViewModel viewModel, LogicModelParameters params, Timeline timeline, Map<Pin, Wire> externalWires)
46 Map<Pin, Wire> logicWiresPerPin = convertWires(getAllPins(viewModel), viewModel.getWiresByName().values(), externalWires, params,
48 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
50 for (GUIComponent guiComp : viewModel.getComponentsByName().values())
52 if (guiComp instanceof SubmodelComponent)
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)
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);
68 private static Set<Pin> getAllPins(ViewModel viewModel)
70 return viewModel.getComponentsByName().values().stream().flatMap(component -> component.getPins().values().stream())
71 .collect(Collectors.toSet());
74 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, Collection<GUIWire> wires, Map<Pin, Wire> externalWires,
75 LogicModelParameters params, Timeline timeline)
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;
83 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
84 Map<Pin, Wire> externalWires)
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 ->
91 Wire externalWire = null;
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);
101 throw new IllegalArgumentException(
102 "Two pins to external wires with different logicWidths can't be connected directly");
104 return externalWire == null ? new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
106 return logicWiresPerPin;
109 private static void setGUIWiresLogicModelBinding(Collection<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
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())));
117 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, Collection<GUIWire> wires)
119 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
121 for (Pin p : allPins)
123 HashSet<Pin> connectedPins = new HashSet<>();
124 connectedPins.add(p);
125 connectedPinsPerPin.put(p, connectedPins);
128 wires.forEach(wire ->
130 Pin pin1 = wire.getPin1();
131 Pin pin2 = wire.getPin2();
133 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
134 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
136 pin1ConnectedPins.addAll(pin2ConnectedPins);
137 pin1ConnectedPins.add(pin1);
138 pin1ConnectedPins.add(pin2);
140 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
142 return connectedPinsPerPin;
145 @SuppressWarnings("unchecked")
146 private static <G extends GUIComponent> void createAndLinkComponent(Timeline timeline, LogicModelParameters params,
147 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin)
149 Class<?> cls = guiComponent.getClass();
150 ComponentAdapter<? super G> adapter = null;
151 while (cls != GUIComponent.class && adapter == null)
153 adapter = (ComponentAdapter<? super G>) componentAdapters.get(cls);
154 cls = cls.getSuperclass();
157 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
158 adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
161 private ViewLogicModelAdapter()
163 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");