package net.mograsim.logic.model.examples;
+import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Scanner;
import java.util.Set;
-import java.util.function.Function;
+import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.mograsim.logic.model.am2900.Am2900Loader;
import net.mograsim.logic.model.model.LogicModelModifiable;
import net.mograsim.logic.model.model.components.ModelComponent;
+import net.mograsim.logic.model.model.components.atomic.ModelTextComponent;
import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
-import net.mograsim.logic.model.model.components.submodels.SubmodelInterface;
import net.mograsim.logic.model.model.wires.ModelWire;
import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
import net.mograsim.logic.model.model.wires.MovablePin;
import net.mograsim.logic.model.model.wires.PinUsage;
import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
import net.mograsim.logic.model.serializing.IdentifyParams;
-import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
+import net.mograsim.logic.model.serializing.LogicModelParams.ComponentParams;
+import net.mograsim.logic.model.serializing.LogicModelParams.WireParams;
+import net.mograsim.logic.model.serializing.SubmodelComponentParams;
import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
-import net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler.StandardHighLevelStateHandlerParams;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.AtomicHighLevelStateHandler.AtomicHighLevelStateHandlerParams;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler.DelegatingAtomicHighLevelStateHandlerParams;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.WireForcingAtomicHighLevelStateHandler.WireForcingAtomicHighLevelStateHandlerParams;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.DelegatingSubcomponentHighLevelStateHandler.DelegatingSubcomponentHighLevelStateHandlerParams;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.SubcomponentHighLevelStateHandler.SubcomponentHighLevelStateHandlerParams;
+import net.mograsim.logic.model.util.JsonHandler;
public class ReserializeAndVerifyJSONs
{
public static double GRIDSIZE = 2.5;
public static boolean changePinUsages = false;
- public static boolean changeComponentNames = false;
+ public static boolean changeComponentNames = true;
+ public static boolean forceDefaultComponentNames = true;
+ public static boolean changeWireNames = true;
+ public static boolean forceDefaultWireNames = true;
public static boolean snapWCPs = true;
public static boolean warnNonSnappedPoints = true;
public static boolean warnNonVertHorizLines = true;
{
try
{
- DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) IndirectModelComponentCreator
- .createComponent(new LogicModelModifiable(), "jsonfile:" + componentPath.toString());
+ SubmodelComponentParams oldComponentJSON = JsonHandler.readJson(componentPath.toString(), SubmodelComponentParams.class);
+ DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) SubmodelComponentSerializer
+ .deserialize(new LogicModelModifiable(), oldComponentJSON);
System.out.println("Reserializing " + componentPath);
LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
Map<String, String> componentNameRemapping = new HashMap<>();
+ Map<String, String> wireNameRemapping = new HashMap<>();
if (changePinUsages)
changePinUsages(sysin, comp);
if (changeComponentNames)
changeComponentNames(sysin, submodelModifiable, componentNameRemapping);
+ if (changeWireNames)
+ changeWireNames(sysin, submodelModifiable, wireNameRemapping);
if (snapWCPs)
snapWCPs(submodelModifiable);
if (warnNonSnappedPoints)
if (warnRedundantWires)
warnRedundantWires(submodelModifiable);
- SubmodelComponentSerializer.serialize(comp, componentPath.toString());
+ SubmodelComponentParams newComponentJSON = SubmodelComponentSerializer.serialize(comp);
if (changeComponentNames)
- changeComponentNames_AfterSerialization(sysin, comp, componentNameRemapping);
+ changeComponentNames_AfterSerialization(newComponentJSON, componentNameRemapping);
+ if (changeWireNames)
+ changeWireNames_AfterSerialization(newComponentJSON, wireNameRemapping);
+ sortAllJSONArrays(newComponentJSON);
+
+ try (FileWriter writer = new FileWriter(componentPath.toString()))
+ {
+ String json = JsonHandler.toJson(newComponentJSON);
+ json = json.replace("\u00b5", "\\u00b5");
+ writer.write(json);
+ }
}
catch (Exception e)
{
private static void changePinUsages(Scanner sysin, DeserializedSubmodelComponent comp)
{
- comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
+ comp.getSubmodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
{
+ PinUsage oldUsage = comp.getSupermodelPin(pin.name).usage;
PinUsage usage = null;
- while (usage == null)
+ do
try
{
- System.out.print(" Usage for interface pin " + pin.name + " (empty: " + pin.usage + ") >");
+ System.out.print(" Usage for interface pin " + pin.name + " (empty: " + oldUsage + ") >");
String usageStr = sysin.nextLine().toUpperCase();
- usage = usageStr.equals("") ? pin.usage
- : usageStr.equals("I") ? PinUsage.INPUT
- : usageStr.equals("O") ? PinUsage.OUTPUT
- : usageStr.equals("T") ? PinUsage.TRISTATE : PinUsage.valueOf(usageStr);
+ // TODO replace with switch expression if we upgrade to Java 12
+ switch (usageStr)
+ {
+ case "":
+ usage = oldUsage;
+ break;
+ case "I":
+ usage = PinUsage.INPUT;
+ break;
+ case "O":
+ usage = PinUsage.OUTPUT;
+ break;
+ case "T":
+ usage = PinUsage.TRISTATE;
+ break;
+ default:
+ usage = PinUsage.valueOf(usageStr);
+ break;
+ }
}
catch (@SuppressWarnings("unused") IllegalArgumentException e)
{
System.err.println(" Illegal usage");
}
+ while (usage == null);
setInterfacePinUsage(comp, pin, usage);
});
}
+ @SuppressWarnings("unused") // TextComponent
private static void changeComponentNames(Scanner sysin, LogicModelModifiable submodelModifiable,
Map<String, String> componentNameRemapping)
{
String oldName = e.getKey();
ModelComponent subcomp = e.getValue();
String defaultName = tempModel.getDefaultComponentName(subcomp);
- String newName = null;
+ String newName = forceDefaultComponentNames ? defaultName : null;
while (newName == null)
{
System.out.print(" New name for component " + oldName + " of type " + subcomp.getIDForSerializing(iP) + " (empty: "
}
}
componentNameRemapping.put(oldName, newName);
- IndirectModelComponentCreator
- .createComponent(tempModel, subcomp.getIDForSerializing(iP), subcomp.getParamsForSerializingJSON(iP), newName)
- .moveTo(subcomp.getPosX(), subcomp.getPosY());
+ new ModelTextComponent(tempModel, "", newName);
});
- SubmodelInterface tempSubmodelInterface = new SubmodelInterface(tempModel);
- for (Pin p : submodelModifiable.getComponentsByName().get(SubmodelComponent.SUBMODEL_INTERFACE_NAME).getPins().values())
- tempSubmodelInterface
- .addPin(new Pin(tempModel, tempSubmodelInterface, p.name, p.logicWidth, p.usage, p.getRelX(), p.getRelY()));
- for (ModelWire w : submodelModifiable.getWiresByName().values())
- createWire(componentNameRemapping::get, tempModel, w);
+ }
- Optional<ModelComponent> o;
- while ((o = submodelModifiable.getComponentsByName().values().stream()
- .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent())
- submodelModifiable.destroyComponent(o.get());
+ private static void changeComponentNames_AfterSerialization(SubmodelComponentParams newComponentJSON,
+ Map<String, String> componentNameRemapping)
+ {
+ for (ComponentParams cParams : newComponentJSON.submodel.components)
+ cParams.name = componentNameRemapping.get(cParams.name);
+ for (WireParams wParams : newComponentJSON.submodel.wires)
+ {
+ wParams.pin1.compName = componentNameRemapping.get(wParams.pin1.compName);
+ wParams.pin2.compName = componentNameRemapping.get(wParams.pin2.compName);
+ }
+ if ("standard".equals(newComponentJSON.highLevelStateHandlerSnippetID))
+ {
+ StandardHighLevelStateHandlerParams hlshParams = JsonHandler.fromJsonTree(newComponentJSON.highLevelStateHandlerParams,
+ StandardHighLevelStateHandlerParams.class);
+ for (AtomicHighLevelStateHandlerParams ahlshParams : hlshParams.atomicHighLevelStates.values())
+ if ("delegating".equals(ahlshParams.id))
+ {
+ DelegatingAtomicHighLevelStateHandlerParams dhlshParams = JsonHandler.fromJsonTree(ahlshParams.params,
+ DelegatingAtomicHighLevelStateHandlerParams.class);
+ dhlshParams.delegateTarget = componentNameRemapping.get(dhlshParams.delegateTarget);
+ ahlshParams.params = JsonHandler.toJsonTree(dhlshParams);
+ }
+ for (SubcomponentHighLevelStateHandlerParams shlshParams : hlshParams.subcomponentHighLevelStates.values())
+ if ("delegating".equals(shlshParams.id))
+ {
+ DelegatingSubcomponentHighLevelStateHandlerParams dhlshParams = JsonHandler.fromJsonTree(shlshParams.params,
+ DelegatingSubcomponentHighLevelStateHandlerParams.class);
+ dhlshParams.delegateTarget = componentNameRemapping.get(dhlshParams.delegateTarget);
+ shlshParams.params = JsonHandler.toJsonTree(dhlshParams);
+ }
+ newComponentJSON.highLevelStateHandlerParams = JsonHandler.toJsonTree(hlshParams);
+ }
+ }
- tempModel.getComponentsByName().values().stream().filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
- .forEach(c -> IndirectModelComponentCreator
- .createComponent(submodelModifiable, c.getIDForSerializing(iP), c.getParamsForSerializingJSON(iP), c.getName())
- .moveTo(c.getPosX(), c.getPosY()));
- for (ModelWire w : tempModel.getWiresByName().values())
- createWire(Function.identity(), submodelModifiable, w);
+ @SuppressWarnings("unused") // Wire
+ private static void changeWireNames(Scanner sysin, LogicModelModifiable submodelModifiable, Map<String, String> wireNameRemapping)
+ {
+ LogicModelModifiable tempModel = new LogicModelModifiable();
+ Pin p = new ModelWireCrossPoint(tempModel, 1).getPin();
+ IdentifyParams iP = new IdentifyParams();
+ submodelModifiable.getWiresByName().entrySet().stream()
+ .sorted(Comparator.comparing(Entry::getKey, ReserializeAndVerifyJSONs::compareStringsWithIntegers)).forEach(e ->
+ {
+ String oldName = e.getKey();
+ String defaultName = tempModel.getDefaultWireName();
+ String newName = forceDefaultWireNames ? defaultName : null;
+ while (newName == null)
+ {
+ System.out.print(" New name for wire " + oldName + " (empty: " + defaultName + ") >");
+ newName = sysin.nextLine();
+ if (newName.equals(""))
+ newName = defaultName;
+ if (tempModel.getComponentsByName().containsKey(newName))
+ {
+ System.err.println(" There already is a component with that name");
+ newName = null;
+ }
+ }
+ wireNameRemapping.put(oldName, newName);
+ new ModelWire(tempModel, newName, p, p);
+ });
}
- private static void changeComponentNames_AfterSerialization(Scanner sysin, DeserializedSubmodelComponent comp,
- Map<String, String> componentNameRemapping)
+ private static void changeWireNames_AfterSerialization(SubmodelComponentParams newComponentJSON, Map<String, String> wireNameRemapping)
{
- if (comp.getHighLevelStateHandler() == null || !(comp.getHighLevelStateHandler() instanceof DefaultHighLevelStateHandler))
+ for (WireParams wParams : newComponentJSON.submodel.wires)
+ wParams.name = wireNameRemapping.get(wParams.name);
+ if ("standard".equals(newComponentJSON.highLevelStateHandlerSnippetID))
{
- System.out.println(" A non-default HighLevelStateHandler was detected. Check for changes there manually.");
- System.out.print(" Empty line to continue to next component, old component name to get new component name >");
- for (String line = sysin.nextLine(); !line.equals(""); line = sysin.nextLine())
- System.out.println(" " + line + "->" + componentNameRemapping.get(line) + " >");
+ StandardHighLevelStateHandlerParams hlshParams = JsonHandler.fromJsonTree(newComponentJSON.highLevelStateHandlerParams,
+ StandardHighLevelStateHandlerParams.class);
+ for (AtomicHighLevelStateHandlerParams ahlshParams : hlshParams.atomicHighLevelStates.values())
+ if ("wireForcing".equals(ahlshParams.id))
+ {
+ WireForcingAtomicHighLevelStateHandlerParams whlshParams = JsonHandler.fromJsonTree(ahlshParams.params,
+ WireForcingAtomicHighLevelStateHandlerParams.class);
+ whlshParams.wiresToForce = whlshParams.wiresToForce.stream().map(wireNameRemapping::get).collect(Collectors.toList());
+ whlshParams.wiresToForceInverted = whlshParams.wiresToForceInverted.stream().map(wireNameRemapping::get)
+ .collect(Collectors.toList());
+ ahlshParams.params = JsonHandler.toJsonTree(whlshParams);
+ }
+ newComponentJSON.highLevelStateHandlerParams = JsonHandler.toJsonTree(hlshParams);
}
}
});
}
- private static ModelWire createWire(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
- ModelWire w)
+ private static void sortAllJSONArrays(SubmodelComponentParams newComponentJSON)
{
- return new ModelWire(tempModelForDefaultNames, w.name,
- getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin1()),
- getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin2()), w.getPath());
- }
-
- private static Pin getRemappedPin(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
- Pin pin)
- {
- return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.getName())).getPin(pin.name);
+ Comparator<String> c = ReserializeAndVerifyJSONs::compareStringsWithIntegers;
+ Arrays.sort(newComponentJSON.interfacePins, Comparator.comparing(p -> p.name, c));
+ Arrays.sort(newComponentJSON.submodel.components, Comparator.comparing(p -> p.name, c));
+ Arrays.sort(newComponentJSON.submodel.wires, Comparator.comparing(p -> p.name, c));
+ if ("standard".equals(newComponentJSON.highLevelStateHandlerSnippetID))
+ {
+ StandardHighLevelStateHandlerParams hlshP = JsonHandler.fromJsonTree(newComponentJSON.highLevelStateHandlerParams,
+ StandardHighLevelStateHandlerParams.class);
+ TreeMap<String, AtomicHighLevelStateHandlerParams> tmp1 = new TreeMap<>(c);
+ tmp1.putAll(hlshP.atomicHighLevelStates);
+ hlshP.atomicHighLevelStates = tmp1;
+ TreeMap<String, SubcomponentHighLevelStateHandlerParams> tmp2 = new TreeMap<>(c);
+ tmp2.putAll(hlshP.subcomponentHighLevelStates);
+ hlshP.subcomponentHighLevelStates = tmp2;
+ newComponentJSON.highLevelStateHandlerParams = JsonHandler.toJsonTree(hlshP);
+ }
}
private static int compareStringsWithIntegers(String a, String b)
}
if (bLoc == b.length())
return 1;
- int aInt = 0;
- int aIntLen = 0;
- char nextCharA;
+ int aInt = 1, bInt = 1;// 1 so a longer number is always greater (makes a difference for leading zeroes)
+ boolean aHasNumber = false, bHasNumber = false;
+ char nextCharA, nextCharB;
for (;;)
{
nextCharA = a.charAt(aLoc++);
if (nextCharA < '0' || nextCharA > '9')
break;
- aIntLen++;
+ aHasNumber = true;
aInt = aInt * 10 + nextCharA - '0';
if (aLoc == a.length())
break;
}
- int bInt = 0;
- int bIntLen = 0;
- char nextCharB;
for (;;)
{
nextCharB = b.charAt(bLoc++);
if (nextCharB < '0' || nextCharB > '9')
break;
- bIntLen++;
+ bHasNumber = true;
bInt = bInt * 10 + nextCharB - '0';
if (bLoc == b.length())
break;
}
- if (aIntLen != 0)
+ if (aHasNumber)
{
- if (bIntLen == 0)
+ if (!bHasNumber)
return -1;
int comp = Integer.compare(aInt, bInt);
if (comp != 0)
return comp;
} else
{
- if (bIntLen != 0)
+ if (bHasNumber)
return 1;
int comp = Character.compare(nextCharA, nextCharB);
if (comp != 0)
private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
{
- Set<ModelWire> wiresConnectedToPin = comp.submodel.getWiresByName().values().stream()
- .filter(w -> w.getPin1() == interfacePin || w.getPin2() == interfacePin).collect(Collectors.toSet());
+ Set<ModelWire> wiresConnectedToPin1 = comp.submodel.getWiresByName().values().stream().filter(w -> w.getPin1() == interfacePin)
+ .collect(Collectors.toSet());
+ Set<ModelWire> wiresConnectedToPin2 = comp.submodel.getWiresByName().values().stream().filter(w -> w.getPin2() == interfacePin)
+ .collect(Collectors.toSet());
LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
- wiresConnectedToPin.forEach(submodelModifiable::destroyWire);
+ wiresConnectedToPin1.forEach(submodelModifiable::destroyWire);
+ wiresConnectedToPin2.forEach(submodelModifiable::destroyWire);
+ Pin supermodelPin = comp.getSupermodelPin(interfacePin.name);
comp.removeSubmodelInterface(interfacePin.name);
comp.addSubmodelInterface(new MovablePin(submodelModifiable, comp, interfacePin.name, interfacePin.logicWidth, usage,
- interfacePin.getRelX(), interfacePin.getRelY()));
- wiresConnectedToPin.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), w.getPin2()));
+ supermodelPin.getRelX(), supermodelPin.getRelY()));
+ Pin interfacePinNew = comp.getSubmodelPin(interfacePin.name);
+ wiresConnectedToPin1.forEach(w -> new ModelWire(submodelModifiable, interfacePinNew, w.getPin2(), w.getPath()));
+ wiresConnectedToPin2.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), interfacePinNew, w.getPath()));
}
}
\ No newline at end of file