From 161ecd36b6dcfe0e38cee3fd6ea8ad42d0705777 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Wed, 7 Oct 2020 22:34:01 +0200 Subject: [PATCH] Using Builder pattern for CoreModelParameters now --- .../model/am2900/machine/Am2900Machine.java | 11 ++-- .../examples/CountGatesPerComponent.java | 4 +- .../logic/model/SimpleLogicUIStandalone.java | 11 ++-- .../modeladapter/CoreModelParameters.java | 50 +++++++++++++++++-- .../mograsim/plugin/SimulationPreview.java | 11 ++-- .../am2900/util/TestEnvironmentHelper.java | 12 ++--- 6 files changed, 75 insertions(+), 24 deletions(-) diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java index a7f7379e..3d486f47 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java @@ -16,6 +16,7 @@ import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.components.atomic.ModelClock; import net.mograsim.logic.model.modeladapter.CoreModelParameters; +import net.mograsim.logic.model.modeladapter.CoreModelParameters.CoreModelParametersBuilder; import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.machine.AssignableMainMemory; @@ -55,10 +56,12 @@ public class Am2900Machine implements Machine "resloader:Am2900Loader:jsonres:net/mograsim/logic/model/am2900/components/Am2900.json", "Am2900"); this.amicListeners = new HashSet<>(); - CoreModelParameters params = new CoreModelParameters(); - params.gateProcessTime = 50; - params.hardcodedComponentProcessTime = params.gateProcessTime * 5; - params.wireTravelTime = 10; + CoreModelParametersBuilder paramsBuilder = new CoreModelParametersBuilder(); + paramsBuilder.gateProcessTime = 50; + paramsBuilder.hardcodedComponentProcessTime = paramsBuilder.gateProcessTime * 5; + paramsBuilder.wireTravelTime = 10; + CoreModelParameters params = paramsBuilder.build(); + mainMemory = new AssignableMainMemory(new StandardMainMemory(am2900MachineDefinition.getMainMemoryDefinition())); instMemory = new AssignableMicroInstructionMemory( new StandardMicroInstructionMemory(am2900MachineDefinition.getMicroInstructionMemoryDefinition())); diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/CountGatesPerComponent.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/CountGatesPerComponent.java index cac3aa7e..b9c73985 100755 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/CountGatesPerComponent.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/CountGatesPerComponent.java @@ -8,7 +8,7 @@ import java.util.Map.Entry; 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.modeladapter.CoreModelParameters; +import net.mograsim.logic.model.modeladapter.CoreModelParameters.CoreModelParametersBuilder; import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; @@ -29,7 +29,7 @@ public class CountGatesPerComponent IndirectModelComponentCreator.createComponent(model, componentID); LogicCoreAdapter.gateCountsPerComponentClass.clear(); - LogicCoreAdapter.convert(model, new CoreModelParameters()); + LogicCoreAdapter.convert(model, new CoreModelParametersBuilder().build()); System.out.println(componentID + ':'); for (Entry, Integer> e : LogicCoreAdapter.gateCountsPerComponentClass.entrySet()) diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/SimpleLogicUIStandalone.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/SimpleLogicUIStandalone.java index 829abaa5..dab45843 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/SimpleLogicUIStandalone.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/SimpleLogicUIStandalone.java @@ -5,6 +5,7 @@ import java.util.function.Consumer; import net.mograsim.logic.core.timeline.Timeline; import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.logic.model.modeladapter.CoreModelParameters; +import net.mograsim.logic.model.modeladapter.CoreModelParameters.CoreModelParametersBuilder; import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.preferences.DefaultRenderPreferences; @@ -17,11 +18,11 @@ public class SimpleLogicUIStandalone public static void executeVisualisation(Consumer setupLogicModel, Consumer beforeRun) { - CoreModelParameters params = new CoreModelParameters(); - params.gateProcessTime = 50; - params.hardcodedComponentProcessTime = params.gateProcessTime * 5; - params.wireTravelTime = 10; - executeVisualisation(setupLogicModel, params, beforeRun); + CoreModelParametersBuilder paramsBuilder = new CoreModelParametersBuilder(); + paramsBuilder.gateProcessTime = 50; + paramsBuilder.hardcodedComponentProcessTime = paramsBuilder.gateProcessTime * 5; + paramsBuilder.wireTravelTime = 10; + executeVisualisation(setupLogicModel, paramsBuilder.build(), beforeRun); } public static void executeVisualisation(Consumer setupLogicModel, CoreModelParameters params) diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/CoreModelParameters.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/CoreModelParameters.java index 96be5c2a..6111e0b2 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/CoreModelParameters.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/CoreModelParameters.java @@ -2,7 +2,51 @@ package net.mograsim.logic.model.modeladapter; public class CoreModelParameters { - public int wireTravelTime; - public int gateProcessTime; - public int hardcodedComponentProcessTime; + public final int wireTravelTime; + public final int gateProcessTime; + public final int hardcodedComponentProcessTime; + + public CoreModelParameters(int wireTravelTime, int gateProcessTime, int hardcodedComponentProcessTime) + { + this.wireTravelTime = wireTravelTime; + this.gateProcessTime = gateProcessTime; + this.hardcodedComponentProcessTime = hardcodedComponentProcessTime; + } + + private CoreModelParameters(CoreModelParametersBuilder builder) + { + this.wireTravelTime = builder.wireTravelTime; + this.gateProcessTime = builder.gateProcessTime; + this.hardcodedComponentProcessTime = builder.hardcodedComponentProcessTime; + } + + public static class CoreModelParametersBuilder + { + public int wireTravelTime; + public int gateProcessTime; + public int hardcodedComponentProcessTime; + + public CoreModelParametersBuilder wireTravelTime(int wireTravelTime) + { + this.wireTravelTime = wireTravelTime; + return this; + } + + public CoreModelParametersBuilder gateProcessTime(int gateProcessTime) + { + this.gateProcessTime = gateProcessTime; + return this; + } + + public CoreModelParametersBuilder hardcodedComponentProcessTime(int hardcodedComponentProcessTime) + { + this.hardcodedComponentProcessTime = hardcodedComponentProcessTime; + return this; + } + + public CoreModelParameters build() + { + return new CoreModelParameters(this); + } + } } \ No newline at end of file diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java index 03a0e34e..45d5540a 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java @@ -16,6 +16,7 @@ import net.mograsim.logic.model.model.components.atomic.ModelOrGate; import net.mograsim.logic.model.model.wires.ModelWire; import net.mograsim.logic.model.model.wires.ModelWireCrossPoint; import net.mograsim.logic.model.modeladapter.CoreModelParameters; +import net.mograsim.logic.model.modeladapter.CoreModelParameters.CoreModelParametersBuilder; import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.preferences.RenderPreferences; import net.mograsim.plugin.preferences.EclipseRenderPreferences; @@ -33,10 +34,12 @@ public class SimulationPreview implements IThemePreview MograsimActivator.instance().getPreferenceStore()); LogicModelModifiable model = new LogicModelModifiable(); - CoreModelParameters params = new CoreModelParameters(); - params.gateProcessTime = 50; - params.hardcodedComponentProcessTime = params.gateProcessTime * 5; - params.wireTravelTime = 10; + + CoreModelParametersBuilder paramsBuilder = new CoreModelParametersBuilder(); + paramsBuilder.gateProcessTime = 50; + paramsBuilder.hardcodedComponentProcessTime = paramsBuilder.gateProcessTime * 5; + paramsBuilder.wireTravelTime = 10; + CoreModelParameters params = paramsBuilder.build(); ModelManualSwitch rIn = new ModelManualSwitch(model, 1); rIn.moveTo(10, 10); diff --git a/tests/net.mograsim.logic.model.am2900.tests/src/net/mograsim/logic/model/am2900/util/TestEnvironmentHelper.java b/tests/net.mograsim.logic.model.am2900.tests/src/net/mograsim/logic/model/am2900/util/TestEnvironmentHelper.java index 54321cb2..071bd783 100644 --- a/tests/net.mograsim.logic.model.am2900.tests/src/net/mograsim/logic/model/am2900/util/TestEnvironmentHelper.java +++ b/tests/net.mograsim.logic.model.am2900.tests/src/net/mograsim/logic/model/am2900/util/TestEnvironmentHelper.java @@ -27,7 +27,7 @@ import net.mograsim.logic.model.model.components.atomic.ModelManualSwitch; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; import net.mograsim.logic.model.model.wires.ModelWire; import net.mograsim.logic.model.model.wires.Pin; -import net.mograsim.logic.model.modeladapter.CoreModelParameters; +import net.mograsim.logic.model.modeladapter.CoreModelParameters.CoreModelParametersBuilder; import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.preferences.DefaultRenderPreferences; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; @@ -89,11 +89,11 @@ public class TestEnvironmentHelper component.getPins().values().forEach(this::extendModelPin); // Create core model - CoreModelParameters params = new CoreModelParameters(); - params.gateProcessTime = 50; - params.hardcodedComponentProcessTime = params.gateProcessTime * 5; - params.wireTravelTime = 10; - timeline = LogicCoreAdapter.convert(logicModel, params); + CoreModelParametersBuilder paramsBuilder = new CoreModelParametersBuilder(); + paramsBuilder.gateProcessTime = 50; + paramsBuilder.hardcodedComponentProcessTime = paramsBuilder.gateProcessTime * 5; + paramsBuilder.wireTravelTime = 10; + timeline = LogicCoreAdapter.convert(logicModel, paramsBuilder.build()); timelineField.ifPresent(f -> setField(f, timeline)); // Bind switches/displays to this test class -- 2.17.1