Fix exceptions in documentation
Possibly due to missing extras in the environment for building the docs.
Tutorials
Model Validation
Appears to be due to different Brick versions, which is fixed if all are updated to 1.4.
- 1.3 = ../../libraries/brick/Brick-subset.ttl
- 1.4 = ../../libraries/ashrae/guideline36/*
- 1.3 = tutorial1_manifest.ttl
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
Cell In[5], line 1
----> 1 validation_result = model.validate()
2 print(f"Model is valid? {validation_result.valid}")
4 # print reasons
details
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/model.py:218, in Model.validate(self, shape_collections, error_on_missing_imports, shacl_engine) 194 """Validates this model against the given list of ShapeCollections. 195 If no list is provided, the model will be validated against the model's "manifest". 196 If a list of shape collections is provided, the manifest will *not* be automatically (...) 215 :rtype: ValidationContext 216 """ 217 compiled_model = self.compile(shape_collections or [self.get_manifest()]) --> 218 return compiled_model.validate(error_on_missing_imports)File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/compiled_model.py:145, in CompiledModel.validate(self, error_on_missing_imports) 143 # aggregate shape graphs 144 for sc in self.shape_collections: --> 145 shapeg += sc.resolve_imports( 146 error_on_missing_imports=error_on_missing_imports 147 ).graph 148 # inline sh:node for interpretability 149 shapeg = rewrite_shape_graph(shapeg)
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:137, in ShapeCollection.resolve_imports(self, recursive_limit, error_on_missing_imports)
120 """Resolves owl:imports to as many levels as requested.
121
122 By default, all owl:imports are recursively resolved. This limit can
(...)
134 :rtype: ShapeCollection
135 """
136 resolved_namespaces: Set[rdflib.URIRef] = set()
--> 137 resolved = _resolve_imports(
138 self.graph,
139 recursive_limit,
140 resolved_namespaces,
141 error_on_missing_imports=error_on_missing_imports,
142 )
143 new_sc = ShapeCollection.create()
144 new_sc.add_graph(resolved)
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:577, in _resolve_imports(graph, recursive_limit, seen, error_on_missing_imports) 574 raise Exception("Could not resolve import of %s", ontology) 575 continue --> 577 dependency = _resolve_imports( 578 sc_to_add.graph, 579 recursive_limit - 1, 580 seen, 581 error_on_missing_imports=error_on_missing_imports, 582 ) 583 new_g += dependency 584 return new_g
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:574, in _resolve_imports(graph, recursive_limit, seen, error_on_missing_imports) 572 if sc_to_add is None: 573 if error_on_missing_imports: --> 574 raise Exception("Could not resolve import of %s", ontology) 575 continue 577 dependency = _resolve_imports( 578 sc_to_add.graph, 579 recursive_limit - 1, 580 seen, 581 error_on_missing_imports=error_on_missing_imports, 582 )
Exception: ('Could not resolve import of %s', rdflib.term.URIRef('https://brickschema.org/schema/1.4/Brick'))
Model Correction
Appears to be due to different Brick versions, which is fixed if all are updated to 1.4.
- 1.3 = ../../libraries/brick/Brick-subset.ttl
- 1.4 = ../../libraries/ashrae/guideline36/*
- 1.3 = tutorial2_manifest.ttl
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
Cell In[2], line 1
----> 1 validation_result = model.validate()
2 print(f"Model is valid? {validation_result.valid}")
4 # print reasons
details
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/model.py:218, in Model.validate(self, shape_collections, error_on_missing_imports, shacl_engine) 194 """Validates this model against the given list of ShapeCollections. 195 If no list is provided, the model will be validated against the model's "manifest". 196 If a list of shape collections is provided, the manifest will *not* be automatically (...) 215 :rtype: ValidationContext 216 """ 217 compiled_model = self.compile(shape_collections or [self.get_manifest()]) --> 218 return compiled_model.validate(error_on_missing_imports)File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/compiled_model.py:145, in CompiledModel.validate(self, error_on_missing_imports) 143 # aggregate shape graphs 144 for sc in self.shape_collections: --> 145 shapeg += sc.resolve_imports( 146 error_on_missing_imports=error_on_missing_imports 147 ).graph 148 # inline sh:node for interpretability 149 shapeg = rewrite_shape_graph(shapeg)
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:137, in ShapeCollection.resolve_imports(self, recursive_limit, error_on_missing_imports)
120 """Resolves owl:imports to as many levels as requested.
121
122 By default, all owl:imports are recursively resolved. This limit can
(...)
134 :rtype: ShapeCollection
135 """
136 resolved_namespaces: Set[rdflib.URIRef] = set()
--> 137 resolved = _resolve_imports(
138 self.graph,
139 recursive_limit,
140 resolved_namespaces,
141 error_on_missing_imports=error_on_missing_imports,
142 )
143 new_sc = ShapeCollection.create()
144 new_sc.add_graph(resolved)
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:577, in _resolve_imports(graph, recursive_limit, seen, error_on_missing_imports) 574 raise Exception("Could not resolve import of %s", ontology) 575 continue --> 577 dependency = _resolve_imports( 578 sc_to_add.graph, 579 recursive_limit - 1, 580 seen, 581 error_on_missing_imports=error_on_missing_imports, 582 ) 583 new_g += dependency 584 return new_g
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:574, in _resolve_imports(graph, recursive_limit, seen, error_on_missing_imports) 572 if sc_to_add is None: 573 if error_on_missing_imports: --> 574 raise Exception("Could not resolve import of %s", ontology) 575 continue 577 dependency = _resolve_imports( 578 sc_to_add.graph, 579 recursive_limit - 1, 580 seen, 581 error_on_missing_imports=error_on_missing_imports, 582 )
Exception: ('Could not resolve import of %s', rdflib.term.URIRef('https://brickschema.org/schema/1.4/Brick'))
Guides
Generating SPARQL Queries
# refer to the shape by its URI
app1_shape = URIRef("urn:simpleapp/app1")
print(sc.shape_to_query(app1_shape))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 4
1 # refer to the shape by its URI
2 app1_shape = URIRef("urn:simpleapp/app1")
----> 4 print(sc.shape_to_query(app1_shape))
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:311, in ShapeCollection.shape_to_query(self, shape)
300 def shape_to_query(self, shape: URIRef) -> str:
301 """
302 This method takes a URI representing a SHACL shape as an argument and returns
303 a SPARQL query selecting the information which would be used to satisfy that
(...)
309 ?target <path> <value>
310 """
--> 311 clauses, project = _shape_to_where(self.graph, shape, "?target")
312 preamble = """PREFIX sh: <http://www.w3.org/ns/shacl#>
313 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
314 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
315 """
316 return f"{preamble} SELECT DISTINCT {' '.join(project)} WHERE {{\n{clauses}\n}}"
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/buildingmotif/dataclasses/shape_collection.py:475, in _shape_to_where(graph, shape, root_var)
467 for pshape in graph.objects(shape, SH.property):
468 # get the varname if we've already assigned one for this pshape above,
469 # or generate a new one. When generating a name, use the SH.name|RDFS.label field
470 # in the PropertyShape or generate a unique one
471 name = pshape_vars.get(
472 pshape,
473 f"?{get_varname(pshape)}".replace(" ", "_"),
474 )
--> 475 path = shacl_path_to_sparql_path(graph, graph.value(pshape, SH.path))
476 qMinCount = graph.value(pshape, SH.qualifiedMinCount) or 0
478 pclass = graph.value(
479 pshape, (SH["qualifiedValueShape"] * ZeroOrOne / SH["class"]) # type: ignore
480 )
File ~/checkouts/readthedocs.org/user_builds/buildingmotif/envs/latest/lib/python3.10/site-packages/pyshacl/helper/path_helper.py:43, in shacl_path_to_sparql_path(shapes_graph, path_node, prefixes, recursion)
41 raise ReportableRuntimeError("Path traversal depth is too much!")
42 top_level = recursion == 0
---> 43 sequence_list = list(shapes_graph.graph.items(path_node))
44 if len(sequence_list) > 0:
45 all_collected = []
AttributeError: 'Graph' object has no attribute 'graph'