-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
373 lines (308 loc) · 11.6 KB
/
model.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""Contains entities used for the app.
"""
class Entity:
"""The Item represents one entity in the model"""
pk = None
key = ''
unit = ''
keywords = ''
def set_id(self, pk):
self.pk = pk
def get_id(self):
return self.pk
def from_dict(self, d: dict):
self.pk = d.get('pk', '')
self.key = d.get('key', '')
self.unit = d.get('unit', '')
self.keywords = d.get('keywords', '')
def to_dict(self):
return {'pk': self.pk,
'key': self.key,
'unit': self.unit,
'keywords': self.keywords}
def extrakeys(self):
if self.key is not None:
return {'key': self.key}
return None
class Resource(Entity):
"""A simple resource entity, like a tool,a machine or a role.
"""
def __init__(self):
self.url = ''
self.name = ''
self.description = ''
self.image = ''
self.color = ''
self.category = ''
def contains(self, txt: str):
"""Returns true if the resource contains the text."""
txt = txt.lower()
if (txt in self.name.lower()
or txt in self.description.lower()
or txt in self.key.lower()) or txt in self.category.lower():
return True
return False
def to_dict(self):
return super().to_dict() | {
'url': self.url,
'name': self.name,
'description': self.description,
'image': self.image,
'category': self.category,
'color': self.color}
def from_dict(self, d: dict):
super().from_dict(d)
self.name = d.get('name', '')
self.description = d.get('description', '')
self.image = d.get('image', '')
self.color = d.get('color', '')
self.category = d.get('category', '')
class Part(Resource):
"""Represents a basic part in the technology.
"""
def __init__(self):
super().__init__()
self.bom = {}
def to_dict(self):
return super().to_dict() | {'bom': self.bom}
def from_dict(self, d: dict):
super().from_dict(d)
self.bom = d.get('bom', {})
@staticmethod
def extract_stepkey(composite_key: str) -> (str, str):
"""Returns the beginning of the key and the step number between parenthesis as a tuple."""
key = composite_key.strip()
if '(' in key:
key, step = key.split('(', 1)
if ')' in step:
step = step[:step.index(')')]
return key.strip(), step.strip()
else:
return composite_key.strip(), None
return key.strip(), None
class Action(Resource):
"""Represents a basic action in the technology.
"""
class Location(Resource):
"""Represents a location.
"""
class Machine(Resource):
"""Represents a machine used in manufacturing.
"""
class Consumable(Resource):
"""Represents consumables (mostly untracked amounts).
"""
class Role(Resource):
"""Represents a role, job position or qualification, not specific employee.
"""
class Tool(Resource):
"""Represents a tool necessary for manufacturing. Includes custom tools and generic tools.
"""
class Company(Resource):
"""Represents a company who is responsible for the step.
"""
class Step(Resource):
"""Represents one build step in the manufacturing process, that results in outputs.
The process itself uses the same tools and machines during all the subprocesses.
"""
def __init__(self):
super().__init__()
self.qcfailstep = None
self.cleanup_text = ''
self.prepare_text = ''
self.consumables = {}
self.acceptance = ''
self.actions = {}
self.inputparts = {}
self.outputparts = {}
self.tools = {}
self.machines = {}
self.roles = {}
self.location = None
self.company = None
self.final = False
self.start_after = {}
self.start_after_start = {}
self.unit_time_hours = 0
self.prepare_hours = 0
self.cooldown_hours = 0
def from_dict(self, d: dict):
super().from_dict(d)
self.inputparts = d.get('inputparts', {})
self.outputparts = d.get('outputparts', {})
self.final = d.get('final', False)
self.qcfailstep = d.get('qcfailstep', False)
self.tools = d.get('tools', {})
self.machines = d.get('machines', {})
self.roles = d.get('roles', {})
self.consumables = d.get('consumables', {})
self.location = d.get('location', None)
self.company = d.get('company', None)
self.actions = d.get('actions', {})
self.acceptance = d.get('acceptance', '')
self.prepare_text = d.get('prepare_text', '')
self.cleanup_text = d.get('cleanup_text', '')
self.start_after = d.get('start_after', {})
self.start_after_start = d.get('start_after_start', {})
self.unit_time_hours = d.get('unit_time_hours', 0)
self.prepare_hours = d.get('prepare_hours', 0)
self.cooldown_hours = d.get('cooldown_hours', 0)
def to_dict(self):
return super().to_dict() | {
'inputparts': self.inputparts,
'outputparts': self.outputparts,
'tools': self.tools,
'machines': self.machines,
'final': self.final,
'qcfailstep': self.qcfailstep,
'roles': self.roles,
'actions': self.actions,
'consumables': self.consumables,
'prepare_text': self.prepare_text,
'cleanup_text': self.cleanup_text,
'company': self.company,
'location': self.location,
'acceptance': self.acceptance,
'start_after': self.start_after,
'start_after_start': self.start_after_start,
'unit_time_hours': self.unit_time_hours,
'prepare_hours': self.prepare_hours,
'cooldown_hours': self.cooldown_hours}
def get_relations(self, key):
response = {}
if key in self.inputparts:
response['inputparts'] = True
if key in self.outputparts:
response['outputparts'] = True
if key in self.actions:
response['actions'] = True
if key in self.tools:
response['tools'] = True
if key in self.machines:
response['machines'] = True
if key in self.roles:
response['roles'] = True
if key in self.consumables:
response['consumables'] = True
if key in self.start_after:
response['start_after'] = True
if key in self.start_after_start:
response['start_after_start'] = True
if key == self.location:
response['location'] = True
if key == self.company:
response['company'] = True
return response
def has_relation_to(self, key):
return (key in self.inputparts or
key in self.outputparts or
key in self.tools or
key in self.actions or
key in self.roles or
key in self.machines or
key == self.location or
key == self.company or
key in self.start_after or
key in self.start_after_start)
def add_inputpart(self, partkey, amount):
if partkey in self.inputparts:
self.inputparts[partkey] += amount
else:
self.inputparts[partkey] = amount
def remove_inputpart(self, partkey):
if partkey in self.inputparts:
del self.inputparts[partkey]
def add_outputpart(self, partkey, amount):
if partkey in self.outputparts:
self.outputparts[partkey] += amount
else:
self.outputparts[partkey] = amount
def remove_outputpart(self, partkey):
if partkey in self.outputparts:
del self.outputparts[partkey]
def add_tool(self, partkey, amount):
if partkey in self.tools:
self.inputparts[partkey] += amount
else:
self.inputparts[partkey] = amount
def remove_tool(self, partkey):
if partkey in self.tools:
del self.tools[partkey]
def add_action(self, key, amount):
if key in self.actions:
self.inputparts[key] += amount
else:
self.inputparts[key] = amount
def remove_action(self, key):
if key in self.actions:
del self.actions[key]
def add_machine(self, partkey, amount):
if partkey in self.machines:
self.inputparts[partkey] += amount
else:
self.inputparts[partkey] = amount
def remove_machine(self, partkey):
if partkey in self.machines:
del self.machines[partkey]
def add_role(self, partkey, amount):
if partkey in self.roles:
self.roles[partkey] += amount
else:
self.roles[partkey] = amount
def remove_role(self, partkey):
if partkey in self.roles:
del self.roles[partkey]
def add_start_after(self, stepkey, amount):
self.start_after[stepkey] = amount
def remove_start_after(self, stepkey):
if stepkey in self.start_after:
del self.start_after[stepkey]
def add_start_after_start(self, stepkey, amount):
self.start_after_start[stepkey] = amount
def remove_start_after_start(self, stepkey):
if stepkey in self.start_after_start:
del self.start_after_start[stepkey]
def set_location(self, locationkey):
self.location = locationkey
def set_company(self, companykey):
self.company = companykey
def set_prepare_hours(self, prepare_hours: float):
self.prepare_hours = prepare_hours
def set_cooldown_hours(self, cooldown_hours: float):
self.cooldown_hours = cooldown_hours
def contains(self, txt: str):
"""Returns true if the step contains the text."""
if super().contains(txt):
return True
txt = txt.lower()
if (txt in self.name.lower()
or txt in self.description.lower()
or txt in self.acceptance.lower()
or txt in self.prepare_text.lower()
or txt in self.cleanup_text.lower()
or txt in self.key.lower()):
return True
if self.location is not None and txt in self.location.lower():
return True
if self.multi_partial_keymatch([self.inputparts, self.outputparts,
self.tools, self.actions,
self.machines, self.roles,
self.consumables], txt):
return True
return False
def partial_keymatch(self, dictionary, text) -> bool:
"""Returns true if any key or name or description in the dictionary contains the text."""
for key in dictionary.keys():
# if text in key or text in value.name or text in value.description:
# TODO it could be nice to look up every relation
# and check if it contains the text too, not just look for the keys
# but it could cause too much noise probably
if text in key.lower():
return True
return False
def multi_partial_keymatch(self, list_of_dicts: [dict], text: str) -> bool:
"""Returns true if any dictionary in the list has a key or name matching contains the text."""
for value in list_of_dicts:
if self.partial_keymatch(value, text):
return True
return False