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 if (!(guiComp instanceof WireCrossPoint))
62 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
63 componentAdapters.get(guiComp.getClass())));
66 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
67 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
71 // TODO handle complex components
73 List<Component> logicComponents = new ArrayList<>();
74 logicComponents.addAll(oneToOneComponents.values());
79 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
81 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
82 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
83 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
84 return logicWiresPerPin;
87 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
89 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
90 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
91 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
92 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
93 set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
94 return logicWiresPerPin;
97 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
99 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
100 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
101 for (GUIWire guiWire : wires)
102 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
105 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
107 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
109 for (Pin p : allPins)
111 HashSet<Pin> connectedPins = new HashSet<>();
112 connectedPins.add(p);
113 connectedPinsPerPin.put(p, connectedPins);
116 wires.forEach(wire ->
118 Pin pin1 = wire.getPin1();
119 Pin pin2 = wire.getPin2();
121 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
122 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
124 pin1ConnectedPins.addAll(pin2ConnectedPins);
125 pin1ConnectedPins.add(pin1);
126 pin1ConnectedPins.add(pin2);
128 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
130 return connectedPinsPerPin;
133 @SuppressWarnings("unchecked")
134 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
135 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
138 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
139 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
142 private ViewLogicModelAdapter()
144 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");