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.NandGate;
15 import net.mograsim.logic.core.components.gates.NotGate;
16 import net.mograsim.logic.core.components.gates.OrGate;
17 import net.mograsim.logic.core.timeline.Timeline;
18 import net.mograsim.logic.core.wires.Wire;
19 import net.mograsim.logic.core.wires.Wire.ReadEnd;
20 import net.mograsim.logic.ui.model.ViewModel;
21 import net.mograsim.logic.ui.model.components.GUIAndGate;
22 import net.mograsim.logic.ui.model.components.GUIComponent;
23 import net.mograsim.logic.ui.model.components.GUINandGate;
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.components.SubmodelComponent;
27 import net.mograsim.logic.ui.model.components.SubmodelInterface;
28 import net.mograsim.logic.ui.model.wires.GUIWire;
29 import net.mograsim.logic.ui.model.wires.Pin;
30 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
31 import net.mograsim.logic.ui.modeladapter.componentadapters.AtomicAm2901NANDBasedAdapter;
32 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
33 import net.mograsim.logic.ui.modeladapter.componentadapters.ComponentAdapter;
34 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
35 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
37 public class ViewLogicModelAdapter
39 private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
42 Set<ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashSet<>();
43 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
44 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
45 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
46 componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINandGate.class, NandGate::new));
47 componentAdaptersModifiable.add(new ManualSwitchAdapter());
48 componentAdaptersModifiable.add(new BitDisplayAdapter());
49 componentAdaptersModifiable.add(new AtomicAm2901NANDBasedAdapter());
50 // TODO list all adapters here
51 componentAdapters = Collections.unmodifiableMap(
52 componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
55 public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
57 // TODO replace Timeline with LogicModel as soon as it exists
58 Timeline timeline = new Timeline(10);
60 convert(viewModel, params, timeline, Map.of());
65 private static void convert(ViewModel viewModel, LogicModelParameters params, Timeline timeline, Map<Pin, Wire> externalWires)
67 Map<Pin, Wire> logicWiresPerPin = convertWires(getAllPins(viewModel), viewModel.getWires(), externalWires, params, timeline);
68 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
70 for (GUIComponent guiComp : viewModel.getComponents())
72 if (guiComp instanceof SubmodelComponent)
74 SubmodelComponent guiCompCasted = (SubmodelComponent) guiComp;
75 Map<Pin, Pin> supermodelPinsPerSubmodelPin = guiCompCasted.getSupermodelPinsPerSubmodelPin();
76 Map<Pin, Wire> externalWiresForSubmodel = supermodelPinsPerSubmodelPin.entrySet().stream()
77 .collect(Collectors.toMap(Entry::getKey, e -> logicWiresPerPin.get(e.getValue())));
78 convert(guiCompCasted.submodel, params, timeline, externalWiresForSubmodel);
79 } else if (guiComp instanceof WireCrossPoint)
81 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
82 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
83 } else if (!(guiComp instanceof SubmodelInterface))// nothing to do for SubmodelInterfaces
84 createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable, componentAdapters.get(guiComp.getClass()));
88 private static Set<Pin> getAllPins(ViewModel viewModel)
90 return viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet());
93 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, Map<Pin, Wire> externalWires,
94 LogicModelParameters params, Timeline timeline)
96 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
97 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups, externalWires);
98 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
99 return logicWiresPerPin;
102 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
103 Map<Pin, Wire> externalWires)
105 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
106 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
107 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
108 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(), set ->
110 Wire externalWire = null;
113 Wire externalWireCandidate = externalWires.get(p);
114 if (externalWireCandidate != null)
115 if (externalWire == null)
116 externalWire = externalWireCandidate;
118 throw new IllegalArgumentException("Two pins to external wires can't be connected directly");
120 return externalWire == null ? new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
122 return logicWiresPerPin;
125 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
127 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
128 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
129 for (GUIWire guiWire : wires)
130 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
133 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
135 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
137 for (Pin p : allPins)
139 HashSet<Pin> connectedPins = new HashSet<>();
140 connectedPins.add(p);
141 connectedPinsPerPin.put(p, connectedPins);
144 wires.forEach(wire ->
146 Pin pin1 = wire.getPin1();
147 Pin pin2 = wire.getPin2();
149 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
150 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
152 pin1ConnectedPins.addAll(pin2ConnectedPins);
153 pin1ConnectedPins.add(pin1);
154 pin1ConnectedPins.add(pin2);
156 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
158 return connectedPinsPerPin;
161 @SuppressWarnings("unchecked")
162 private static <G extends GUIComponent> void createAndLinkComponent(Timeline timeline, LogicModelParameters params,
163 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
166 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
167 adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
170 private ViewLogicModelAdapter()
172 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");