Skip to content

Commit

Permalink
add docs on converting shapes to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Dec 31, 2024
1 parent b40079c commit 25bab24
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
49 changes: 48 additions & 1 deletion docs/explanations/shapes-and-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ a **Template** is a function that generates an RDF graph.

## Converting Shapes to Templates

BuildingMOTIF automatically converts shapes to templates.
### When Loading a Library

BuildingMOTIF can automatically convert shapes to templates when loading a Library.
Evaluating the resulting template will generate a graph that validates against the shape.

When BuildingMOTIF loads a Library, it makes an attempt to find any shapes defined within it.
Expand Down Expand Up @@ -49,6 +51,51 @@ BuildingMOTIF currently uses the name of the SHACL shape as the name of the gene
All other parameters (i.e., nodes corresponding to `sh:property`) are given invented names *unless*
there is a `sh:name` attribute on the property shape.

This feature can be disabled by setting `infer_templates=False` when calling `Library.load`

### From Shape Collections

It is also possible to convert the shapes defined in a Shape Collection to templates.
This is done by calling the `infer_templates` method on the Shape Collection.
If `infer_templates` is True when calling `Library.load`, then BuildingMOTIF will automatically call `infer_templates` on the Shape Collection
within that Library.

Being able to call `infer_templates` on a Shape Collection is useful when you have
a graph of shapes that you programmatically created or loaded without packaging them
in a Library.

```{code-cell} python3
from buildingmotif import BuildingMOTIF
from buildingmotif.dataclasses import Library, ShapeCollection
# in-memory instance
bm = BuildingMOTIF("sqlite://")
my_shapes_source = """
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix brick: <https://brickschema.org/schema/Brick#> .
@prefix ex: <http://example.org/> .
ex:SimpleShape a sh:NodeShape, owl:Class ;
sh:property [
sh:path ex:hasPoint ;
sh:qualifiedValueShape [ sh:class brick:Sensor ] ;
sh:qualifiedMinCount 1 ;
] .
"""
# create a ShapeCollection to hold the shapes
my_shapes = ShapeCollection.create()
my_shapes.graph.parse(data=my_shapes_source, format="ttl")
# create a Library to hold the generated templates
lib = Library.create("my-library")
my_shapes.infer_templates(lib)
print(lib.get_templates())
```

### Example

Consider the following shape which has been loaded into BuildingMOTIF as part of a Library:
Expand Down
6 changes: 3 additions & 3 deletions libraries/ZonePAC/shapes.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
owl:imports <https://brickschema.org/schema/1.4/Brick> ;
.

:zonepac-zone a sh:NodeShape ;
:zonepac-zone a sh:NodeShape, owl:Class ;
sh:targetClass brick:HVAC_Zone ;
sh:property [
sh:path brick:hasPoint ;
Expand All @@ -37,7 +37,7 @@
] ;
.

:zonepac-vav a sh:NodeShape ;
:zonepac-vav a sh:NodeShape, owl:Class ;
sh:targetClass brick:Terminal_Unit ;
sh:property [
sh:path brick:hasPart ;
Expand All @@ -56,7 +56,7 @@
] ;
.

:heating-coil a sh:NodeShape ;
:heating-coil a sh:NodeShape, owl:Class ;
sh:targetClass brick:Heating_Coil ;
sh:property [
sh:path brick:hasPoint ;
Expand Down

0 comments on commit 25bab24

Please sign in to comment.