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.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 SimpleGateAdapter<>(GUINandGate.class, NandGate::new));
46 componentAdaptersModifiable.add(new ManualSwitchAdapter());
47 componentAdaptersModifiable.add(new BitDisplayAdapter());
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<String, Pin> supermodelPins = guiCompCasted.getSupermodelPins();
74 Map<Pin, Wire> externalWiresForSubmodel = supermodelPins.entrySet().stream()
75 .collect(Collectors.toMap(e -> guiCompCasted.getSubmodelPin(e.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;
115 else if (externalWire.length == externalWireCandidate.length)
116 Wire.fuse(externalWire, externalWireCandidate);
118 throw new IllegalArgumentException(
119 "Two pins to external wires with different logicWidths can't be connected directly");
121 return externalWire == null ? new Wire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
123 return logicWiresPerPin;
126 private static void setGUIWiresLogicModelBinding(List<GUIWire> wires, Map<Pin, Wire> logicWiresPerPin)
128 Map<Wire, ReadEnd> guiWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
129 .collect(Collectors.toMap(Function.identity(), Wire::createReadOnlyEnd));
130 for (GUIWire guiWire : wires)
131 guiWire.setLogicModelBinding(guiWireSharedReadEnd.get(logicWiresPerPin.get(guiWire.getPin1())));
134 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, List<GUIWire> wires)
136 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
138 for (Pin p : allPins)
140 HashSet<Pin> connectedPins = new HashSet<>();
141 connectedPins.add(p);
142 connectedPinsPerPin.put(p, connectedPins);
145 wires.forEach(wire ->
147 Pin pin1 = wire.getPin1();
148 Pin pin2 = wire.getPin2();
150 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
151 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
153 pin1ConnectedPins.addAll(pin2ConnectedPins);
154 pin1ConnectedPins.add(pin1);
155 pin1ConnectedPins.add(pin2);
157 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
159 return connectedPinsPerPin;
162 @SuppressWarnings("unchecked")
163 private static <G extends GUIComponent> void createAndLinkComponent(Timeline timeline, LogicModelParameters params,
164 GUIComponent guiComponent, Map<Pin, Wire> logicWiresPerPin, ComponentAdapter<G> adapter)
167 throw new IllegalArgumentException("Unknown component class: " + guiComponent.getClass());
168 adapter.createAndLinkComponent(timeline, params, (G) guiComponent, logicWiresPerPin);
171 private ViewLogicModelAdapter()
173 throw new UnsupportedOperationException("No ViewLogicModelConverter instances");