From e886d6e8b24e269a404604b8a64f81f06fb89dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85smund=20V=C3=A5ge=20Fannemel?= <34712686+asmfstatoil@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:01:54 +0200 Subject: [PATCH] feat: Do not allow multiple ProcessEquipment with same name in ProcessSystem (#1109) * feat: do not allow multiple processEquipemt with same name in ProcessSystem * refact: reuse code in add * test: update tests --- .../processSystem/ProcessSystem.java | 62 ++++++------------- .../processSystem/ProcessSystemTest.java | 39 ++++++++---- 2 files changed, 45 insertions(+), 56 deletions(-) diff --git a/src/main/java/neqsim/processSimulation/processSystem/ProcessSystem.java b/src/main/java/neqsim/processSimulation/processSystem/ProcessSystem.java index 80858bde7..fc3e1775f 100644 --- a/src/main/java/neqsim/processSimulation/processSystem/ProcessSystem.java +++ b/src/main/java/neqsim/processSimulation/processSystem/ProcessSystem.java @@ -64,44 +64,23 @@ public ProcessSystem(String name) { /** *

- * add. + * Add to end. *

* * @param operation a {@link neqsim.processSimulation.processEquipment.ProcessEquipmentInterface} * object */ public void add(ProcessEquipmentInterface operation) { - ArrayList units = this.getUnitOperations(); - - for (ProcessEquipmentInterface unit : units) { - if (unit == operation) { - return; - } - } - - if (getAllUnitNames().contains(operation.getName())) { - String currClass = operation.getClass().getSimpleName(); - int num = 1; - for (ProcessEquipmentInterface unit : units) { - if (unit.getClass().getSimpleName().equals(currClass)) { - num++; - } - } - operation.setName(currClass + Integer.toString(num)); - } - - getUnitOperations().add(operation); - if (operation instanceof ModuleInterface) { - ((ModuleInterface) operation).initializeModule(); - } + // Add to end + add(this.getUnitOperations().size(), operation); } /** *

- * add. + * Add to specific position. *

* - * @param position a int + * @param position 0-based position * @param operation a {@link neqsim.processSimulation.processEquipment.ProcessEquipmentInterface} * object */ @@ -110,19 +89,18 @@ public void add(int position, ProcessEquipmentInterface operation) { for (ProcessEquipmentInterface unit : units) { if (unit == operation) { + logger.info("Equipment " + operation.getName() + " is already included in ProcessSystem"); return; } } if (getAllUnitNames().contains(operation.getName())) { - String currClass = operation.getClass().getSimpleName(); - int num = 1; - for (ProcessEquipmentInterface unit : units) { - if (unit.getClass().getSimpleName().equals(currClass)) { - num++; - } - } - operation.setName(currClass + Integer.toString(num)); + ProcessEquipmentInterface existing = + (ProcessEquipmentInterface) this.getUnit(operation.getName()); + throw new RuntimeException(new neqsim.util.exception.InvalidInputException("ProcessSystem", + "add", "operation", "- Process equipment of type " + existing.getClass().getSimpleName() + + " named " + operation.getName() + " already included in ProcessSystem")); + } getUnitOperations().add(position, operation); @@ -133,7 +111,7 @@ public void add(int position, ProcessEquipmentInterface operation) { /** *

- * add. + * Add measurementdevice. *

* * @param measurementDevice a @@ -145,7 +123,7 @@ public void add(MeasurementDeviceInterface measurementDevice) { /** *

- * add. + * Add multiple process equipment to end. *

* * @param operations an array of @@ -157,10 +135,10 @@ public void add(ProcessEquipmentInterface[] operations) { /** *

- * getUnit. + * Get process equipmen by name. *

* - * @param name a {@link java.lang.String} object + * @param name Name of * @return a {@link java.lang.Object} object */ public Object getUnit(String name) { @@ -199,10 +177,10 @@ public boolean hasUnitName(String name) { /** *

- * getMeasurementDevice. + * Get MeasureDevice by name. *

* - * @param name a {@link java.lang.String} object + * @param name Name of measurement device * @return a {@link neqsim.processSimulation.measurementDevice.MeasurementDeviceInterface} object */ public MeasurementDeviceInterface getMeasurementDevice(String name) { @@ -757,10 +735,6 @@ public String getName() { /** * {@inheritDoc} - * - *

- * Setter for the field name. - *

*/ @Override public void setName(String name) { diff --git a/src/test/java/neqsim/processSimulation/processSystem/ProcessSystemTest.java b/src/test/java/neqsim/processSimulation/processSystem/ProcessSystemTest.java index 0e7cbef52..5d7fca765 100644 --- a/src/test/java/neqsim/processSimulation/processSystem/ProcessSystemTest.java +++ b/src/test/java/neqsim/processSimulation/processSystem/ProcessSystemTest.java @@ -19,6 +19,7 @@ import neqsim.processSimulation.processEquipment.separator.Separator; import neqsim.processSimulation.processEquipment.splitter.Splitter; import neqsim.processSimulation.processEquipment.stream.Stream; +import neqsim.processSimulation.processEquipment.tank.Tank; import neqsim.processSimulation.processEquipment.util.Calculator; import neqsim.processSimulation.processEquipment.util.Recycle; import neqsim.processSimulation.processEquipment.util.StreamSaturatorUtil; @@ -116,16 +117,30 @@ public void testRemoveUnit() { } @Test - public void testAddUnitsWithNoName() { - Separator sep = new Separator("Separator"); - p.add(sep); - sep = new Separator("Separator"); + public void testAddUnitsWithDuplicateName() { + String name = "TestSeparator"; + Separator sep = new Separator(name); p.add(sep); - Assertions.assertEquals(2, p.size()); - p.removeUnit("Separator2"); - Assertions.assertEquals(1, p.size()); - p.removeUnit("Separator"); + + RuntimeException thrown = Assertions.assertThrows(RuntimeException.class, () -> { + p.add(new Separator(name)); + }); + Assertions.assertEquals( + "neqsim.util.exception.InvalidInputException: ProcessSystem:add - Input operation - Process equipment of type Separator named " + + name + " already included in ProcessSystem", + thrown.getMessage()); + p.removeUnit(name); Assertions.assertEquals(0, p.size()); + p.add(new Tank(name)); + Assertions.assertEquals(1, p.size()); + + RuntimeException thrown2 = Assertions.assertThrows(RuntimeException.class, () -> { + p.add(new Separator(name)); + }); + Assertions.assertEquals( + "neqsim.util.exception.InvalidInputException: ProcessSystem:add - Input operation - Process equipment of type Tank named " + + name + " already included in ProcessSystem", + thrown2.getMessage()); } @Test @@ -134,11 +149,13 @@ public void testGetUnitNumber() { p.add(sep); Separator sep2 = new Separator("Separator2"); p.add(sep2); + Assertions.assertEquals(2, p.size()); Assertions.assertEquals(0, p.getUnitNumber("Separator")); Assertions.assertEquals(1, p.getUnitNumber("Separator2")); p.removeUnit("Separator"); + Assertions.assertEquals(1, p.size()); p.add(sep); Assertions.assertEquals(0, p.getUnitNumber("Separator2")); @@ -420,8 +437,7 @@ public void runTEGProcessTest2() { strippingGas.setTemperature(180.0, "C"); strippingGas.setPressure(feedPressureStripGas, "bara"); - Stream gasToReboiler = (Stream) strippingGas.clone(); - gasToReboiler.setName("gas to reboiler"); + Stream gasToReboiler = strippingGas.clone("gas to reboiler"); DistillationColumn column = new DistillationColumn("TEG regeneration column", 1, true, true); column.addFeedStream(glycol_flash_valve2.getOutletStream(), 1); @@ -738,8 +754,7 @@ public void testRun_step() { strippingGas.setTemperature(180.0, "C"); strippingGas.setPressure(feedPressureStripGas, "bara"); - Stream gasToReboiler = (Stream) strippingGas.clone(); - gasToReboiler.setName("gas to reboiler"); + Stream gasToReboiler = strippingGas.clone("gas to reboiler"); DistillationColumn column = new DistillationColumn("TEG regeneration column", 1, true, true); column.addFeedStream(glycol_flash_valve2.getOutletStream(), 1);