Simplified file representation of SubmodelComponents
authorFabian Stemmler <stemmler@in.tum.de>
Sun, 16 Jun 2019 21:21:12 +0000 (23:21 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Sun, 16 Jun 2019 21:21:12 +0000 (23:21 +0200)
Merged SubmodelComponentParams and the SimpleRectangularComponentParams
into one, by adding a map of specialized parameters to
SubmodelComponentParams. There is only one file type now, so the
distinction via file extension is no longer required. This new system
still requires some clean up, but should be extensible when new
subclasses of SubmodelComponent are created.

net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/GeneralComponentParams.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/RectComponentParams.java [deleted file]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/SubComponentParams.java

index c6a2e7b..ac39fa3 100644 (file)
@@ -5,18 +5,12 @@ import java.io.IOException;
 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.model.components.GUIBitDisplay;
-import net.mograsim.logic.ui.model.components.GUIComponent;
 import net.mograsim.logic.ui.model.components.GUICustomComponentCreator;
 import net.mograsim.logic.ui.model.components.GUIManualSwitch;
 import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
 import net.mograsim.logic.ui.model.components.SubmodelComponent;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUIand;
 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUImux1;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUImux1_4;
-import net.mograsim.logic.ui.model.components.mi.nandbased.GUIsel2_4;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams;
 import net.mograsim.logic.ui.model.components.params.SubComponentParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 
@@ -24,7 +18,7 @@ public class JsonExample
 {
        public static void main(String[] args)
        {
-               SimpleLogicUIStandalone.executeVisualisation(JsonExample::createFromJsonExample);
+               SimpleLogicUIStandalone.executeVisualisation(JsonExample::createHalfAdderExample);
        }
 
        private static class TestComponent extends SimpleRectangularSubmodelComponent
@@ -52,13 +46,13 @@ public class JsonExample
        {
                GUIhalfadder tmp = new GUIhalfadder(model);
                tmp.moveTo(1000, 50);
-               RectComponentParams p = tmp.calculateRectParams();
+               SubComponentParams p = tmp.calculateParams();
                SubComponentParams pC = tmp.calculateParams();
                try
                {
                        p.writeJson("HalfAdder.rc");
                        pC.writeJson("HalfAdder.sc");
-                       p = RectComponentParams.readJson("HalfAdder.rc");
+                       p = SubComponentParams.readJson("HalfAdder.rc");
                        pC = SubComponentParams.readJson("HalfAdder.sc");
                }
                catch (IOException e)
@@ -76,11 +70,11 @@ public class JsonExample
        {
                SimpleRectangularSubmodelComponent tmp = new GUIhalfadder(model);
                tmp.moveTo(1000, 50);
-               RectComponentParams p = tmp.calculateRectParams();
+               SubComponentParams p = tmp.calculateParams();
                try
                {
                        p.writeJson("HalfAdder.rc");
-                       p = RectComponentParams.readJson("HalfAdder.rc");
+                       p = SubComponentParams.readJson("HalfAdder.rc");
                }
                catch (IOException e)
                {
index 691f552..c27b015 100644 (file)
@@ -3,18 +3,20 @@ package net.mograsim.logic.ui.model.components;
 import java.io.IOException;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
 
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.model.components.params.GeneralComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams;
+import net.mograsim.logic.ui.model.components.params.GeneralComponentParams.InnerComponentParams;
 import net.mograsim.logic.ui.model.components.params.SubComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
 import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 
 public class GUICustomComponentCreator
 {
+       private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName();
+
        private static class CustomRectComponent extends SimpleRectangularSubmodelComponent
        {
                private String path;
@@ -36,19 +38,9 @@ public class GUICustomComponentCreator
        {
                try
                {
-                       if (path.endsWith(RectComponentParams.fileExtension))
-                       {
-                               RectComponentParams params = RectComponentParams.readJson(path);
-                               SubmodelComponent ret = create(model, params, path);
-                               return ret;
-                       } else if (path.endsWith(SubComponentParams.fileExtension))
-                       {
-                               SubComponentParams params = SubComponentParams.readJson(path);
-                               SubmodelComponent ret = create(model, params, path);
-                               return ret;
-                       } else
-                               throw new IOException(String.format("\"%s\" does not have a valid file extension. Must be either %s or %s", path,
-                                               RectComponentParams.fileExtension, SubComponentParams.fileExtension));
+                       SubComponentParams params = SubComponentParams.readJson(path);
+                       SubmodelComponent ret = create(model, params, path);
+                       return ret;
                }
                catch (IOException e)
                {
@@ -58,33 +50,45 @@ public class GUICustomComponentCreator
                return new CustomRectComponent(model, 0, "ERROR", "NONE");
        }
 
-       /**
-        * @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being
-        *             saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file.
-        */
-       public static SimpleRectangularSubmodelComponent create(ViewModelModifiable model, RectComponentParams params, String path)
-       {
-               CustomRectComponent comp = new CustomRectComponent(model, params.logicWidth, params.displayName, path);
-               comp.setSubmodelScale(params.composition.innerScale);
-               comp.setInputCount(params.inputCount);
-               comp.setOutputCount(params.outputCount);
-               initSubmodelComponents(comp, params.composition);
-               return comp;
-       }
-
        /**
         * @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being
         *             saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file.
         */
        public static SubmodelComponent create(ViewModelModifiable model, SubComponentParams params, String path)
        {
-               // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
-               CustomRectComponent comp = new CustomRectComponent(model, 0, "", path);
-               comp.setSubmodelScale(params.composition.innerScale);
-               comp.setSize(params.width, params.height);
-               for (InterfacePinParams iPinParams : params.interfacePins)
+               // TODO: Clean up this mess
+               SubmodelComponent comp = null;
+               if (rectC.equals(params.type))
+               {
+                       try
+                       {
+                               Map<String, Object> m = params.specialized;
+                               SimpleRectangularSubmodelComponent rect = new CustomRectComponent(model,
+                                               ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
+                                               (String) m.get(SimpleRectangularSubmodelComponent.kLabel), path);
+                               rect.setSubmodelScale(params.composition.innerScale);
+                               rect.setSize(params.width, params.height);
+                               rect.setInputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kInCount)).intValue());
+                               rect.setOutputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kOutCount)).intValue());
+                               comp = rect;
+                       }
+                       catch (ClassCastException | NullPointerException e)
+                       {
+                               System.err.println("Failed to specialize component!");
+                               e.printStackTrace();
+                       }
+               }
+               if (comp == null)
                {
-                       comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
+                       // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
+                       comp = new CustomRectComponent(model, 0, "", path);
+
+                       comp.setSubmodelScale(params.composition.innerScale);
+                       comp.setSize(params.width, params.height);
+                       for (InterfacePinParams iPinParams : params.interfacePins)
+                       {
+                               comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
+                       }
                }
                initSubmodelComponents(comp, params.composition);
                return comp;
index 3373553..0e6e326 100644 (file)
@@ -3,17 +3,21 @@ package net.mograsim.logic.ui.model.components;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams;
 import net.mograsim.logic.ui.model.wires.Pin;
 
 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
 {
+       public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
+
        private static final double width = 35;
        private static final double pinDistance = 10;
        private static final double fontHeight = 5;
@@ -124,14 +128,17 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
                gc.drawRectangle(getBounds());
        }
 
-       public RectComponentParams calculateRectParams()
+       @Override
+       public SubComponentParams calculateParams()
        {
-               RectComponentParams params = new RectComponentParams();
-               params.displayName = label;
-               params.inputCount = inputSupermodelPins.size();
-               params.outputCount = outputSubmodelPins.size();
-               params.logicWidth = logicWidth;
-               params.composition = calculateCompositionParams();
-               return params;
+               SubComponentParams ret = super.calculateParams();
+               ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
+               Map<String, Object> m = new TreeMap<>();
+               m.put(kLabel, label);
+               m.put(kInCount, inputSupermodelPins.size());
+               m.put(kOutCount, outputSupermodelPins.size());
+               m.put(kLogicWidth, logicWidth);
+               ret.specialized = m;
+               return ret;
        }
 }
\ No newline at end of file
index b9939af..106c5ff 100644 (file)
@@ -16,13 +16,13 @@ import net.mograsim.logic.ui.LogicUIRenderer;
 import net.mograsim.logic.ui.model.ViewModel;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.model.components.params.GeneralComponentParams;
+import net.mograsim.logic.ui.model.components.params.GeneralComponentParams.InnerComponentParams;
 import net.mograsim.logic.ui.model.components.params.SubComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerPinParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams;
 import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.model.wires.Pin;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerPinParams;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
 
 public abstract class SubmodelComponent extends GUIComponent
 {
@@ -213,6 +213,7 @@ public abstract class SubmodelComponent extends GUIComponent
        public SubComponentParams calculateParams()
        {
                SubComponentParams params = new SubComponentParams();
+               params.type = SubmodelComponent.class.getSimpleName();
                params.composition = calculateCompositionParams();
 
                Rectangle bounds = getBounds();
index cf82db4..5d5bc11 100644 (file)
@@ -1,11 +1,18 @@
 package net.mograsim.logic.ui.model.components.params;
 
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
 
 public class GeneralComponentParams
 {
        public double innerScale;
        public InnerComponentParams[] subComps;
        public InnerWireParams[] innerWires;
+
+       public static class InnerComponentParams
+       {
+               public Point pos;
+               public String type;
+               public int logicWidth;
+       }
 }
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/RectComponentParams.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/RectComponentParams.java
deleted file mode 100644 (file)
index 8fafaf9..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-package net.mograsim.logic.ui.model.components.params;
-
-import java.io.IOException;
-
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-
-public class RectComponentParams
-{
-       public final static String fileExtension = ".rc";
-       public String displayName;
-       public int inputCount, outputCount, logicWidth;
-       public GeneralComponentParams composition;
-
-       public static class InnerComponentParams
-       {
-               public Point pos;
-               public String type;
-               public int logicWidth;
-       }
-
-       public static class InnerWireParams
-       {
-               public InnerPinParams pin1, pin2;
-               public Point[] path;
-       }
-
-       public static class InnerPinParams
-       {
-               public int compId, pinIndex;
-       }
-
-       public static RectComponentParams readJson(String path) throws IOException
-       {
-               return JsonHandler.readJson(path, RectComponentParams.class);
-       }
-
-       /**
-        * Writes this {@link RectComponentParams} object into a file in json format. The correct file extension is important! Check
-        * {@link RectComponentParams}.fileExtension
-        */
-       public void writeJson(String path)
-       {
-               try
-               {
-                       JsonHandler.writeJson(this, path);
-               }
-               catch (IOException e)
-               {
-                       System.err.println("Failed to write RectComponentParams to file");
-                       e.printStackTrace();
-               }
-       }
-}
index dc05114..8ea235f 100644 (file)
@@ -1,15 +1,18 @@
 package net.mograsim.logic.ui.model.components.params;
 
 import java.io.IOException;
+import java.util.Map;
 
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 
 public class SubComponentParams
 {
+       public String type;
        public final static String fileExtension = ".sc";
        public double width, height;
        public InterfacePinParams[] interfacePins;
        public GeneralComponentParams composition;
+       public Map<String, Object> specialized;
 
        public static class InterfacePinParams
        {
@@ -17,6 +20,17 @@ public class SubComponentParams
                public int logicWidth;
        }
 
+       public static class InnerWireParams
+       {
+               public InnerPinParams pin1, pin2;
+               public Point[] path;
+       }
+
+       public static class InnerPinParams
+       {
+               public int compId, pinIndex;
+       }
+
        public static SubComponentParams readJson(String path) throws IOException
        {
                return JsonHandler.readJson(path, SubComponentParams.class);