Skip to content

Commit

Permalink
Merge branch 'develop' into gtf-polish-ingresses
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro authored Jun 26, 2024
2 parents 56d197e + 5633c0b commit 9700bd3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 4 additions & 2 deletions buildingmotif/dataclasses/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from buildingmotif.template_matcher import Mapping, TemplateMatcher
from buildingmotif.utils import (
PARAM,
_strip_param,
combine_graphs,
copy_graph,
remove_triples_with_node,
Expand Down Expand Up @@ -280,17 +281,18 @@ def inline_dependencies(self) -> "Template":
# replace dependency parameters with the names they inherit
# through the provided bindings
rename_params: Dict[str, str] = {
ours: theirs for ours, theirs in dep.args.items()
ours: _strip_param(theirs) for ours, theirs in dep.args.items()
}
# replace all parameters *not* mentioned in the args by prefixing
# them with the 'name' parameter binding; this is guaranteed
# to exist
name_prefix = dep.args.get("name")
name_prefix = _strip_param(dep.args["name"])
# for each parameter in the dependency...
for param in deptempl.parameters:
# if it does *not* have a mapping in the dependency, then
# prefix the parameter with the value of the 'name' binding
# to scope it properly
param = _strip_param(param)
if param not in dep.args and param != "name":
rename_params[param] = f"{name_prefix}-{param}"

Expand Down
12 changes: 11 additions & 1 deletion buildingmotif/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, Union

import pyshacl # type: ignore
from rdflib import BNode, Graph, Literal, URIRef
Expand All @@ -22,6 +22,16 @@
_gensym_counter = 0


def _strip_param(param: Union[Node, str]) -> str:
"""
Strips all PARAM namespaces from the input parameter
"""
param = str(param)
while param.startswith(PARAM):
param = param[len(PARAM) :]
return param


def _gensym(prefix: str = "p") -> URIRef:
"""
Generate a unique identifier.
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from buildingmotif.utils import (
PARAM,
_param_name,
_strip_param,
get_parameters,
get_template_parts_from_shape,
graph_hash,
Expand Down Expand Up @@ -357,3 +358,23 @@ def test_hash(bm: BuildingMOTIF):

after_hash = graph_hash(model.graph)
assert before_hash == after_hash, "Graph with same state resulted in different hash"


def test_strip_param():
# if value is 'None', key should remain unchanged
inputs = {
"p123": None,
"urn:___param___#123": "123",
"urn:___param___/123": None,
"urn:___param___#urn:___param___#123": "123",
}
for input_val, expected in inputs.items():
output = _strip_param(input_val)
if expected is None:
assert (
input_val == output
), f"Input {input_val} should remain unchanged but was {output}"
else:
assert (
expected == output
), f"Input {input_val} should be {expected} but was {output}"

0 comments on commit 9700bd3

Please sign in to comment.