#!/usr/bin/env python3.5

from sys import argv
from rdflib import Graph, Namespace, URIRef, Literal

# guard: commandline arguments
if len(argv) != 3:
    print('Syntax: %s PATH_TO_BRICK_DATA_TTL_FILE OUTPUT_FILENAME' % argv[0])
    print('        %s ../../brick-data/src/brick-data.ttl ../var/floorviz.ttl' % argv[0])
    exit(1)
bd_filename = argv[1]
fv_filename = argv[2]

################################################################ namespaces ####

g = Graph()

# source namespaces
RDF  = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')
OWL  = Namespace('http://www.w3.org/2002/07/owl#')
g.bind('rdf' , RDF)
g.bind('rdfs', RDFS)
g.bind('owl' , OWL)

# load brick-data namespace
BD     = Namespace('https://brickschema.org/schema/1.0.1/BrickData#')
BDSMAP = Namespace('https://brickschema.org/schema/1.0.1/BrickDataSmap#')
BDS    = Namespace('https://brickschema.org/schema/1.0.1/BrickDataStatic#')
g.parse(bd_filename, format='turtle')

# floorviz namespace
FV = Namespace('http://asjo.dk/ontologies/floorviz/20180101#')
g.bind('fv', FV)

######################################################### modeling approach ####

FloorViz = FV['FloorViz']
g.add( (FloorViz, RDF.type, BD['Model_Approach']) )

######################################################## floorplan property ####

FloorPlan = FV['Floorplan']
g.add( (FloorPlan, RDFS.subClassOf, BD['Property']) )
g.add( (FloorViz, BD['hasProperty'], FloorPlan) )
g.add( (FloorPlan, RDFS.label, Literal("URL of svg file")) )

hasFloorPlan = FV['hasFloorplan']
g.add( (hasFloorPlan, RDFS.subPropertyOf, BD['hasData']) )
g.add( (hasFloorPlan, RDFS.label, Literal("Connects an entity to an fv:Floorplan")) )

############################################################# path property ####

Path = FV['Path']
g.add( (Path, RDFS.subClassOf, BD['Property']) )
g.add( (FloorViz, BD['hasProperty'], Path) )
g.add( (Path, RDFS.label, Literal("Path within an svg file")) )

hasPath = FV['hasPath']
g.add( (hasPath, RDFS.subPropertyOf, BD['hasData']) )
g.add( (hasPath, RDFS.label, Literal("Connects an entity to a path id in an svg file")) )

############################################################# area property ####

Area = FV['Area']
g.add( (Area, RDFS.subClassOf, BD['Property']) )
g.add( (FloorViz, BD['hasProperty'], Area) )

hasArea = FV['hasArea']
g.add( (hasArea, RDFS.subPropertyOf, BD['hasData']) )
g.add( (hasArea, RDFS.label, Literal("Connects an entity to an fv:Area")) )

MetricArea = FV['MetricArea']
g.add( (MetricArea, RDFS.subClassOf, Area) )
g.add( (MetricArea, RDFS.label, Literal("Metric area (m^2)")) )

############################################################## store result ####

# store result
print('Number of relations: %u' % (len(g)))
g.serialize(fv_filename, 'turtle')

