Skip to main content

A library for working with the Brick ontology for buildings (brickschema.org)

Project description

Brick Ontology Python package

Build Status Documentation Status

Documentation available at readthedocs

Installation

The brickschema package requires Python >= 3.6. It can be installed with pip:

pip install brickschema

The brickschema package offers several installation configuration options for reasoning. The default bundled OWLRL reasoner delivers correct results, but exhibits poor performance on large or complex ontologies (we have observed minutes to hours) due to its bruteforce implementation.

The Allegro reasoner has better performance and implements enough of the OWLRL profile to be useful. We execute Allegrograph in a Docker container, which requires the docker package. To install support for the Allegrograph reasoner, use

pip install brickschema[allegro]

The reasonable Reasoner offers even better performance than the Allegro reasoner, but is currently only packaged for Linux platforms. (Note: no fundamental limitations here, just some packaging complexity due to cross-compiling the .so). To install support for the reasonable Reasoner, use

pip install brickschema[reasonable]

Features

OWLRL Inference

brickschema makes it easier to employ OWLRL reasoning on your graphs. The package will automatically use the fastest available reasoning implementation for your system:

  • reasonable (fastest, Linux-only for now): pip install brickschema[reasonable]
  • Allegro (next-fastest, requires Docker): pip install brickschema[allegro]
  • OWLRL (default, native Python implementation): pip install brickschema

To use OWL inference, import the OWLRLInferenceSession class (this automatically chooses the fastest reasoner; check out the inference module documentation for how to use a specific reasoner). Create a brickschema.Graph with your ontology rules and instances loaded in and apply the reasoner's session to it:

from brickschema.graph import Graph
from brickschema.namespaces import BRICK
from brickschema.inference import OWLRLInferenceSession

g = Graph(load_brick=True)
g.load_file("test.ttl")

sess = OWLRLInferenceSession()
inferred_graph = sess.expand(g)
print(f"Inferred graph has {len(inferred_graph)} triples")

Haystack Inference

Requires a JSON export of a Haystack model. First, export your Haystack model as JSON; we are using the public reference model carytown.json. Then you can use this package as follows:

import json
from brickschema.inference import HaystackInferenceSession
haysess = HaystackInferenceSession("http://project-haystack.org/carytown#")
model = json.load(open('carytown.json'))
model = haysess.infer_model(model)
print(len(model))

points = model.query("""SELECT ?point ?type WHERE {
    ?point rdf:type/rdfs:subClassOf* brick:Point .
    ?point rdf:type ?type
}""")
print(points)

SQL ORM

from brickschema.graph import Graph
from brickschema.namespaces import BRICK
from brickschema.orm import SQLORM, Location, Equipment, Point

# loads in default Brick ontology
g = Graph(load_brick=True)
# load in our model
g.load_file("test.ttl")
# put the ORM in a SQLite database file called "brick_test.db"
orm = SQLORM(g, connection_string="sqlite:///brick_test.db")

# get the points for each equipment
for equip in orm.session.query(Equipment):
    print(f"Equpiment {equip.name} is a {equip.type} with {len(equip.points)} points")
    for point in equip.points:
        print(f"    Point {point.name} has type {point.type}")
# filter for a given name or type
hvac_zones = orm.session.query(Location)\
                        .filter(Location.type==BRICK.HVAC_Zone)\
                        .all()
print(f"Model has {len(hvac_zones)} HVAC Zones")

Validate with Shape Constraint Language

The module utilizes the pySHACL package to validate a building ontology against the Brick Schema, its default constraints (shapes) and user provided shapes.

from brickschema.validate import Validator
from rdflib import Graph

dataG = Graph()
dataG.parse('myBuilding.ttl', format='turtle')
shapeG = Graph()
shapeG.parse('extraShapes.ttl', format='turtle')
v = Validator()
result = v.validate(dataG, shacl_graphs=[shapeG])
print(result.textOutput)
  • result.conforms: If True, result.textOutput is Validation Report\nConforms: True.
  • result.textOutput: Text output of pyshacl.validate(), appended with extra info that provides offender hint for each violation to help the user locate the particular violation in the data graph. See readthedocs for sample output.
  • result.violationGraphs: List of violations, each presented as a graph.

The module provides a command brick_validate similar to the pyshacl command. The following command is functionally equivalent to the code above.

brick_validate myBuilding.ttl -s extraShapes.ttl

Development

Brick requires Python >= 3.6. We use pre-commit hooks to automatically run code formatters and style checkers when you commit.

Use Poetry to manage packaging and dependencies. After installing poetry, install dependencies with:

# -D flag installs development dependencies
poetry install -D

Enter the development environment with the following command (this is analogous to activating a virtual environment.

poetry shell

On first setup, make sure to install the pre-commit hooks for running the formatting and linting tools:

# from within the environment; e.g. after running 'poetry shell'
pre-commit install

Run tests to make sure build is not broken

# from within the environment; e.g. after running 'poetry shell'
make test

Docs

Docs are written in reStructured Text. Make sure that you add your package requirements to docs/requirements.txt

Project details


Release history Release notifications | RSS feed

This version

0.1.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brickschema-0.1.3.tar.gz (91.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brickschema-0.1.3-py3-none-any.whl (90.3 kB view details)

Uploaded Python 3

File details

Details for the file brickschema-0.1.3.tar.gz.

File metadata

  • Download URL: brickschema-0.1.3.tar.gz
  • Upload date:
  • Size: 91.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.5 Linux/5.3.0-7648-generic

File hashes

Hashes for brickschema-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5a3aac089fe3b7f1e54fa27b0eef5f0e8d637661f93c22ec73c420557e79d273
MD5 0fd5aec9bbdf3bb5f122e6c229d6b3b4
BLAKE2b-256 b84ec02eca2e22cc2ed20b8212daa1bd92beadc31df40643d0887fdcd356d8f7

See more details on using hashes here.

File details

Details for the file brickschema-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: brickschema-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 90.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.5 Linux/5.3.0-7648-generic

File hashes

Hashes for brickschema-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 68c9e6c592d7f06fd75509051f129b0f83e9620e595ffb6accf3aaf62f9c7807
MD5 8dd930d490bf022dd4fbfefa45540ab1
BLAKE2b-256 702e8f0ed1d6102c8e10d1be1c055c0c5308e9ef216c8fbc1f67f8f05d2db045

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page