1 package net.mograsim.logic.model.modeladapter;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 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.timeline.Timeline;
14 import net.mograsim.logic.core.wires.CoreWire;
15 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
16 import net.mograsim.logic.model.model.LogicModel;
17 import net.mograsim.logic.model.model.components.ModelComponent;
18 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
19 import net.mograsim.logic.model.model.components.submodels.SubmodelInterface;
20 import net.mograsim.logic.model.model.wires.ModelWire;
21 import net.mograsim.logic.model.model.wires.Pin;
22 import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
23 import net.mograsim.logic.model.modeladapter.componentadapters.ComponentAdapter;
25 public class LogicCoreAdapter
27 private final static Map<Class<? extends ModelComponent>, ComponentAdapter<? extends ModelComponent>> componentAdapters = new HashMap<>();
29 public static void addComponentAdapter(ComponentAdapter<? extends ModelComponent> componentAdapter)
31 componentAdapters.put(componentAdapter.getSupportedClass(), componentAdapter);
34 public static Timeline convert(LogicModel logicModel, CoreModelParameters params)
36 // TODO replace Timeline with CoreModel as soon as it exists
37 Timeline timeline = new Timeline(10);
39 convert(logicModel, params, timeline, Map.of());
44 private static void convert(LogicModel logicModel, CoreModelParameters params, Timeline timeline, Map<Pin, CoreWire> externalWires)
46 Map<Pin, CoreWire> logicWiresPerPin = convertWires(getAllPins(logicModel), logicModel.getWiresByName().values(), externalWires,
48 Map<Pin, CoreWire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
50 for (ModelComponent modelComp : logicModel.getComponentsByName().values())
52 if (modelComp instanceof SubmodelComponent)
54 SubmodelComponent modelCompCasted = (SubmodelComponent) modelComp;
55 Map<String, Pin> supermodelPins = modelCompCasted.getSupermodelPins();
56 Map<Pin, CoreWire> externalWiresForSubmodel = supermodelPins.entrySet().stream().collect(
57 Collectors.toMap(e -> modelCompCasted.getSubmodelPin(e.getKey()), e -> logicWiresPerPin.get(e.getValue())));
58 convert(modelCompCasted.submodel, params, timeline, externalWiresForSubmodel);
59 } else if (modelComp instanceof ModelWireCrossPoint)
61 ModelWireCrossPoint modelCompCasted = (ModelWireCrossPoint) modelComp;
62 modelCompCasted.setCoreModelBinding(logicWiresPerPin.get(modelCompCasted.getPin()).createReadOnlyEnd());
63 } else if (!(modelComp instanceof SubmodelInterface))// nothing to do for SubmodelInterfaces
64 createAndLinkComponent(timeline, params, modelComp, logicWiresPerPinUnmodifiable);
68 private static Set<Pin> getAllPins(LogicModel logicModel)
70 return logicModel.getComponentsByName().values().stream().flatMap(component -> component.getPins().values().stream())
71 .collect(Collectors.toSet());
74 private static Map<Pin, CoreWire> convertWires(Set<Pin> allPins, Collection<ModelWire> wires, Map<Pin, CoreWire> externalWires,
75 CoreModelParameters params, Timeline timeline)
77 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
78 Map<Pin, CoreWire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups, externalWires);
79 setModelWiresCoreModelBinding(wires, logicWiresPerPin);
80 return logicWiresPerPin;
83 private static Map<Pin, CoreWire> createLogicWires(CoreModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
84 Map<Pin, CoreWire> externalWires)
86 Map<Pin, CoreWire> logicWiresPerPin = new HashMap<>();
87 Map<Set<Pin>, CoreWire> logicWiresPerPinGroup = new HashMap<>();
88 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
89 logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(), set ->
91 CoreWire externalWire = null;
94 CoreWire externalWireCandidate = externalWires.get(p);
95 if (externalWireCandidate != null)
96 if (externalWire == null)
97 externalWire = externalWireCandidate;
98 else if (externalWire.width == externalWireCandidate.width)
99 CoreWire.fuse(externalWire, externalWireCandidate);
101 throw new IllegalArgumentException(
102 "Two pins to external wires with different logicWidths can't be connected directly");
104 return externalWire == null ? new CoreWire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
106 return logicWiresPerPin;
109 private static void setModelWiresCoreModelBinding(Collection<ModelWire> wires, Map<Pin, CoreWire> logicWiresPerPin)
111 Map<CoreWire, ReadEnd> modelWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
112 .collect(Collectors.toMap(Function.identity(), CoreWire::createReadOnlyEnd));
113 for (ModelWire modelWire : wires)
114 modelWire.setCoreModelBinding(modelWireSharedReadEnd.get(logicWiresPerPin.get(modelWire.getPin1())));
117 private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, Collection<ModelWire> wires)
119 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
121 for (Pin p : allPins)
123 HashSet<Pin> connectedPins = new HashSet<>();
124 connectedPins.add(p);
125 connectedPinsPerPin.put(p, connectedPins);
128 wires.forEach(wire ->
130 Pin pin1 = wire.getPin1();
131 Pin pin2 = wire.getPin2();
133 Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
134 Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
136 pin1ConnectedPins.addAll(pin2ConnectedPins);
137 pin1ConnectedPins.add(pin1);
138 pin1ConnectedPins.add(pin2);
140 pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
142 return connectedPinsPerPin;
145 @SuppressWarnings("unchecked")
146 private static <G extends ModelComponent> void createAndLinkComponent(Timeline timeline, CoreModelParameters params,
147 ModelComponent modelComponent, Map<Pin, CoreWire> logicWiresPerPin)
149 Class<?> cls = modelComponent.getClass();
150 ComponentAdapter<? super G> adapter = null;
151 while (cls != ModelComponent.class && adapter == null)
153 adapter = (ComponentAdapter<? super G>) componentAdapters.get(cls);
154 cls = cls.getSuperclass();
157 throw new IllegalArgumentException("Unknown component class: " + modelComponent.getClass());
158 adapter.createAndLinkComponent(timeline, params, (G) modelComponent, logicWiresPerPin);
161 private LogicCoreAdapter()
163 throw new UnsupportedOperationException("No LogicCoreAdapter instances");