Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test and fix for #326 #327

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion buildingmotif/dataclasses/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,15 @@ def inline_dependencies(self) -> "Template":
deptempl_opt_args.update(deptempl.parameters)

# convert our set of optional params to a list and assign to the parent template
templ.optional_args = list(templ_optional_args.union(deptempl_opt_args))
# 1. get required parameters from the original template
# 2. calculate all optional requirements from the dependency template and the original template
# 3. remove required parameters from the optional requirements
# This avoids a bug where an optional dependency makes reference to a required parameter, and then
# subsequent inlining of the dependency without optional args would remove the required parameter
required = templ.parameters - templ_optional_args
templ.optional_args = list(
templ_optional_args.union(deptempl_opt_args) - required
)

# append the inlined template into the parent's body
templ.body += deptempl.body
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/fixtures/optional-inline/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
sensor:
body: >
@prefix P: <urn:___param___#> .
@prefix s223: <http://data.ashrae.org/standard223#> .
P:name a s223:Sensor ;
s223:observes P:property .


water-temperature:
body: >
@prefix P: <urn:___param___#> .
@prefix quantitykind: <http://qudt.org/vocab/quantitykind/> .
@prefix qudt: <http://qudt.org/schema/qudt/> .
@prefix unit: <http://qudt.org/vocab/unit/> .
@prefix s223: <http://data.ashrae.org/standard223#> .
@prefix g36: <http://data.ashrae.org/standard223/1.0/extensions/g36#> .
P:name a s223:QuantifiableObservableProperty ;
qudt:hasQuantityKind quantitykind:Temperature;
qudt:hasUnit unit:DEG_C .



hot-water-coil:
body: >
@prefix P: <urn:___param___#> .
@prefix s223: <http://data.ashrae.org/standard223#> .
@prefix g36: <http://data.ashrae.org/standard223/1.0/extensions/g36#> .
P:name a s223:HeatingCoil;
s223:hasProperty P:supply-water-temp .
P:supply-water-temp-sensor a s223:Sensor .
optional: ["supply-water-temp-sensor"]
dependencies:
- template: water-temperature
args: {"name": "supply-water-temp"}
- template: sensor
args: {"name": "supply-water-temp-sensor", "property": "supply-water-temp"}
10 changes: 10 additions & 0 deletions tests/unit/test_template_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def test_template_inline_dependencies(bm: BuildingMOTIF):
}


def test_template_inline_dependencies_with_optional(bm: BuildingMOTIF):
# fixes https://github.com/NREL/BuildingMOTIF/issues/237
lib = Library.load(directory="tests/unit/fixtures/optional-inline")
templ = lib.get_template_by_name("hot-water-coil")
templ = templ.inline_dependencies()
bindings, _ = templ.fill(BLDG, include_optional=False)
assert "supply-water-temp" in bindings.keys()
assert "name" in bindings.keys()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a negative test as well? fill with optional included and assert these keys are not present?



def test_template_evaluate_with_optional(bm: BuildingMOTIF):
"""
Test that template evaluation works with optional parameters.
Expand Down
Loading