def test_haystack_inference(): data = pkgutil.get_data(__name__, "data/carytown.json").decode() raw_model = json.loads(data) brick_model = Graph(load_brick=True).from_haystack( "http://example.org/carytown", raw_model) points = brick_model.query("""SELECT ?p WHERE { ?p rdf:type/rdfs:subClassOf* brick:Point }""") assert len(points) == 17 equips = brick_model.query("""SELECT ?e WHERE { ?e rdf:type/rdfs:subClassOf* brick:Equipment }""") assert len(equips) == 4
def test_tagset_inference(): g = Graph(load_brick=False) data = pkgutil.get_data(__name__, "data/tags.ttl").decode() g.load_file(source=io.StringIO(data)) g.expand(profile="tag") afs1 = g.query("SELECT ?x WHERE { ?x rdf:type brick:Air_Flow_Sensor }") assert len(afs1) == 1 afsp1 = g.query("SELECT ?x WHERE { ?x rdf:type brick:Air_Flow_Setpoint }") assert len(afsp1) == 1 mafs1 = g.query( "SELECT ?x WHERE { ?x rdf:type brick:Max_Air_Flow_Setpoint_Limit }") assert len(mafs1) == 1
def test_tagset_inference(): session = TagInferenceSession(approximate=False) assert session is not None g = Graph(load_brick=False) data = pkgutil.get_data(__name__, "data/tags.ttl").decode() g.load_file(source=io.StringIO(data)) g = session.expand(g) afs1 = g.query("SELECT ?x WHERE { ?x rdf:type brick:Air_Flow_Sensor }") assert len(afs1) == 1 afsp1 = g.query("SELECT ?x WHERE { ?x rdf:type brick:Air_Flow_Setpoint }") assert len(afsp1) == 1 mafs1 = g.query( "SELECT ?x WHERE { ?x rdf:type brick:Max_Air_Flow_Setpoint_Limit }") assert len(mafs1) == 1
def test_rdfs_inference_subclass(): EX = Namespace("http://example.com/building#") graph = Graph(load_brick=True).from_triples([(EX["a"], RDF.type, BRICK.Temperature_Sensor)]) graph.expand(profile="rdfs") res1 = graph.query(f"""SELECT ?type WHERE {{ <{EX["a"]}> rdf:type ?type }}""") expected = [ BRICK.Point, BRICK.Class, BRICK.Sensor, RDFS.Resource, BRICK.Temperature_Sensor, ] # filter out BNodes res = [] for row in res1: row = tuple(filter(lambda x: not isinstance(x, BNode), row)) res.append(row) res = list(filter(lambda x: len(x) > 0, res)) assert len(res) == len(expected), f"Results were {res}" for expected_class in expected: assert ( expected_class, ) in res, f"{expected_class} not found in {res}"
def test_simplify(): g = Graph(load_brick=True) data = pkgutil.get_data(__name__, "data/test.ttl").decode() g.load_file(source=io.StringIO(data)) g.expand("brick", simplify=False, backend="owlrl") g.serialize("/tmp/test.ttl", format="ttl") q = "SELECT ?type WHERE { bldg:VAV2-4.ZN_T a ?type }" rows = list(g.query(q)) bnodes = [r[0] for r in rows if isinstance(r[0], rdflib.BNode)] assert len(bnodes) > 0 g.simplify() rows = list(g.query(q)) bnodes = [r[0] for r in rows if isinstance(r[0], rdflib.BNode)] assert len(bnodes) == 0
def test_owl_inference_tags(): EX = Namespace("http://example.com/building#") graph = Graph(load_brick=True).from_triples([(EX["a"], RDF.type, BRICK.Air_Flow_Setpoint)]) graph.expand(profile="owlrl", backend="owlrl") res1 = graph.query(f"""SELECT ?type WHERE {{ <{EX["a"]}> rdf:type ?type }}""") expected = [ # RDF.Resource, # RDFS.Resource, OWL.Thing, BRICK.Point, BRICK.Class, BRICK.Setpoint, BRICK.Flow_Setpoint, BRICK.Air_Flow_Setpoint, ] # filter out BNodes res1 = filter_bnodes(res1) assert set(res1) == set(map(lambda x: (x, ), expected)) res2 = graph.query(f"""SELECT ?tag WHERE {{ <{EX["a"]}> brick:hasTag ?tag }}""") expected = [ TAG.Point, TAG.Air, TAG.Flow, TAG.Setpoint, ] res2 = filter_bnodes(res2) assert set(res2) == set(map(lambda x: (x, ), expected))
def test_brick_inference(): g = Graph(load_brick=True) data = pkgutil.get_data(__name__, "data/brick_inference_test.ttl").decode() g.load_file(source=io.StringIO(data)) g.expand(profile="owlrl") g.expand(profile="tag") r = g.query("SELECT ?x WHERE { ?x rdf:type brick:Air_Temperature_Sensor }") # assert len(r) == 5 urls = set([str(row[0]) for row in r]) real_sensors = set([ "http://example.com/mybuilding#sensor1", "http://example.com/mybuilding#sensor2", "http://example.com/mybuilding#sensor3", "http://example.com/mybuilding#sensor4", "http://example.com/mybuilding#sensor5", ]) assert urls == real_sensors
def test_brick_to_vbis_inference_with_owlrl(): ALIGN = Namespace("https://brickschema.org/schema/Brick/alignments/vbis#") # input brick model; instances should have appropriate VBIS tags g = Graph(load_brick=True) data = pkgutil.get_data(__name__, "data/vbis_inference_test.ttl").decode() g.load_file(source=io.StringIO(data)) g.expand(profile="owlrl") g.expand(profile="vbis") test_cases = [ ("http://bldg#f1", "ME-Fa"), ("http://bldg#rtu1", "ME-ACU"), ] for (entity, vbistag) in test_cases: query = f"SELECT ?tag WHERE {{ <{entity}> <{ALIGN.hasVBISTag}> ?tag }}" res = list(g.query(query)) assert len(res) == 1 assert str(res[0][0]) == vbistag conforms, _, results = g.validate() assert conforms, results