-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.py
315 lines (239 loc) · 11.1 KB
/
database.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
##################################################################################
# #
# Copyright (c) 2020 AECgeeks #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
# #
##################################################################################
import os
from xmlrpc.client import Boolean
DEVELOPMENT = os.environ.get('environment', 'production').lower() == 'development'
NO_POSTGRES = os.environ.get('NO_POSTGRES', '0').lower() in {'1', 'true'}
"""
if not DEVELOPMENT:
from psycopg2cffi import compat
compat.register()
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
from sqlalchemy.inspection import inspect
from sqlalchemy import Column, Boolean, Integer, Float, String, DateTime, ForeignKey, Enum
from sqlalchemy_utils import database_exists, create_database
from sqlalchemy.orm import relationship
import datetime
if DEVELOPMENT or NO_POSTGRES:
file_path = os.path.join(os.path.dirname(__file__), "ifc-pipeline.db")
engine = create_engine(f'sqlite:///{file_path}', connect_args={'check_same_thread': False})
else:
host = os.environ.get('POSTGRES_HOST', 'localhost')
password = os.environ['POSTGRES_PASSWORD']
engine = create_engine(f"postgresql://postgres:{password}@{host}:5432/bimsurfer2")
Session = sessionmaker(bind=engine)
Base = declarative_base()
class Serializable(object):
def serialize(self, full=False):
# Transforms data from dataclasses to a dict,
# storing primary key of references and handling date format
d = {}
for attribute in inspect(self).attrs.keys():
if isinstance(getattr(self, attribute), (list, tuple)):
if full:
d[attribute] = [element.serialize(full) for element in getattr(self, attribute)]
else:
d[attribute] = [element.id for element in getattr(self, attribute)]
elif isinstance(getattr(self, attribute), datetime.datetime):
d[attribute] = getattr(self, attribute).strftime("%Y-%m-%d %H:%M:%S")
else:
d[attribute] = getattr(self, attribute)
return d
class user(Base, Serializable):
__tablename__ = 'users'
id = Column(String, primary_key=True)
email = Column(String)
family_name = Column(String)
given_name = Column(String)
name = Column(String)
models = relationship("model")
def __init__(self, id, email, family_name, given_name, name):
self.id = id
self.email = email
self.family_name = family_name
self.given_name = given_name
self.name = name
class model(Base, Serializable):
__tablename__ = 'models'
id = Column(Integer, primary_key=True)
code = Column(String)
filename = Column(String)
user_id = Column(String, ForeignKey('users.id'))
progress = Column(Integer, default=-1)
date = Column(DateTime, server_default=func.now())
license = Column(Enum('private','CC','MIT','GPL','LGPL', name='licenses_types'), default="private")
hours = Column(Float)
details = Column(String)
number_of_elements = Column(Integer)
number_of_geometries = Column(Integer)
number_of_properties = Column(Integer)
authoring_application = Column(String)
schema = Column(String)
size = Column(String)
mvd = Column(String)
# @todo rather these should be relations on the corresponding tasks
status_syntax = Column(Enum('n','v','w','i', name='status_types'), default='n')
status_schema = Column(Enum('n','v','w','i', name='status_types'), default='n')
status_bsdd = Column(Enum('n','v','w','i', name='status_types'), default='n')
status_mvd = Column(Enum('n','v','w','i', name='status_types'), default='n')
status_ids = Column(Enum('n','v','w','i', name='status_types'), default='n')
status_ia = Column(Enum('n','v','w','i', name='status_types'), default='n')
status_ip = Column(Enum('n','v','w','i', name='status_types'), default='n')
instances = relationship("ifc_instance")
tasks = relationship("validation_task")
deleted = Column(Integer, default=0)
# ALTER TABLE models ADD COLUMN commit_id VARCHAR(40);
commit_id = Column(String(40))
def __init__(self, code, filename, user_id, commit_id=None):
self.code = code
self.filename = filename
self.user_id = user_id
self.commit_id = commit_id
class validation_task(Base, Serializable):
__tablename__ = 'validation_tasks'
id = Column(Integer, primary_key=True)
task_type = Column(String(32))
validated_file = Column(Integer, ForeignKey('models.id'))
validation_start_time = Column(DateTime)
validation_end_time = Column(DateTime)
__mapper_args__ = {
"polymorphic_identity": "validation_task",
"polymorphic_on": task_type,
}
def __init__(self, validated_file):
self.validated_file = validated_file
class general_info_task(validation_task):
__mapper_args__ = {
"polymorphic_identity": "general_info_task",
}
class bsdd_validation_task(validation_task):
results = relationship("bsdd_result")
__mapper_args__ = {
"polymorphic_identity": "bsdd_validation_task",
}
class schema_validation_task(validation_task):
results = relationship("schema_result", order_by='schema_result.constraint_type.asc(),schema_result.attribute.asc()')
__mapper_args__ = {
"polymorphic_identity": "schema_validation_task",
}
class syntax_validation_task(validation_task):
results = relationship("syntax_result")
__mapper_args__ = {
"polymorphic_identity": "syntax_validation_task",
}
class gherkin_task(validation_task):
results = relationship("gherkin_result")
class informal_propositions_task(gherkin_task):
__mapper_args__ = {
"polymorphic_identity": "informal_propositions_task",
}
class implementer_agreements_task(gherkin_task):
__mapper_args__ = {
"polymorphic_identity": "implementer_agreements_task",
}
class gherkin_result(Base, Serializable):
__tablename__ = 'gherkin_result'
id = Column(Integer, primary_key=True)
task_id = Column(Integer, ForeignKey('validation_tasks.id'))
feature = Column(String)
feature_url = Column(String)
step = Column(String)
instance_id = Column(Integer, ForeignKey('instances.id'))
message = Column(String)
def __init__(self, task_id, feature, feature_url, step, instance_id, message):
self.task_id = task_id
self.feature = feature
self.feature_url = feature_url
self.step = step
self.instance_id = instance_id
self.message = message
class ifc_instance(Base, Serializable):
__tablename__ = 'instances'
id = Column(Integer, primary_key=True)
# @todo there should be the instance numeric id in here as well, in case of
# non-rooted instances.
global_id = Column(String)
file = Column(Integer, ForeignKey('models.id'))
ifc_type = Column(String)
bsdd_results = relationship("bsdd_result")
gherkin_results = relationship("gherkin_result")
def __init__(self, global_id, ifc_type, file):
self.global_id = global_id
self.ifc_type = ifc_type
self.file = file
class bsdd_result(Base, Serializable):
__tablename__ = 'bSDD_results'
id = Column(Integer, primary_key=True)
task_id = Column(Integer, ForeignKey('validation_tasks.id'))
instance_id = Column(Integer, ForeignKey('instances.id'))
domain_file = Column(String)
classification_file = Column(String)
classification_name = Column(String)
classification_code = Column(String)
classification_domain = Column(String)
bsdd_classification_uri = Column(String)
bsdd_property_uri = Column(String)
bsdd_property_constraint = Column(String)
bsdd_type_constraint = Column(String)
ifc_property_set = Column(String)
ifc_property_name = Column(String)
ifc_property_type = Column(String)
ifc_property_value = Column(String)
val_ifc_type = Column(Boolean)
val_property_set = Column(Boolean)
val_property_name = Column(Boolean)
val_property_type = Column(Boolean)
val_property_value = Column(Boolean)
def __init__(self, task_id):
self.task_id = task_id
class schema_result(Base, Serializable):
__tablename__ = 'schema_results'
id = Column(Integer, primary_key=True)
task_id = Column(Integer, ForeignKey('validation_tasks.id'))
msg = Column(String, default="msg")
constraint_type = Column(Enum("uncategorized", 'schema', 'global_rule', 'simpletype_rule', 'entity_rule', name='schema_constraint_types'), default="uncategorized")
attribute = Column(String)
instance_id = Column(Integer, ForeignKey('instances.id'))
def __init__(self, task_id):
self.task_id = task_id
class syntax_result(Base, Serializable):
__tablename__ = 'syntax_results'
id = Column(Integer, primary_key=True)
task_id = Column(Integer, ForeignKey('validation_tasks.id'))
msg = Column(String, default="msg")
error_type = Column(Enum("uncategorized", 'unexpected_token', 'unexpected_character', 'duplicate_name', name='syntax_error_types'), default="uncategorized")
lineno = Column(Integer)
column = Column(Integer)
def __init__(self, task_id):
self.task_id = task_id
def initialize():
if not database_exists(engine.url):
create_database(engine.url)
Base.metadata.create_all(engine)
if __name__ == "__main__" or DEVELOPMENT:
initialize()