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.Component;
14 import net.mograsim.logic.core.components.gates.AndGate;
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.GUINotGate;
24 import net.mograsim.logic.ui.model.components.GUIOrGate;
25 import net.mograsim.logic.ui.model.components.SubmodelComponent;
26 import net.mograsim.logic.ui.model.components.SubmodelInterface;
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 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 convert(viewModel, params, timeline, Map.of());
63 private static void convert(ViewModel viewModel, LogicModelParameters params, Timeline timeline, Map<Pin, Wire> externalWires)
65 Map<Pin, Wire> logicWiresPerPin = convertWires(getAllPins(viewModel), viewModel.getWires(), externalWires, params, timeline);
66 Map<Pin, Wire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
68 for (GUIComponent guiComp : viewModel.getComponents())
70 if (guiComp instanceof SubmodelComponent)
72 SubmodelComponent guiCompCasted = (SubmodelComponent) guiComp;
73 Map<Pin, Pin> supermodelPinsPerSubmodelPin = guiCompCasted.getSupermodelPinsPerSubmodelPin();
74 Map<Pin, Wire> externalWiresForSubmodel = supermodelPinsPerSubmodelPin.entrySet().stream()
75 .collect(Collectors.toMap(Entry::getKey, e -> logicWiresPerPin.get(e.getValue())));
76 convert(guiCompCasted.submodel, params, timeline, externalWiresForSubmodel);
77 } else if (guiComp instanceof WireCrossPoint)
79 WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
80 guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
81 } else if (!(guiComp instanceof SubmodelInterface))// nothing to do for SubmodelInterfaces
82 createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable, componentAdapters.get(guiComp.getClass()));
86 private static Set<Pin> getAllPins(ViewModel viewModel)
88 return viewModel.getComponents().stream().flatMap(component -> component.getPins().stream()).collect(Collectors.toSet());
91 private static Map<Pin, Wire> convertWires(Set<Pin> allPins, List<GUIWire> wires, Map<Pin, Wire> externalWires,
92 LogicModelParameters params, Timeline timeline)
94 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
95 Map<Pin, Wire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups, externalWires);
96 setGUIWiresLogicModelBinding(wires, logicWiresPerPin);
97 return logicWiresPerPin;
100 private static Map<Pin, Wire> createLogicWires(LogicModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
101 Map<Pin, Wire> externalWires)
103 Map<Pin, Wire> logicWiresPerPin = new HashMap<>();
104 Map<Set<Pin>, Wire> logicWiresPerPinGroup = new HashMap<>();
105 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
106 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(), set ->
108 Wire externalWire = null;
111 Wire externalWireCandidate = externalWires.get(p);
112 if (externalWireCandidate != null)
113 if (externalWire == null)
114 externalWire = externalWireCandidate;
116 throw new IllegalArgumentException("Two pins to external wires can't be connected directly");
118 return new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime);
120 return logicWiresPerPin;
123 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
125 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
126 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
127 for (GUIWire guiWire : wires)
128 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
131 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
133 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
135 for (Pin p : allPins)
137 HashSet<Pin> connectedPins = new HashSet<>();
138 connectedPins.add(p);
139 connectedPinsPerPin.put(p, connectedPins);
142 wires.forEach(wire ->
144 Pin pin1 = wire.getPin1();
145 Pin pin2 = wire.getPin2();
147 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
148 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
150 pin1ConnectedPins.addAll(pin2ConnectedPins);
151 pin1ConnectedPins.add(pin1);
152 pin1ConnectedPins.add(pin2);
154 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
156 return connectedPinsPerPin;
159 @SuppressWarnings("unchecked")
160 private static <G extends GUIComponent> Component createAndLinkComponent(Timeline timeline, LogicModelParameters params,
161 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
164 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
165 return adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
168 private ViewLogicModelAdapter()
170 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");