-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
298 lines (260 loc) · 12 KB
/
extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import itertools
import uuid
from functools import reduce
from SPARQLWrapper import SPARQLWrapper, JSON, POST
from mapping import Mapping
SPARQLQuery = SPARQLWrapper(
"http://localhost:3030/GenScen/query")
SPARQLRemove = SPARQLWrapper(
"http://localhost:3030/GenScen/update")
SPARQLInsert = SPARQLWrapper(
"http://localhost:3030/GenScen/update")
def _get_prefix():
return f'''
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX proj: <https://ensnare.nobatek.com/project#>
PREFIX bldg: <https://ensnare.nobatek.com/building#>
PREFIX intv: <https://ensnare.nobatek.com/intervention#>
'''
def insert_data(data):
"""Function to insert the data into the SPARQL database."""
# Check if the request body contains all the necessary parameters
# Check Parameters
all_parameters = ["euroregion", "sh.layout", "sh.fuel", "vent.system", "u.envelope",
"floorarea", "ndwellings", "type.window", "u.roofs"]
for key in all_parameters:
if key not in data['data']['Parameters']:
raise Exception(f"Parameters {key} not found in the request body")
# Check Surfaces
if "Surfaces" not in data['data']:
raise Exception(f"Parameters surfaces not found in the request body")
else:
# Check if each surface contains the necessary parameters
surface_parameters = ["type", "orientation", "area", "name"]
for key in surface_parameters:
for surface in data['data']['Surfaces']:
if key not in surface:
raise Exception(f"Parameters {key} not found in Surface {surface} in the request body")
if surface["type"] == "roof":
if "area.pv" not in surface:
raise Exception(f"Parameter area.pv not found in Surface {surface} in the request body")
# Building
mapping = Mapping()
query_content = f"""
tst:pjct{data['data']['project_id']} rdf:type proj:Project ;
{mapping.mapping_dict['project_id']} {data['data']['project_id']} ;
{mapping.mapping_dict['euroregion']} proj:{mapping.get_euroregion_name(data['data']['Parameters']['euroregion'])} .
tst:batiment-{data['data']['project_id']} rdf:type bldg:Building .
tst:pjct{data['data']['project_id']} proj:building tst:batiment-{data['data']['project_id']} ;
proj:targetThermal 1.0, 0.7, 0.5, 0.3 ;
proj:targetElectricity 1.0, 0.7, 0.5, 0.3 .
"""
# Facades
facade_statements = []
max_facade_area = 0
roof_area = 0
roof_insulation = 0
for surface in data['data']['Surfaces']:
# Type Wall
if surface['type'] == "wall":
orientation = mapping.get_orientation(int(surface['orientation']))
facade_area = float(surface['area'])
facade_statement = f"""
tst:batiment-{data['data']['project_id']} bldg:hasFacade [
bldg:area "{facade_area}"^^xsd:double ;
bldg:orientation "{orientation}" ;
bldg:facadeInsulation "{mapping.get_level(float(data['data']['Parameters']['u.envelope']))}" ;
] .
"""
facade_statements.append(facade_statement)
if facade_area > max_facade_area:
max_facade_area = facade_area
# Type Roof
elif surface['type'] == "roof":
roof_area = float(surface['area'])
roof_insulation = mapping.get_level(float(data['data']['Parameters']['u.roofs']))
query_content += "".join(facade_statement for facade_statement in facade_statements)
# Parameters
query_content += f"""
tst:batiment-{data['data']['project_id']} """
for key, value in data['data']['Parameters'].items():
if key in mapping.mapping_dict and key != "euroregion":
# Check if the value need to be a double, a string or an integer
if key == "ndwellings":
query_content += f"""{mapping.mapping_dict[key]} {value} ;"""
elif key == "sh.layout":
query_content += f"""{mapping.mapping_dict[key]} "{mapping.get_central_heating(value)}" ;"""
elif value.replace('.', '', 1).isdigit() or (key == "floorarea" and value.isdigit()):
query_content += f"""{mapping.mapping_dict[key]} "{float(value)}"^^xsd:double ;"""
else:
query_content += f"""{mapping.mapping_dict[key]} "{value}" ;"""
query_content += f"""
bldg:maxFacadeArea "{max_facade_area}"^^xsd:double ;
bldg:roofArea "{roof_area}"^^xsd:double ;
bldg:roofInsulation "{roof_insulation}" ."""
# Query construction
query = f"""
{_get_prefix() + "PREFIX tst: <https://nobatek.inef4.com/renovation/test#>" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"}
INSERT DATA
{{
{query_content}
}}
"""
SPARQLInsert.method= 'POST'
SPARQLInsert.setQuery(query)
result = SPARQLInsert.query()
return result.response.code
def get_baseline(project_id):
query = f"""
{_get_prefix()}
SELECT ?str_type_intv ?scen (group_concat(?str_intv; separator=" | ") as ?intvs) ?label WHERE {{
?scen a [rdfs:subClassOf* proj:BaselineScenario];
proj:forProject [proj:id {project_id}] ;
rdfs:label ?label ;
proj:isMadeOf ?intv .
?intv intv:refines ?type_intv ;
rdfs:label ?str_intv.
BIND(strafter(str(?type_intv), '#') as ?str_type_intv)
}} GROUP BY ?str_type_intv ?scen ?label
"""
return _format_basic(_execute(query), "baseline")
def get_nZeB(project_id):
query = f"""
{_get_prefix()}
SELECT ?str_type_intv ?scen (group_concat(?str_intv; separator=" | ") as ?intvs) ?label WHERE {{
?scen a [rdfs:subClassOf* proj:nZeBScenario];
proj:forProject [proj:id {project_id}] ;
proj:isMadeOf ?intv ;
rdfs:label ?label .
?intv intv:refines ?type_intv ;
rdfs:label ?str_intv.
BIND(strafter(str(?type_intv), '#') as ?str_type_intv)
}} GROUP BY ?str_type_intv ?scen ?label
"""
return _format_basic(_execute(query), "nZeB")
def get_Ensnare_Passive(project_id):
query = f"""
{_get_prefix()}
SELECT ?scen_passive ?intvs_passive ?scen_active (group_concat(DISTINCT ?active_desc; separator=" && ") as ?intvs_active) ?lbl_passive ?lbl_active
WHERE {{
?scen_active a [rdfs:subClassOf* proj:EnsnareScenario_Active];
proj:refines ?scen_passive ;
rdfs:label ?lbl_active .
?intv_active intv:scenario ?scen_active ;
intv:needed_surface ?surface ;
intv:facade [bldg:orientation ?orientation] ;
a ?intv_type .
BIND(concat(strafter(str(?intv_type), 'needed_'), '_', str(?orientation), ':', str(?surface)) as ?active_desc)
{{SELECT ?scen_passive (group_concat(DISTINCT ?res; separator=" || ") as ?intvs_passive) ?lbl_passive {{
?scen_passive a [rdfs:subClassOf* proj:EnsnareScenario_Passive];
proj:forProject [proj:id {project_id}] ;
a ?type ; proj:isMadeOf ?intv ;
rdfs:label ?lbl_passive .
?intv intv:refines ?type_intv ;
rdfs:label ?intv_desc .
BIND(concat(strafter(str(?type_intv), '#'), ':', ?intv_desc) as ?res)
FILTER(?type_intv IN (intv:InsulateFacade, intv:InsulateRoof, intv:ChangeWindows))
}} group by ?scen_passive ?lbl_passive}}
}} group by ?scen_active ?scen_passive ?intvs_passive ?lbl_active ?lbl_passive
"""
return _format_ensnare(_execute(query))
def _execute(query):
"""Function to execute a SPARQL query. Returns the resulting bindings.
@:param query: a string for the SPARQL query to execute.
@:returns a list of bindings."""
SPARQLQuery.setReturnFormat(JSON)
SPARQLQuery.setQuery(query)
try:
result = SPARQLQuery.queryAndConvert()
results = result['results']['bindings']
return results # {k: results[k]['value'] for k in results.keys()}
except Exception as e:
print(e)
def _format_basic(scenario, name):
"""Function to format a simple scenario. The input scenario should result from a SPARQL query on either
baseline or nZeB scenarios. Produces a dictionary.
@:param scenario: the scenario resulting from a SPARQL query.
@:param name: the name of the scenario. Should be either 'baseline' or 'nZeB'
@:returns a dictionary formatting the results in a friendly and comprehensive way, to produce a JSON file"""
desc_scenario = {}
for intv in scenario:
desc_scenario[intv['str_type_intv']['value']] = intv['intvs']['value']
desc_scenario["id"] = str(uuid.uuid1())
if name == "baseline":
desc_scenario["description"] = "Baseline scenario - low ambitions, no renewable"
elif name == "nZeB":
desc_scenario["description"] = "Positive scenario - high ambitions, as many renewable as possible"
return {name : desc_scenario}
def _format_ensnare(scenarios):
"""Function to format the resulting ENSNARE scenarios."""
results = []
passive = {}
active = {}
labels = {}
for scenario in scenarios:
# format passive
id_passive = scenario['scen_passive']['value']
desc_passive = scenario['lbl_passive']['value']
labels[id_passive] = desc_passive
if id_passive not in passive.keys():
passive[id_passive] = _format_passive(scenario['intvs_passive'])
# add active scenarios
if id_passive not in active:
active[id_passive] = []
active[id_passive].append(_format_active(scenario['intvs_active'], scenario['lbl_active']['value']))
# create the final JSON file
for id_passive in passive.keys():
for passive_scen in passive[id_passive]:
format_scenario = {'description': labels[id_passive]}
for it in passive_scen:
intervention, material = it.split(':')
format_scenario[intervention] = material
format_scenario['active'] = active[id_passive]
results.append(format_scenario)
return {"ensnare_scenarios" : results}
def _format_passive(intvs_passive):
intvs = intvs_passive['value'].split('||')
# create dictionary of intervention
passive_ = {}
for intv in intvs:
action, material = intv.strip().split(':')
if action in passive_.keys():
passive_[action].append(intv.strip())
else:
passive_[action] = [intv.strip()]
passive__ = list(map(lambda x: list(passive_[x]), passive_.keys()))
# compute all combinations
return list(itertools.product(*passive__))
def _format_active(intvs_active, description):
intvs = intvs_active['value'].split('&&')
# create dictionary of intervention
facades = {"id": str(uuid.uuid1()), "description": description}
for intv in intvs:
action, surface = intv.strip().split(':')
system, facade = action.split('_')
if facade not in facades:
facades[facade] = {}
facades[facade][system+'_area'] = float(surface)
return facades
def remove_data(project_id):
# SPARQLRemove.reset()
SPARQLRemove.setReturnFormat(JSON)
SPARQLRemove.setMethod(POST)
SPARQLRemove.setQuery(f"""
{_get_prefix()}
delete {{
?proj ?pred ?obj .
?bldg ?pred_ ?obj_ .
?fcd ?pred__ ?ojb__ .
}}
where {{
?proj proj:id {project_id} ;
proj:building ?bldg .
?bldg bldg:hasFacade ?fcd .
?proj ?pred ?obj .
?bldg ?pred_ ?obj_ .
?fcd ?pred__ ?ojb__ .
}}""")
results = SPARQLRemove.query()
return results