The WNTREPANETNetwork class

class epanetparser.core.epanettypes.network.WNTREPANETNetwork(parser: WNTRJSONParser)

An EPANET water distribution network.

Parameters:

parser (WNTRJSONParser) – Parser that has already produced the model’s components.

network_info

Model metadata: name, comment, version, references.

Type:

WNTREPANETNetworkInfo

options

Simulation options.

Type:

WNTREPANETOptions

curves

Curves, in document order.

Type:

List[WNTREPANETCurve]

patterns

Patterns, in document order.

Type:

List[WNTREPANETPattern]

nodes

Nodes, in document order.

Type:

List[WNTREPANETNode]

Links, in document order.

Type:

List[WNTREPANETLink]

sources

Water quality sources, in document order.

Type:

List[WNTREPANETSource]

controls

Controls, in document order.

Type:

List[WNTREPANETControl]

index

Name-to-component index, built on first use. Network rules resolve references through it.

Type:

NetworkIndex

Notes

The component collections are plain lists in document order. They are not keyed by name, because a model may contain duplicate names, and collapsing them here would hide exactly the defect that validation needs to report.

Examples

>>> network = WNTREPANETNetwork(parser)   
>>> network.report()                     
{'nodes': 11, 'links': 13, 'patterns': 2}
>>> network.validate().is_valid          
True
classmethod from_file(filename: str | Path, raise_on_parser_error: bool = False, raise_on_parser_warning: bool = False, ignore_warnings: bool = False) → Tuple[WNTREPANETNetwork | None, Dict | None, Dict | None]

Load a network from an .inp or WNTR .json file.

Parameters:
  • filename (str or pathlib.Path) – Path to the model. .inp files are converted to WNTR’s JSON representation with WNTR before being parsed.

  • raise_on_parser_error (bool) – If True, raise a structural parsing problem instead of returning it. Rule failures are not involved: nothing is validated here.

  • raise_on_parser_warning (bool) – Accepted for compatibility. The parser produces no warnings.

  • ignore_warnings (bool) – Accepted for compatibility. The parser produces no warnings.

Returns:
  • network (WNTREPANETNetwork or None) – The parsed model, or None if the document could not be parsed.

  • errors (dict or None) – Structural problems, grouped by collection, or None if there were none.

  • warnings (dict or None) – Always None. The parser produces no warnings; non-fatal findings come from validate().

Raises:

WNTREPANETParserException – If the file cannot be read or parsed and raise_on_parser_error is True.

Examples

>>> network, errors, _ = WNTREPANETNetwork.from_file("Net1.inp")
>>> report = network.validate()   
classmethod from_json(json_src: str, raise_on_parser_error: bool = False, raise_on_parser_warning: bool = False, ignore_warnings: bool = False) → Tuple[WNTREPANETNetwork | None, Dict | None, Dict | None]

Load a network from a WNTR JSON string.

Parameters:
  • json_src (str) – JSON-encoded text of an EPANET model in WNTR’s format.

  • raise_on_parser_error (bool) – If True, raise a structural parsing problem instead of returning it.

  • raise_on_parser_warning (bool) – Accepted for compatibility. The parser produces no warnings.

  • ignore_warnings (bool) – Accepted for compatibility. The parser produces no warnings.

Returns:
  • network (WNTREPANETNetwork or None) – The parsed model, or None if the document could not be parsed.

  • errors (dict or None) – Structural problems, grouped by collection, or None if there were none.

  • warnings (dict or None) – Always None.

Raises:

WNTREPANETParserException – If the text is not valid JSON, a required key is missing, or raise_on_parser_error is True and a structural problem occurs.

Notes

A model that parses successfully may still be invalid. Check the result of validate() for that.

Examples

>>> network, errors, _ = WNTREPANETNetwork.from_json(json_text)
>>> network.validate().is_valid   
False
build_index() → NetworkIndex

Return the network’s name index, building and caching it if needed.

Returns:

NetworkIndex – Name-to-component index of this network.

Notes

The index is cached on the instance and reused by validate(). Call invalidate_index() after modifying a component collection, or the index will describe the model as it was when first built.

invalidate_index() → None

Discard the cached name index so the next use rebuilds it.

validate(context: Any = None) → ValidationReport

Validate the whole model.

Parameters:

context (Any) – Rule set selection. Anything from_any() accepts. Defaults to the core ruleset alone; pass a custom ruleset to apply application-specific constraints as well.

Returns:

ValidationReport – Component issues in component order, followed by network issues. Check is_valid.

Notes

This method contains no rule logic; it delegates to the engine, which selects rules by class name. Custom rulesets therefore apply to this class without it knowing they exist.

Raises:
  • RuleSetSelectionError – If context names a rule set that has not been discovered, or does not select exactly one core rule set.

  • RuleExecutionError – If a selected rule raises an exception other than AssertionError, which indicates a defect in the rule.

Examples

>>> from epanetparser.core.validation import ValidationContext
>>> network.validate().is_valid
True
>>> network.validate(ValidationContext(custom=["milp"])).is_valid
False