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

Adding/fixing template-to-shape functionality #259

Draft
wants to merge 21 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
af436ef
move validate_uri to utils
gtfierro May 31, 2023
a3588cc
Initial implementation of template_to_shape
gtfierro May 31, 2023
dc81819
ignore types on rfc package
gtfierro May 31, 2023
0fc4c81
improving shape generation; adding tests
gtfierro Jun 1, 2023
8bcb17d
Merge branch 'develop' into gtf-template-to-shape
gtfierro Jun 20, 2023
7b6835b
Merge remote-tracking branch 'origin/develop' into gtf-template-to-shape
gtfierro Aug 30, 2023
208f257
Merge branch 'develop' into gtf-template-to-shape
gtfierro Aug 30, 2023
158772f
fix typo
gtfierro Aug 30, 2023
089c1db
fix tests to match current bmotif behavior
gtfierro Aug 30, 2023
7bcf600
skip 223P
gtfierro Aug 30, 2023
001801c
Merge branch 'gtf-template-to-shape' of github.com:NREL/BuildingMOTIF…
gtfierro Aug 30, 2023
4a977ba
Merge remote-tracking branch 'origin/develop' into gtf-template-to-shape
gtfierro May 14, 2024
304f3d0
fixing templates to support conversion to shapes
gtfierro May 20, 2024
c9d38e8
Merge remote-tracking branch 'origin/develop' into gtf-template-to-shape
gtfierro May 20, 2024
08a8a57
fixing up tests and how params are handled
gtfierro May 21, 2024
6c244fa
fixing isomorphic graphs to reflect putting most things inside a qual…
gtfierro May 21, 2024
b069897
update mypy
gtfierro May 21, 2024
8dda522
Merge branch 'develop' into gtf-template-to-shape
gtfierro May 29, 2024
657bc9d
working on docs...
gtfierro May 29, 2024
fbd51bd
Merge branch 'gtf-template-to-shape' of github.com:NREL/BuildingMOTIF…
gtfierro May 29, 2024
5ffc049
Merge remote-tracking branch 'origin/develop' into gtf-template-to-shape
gtfierro Jun 1, 2024
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
entry: poetry run flake8 buildingmotif
# can't poetry run becuase not present in repository https://github.com/pre-commit/mirrors-mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
rev: v1.10.0
hooks:
- id: mypy
args: ["--install-types", "--non-interactive", "--ignore-missing-imports", "--follow-imports=skip", "--disable-error-code=import-untyped"]
Expand Down
4 changes: 2 additions & 2 deletions buildingmotif/dataclasses/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,10 @@ def _resolve_dependency(
return
except Exception as e:
logging.debug(
f"Could not find dependee {dep['template']} in library {imp}: {e}"
f"{template.name} could not find dependee {dep['template']} in library {imp}: {e}"
)
logging.warning(
f"Warning: could not find dependee {dep['template']} in libraries {self.graph_imports}"
f"Warning: {template.name} could not find dependee {dep['template']} in libraries {self.graph_imports}"
)

def _resolve_template_dependencies(
Expand Down
12 changes: 2 additions & 10 deletions buildingmotif/dataclasses/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import TYPE_CHECKING, Dict, List, Optional

import rdflib
import rfc3987
from rdflib import URIRef

from buildingmotif import get_building_motif
Expand All @@ -16,20 +15,13 @@
shacl_inference,
shacl_validate,
skolemize_shapes,
validate_uri,
)

if TYPE_CHECKING:
from buildingmotif import BuildingMOTIF


def _validate_uri(uri: str):
parsed = rfc3987.parse(uri)
if not parsed["scheme"]:
raise ValueError(
f"{uri} does not look like a valid URI, trying to serialize this will break."
)


@dataclass
class Model:
"""This class mirrors :py:class:`database.tables.DBModel`."""
Expand All @@ -54,7 +46,7 @@ def create(cls, name: str, description: str = "") -> "Model":
"""
bm = get_building_motif()

_validate_uri(name)
validate_uri(name)
db_model = bm.table_connection.create_db_model(name, description)

g = rdflib.Graph()
Expand Down
22 changes: 20 additions & 2 deletions buildingmotif/dataclasses/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from buildingmotif.dataclasses.model import Model
from buildingmotif.namespaces import bind_prefixes
from buildingmotif.template_matcher import Mapping, TemplateMatcher
from buildingmotif.template_to_shape import template_to_nodeshape
from buildingmotif.utils import (
PARAM,
combine_graphs,
Expand Down Expand Up @@ -288,15 +289,21 @@ def inline_dependencies(self) -> "Template":
name_prefix = dep.args.get("name")
# for each parameter in the dependency...
for param in deptempl.parameters:
# if param already starts with PARAM, then keep it as-is
if param.startswith(PARAM):
rename_params[param] = param
# 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
if param not in dep.args and param != "name":
rename_params[param] = f"{name_prefix}-{param}"

# replace the parameters in the dependency template
replace_nodes(
deptempl.body, {PARAM[k]: PARAM[v] for k, v in rename_params.items()}
deptempl.body,
{
PARAM[k]: PARAM[v] if not v.startswith(PARAM) else rdflib.URIRef(v)
for k, v in rename_params.items()
},
)
# rename the optional_args in the dependency template too
deptempl.optional_args = [
Expand Down Expand Up @@ -484,6 +491,17 @@ def find_subgraphs(
for mapping, sg in matcher.building_mapping_subgraphs_iter():
yield mapping, sg, matcher.remaining_template(mapping)

def to_nodeshape(self) -> rdflib.Graph:
"""
Interprets the template body as a SHACL shape and returns the SHACL
shape in its own graph. See buildingmotif.template_to_shape.template_to_nodeshape
for the specific translation rules and implementation.

:return: a graph containing the NodeShape
:rtype: rdflib.Graph
"""
return template_to_nodeshape(self)

def generate_csv(self, path: Optional[PathLike] = None) -> Optional[StringIO]:
"""
Generate a CSV for this template which contains a column for each template parameter.
Expand Down
2 changes: 1 addition & 1 deletion buildingmotif/template_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def bacnet(*args, **kwargs) -> rdflib.Graph:
ref_name = rdflib.BNode()
object_name = PARAM[param + "_object"]
G.add((PARAM[param], REF.hasExternalReference, ref_name))
G.add((ref_name, BACNET["object-identifier"], rdflib.Literal(object_name)))
G.add((ref_name, BACNET["object-identifier"], object_name))
G.add((ref_name, BACNET["objectOf"], PARAM["BACnet_device"]))
return G

Expand Down
Loading
Loading