From c0194bd5a3985d3e0a8c2a758f1f8e5b832b7927 Mon Sep 17 00:00:00 2001 From: Erik Paulson Date: Tue, 19 Jan 2021 15:16:25 -0600 Subject: [PATCH] update to new pybrickschema API 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 --- bricksrc/equipment.py | 4 +++- bricksrc/quantities.py | 8 +++----- bricksrc/sensor.py | 20 +++++++++++++++++--- requirements.txt | 2 +- taglookup.pickle | Bin 65 -> 0 bytes tests/test_class_structure.py | 9 ++------- tests/test_generate_shacl.py | 6 +++--- tests/test_hierarchy_inference.py | 12 +++++------- tests/test_inference.py | 9 +++------ tests/test_measures_inference.py | 6 +++--- tests/test_no_inference.py | 6 +++--- tests/test_quantities.py | 8 ++++---- tests/test_subclass_hierarchy.py | 2 +- tests/test_units.py | 18 +++++++++--------- 14 files changed, 57 insertions(+), 53 deletions(-) delete mode 100644 taglookup.pickle diff --git a/bricksrc/equipment.py b/bricksrc/equipment.py index 7a8a3683..d9f68366 100644 --- a/bricksrc/equipment.py +++ b/bricksrc/equipment.py @@ -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]}, }, }, diff --git a/bricksrc/quantities.py b/bricksrc/quantities.py index c4cf4173..6fe116d7 100644 --- a/bricksrc/quantities.py +++ b/bricksrc/quantities.py @@ -1,5 +1,4 @@ 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 @@ -7,10 +6,9 @@ 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): diff --git a/bricksrc/sensor.py b/bricksrc/sensor.py index f451d7d8..3cd84423 100644 --- a/bricksrc/sensor.py +++ b/bricksrc/sensor.py @@ -762,7 +762,7 @@ "CO_Differential_Sensor": { "tags": [ TAG.Point, - TAG.CO2, + TAG.CO, TAG.Differential, TAG.Sensor, ], @@ -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], @@ -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], @@ -873,6 +886,7 @@ TAG.Sensor, TAG.Particulate, TAG.Matter, + TAG.PM25, ], } }, diff --git a/requirements.txt b/requirements.txt index bc311a38..2077dd34 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/taglookup.pickle b/taglookup.pickle deleted file mode 100644 index f4a5f43a2f23389d19a67635b8db8be588b5ea13..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 65 zcmZo*nQF!W0X^Kw`8heM$t9WjdBszDxKmQo5=(PRQZkcErc9pF!;w^)nNyMpl434S M1qroI37Vn@0J=sPN&o-= diff --git a/tests/test_class_structure.py b/tests/test_class_structure.py index a7b4a16d..77751e02 100644 --- a/tests/test_class_structure.py +++ b/tests/test_class_structure.py @@ -1,5 +1,4 @@ import sys -import rdflib import brickschema from rdflib import RDF, OWL, RDFS, Namespace, BNode @@ -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) diff --git a/tests/test_generate_shacl.py b/tests/test_generate_shacl.py index 0c0b0f28..79943cef 100644 --- a/tests/test_generate_shacl.py +++ b/tests/test_generate_shacl.py @@ -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) diff --git a/tests/test_hierarchy_inference.py b/tests/test_hierarchy_inference.py index 8e6b0a20..3140b6b2 100644 --- a/tests/test_hierarchy_inference.py +++ b/tests/test_hierarchy_inference.py @@ -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 @@ -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 = ( @@ -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. diff --git a/tests/test_inference.py b/tests/test_inference.py index 7257fac2..a1e134b4 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -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 @@ -8,7 +8,7 @@ BLDG = Namespace("https://brickschema.org/schema/ExampleBuilding#") -g = Graph() +g = brickschema.Graph() g.parse("Brick.ttl", format="turtle") # Instances @@ -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) diff --git a/tests/test_measures_inference.py b/tests/test_measures_inference.py index b2662183..bbcfd73b 100644 --- a/tests/test_measures_inference.py +++ b/tests/test_measures_inference.py @@ -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. @@ -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. diff --git a/tests/test_no_inference.py b/tests/test_no_inference.py index c81075c9..51d6eec2 100644 --- a/tests/test_no_inference.py +++ b/tests/test_no_inference.py @@ -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) @@ -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(): diff --git a/tests/test_quantities.py b/tests/test_quantities.py index ae61eca6..a0d4df68 100644 --- a/tests/test_quantities.py +++ b/tests/test_quantities.py @@ -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 { @@ -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) diff --git a/tests/test_subclass_hierarchy.py b/tests/test_subclass_hierarchy.py index 4b2a6ca8..dc30353f 100644 --- a/tests/test_subclass_hierarchy.py +++ b/tests/test_subclass_hierarchy.py @@ -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!" diff --git a/tests/test_units.py b/tests/test_units.py index 24ec39b8..9ac4bfa9 100644 --- a/tests/test_units.py +++ b/tests/test_units.py @@ -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 .\ @@ -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 @@ -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 {\ @@ -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 @@ -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