RakipInitiative / ModelRepository

Joint project of EFSA, Federal Institute For Risk Assessment, DTU and ANSES to create a online model repository.
GNU General Public License v3.0
2 stars 0 forks source link

Port objects are initialised in two steps in JoinerNodeModel 1.9 #191

Closed miguelalba closed 3 years ago

miguelalba commented 3 years ago

Class de.bund.bfr.knime.fsklab.v1_9.joiner.JoinerNodeModel Originated in PR https://github.com/SiLeBAT/FSK-Lab/pull/722#pullrequestreview-533674137

Current state

  private static FskPortObject getFSKObjectFromStringArray(FskPortObject portObject, String[] model,
      String modelType) throws JsonMappingException, JsonProcessingException, IOException {

    portObject.modelMetadata = MAPPER.readValue(model[0], SwaggerUtil.modelClasses.get(modelType));
    if(StringUtils.isNotEmpty(model[1]))
      portObject.setModel(MAPPER.readValue(model[1], String.class));
    if(StringUtils.isNotEmpty(model[2]))
      portObject.setViz(MAPPER.readValue(model[2], String.class));
    portObject.simulations.clear();
    portObject.packages.clear();
    portObject.simulations
        .addAll(MAPPER.readValue(model[3], new TypeReference<List<FskSimulation>>() {}));

    portObject.packages.addAll(MAPPER.readValue(model[4], new TypeReference<List<String>>() {}));

    return portObject;
  }

In this method we are taking a passed port object and modifying it. It would be better to leave as much as possible of the port object immutable and return instead a new port object in this method. The first use of this is in the JoinerNodeModel

        FskPortObject jFirstInputPort = getFSKObjectFromStringArray(
            new FskPortObject(firstInputPort.getEnvironmentManager(), "", new ArrayList<String>()),
            joinerModelsData.firstModel, joinerModelsData.firstModelType);

Here we create a port object with few data and then pass it to getFSKObjectFromStringArray which fills more data in the port object from the array.

Proposal

To replace getFSKObjectFromStringArray with another helper method that creates a port object from the passed environment manager, which can be optional, and a string arrays (needs to be documented).

miguelalba commented 3 years ago

Done in PR https://github.com/SiLeBAT/FSK-Lab/pull/722#pullrequestreview-534257825. This ticket can be closed when that PR is merged.