Skip to content

Commit

Permalink
update to new pybrickschema API
Browse files Browse the repository at this point in the history
Update tests to new 0.2.0 brickschema API

In a few cases, switched from using rdflib.Graph to brickschema.Graph.
Using the new "brick" entailment profile supported by the package

update brickschema package minimum version

more fixing of tests

remove taglookup.pickle

rebuild tag lookup as the previous tests did

rebuild tags *before* doing inference

fixing tag inferences
  • Loading branch information
epaulson authored and gtfierro committed Jan 20, 2021
1 parent 3645bca commit c0194bd
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 53 deletions.
4 changes: 3 additions & 1 deletion bricksrc/equipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"Switchgear": {"tags": [TAG.Switchgear, TAG.Equipment]},
"Bus_Riser": {"tags": [TAG.Riser, TAG.Equipment]},
"Transformer": {"tags": [TAG.Transformer, TAG.Equipment]},
"Motor_Control_Center": {"tags": [TAG.Motor, TAG.Equipment]},
"Motor_Control_Center": {
"tags": [TAG.Motor, TAG.Equipment, TAG.Control, TAG.Center]
},
"Breaker_Panel": {"tags": [TAG.Breaker, TAG.Equipment]},
},
},
Expand Down
8 changes: 3 additions & 5 deletions bricksrc/quantities.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from brickschema.graph import Graph
from brickschema.inference import BrickInferenceSession
from rdflib import Literal, URIRef
from .namespaces import SKOS, OWL, RDFS, BRICK, QUDTQK, QUDTDV, QUDT, UNIT


g = Graph()
g.load_file("support/VOCAB_QUDT-QUANTITY-KINDS-ALL-v2.1.ttl")
g.load_file("support/VOCAB_QUDT-UNITS-ALL-v2.1.ttl")
g.g.bind("qudt", QUDT)
g.g.bind("qudtqk", QUDTQK)
sess = BrickInferenceSession()
g = sess.expand(g)
g.bind("qudt", QUDT)
g.bind("qudtqk", QUDTQK)
g.expand(profile="brick")


def get_units(brick_quantity):
Expand Down
20 changes: 17 additions & 3 deletions bricksrc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@
"CO_Differential_Sensor": {
"tags": [
TAG.Point,
TAG.CO2,
TAG.CO,
TAG.Differential,
TAG.Sensor,
],
Expand Down Expand Up @@ -842,7 +842,13 @@
},
},
"PM10_Sensor": {
"tags": [TAG.Point, TAG.Sensor, TAG.Particulate, TAG.Matter],
"tags": [
TAG.Point,
TAG.Sensor,
TAG.Particulate,
TAG.Matter,
TAG.PM10,
],
"substances": [
[BRICK.measures, BRICK.Air],
[BRICK.measures, BRICK.PM10_Concentration],
Expand All @@ -855,12 +861,19 @@
TAG.Sensor,
TAG.Particulate,
TAG.Matter,
TAG.PM10,
],
}
},
},
"PM25_Sensor": {
"tags": [TAG.Point, TAG.Sensor, TAG.Particulate, TAG.Matter],
"tags": [
TAG.Point,
TAG.Sensor,
TAG.Particulate,
TAG.Matter,
TAG.PM25,
],
"substances": [
[BRICK.measures, BRICK.Air],
[BRICK.measures, BRICK.PM25_Concentration],
Expand All @@ -873,6 +886,7 @@
TAG.Sensor,
TAG.Particulate,
TAG.Matter,
TAG.PM25,
],
}
},
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pytest>=5.4.3
tqdm>=4.46.1
pyshacl>=0.12.0
docker>=4.3.0
brickschema>=0.1.8
brickschema>=0.2.3
black==19.10b0
pre-commit==2.4.0
flake8==3.8.2
Expand Down
Binary file removed taglookup.pickle
Binary file not shown.
9 changes: 2 additions & 7 deletions tests/test_class_structure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import rdflib
import brickschema
from rdflib import RDF, OWL, RDFS, Namespace, BNode

Expand All @@ -9,13 +8,9 @@

BLDG = Namespace("https://brickschema.org/schema/ExampleBuilding#")

g = rdflib.Graph()
g = brickschema.Graph()
g.parse("Brick.ttl", format="turtle")

g = brickschema.inference.TagInferenceSession(
approximate=False, load_brick=False
).expand(g)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.expand("tag+owlrl")

g.bind("rdf", RDF)
g.bind("owl", OWL)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_generate_shacl.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys
from rdflib import Graph
import brickschema
from bricksrc.namespaces import A, OWL, RDFS, SKOS, BRICK, SH, BSH, bind_prefixes
from .util import make_readable

sys.path.append("..")
from bricksrc.properties import properties # noqa: E402

g = Graph()
g.parse("shacl/BrickShape.ttl", format="turtle")
g = brickschema.Graph()
g.load_file("shacl/BrickShape.ttl")
bind_prefixes(g)


Expand Down
12 changes: 5 additions & 7 deletions tests/test_hierarchy_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import brickschema
from tqdm import tqdm
from rdflib import URIRef, Graph
from rdflib import URIRef
from .util import make_readable
import sys

Expand Down Expand Up @@ -36,8 +36,8 @@

def test_hierarchyinference():
# Load the schema
g = Graph()
g.parse("Brick.ttl", format="turtle")
g = brickschema.Graph()
g.load_file("Brick.ttl")

# Get all the Classes with their restrictions.
qstr = (
Expand All @@ -62,10 +62,8 @@ def test_hierarchyinference():
# Infer classes of the entities.
# Apply reasoner
g.serialize("test.ttl", format="ttl")
g = brickschema.inference.TagInferenceSession(
approximate=False, load_brick=False, rebuild_tag_lookup=True
).expand(g)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.rebuild_tag_lookup()
g.expand(profile="tag+owlrl")
g.serialize(inference_file, format="turtle") # Store the inferred graph.

# Find all instances and their parents from the inferred graph.
Expand Down
9 changes: 3 additions & 6 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import RDF, RDFS, OWL, Namespace, Graph
from rdflib import RDF, RDFS, OWL, Namespace
import brickschema
from .util import make_readable
import sys
Expand All @@ -8,7 +8,7 @@

BLDG = Namespace("https://brickschema.org/schema/ExampleBuilding#")

g = Graph()
g = brickschema.Graph()
g.parse("Brick.ttl", format="turtle")

# Instances
Expand Down Expand Up @@ -60,10 +60,7 @@
g.add((BLDG.standalone, A, BRICK.Temperature_Sensor))

# Apply reasoner
g = brickschema.inference.TagInferenceSession(
load_brick=False, approximate=False
).expand(g)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.expand(profile="tag+owlrl")

g.bind("rdf", RDF)
g.bind("owl", OWL)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_measures_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def test_measurable_hierarchy():


def test_measures_infers():
g = rdflib.Graph()
g.parse("Brick.ttl", format="turtle")
g = brickschema.Graph()
g.load_file("Brick.ttl")

qstr = """select ?class ?o where {
?class rdfs:subClassOf+ brick:Class.
Expand All @@ -66,7 +66,7 @@ def test_measures_infers():

# Infer classes of the entities.
# Apply reasoner
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.expand(profile="owlrl")

qstr = """select ?instance ?class where {
?instance a ?class.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_no_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

BLDG = Namespace("https://brickschema.org/schema/ExampleBuilding#")

g = rdflib.Graph()
g.parse("Brick.ttl", format="turtle")
g = brickschema.Graph()
g.load_file("Brick.ttl")
g.bind("rdf", RDF)
g.bind("owl", OWL)
g.bind("rdfs", RDFS)
Expand Down Expand Up @@ -55,7 +55,7 @@
g.add((BLDG.TS1, BRICK.hasLocation, BLDG.Room1))

# lets us use both relationships
g = brickschema.inference.InverseEdgeInferenceSession(load_brick=False).expand(g)
g.expand(profile="owlrl")


def test_query_equipment():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

BLDG = Namespace("https://brickschema.org/schema/ExampleBuilding#")

g = rdflib.Graph()
g.parse("Brick.ttl", format="turtle")
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g = brickschema.Graph()
g.load_file("Brick.ttl")
g.expand(profile="owlrl")

res = g.query(
"""SELECT ?m ?class WHERE {
Expand All @@ -38,7 +38,7 @@
for m in measurables:
g.add((inst, BRICK.measures, m))

g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.expand(profile="owlrl")

g.bind("rdf", RDF)
g.bind("owl", OWL)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_subclass_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def test_cycles():
)
for s, o in res2:
loops.add("%s -> subClassOf -> %s" % (minify(s), minify(o)))
assert len(loops) == 0, f"Loops found in the class hierarchy!"
assert len(loops) == 0, "Loops found in the class hierarchy!"
18 changes: 9 additions & 9 deletions tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_quantity_has_one_quantitykind():
"""
g = brickschema.graph.Graph()
g.load_file("Brick.ttl")
g.g.bind("qudt", QUDT)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.bind("qudt", QUDT)
g.expand(profile="owlrl")
quantity_qk = g.query(
"SELECT ?quantity ?kind WHERE {\
?quantity a brick:Quantity .\
Expand Down Expand Up @@ -61,8 +61,8 @@ def test_instances_measure_correct_units():

g = brickschema.graph.Graph()
g.load_file("Brick.ttl")
g.g.bind("qudt", QUDT)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.bind("qudt", QUDT)
g.expand(profile="owlrl")

# test the definitions by making sure that some quantities have applicable
# units
Expand All @@ -80,7 +80,7 @@ def test_instances_measure_correct_units():
triples.append((BLDG[instance_name], A, brickclass))
triples.append((BLDG[instance_name], BRICK.hasUnit, unit))
g.add(*triples)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.expand(profile="owlrl")

instances = g.query(
"SELECT ?inst ?quantity ?unit WHERE {\
Expand All @@ -96,8 +96,8 @@ def test_instances_measure_correct_units():
def test_quantity_units():
g = brickschema.graph.Graph()
g.load_file("Brick.ttl")
g.g.bind("qudt", QUDT)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.bind("qudt", QUDT)
g.expand(profile="owlrl")

# test the definitions by making sure that some quantities have applicable
# units
Expand All @@ -112,8 +112,8 @@ def test_quantity_units():
def test_all_quantities_have_units():
g = brickschema.graph.Graph()
g.load_file("Brick.ttl")
g.g.bind("qudt", QUDT)
g = brickschema.inference.OWLRLInferenceSession(load_brick=False).expand(g)
g.bind("qudt", QUDT)
g.expand(profile="owlrl")

# test the definitions by making sure that some quantities have applicable
# units
Expand Down

0 comments on commit c0194bd

Please sign in to comment.