Class JsonProcessBuilder

java.lang.Object
neqsim.process.processmodel.JsonProcessBuilder

public class JsonProcessBuilder extends Object
Builds a ProcessSystem from a JSON definition string.

Supports declarative definition of fluids, streams, and equipment with automatic stream wiring by name reference. Designed for web API integration where clients submit JSON process definitions and receive simulation results.

JSON Format:

{ "fluid": { "model": "SRK", "temperature": 298.15, "pressure": 50.0, "mixingRule":
"classic", "components": { "methane": 0.85, "ethane": 0.10, "propane": 0.05 } }, "process": [ {
"type": "Stream", "name": "feed", "properties": { "flowRate": [50000.0, "kg/hr"] } }, { "type":
"Separator", "name": "HP Sep", "inlet": "feed" }, { "type": "Compressor", "name": "Comp",
"inlet": "HP Sep.gasOut", "properties": { "outletPressure": [80.0, "bara"] } } ] } 
Author:
Even Solbraa @version 1.0
  • Field Details

    • logger

      private static final org.apache.logging.log4j.Logger logger
      Logger object for class.
    • namedFluids

      private final Map<String, SystemInterface> namedFluids
      Registry of named fluids created during build.
    • namedEquipment

      private final Map<String, ProcessEquipmentInterface> namedEquipment
      Registry of named equipment created during build.
    • errors

      private final List<SimulationResult.ErrorDetail> errors
      Errors accumulated during build.
    • warnings

      private final List<String> warnings
      Warnings accumulated during build.
  • Constructor Details

    • JsonProcessBuilder

      public JsonProcessBuilder()
      Constructs a new JsonProcessBuilder.
  • Method Details

    • build

      public SimulationResult build(String json)
      Builds a ProcessSystem from a JSON string.
      Parameters:
      json - the JSON process definition
      Returns:
      a SimulationResult containing the built ProcessSystem or errors
    • buildFromJsonObject

      public SimulationResult buildFromJsonObject(com.google.gson.JsonObject root)
      Builds a ProcessSystem from a parsed JsonObject.
      Parameters:
      root - the root JSON object
      Returns:
      a SimulationResult containing the built ProcessSystem or errors
    • buildFromJsonObject

      public SimulationResult buildFromJsonObject(com.google.gson.JsonObject root, SystemInterface preBuiltFluid)
      Builds a ProcessSystem from a parsed JsonObject, optionally with a pre-built fluid.

      When preBuiltFluid is non-null, it is used as the default fluid for all streams instead of parsing the 'fluid' section from JSON. This is the recommended path when importing fluids from external sources (e.g., Eclipse E300 files) that preserve all component critical properties and BIPs.

      Parameters:
      root - the root JSON object
      preBuiltFluid - optional pre-built fluid system (null to use JSON fluid section)
      Returns:
      a SimulationResult containing the built ProcessSystem or errors
    • buildFluid

      private SystemInterface buildFluid(com.google.gson.JsonObject fluidDef)
      Builds a fluid (SystemInterface) from a JSON definition.
      Parameters:
      fluidDef - the JSON object defining the fluid
      Returns:
      the created fluid, or null if creation failed
    • createFluidByModel

      private SystemInterface createFluidByModel(String model, double temperature, double pressure)
      Creates a SystemInterface based on the model type string.
      Parameters:
      model - the model name (e.g., "SRK", "PR", "CPA")
      temperature - temperature in Kelvin
      pressure - pressure in bara
      Returns:
      the created fluid system, or null if unknown model
    • createUnit

      private void createUnit(ProcessSystem process, com.google.gson.JsonObject unitDef, SystemInterface defaultFluid, int index)
      Pass 1: Creates a unit (no wiring) and registers it.
      Parameters:
      process - the process system
      unitDef - the JSON definition
      defaultFluid - the default fluid
      index - the unit index
    • createDistillationColumn

      private ProcessEquipmentInterface createDistillationColumn(String name, com.google.gson.JsonObject unitDef)
      Creates a DistillationColumn from its JSON definition, extracting tray count and condenser/reboiler flags from properties.
      Parameters:
      name - the column name
      unitDef - the JSON definition
      Returns:
      the created DistillationColumn
    • reportUnwiredUnit

      private void reportUnwiredUnit(String name, com.google.gson.JsonObject unitDef)
      Reports an unwired unit as a warning (not a hard error). Extracts reference details so the user knows which connections could not be resolved.
      Parameters:
      name - the unit name
      unitDef - the JSON definition
    • wireUnit

      private void wireUnit(String name, com.google.gson.JsonObject unitDef)
      Pass 2: Wires inlet connections for a unit (all units now exist).
      Parameters:
      name - the unit name
      unitDef - the JSON definition
    • needsWiring

      private boolean needsWiring(com.google.gson.JsonObject unitDef)
      Checks whether a unit definition needs inlet wiring.
      Parameters:
      unitDef - the JSON definition
      Returns:
      true if the unit has inlet references that need to be resolved
    • tryWireUnit

      private boolean tryWireUnit(String name, com.google.gson.JsonObject unitDef)
      Attempts to wire all inlet references for a unit. Returns true only if ALL references resolve successfully, allowing downstream units to use this unit's outlets. Does not add error details on failure (those are added by the fallback wireUnit call).
      Parameters:
      name - the unit name
      unitDef - the JSON definition
      Returns:
      true if all inlet references resolved and were wired
    • buildUnit

      @Deprecated private void buildUnit(ProcessSystem process, com.google.gson.JsonObject unitDef, SystemInterface defaultFluid, int index)
      Deprecated.
      Use createUnit + wireUnit two-pass approach instead
      Builds a single process unit from its JSON definition and adds it to the process.
      Parameters:
      process - the process system to add the unit to
      unitDef - the JSON object defining the unit
      defaultFluid - the default fluid to use for streams
      index - the unit index (for error reporting)
    • createAndWireUnit

      private ProcessEquipmentInterface createAndWireUnit(ProcessSystem process, String type, String name, com.google.gson.JsonObject unitDef, SystemInterface defaultFluid)
      Creates a unit and wires its inlet stream based on the JSON definition.
      Parameters:
      process - the process system
      type - the equipment type
      name - the equipment name
      unitDef - the JSON definition
      defaultFluid - the default fluid
      Returns:
      the created equipment, or null if creation failed
    • createStream

      private ProcessEquipmentInterface createStream(String name, com.google.gson.JsonObject unitDef, SystemInterface defaultFluid)
      Creates a Stream from its JSON definition.
      Parameters:
      name - the stream name
      unitDef - the JSON definition
      defaultFluid - the default fluid
      Returns:
      the created stream
    • resolveStreamReference

      private StreamInterface resolveStreamReference(String ref)
      Resolves a stream reference string to an actual StreamInterface.

      Supports the following reference formats:

      • "unitName" — resolves to the default outlet stream
      • "unitName.gasOut" — resolves to gas outlet of a separator
      • "unitName.liquidOut" — resolves to liquid outlet of a separator
      • "unitName.outlet" — resolves to the outlet stream
      Parameters:
      ref - the stream reference string
      Returns:
      the resolved stream, or null if not found
    • wireInletStream

      private void wireInletStream(ProcessEquipmentInterface equipment, StreamInterface stream)
      Wires an inlet stream to an equipment unit via reflection.
      Parameters:
      equipment - the equipment to wire
      stream - the inlet stream
    • applyProperties

      private void applyProperties(ProcessEquipmentInterface equipment, com.google.gson.JsonObject properties)
      Applies property settings from JSON to an equipment unit via reflection.
      Parameters:
      equipment - the equipment to configure
      properties - the properties JSON object
    • applyMechanicalDesignProperties

      private void applyMechanicalDesignProperties(ProcessEquipmentInterface equipment, com.google.gson.JsonObject mdProps)
    • wireAdjuster

      private void wireAdjuster(Adjuster adjuster, com.google.gson.JsonObject props)
      Wires an Adjuster's adjusted and target variables from JSON properties.

      The Adjuster modifies one equipment property (adjusted variable) to achieve a target value on another equipment property (target variable). JSON format:

      "properties": {
        "adjustedEquipment": "Valve-1",
        "adjustedVariable": "pressure",
        "targetEquipment": "Stream-Out",
        "targetVariable": "temperature",
        "targetValue": 298.15,
        "tolerance": 0.01,
        "stepSize": 0.5
      }
      
      Parameters:
      adjuster - the adjuster to configure
      props - the properties JSON object
    • applyEntrainment

      private void applyEntrainment(Separator separator, com.google.gson.JsonElement entrainmentElement)
      Applies entrainment specifications to a separator from a JSON array.

      Each array element is an object with keys: value, specType, specifiedStream, phaseFrom, phaseTo. These map directly to Separator.setEntrainment(double, String, String, String, String).

      Parameters:
      separator - the separator to configure
      entrainmentElement - the JSON element (expected to be a JsonArray)
    • applyProperty

      private void applyProperty(ProcessEquipmentInterface equipment, String propName, com.google.gson.JsonElement value)
      Applies a single property to an equipment unit.
      Parameters:
      equipment - the equipment
      propName - the property name
      value - the property value
    • applyPropertyObject

      private void applyPropertyObject(Object target, String targetName, String propName, com.google.gson.JsonElement value)
    • invokeNoArg

      private void invokeNoArg(Object target, String targetName, String methodName)
    • buildAndRun

      public static SimulationResult buildAndRun(String json)
      Convenience method to build and run a process from JSON.
      Parameters:
      json - the JSON process definition
      Returns:
      the simulation result with report
    • buildAndRun

      public static SimulationResult buildAndRun(String json, SystemInterface fluid)
      Convenience method to build and run a process from JSON with a pre-built fluid.

      This overload is used when the fluid has been loaded from an external source (e.g., an Eclipse E300 file via EclipseFluidReadWrite) and should be used instead of the fluid definition in the JSON. The pre-built fluid preserves all critical properties (Tc, Pc, acentric factor, MW, BIPs) for both standard and hypothetical/pseudo components.

      Parameters:
      json - the JSON process definition (the 'fluid' section is ignored)
      fluid - the pre-built thermodynamic system to use
      Returns:
      the simulation result with report
    • buildOnly

      public static SimulationResult buildOnly(String json)
      Convenience method to build (without running) a process from JSON.
      Parameters:
      json - the JSON process definition
      Returns:
      the simulation result with the ProcessSystem