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;
10 import java.util.Objects;
12 import java.util.function.Function;
13 import java.util.stream.Collectors;
15 import net.mograsim.logic.core.components.Component;
16 import net.mograsim.logic.core.components.gates.AndGate;
17 import net.mograsim.logic.core.components.gates.NotGate;
18 import net.mograsim.logic.core.components.gates.OrGate;
19 import net.mograsim.logic.core.timeline.Timeline;
20 import net.mograsim.logic.core.wires.Wire;
21 import net.mograsim.logic.core.wires.Wire.ReadEnd;
22 import net.mograsim.logic.ui.model.ViewModel;
23 import net.mograsim.logic.ui.model.components.GUIAndGate;
24 import net.mograsim.logic.ui.model.components.GUIComponent;
25 import net.mograsim.logic.ui.model.components.GUINotGate;
26 import net.mograsim.logic.ui.model.components.GUIOrGate;
27 import net.mograsim.logic.ui.model.wires.GUIWire;
28 import net.mograsim.logic.ui.model.wires.Pin;
29 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
30 import net.mograsim.logic.ui.modeladapter.componentadapters.Am2901NANDBasedAdapter;
31 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
32 import net.mograsim.logic.ui.modeladapter.componentadapters.ComponentAdapter;
33 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
34 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
36 public class ViewLogicModelAdapter
38 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
41 Set<ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashSet<>();
42 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
43 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
44 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
45 componentAdaptersModifiable.add(new ManualSwitchAdapter());
46 componentAdaptersModifiable.add(new BitDisplayAdapter());
47 componentAdaptersModifiable.add(new Am2901NANDBasedAdapter());
48 // TODO list all "primitive" adapters here
49 componentAdapters = Collections.unmodifiableMap(
50 componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
53 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
55 // TODO replace Timeline with LogicModel as soon as it exists
56 Timeline timeline = new Timeline(10);
58 Map<Pin, Wire> logicWiresPerPin = convertWires(
59 viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet()),
60 viewModel.getWires(), params, timeline);
61 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
63 Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
64 for (GUIComponent guiComp : viewModel.getComponents())
66 if (!(guiComp instanceof WireCrossPoint))
67 oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
68 componentAdapters.get(guiComp.getClass())));
71 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
72 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
76 // TODO handle complex components
78 List<Component> logicComponents = new ArrayList<>();
79 // null means "no one to one mapping"
80 oneToOneComponents.values().stream().filter(Objects::nonNull).forEach(logicComponents::add);
85 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, LogicModelParameters params, Timeline timeline)
87 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
88 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups);
89 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
90 return logicWiresPerPin;
93 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups)
95 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
96 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
97 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
98 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(),
99 set -> new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime)));
100 return logicWiresPerPin;
103 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
105 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
106 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
107 for (GUIWire guiWire : wires)
108 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
111 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
113 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
115 for (Pin p : allPins)
117 HashSet<Pin> connectedPins = new HashSet<>();
118 connectedPins.add(p);
119 connectedPinsPerPin.put(p, connectedPins);
122 wires.forEach(wire ->
124 Pin pin1 = wire.getPin1();
125 Pin pin2 = wire.getPin2();
127 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
128 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
130 pin1ConnectedPins.addAll(pin2ConnectedPins);
131 pin1ConnectedPins.add(pin1);
132 pin1ConnectedPins.add(pin2);
134 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
136 return connectedPinsPerPin;
139 @SuppressWarnings("unchecked")
140 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
141 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
144 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
145 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
148 private ViewLogicModelAdapter()
150 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");