1 package net.mograsim.logic.ui.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 net.mograsim.logic.core.components.Component;
15 import net.mograsim.logic.core.components.gates.AndGate;
16 import net.mograsim.logic.core.components.gates.NotGate;
17 import net.mograsim.logic.core.components.gates.OrGate;
18 import net.mograsim.logic.core.timeline.Timeline;
19 import net.mograsim.logic.core.wires.Wire;
20 import net.mograsim.logic.core.wires.Wire.ReadEnd;
21 import net.mograsim.logic.ui.model.ViewModel;
22 import net.mograsim.logic.ui.model.components.GUIAndGate;
23 import net.mograsim.logic.ui.model.components.GUIComponent;
24 import net.mograsim.logic.ui.model.components.GUINotGate;
25 import net.mograsim.logic.ui.model.components.GUIOrGate;
26 import net.mograsim.logic.ui.model.wires.GUIWire;
27 import net.mograsim.logic.ui.model.wires.Pin;
28 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
29 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
30 import net.mograsim.logic.ui.modeladapter.componentadapters.ComponentAdapter;
31 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
32 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
34 public class ViewLogicModelAdapter
36 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
39 Set<ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashSet<>();
40 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
41 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
42 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
43 componentAdaptersModifiable.add(new ManualSwitchAdapter());
44 componentAdaptersModifiable.add(new BitDisplayAdapter());
45 // TODO list all "primitive" adapters here
46 componentAdapters = Collections.unmodifiableMap(
47 componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
50 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
52 // TODO replace Timeline with LogicModel as soon as it exists
53 Timeline timeline = new Timeline(10);
55 Map<Pin, Wire> logicWiresPerPin = convertWires(
56 viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet()),
57 viewModel.getWires(), params, timeline);
58 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
60 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
61 for (GUIComponent guiComp : viewModel.getComponents())
63 if (!(guiComp instanceof WireCrossPoint))
64 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
65 componentAdapters.get(guiComp.getClass())));
68 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
69 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
73 // TODO handle complex components
75 List<Component> logicComponents = new ArrayList<>();
76 logicComponents.addAll(oneToOneComponents.values());
81 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
83 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
84 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
85 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
86 return logicWiresPerPin;
89 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
91 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
92 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
93 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
94 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
95 set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
96 return logicWiresPerPin;
99 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
101 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
102 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
103 for (GUIWire guiWire : wires)
104 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
107 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
109 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
111 for (Pin p : allPins)
113 HashSet<Pin> connectedPins = new HashSet<>();
114 connectedPins.add(p);
115 connectedPinsPerPin.put(p, connectedPins);
118 wires.forEach(wire ->
120 Pin pin1 = wire.getPin1();
121 Pin pin2 = wire.getPin2();
123 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
124 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
126 pin1ConnectedPins.addAll(pin2ConnectedPins);
127 pin1ConnectedPins.add(pin1);
128 pin1ConnectedPins.add(pin2);
130 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
132 return connectedPinsPerPin;
135 @SuppressWarnings("unchecked")
136 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
137 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
140 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
141 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
144 private ViewLogicModelAdapter()
146 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");