Skip to content

Commit

Permalink
[project][infra][dvc][notebooking][mapping] init systemization
Browse files Browse the repository at this point in the history
  • Loading branch information
majidaldo committed Mar 17, 2022
0 parents commit a475b89
Show file tree
Hide file tree
Showing 23 changed files with 1,902 additions and 0 deletions.
1 change: 1 addition & 0 deletions .here
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# root dir stub
59 changes: 59 additions & 0 deletions mapping/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import rdflib

def get_223p():
from pathlib import Path
g = rdflib.Graph()
for f in (f for f in (Path('.') / 'reference-223p').iterdir() if f.suffix == '.ttl'):
g += rdflib.Graph().parse(f)
return g


#%%
namespaces = {
# 'xml': 'http://www.w3.org/XML/1998/namespace',
# 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
# 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
# 'xsd': 'http://www.w3.org/2001/XMLSchema#',
# 'owl': 'http://www.w3.org/2002/07/owl#',
# 'skos': 'http://www.w3.org/2004/02/skos/core#',
# 'qudt': 'http://qudt.org/schema/qudt/',
# 'shacl': 'http://www.w3.org/ns/shacl#',
# 'dc': 'http://purl.org/dc/terms#',
# 'schema': 'http://schema.org/',
# 'our' stuff
'brick': 'https://brickschema.org/schema/Brick#',
'bdg': 'http://example.org/building',
}



def namespace(g):
for p, iri in namespaces.items():
g.namespace_manager.bind(p, iri, override=True, replace=True)
return g

def fix(g):
# https://github.com/BrickSchema/Brick/issues/308
from rdflib import OWL as owl
from rdflib import RDF as rdf
from rdflib import URIRef
v=URIRef('https://brickschema.org/schema/Brick#value')
#g.remove( (v, rdf.type, owl.DatatypeProperty) )
g.remove( (v, rdf.type, owl.ObjectProperty) )
return g


#def mod(g):
# can do all this in the mapping
# add luminaire 'types'
#g.add()

def export(g, fn='mapped'):
from pathlib import Path
o = Path('.') / f'{fn}.ttl'
g.serialize(str(o), format='turtle')
return o.absolute()




79 changes: 79 additions & 0 deletions mapping/mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@


#%%
def get_obda():
from jinja2 import Environment, FileSystemLoader#, select_autoescape
env = Environment(loader=FileSystemLoader('.'))
import yaml
mapping = yaml.safe_load(open('mapping.yaml'))

def strip_comments(lines):
lines = lines.split('\n')
o = ""
for ln in lines:
o += ln.split('#', 1)[0]
o += ' ' # need a lil space sometimes
return o
for map in mapping['maps']:
map['target'] = strip_comments(map['target'])
map['source'] = strip_comments(map['source'])
return env.get_template('obda.jinja').render(**mapping)

def sqlmap(onto='ontology', fn='mapped'):
from pathlib import Path
from shutil import which
ontop = Path(which('ontop')).absolute()
from subprocess import run
# ontop materialize ^
# --properties sqldb.properties ^
# -m mapping.obda ^
# -t Brick.ttl ^
# --disable-reasoning ^
# -f turtle ^
# -o mapping
o = Path('.') / f'{fn}.ttl'
m = Path('.') / 'mapping.obda'
assert(m.exists())
onto = Path('.') / f'{onto}.ttl' if not onto.endswith('.ttl') else onto
assert(onto.exists())
p = Path('.') / 'sqldb.properties'
assert(p.exists())
if o.exists():
o.unlink()
import yaml
r = run([
ontop, 'materialize',
'--properties', str(p),
'-m', str(m),
'-t', str(onto),
'--disable-reasoning',
'-f', 'turtle',
'-o', fn,
'--db-password', 'sdfsdffsd'
], cwd=Path('.'),
shell=False, check=True,
)
assert(o.exists())
return o


def do():
from graph import get_223p, export
import rdflib
b = get_223p()
m = rdflib.Graph().parse('mapping.ttl')
# TODO fig out importing graphs
#for p,u in tuple(m.namespaces()): doesnt work. ontop complians
# if 'qudt' in str(u): m.parse(u)
o = b+m
export(o, 'ontology')
from mapping import get_obda
open('mapping.obda', 'w').write(get_obda())
m = sqlmap('ontology')
m = rdflib.Graph().parse(m)
g = m+o
return export(g)


if __name__ == '__main__':
do()
54 changes: 54 additions & 0 deletions mapping/mapping.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# also these are the prefixes that make their way into 'mapped.ttl'
# else either no prefix or 'ns1' thing
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix unit: <http://qudt.org/vocab/unit/> .
@prefix qkind: <http://qudt.org/vocab/quantitykind/> .
@prefix s223: <http://data.ashrae.org/standard223#> .
@prefix qudt: <http://qudt.org/schema/qudt/> .

# SPECIFICS
@prefix s223x: <http://example.org/s223x/> .

s223x:Lighting a s223:Domain.

s223x:Illumination a s223:Role;
s223:hasDomain s223x:Lighting.

s223:Luminaire s223:hasRole s223x:Illumination.

#s223x:LightingZone a s223:Zone; # or space?
# s223:hasDomain s223x:Lighting.

s223x:LightingSpace a s223:DomainSpace;
s223:hasDomain s223x:Lighting.


s223x:RatedPowerInput a s223:QuantifiableProperty;
a qkind:ElectricPower;
skos:definition "The rated power input of the entity".


# # color temp
s223x:CorrectedColorTemperature a s223:QuantifiableProperty;
a qkind:Temperature; # TODO: but it must be Kelvin. shacl shape?
skos:definition "corrected color temperature" .


# luminous flux http://qudt.org/vocab/quantitykind/LuminousFlux

s223x:GrossArea a qkind:Area.

s223x:Floor a s223:PhysicalSpace.
# physicalspaces contain. domainspaces enclose
#s223:contains s223x:Room.
s223x:Room a s223:PhysicalSpace.





# SPECIFICS
@prefix bdg: <http://example.org/building/> .

175 changes: 175 additions & 0 deletions mapping/mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
---
prefixes:
owl: http://www.w3.org/2002/07/owl#
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
s223: http://data.ashrae.org/standard223#
unit: http://qudt.org/vocab/unit/
qudt: http://qudt.org/schema/qudt/
s223x: http://example.org/s223x/
bdg: http://example.org/building/

maps:
- id: luminaire types
target: |
bdg:LuminaireType/{TypeMark} # keeping id as a 'name'
rdfs:label {TypeMark};
rdfs:subClassOf s223:Luminaire.
source:
# could not do select unique but only unique triples were created
select
TypeMark
from
LightingFixtureTypes
where TypeMark is not NULL

# - id: luminaire instances
# target: |
# bdg:Luminaire/{Id}
# rdfs:label {Id};
# a bdg:LuminaireType/{TypeMark};
# s223x:ratedPowerInput bdg:Luminaire/{Id}/Power;
# s223x:CorrectedColorTemperature bdg:Luminaire/{Id}/ColorTemperature;
# qudt:LuminousFlux bdg:Luminaire/{Id}/LuminousFlux.
# bdg:Luminaire/{Id}/Power
# qudt:value {Wattage};
# qudt:hasUnit unit:W.
# bdg:Luminaire/{Id}/ColorTemperature
# qudt:value {CorrectedColorTemperature};
# qudt:hasUnit unit:K.
# bdg:Luminaire/{Id}/LuminousFlux
# qudt:value {LuminousFlux};
# qudt:hasUnit unit:LM.
# source: |
# select
# LightingFixtures.Id as Id,
# LightingFixtureTypes.TypeMark as TypeMark,
# LightingFixtureTypes.Wattage as Wattage,
# LightingFixtureTypes.InitialColorTemperature as CorrectedColorTemperature,
# LightingFixtureTypes.LuminousFlux as LuminousFlux
# from
# LightingFixtures
# inner join
# LightingFixtureTypes
# on
# LightingFixtureTypes.Id=LightingFixtures.TypeId


# - id: room instances
# #blank node issues
# #https://github.com/ontop/ontop/issues/372
# #https://github.com/ontop/ontop/issues/303
# # graphdb doesn't like blank nodes
# # https://docs.brickschema.org/metadata/entity-properties.html
# target: |
# s223x:{RoomType} a s223x:Room.
# bdg:Room/{Id}
# rdfs:label {Number};
# a s223x:{RoomType};
# s223x:GrossArea bdg:Room/{Id}/Area.
# bdg:Room/{Id}/Area
# qudt:value {Area}; # no need to convert area value since it's a float in the db
# qudt:hasUnit unit:M2.
# source: |
# select
# Id,
# Number,
# replace(Name, ' ', '_')
# as RoomType,
# Area
# from
# Rooms


# - id: floor instances
# target: |
# bdg:Floor/{Id}
# rdfs:label {Name};
# a s223x:Floor .
# source:
# select
# Id,
# Name
# from
# Levels

# - id: level-room
# target: |
# bdg:Floor/{LevelIdentifier}
# # physicalspaces contain. domainspaces enclose
# s223:contains bdg:Room/{RoomsIdentifier} .
# source:
# select
# Levels.Id as LevelIdentifier,
# Rooms.Id as RoomsIdentifier
# from Levels
# inner join
# Rooms
# on
# Levels.Id=Rooms.Level

# - id: luminaire-room # physical relation
# target: bdg:Luminaire/{LuminaireIdentifier}
# s223:hasLocation bdg:Room/{RoomIdentifier} .
# source: |
# select
# LightingFixtures.Id as LuminaireIdentifier,
# Rooms.Id as RoomIdentifier
# from
# LightingFixtures
# inner join
# Rooms
# on
# LightingFixtures.LuminaireRoomParameter=Rooms.Number


# #physical spcs can contain domainspaces
# - id: lighting zones # control relation. physcial spc (room) 'encloses' lightingzone
# target:
# bdg:Room/{RoomIdentifier}
# s223:encloses bdg:LightingControlZone/{LightingControlZoneID}.
# bdg:LightingControlZone/{LightingControlZoneID}
# rdfs:label {LightingControlZoneID};
# a s223x:LightingSpace.
# source:
# select
# Rooms.Id as RoomIdentifier,
# substring(LightingControlZoneID, 1, 5) as LightingControlZoneID
# from
# LightingFixtures
# inner join
# Rooms
# on
# LightingFixtures.LuminaireRoomParameter=Rooms.Number

# - id: hvac zones
# target:
# bdg:HVACZone/{Id}
# rdfs:label {Name};
# a brick:HVAC_Zone.
# source: |
# select # really want ' select distinct' here
# HVACZones.Id as Id,
# HVACZones.Name as Name
# from
# HVACZones
# inner join
# Rooms
# on
# Rooms.ThermalZonetoRoom=HVACZones.Name

# - id: hvac zones - rooms
# target:
# bdg:HVACZone/{HVACZoneId}
# brick:hasPart bdg:Room/{RoomId}.
# source:
# select
# HVACZones.Id as HVACZoneId,
# Rooms.Id as RoomId
# from
# HVACZones
# inner join
# Rooms
# on
# HVACZones.Name=Rooms.ThermalZonetoRoom
8 changes: 8 additions & 0 deletions mapping/materialize.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ontop materialize ^
--properties sqldb.properties ^
-m mapping.obda ^
-t Brick.ttl ^
--disable-reasoning ^
-f turtle ^
-o mapping

Loading

0 comments on commit a475b89

Please sign in to comment.