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.ManualSwitchAdapter;
24 import era.mi.gui.modeladapter.componentadapters.SimpleGateAdapter;
25 import era.mi.logic.components.Component;
26 import era.mi.logic.components.gates.AndGate;
27 import era.mi.logic.components.gates.NotGate;
28 import era.mi.logic.components.gates.OrGate;
29 import era.mi.logic.timeline.Timeline;
30 import era.mi.logic.wires.Wire;
31 import era.mi.logic.wires.Wire.ReadEnd;
33 public class ViewLogicModelAdapter
35 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
38 Set<ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashSet<>();
39 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
40 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
41 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
42 componentAdaptersModifiable.add(new ManualSwitchAdapter());
43 // TODO list all "primitive" adapters here
44 componentAdapters = Collections.unmodifiableMap(
45 componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
48 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
50 // TODO replace Timeline with LogicModel as soon as it exists
51 Timeline timeline = new Timeline(10);
53 Map<Pin, Wire> logicWiresPerPin = convertWires(
54 viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet()),
55 viewModel.getWires(), params, timeline);
56 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
58 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
59 for (GUIComponent guiComp : viewModel.getComponents())
61 // WireCrossPoints just vanish
62 if (!(guiComp instanceof WireCrossPoint))
63 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
64 componentAdapters.get(guiComp.getClass())));
67 // TODO handle complex components
69 List<Component> logicComponents = new ArrayList<>();
70 logicComponents.addAll(oneToOneComponents.values());
75 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
77 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
78 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
79 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
80 return logicWiresPerPin;
83 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
85 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
86 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
87 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
88 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
89 set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
90 return logicWiresPerPin;
93 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
95 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
96 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
97 for (GUIWire guiWire : wires)
98 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
101 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
103 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
105 for (Pin p : allPins)
107 HashSet<Pin> connectedPins = new HashSet<>();
108 connectedPins.add(p);
109 connectedPinsPerPin.put(p, connectedPins);
112 wires.forEach(wire ->
114 Pin pin1 = wire.getPin1();
115 Pin pin2 = wire.getPin2();
117 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
118 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
120 pin1ConnectedPins.addAll(pin2ConnectedPins);
121 pin1ConnectedPins.add(pin1);
122 pin1ConnectedPins.add(pin2);
124 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
126 return connectedPinsPerPin;
129 @SuppressWarnings("unchecked")
130 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
131 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
134 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
135 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
138 private ViewLogicModelAdapter()
140 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");