1 package era.mi.gui.modeladapter;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
9 import java.util.Map.Entry;
11 import java.util.function.Function;
12 import java.util.stream.Collectors;
14 import era.mi.gui.model.ViewModel;
15 import era.mi.gui.model.components.GUIAndGate;
16 import era.mi.gui.model.components.GUIComponent;
17 import era.mi.gui.model.components.GUINotGate;
18 import era.mi.gui.model.components.GUIOrGate;
19 import era.mi.gui.model.wires.GUIWire;
20 import era.mi.gui.model.wires.Pin;
21 import era.mi.gui.model.wires.WireCrossPoint;
22 import era.mi.gui.modeladapter.componentadapters.ComponentAdapter;
23 import era.mi.gui.modeladapter.componentadapters.SimpleGateAdapter;
24 import era.mi.logic.components.Component;
25 import era.mi.logic.components.gates.AndGate;
26 import era.mi.logic.components.gates.NotGate;
27 import era.mi.logic.components.gates.OrGate;
28 import era.mi.logic.timeline.Timeline;
29 import era.mi.logic.wires.Wire;
30 import era.mi.logic.wires.Wire.ReadEnd;
32 public class ViewLogicModelAdapter
34 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
37 Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashMap<>();
38 componentAdaptersModifiable.put(GUIOrGate.class, new SimpleGateAdapter(OrGate::new));
39 componentAdaptersModifiable.put(GUIAndGate.class, new SimpleGateAdapter(AndGate::new));
40 componentAdaptersModifiable.put(GUINotGate.class, new SimpleGateAdapter((t, p, o, i) -> new NotGate(t, p, i[0], o)));
41 // TODO list all "primitive" adapters here
42 componentAdapters = Collections.unmodifiableMap(componentAdaptersModifiable);
45 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
47 // TODO replace Timeline with LogicModel as soon as it exists
48 Timeline timeline = new Timeline(10);
50 Map<Pin, Wire> logicWiresPerPin = convertWires(
51 viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet()),
52 viewModel.getWires(), params, timeline);
53 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
55 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
56 for (GUIComponent guiComp : viewModel.getComponents())
58 // WireCrossPoints just vanish
59 if (!(guiComp instanceof WireCrossPoint))
60 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
61 componentAdapters.get(guiComp.getClass())));
64 // TODO handle complex components
66 List<Component> logicComponents = new ArrayList<>();
67 logicComponents.addAll(oneToOneComponents.values());
72 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
74 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
75 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
76 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
77 return logicWiresPerPin;
80 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
82 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
83 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
84 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
85 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
86 set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
87 return logicWiresPerPin;
90 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
92 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
93 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
94 for (GUIWire guiWire : wires)
95 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
98 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
100 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
102 for (Pin p : allPins)
104 HashSet<Pin> connectedPins = new HashSet<>();
105 connectedPins.add(p);
106 connectedPinsPerPin.put(p, connectedPins);
109 wires.forEach(wire ->
111 Pin pin1 = wire.getPin1();
112 Pin pin2 = wire.getPin2();
114 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
115 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
117 pin1ConnectedPins.addAll(pin2ConnectedPins);
118 pin1ConnectedPins.add(pin1);
119 pin1ConnectedPins.add(pin2);
121 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
123 return connectedPinsPerPin;
126 @SuppressWarnings("unchecked")
127 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
128 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
131 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
132 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
135 private ViewLogicModelAdapter()
137 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");