Skip to content

Commit

Permalink
feat: Do not allow multiple ProcessEquipment with same name in Proces…
Browse files Browse the repository at this point in the history
…sSystem (#1109)

* feat: do not allow multiple processEquipemt with same name in ProcessSystem
* refact: reuse code in add
* test: update tests
  • Loading branch information
asmfstatoil authored Sep 26, 2024
1 parent eefd78b commit e886d6e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,23 @@ public ProcessSystem(String name) {

/**
* <p>
* add.
* Add to end.
* </p>
*
* @param operation a {@link neqsim.processSimulation.processEquipment.ProcessEquipmentInterface}
* object
*/
public void add(ProcessEquipmentInterface operation) {
ArrayList<ProcessEquipmentInterface> 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);
}

/**
* <p>
* add.
* Add to specific position.
* </p>
*
* @param position a int
* @param position 0-based position
* @param operation a {@link neqsim.processSimulation.processEquipment.ProcessEquipmentInterface}
* object
*/
Expand All @@ -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);
Expand All @@ -133,7 +111,7 @@ public void add(int position, ProcessEquipmentInterface operation) {

/**
* <p>
* add.
* Add measurementdevice.
* </p>
*
* @param measurementDevice a
Expand All @@ -145,7 +123,7 @@ public void add(MeasurementDeviceInterface measurementDevice) {

/**
* <p>
* add.
* Add multiple process equipment to end.
* </p>
*
* @param operations an array of
Expand All @@ -157,10 +135,10 @@ public void add(ProcessEquipmentInterface[] operations) {

/**
* <p>
* getUnit.
* Get process equipmen by name.
* </p>
*
* @param name a {@link java.lang.String} object
* @param name Name of
* @return a {@link java.lang.Object} object
*/
public Object getUnit(String name) {
Expand Down Expand Up @@ -199,10 +177,10 @@ public boolean hasUnitName(String name) {

/**
* <p>
* getMeasurementDevice.
* Get MeasureDevice by name.
* </p>
*
* @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) {
Expand Down Expand Up @@ -757,10 +735,6 @@ public String getName() {

/**
* {@inheritDoc}
*
* <p>
* Setter for the field <code>name</code>.
* </p>
*/
@Override
public void setName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e886d6e

Please sign in to comment.