1 package net.mograsim.logic.ui.modeladapter;
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.HashSet;
8 import java.util.Map.Entry;
10 import java.util.function.Function;
11 import java.util.stream.Collectors;
13 import net.mograsim.logic.core.components.gates.AndGate;
14 import net.mograsim.logic.core.components.gates.NotGate;
15 import net.mograsim.logic.core.components.gates.OrGate;
16 import net.mograsim.logic.core.timeline.Timeline;
17 import net.mograsim.logic.core.wires.Wire;
18 import net.mograsim.logic.core.wires.Wire.ReadEnd;
19 import net.mograsim.logic.ui.model.ViewModel;
20 import net.mograsim.logic.ui.model.components.GUIAndGate;
21 import net.mograsim.logic.ui.model.components.GUIComponent;
22 import net.mograsim.logic.ui.model.components.GUINotGate;
23 import net.mograsim.logic.ui.model.components.GUIOrGate;
24 import net.mograsim.logic.ui.model.components.SubmodelComponent;
25 import net.mograsim.logic.ui.model.components.SubmodelInterface;
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.AtomicAm2901NANDBasedAdapter;
30 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
31 import net.mograsim.logic.ui.modeladapter.componentadapters.ComponentAdapter;
32 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
33 import net.mograsim.logic.ui.modeladapter.componentadapters.NandGateAdapter;
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 NandGateAdapter());
46 componentAdaptersModifiable.add(new ManualSwitchAdapter());
47 componentAdaptersModifiable.add(new BitDisplayAdapter());
48 componentAdaptersModifiable.add(new AtomicAm2901NANDBasedAdapter());
49 // TODO list all adapters here
50 componentAdapters = Collections.unmodifiableMap(
51 componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
54 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
56 // TODO replace Timeline with LogicModel as soon as it exists
57 Timeline timeline = new Timeline(10);
59 convert(viewModel, params, timeline, Map.of());
64 private static void convert(ViewModel viewModel, LogicModelParameters params, Timeline timeline, Map<Pin, Wire> externalWires)
66 Map<Pin, Wire> logicWiresPerPin = convertWires(getAllPins(viewModel), viewModel.getWires(), externalWires, params, timeline);
67 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
69 for (GUIComponent guiComp : viewModel.getComponents())
71 if (guiComp instanceof SubmodelComponent)
73 SubmodelComponent guiCompCasted = (SubmodelComponent) guiComp;
74 Map<Pin, Pin> supermodelPinsPerSubmodelPin = guiCompCasted.getSupermodelPinsPerSubmodelPin();
75 Map<Pin, Wire> externalWiresForSubmodel = supermodelPinsPerSubmodelPin.entrySet().stream()
76 .collect(Collectors.toMap(Entry::getKey, e -> logicWiresPerPin.get(e.getValue())));
77 convert(guiCompCasted.submodel, params, timeline, externalWiresForSubmodel);
78 } else if (guiComp instanceof WireCrossPoint)
80 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
81 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
82 } else if (!(guiComp instanceof SubmodelInterface))// nothing to do for SubmodelInterfaces
83 createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable, componentAdapters.get(guiComp.getClass()));
87 private static Set<Pin> getAllPins(ViewModel viewModel)
89 return viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet());
92 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, Map<Pin, Wire> externalWires,
93 LogicModelParameters params, Timeline timeline)
95 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
96 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups, externalWires);
97 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
98 return logicWiresPerPin;
101 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
102 Map<Pin, Wire> externalWires)
104 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
105 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
106 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
107 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(), set ->
109 Wire externalWire = null;
112 Wire externalWireCandidate = externalWires.get(p);
113 if (externalWireCandidate != null)
114 if (externalWire == null)
115 externalWire = externalWireCandidate;
117 throw new IllegalArgumentException("Two pins to external wires can't be connected directly");
119 return externalWire == null ? new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
121 return logicWiresPerPin;
124 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
126 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
127 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
128 for (GUIWire guiWire : wires)
129 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
132 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
134 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
136 for (Pin p : allPins)
138 HashSet<Pin> connectedPins = new HashSet<>();
139 connectedPins.add(p);
140 connectedPinsPerPin.put(p, connectedPins);
143 wires.forEach(wire ->
145 Pin pin1 = wire.getPin1();
146 Pin pin2 = wire.getPin2();
148 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
149 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
151 pin1ConnectedPins.addAll(pin2ConnectedPins);
152 pin1ConnectedPins.add(pin1);
153 pin1ConnectedPins.add(pin2);
155 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
157 return connectedPinsPerPin;
160 @SuppressWarnings("unchecked")
161 private static <G extends GUIComponent> void createAndLinkComponent(Timeline timeline, LogicModelParameters params,
162 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
165 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
166 adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
169 private ViewLogicModelAdapter()
171 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");