-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathModel.swift
412 lines (344 loc) · 19.8 KB
/
Model.swift
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* The MIT License (MIT)
*
* Copyright (C) 2019, CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* 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 CoreData
internal struct ModelIdentifier {
static let entityName: String = "ManagedEntity"
static let entityPropertyName: String = "ManagedEntityProperty"
static let entityTagName: String = "ManagedEntityTag"
static let entityGroupName: String = "ManagedEntityGroup"
static let actionName: String = "ManagedAction"
static let actionPropertyName: String = "ManagedActionProperty"
static let actionTagName: String = "ManagedActionTag"
static let actionGroupName: String = "ManagedActionGroup"
static let relationshipName: String = "ManagedRelationship"
static let relationshipPropertyName: String = "ManagedRelationshipProperty"
static let relationshipTagName: String = "ManagedRelationshipTag"
static let relationshipGroupName: String = "ManagedRelationshipGroup"
}
internal struct Model {
/// A static reference to the managedObjectModel.
static var managedObjectModel: NSManagedObjectModel?
/// Creates a NSManagedObjectModel.
static func create() -> NSManagedObjectModel {
_ = Model.__once
return Model.managedObjectModel!
}
/// Constructs the model once.
private static var __once: () = {
// Entity
let entityDescription = NSEntityDescription()
var entityProperties : [Any] = []
entityDescription.name = ModelIdentifier.entityName
entityDescription.managedObjectClassName = ModelIdentifier.entityName
let entityPropertyDescription = NSEntityDescription()
var entityPropertyProperties : [Any] = []
entityPropertyDescription.name = ModelIdentifier.entityPropertyName
entityPropertyDescription.managedObjectClassName = ModelIdentifier.entityPropertyName
let entityTagDescription = NSEntityDescription()
var entityTagProperties : [Any] = []
entityTagDescription.name = ModelIdentifier.entityTagName
entityTagDescription.managedObjectClassName = ModelIdentifier.entityTagName
let entityGroupDescription = NSEntityDescription()
var entityGroupProperties : [Any] = []
entityGroupDescription.name = ModelIdentifier.entityGroupName
entityGroupDescription.managedObjectClassName = ModelIdentifier.entityGroupName
// Relationship
let relationshipDescription = NSEntityDescription()
var relationshipProperties : [Any] = []
relationshipDescription.name = ModelIdentifier.relationshipName
relationshipDescription.managedObjectClassName = ModelIdentifier.relationshipName
let relationshipPropertyDescription = NSEntityDescription()
var relationshipPropertyProperties : [Any] = []
relationshipPropertyDescription.name = ModelIdentifier.relationshipPropertyName
relationshipPropertyDescription.managedObjectClassName = ModelIdentifier.relationshipPropertyName
let relationshipTagDescription = NSEntityDescription()
var relationshipTagProperties : [Any] = []
relationshipTagDescription.name = ModelIdentifier.relationshipTagName
relationshipTagDescription.managedObjectClassName = ModelIdentifier.relationshipTagName
let relationshipGroupDescription = NSEntityDescription()
var relationshipGroupProperties : [Any] = []
relationshipGroupDescription.name = ModelIdentifier.relationshipGroupName
relationshipGroupDescription.managedObjectClassName = ModelIdentifier.relationshipGroupName
// Action
let actionDescription = NSEntityDescription()
var actionProperties : [Any] = []
actionDescription.name = ModelIdentifier.actionName
actionDescription.managedObjectClassName = ModelIdentifier.actionName
let actionPropertyDescription = NSEntityDescription()
var actionPropertyProperties : [Any] = []
actionPropertyDescription.name = ModelIdentifier.actionPropertyName
actionPropertyDescription.managedObjectClassName = ModelIdentifier.actionPropertyName
let actionTagDescription = NSEntityDescription()
var actionTagProperties : [Any] = []
actionTagDescription.name = ModelIdentifier.actionTagName
actionTagDescription.managedObjectClassName = ModelIdentifier.actionTagName
let actionGroupDescription = NSEntityDescription()
var actionGroupProperties : [Any] = []
actionGroupDescription.name = ModelIdentifier.actionGroupName
actionGroupDescription.managedObjectClassName = ModelIdentifier.actionGroupName
// nodeClass
let nodeClass = NSAttributeDescription()
nodeClass.name = "nodeClass"
nodeClass.attributeType = .integer64AttributeType
nodeClass.isOptional = false
entityProperties.append(nodeClass.copy() as! NSAttributeDescription)
actionProperties.append(nodeClass.copy() as! NSAttributeDescription)
relationshipProperties.append(nodeClass.copy() as! NSAttributeDescription)
// type
let type = NSAttributeDescription()
type.name = "type"
type.attributeType = .stringAttributeType
type.isOptional = false
entityProperties.append(type.copy() as! NSAttributeDescription)
actionProperties.append(type.copy() as! NSAttributeDescription)
relationshipProperties.append(type.copy() as! NSAttributeDescription)
// createdDate
let createdDate = NSAttributeDescription()
createdDate.name = "createdDate"
createdDate.attributeType = .dateAttributeType
createdDate.isOptional = false
entityProperties.append(createdDate.copy() as! NSAttributeDescription)
actionProperties.append(createdDate.copy() as! NSAttributeDescription)
relationshipProperties.append(createdDate.copy() as! NSAttributeDescription)
// property
let propertyName = NSAttributeDescription()
propertyName.name = "name"
propertyName.attributeType = .stringAttributeType
propertyName.isOptional = false
entityPropertyProperties.append(propertyName.copy() as! NSAttributeDescription)
actionPropertyProperties.append(propertyName.copy() as! NSAttributeDescription)
relationshipPropertyProperties.append(propertyName.copy() as! NSAttributeDescription)
let propertyValue = NSAttributeDescription()
propertyValue.name = "object"
propertyValue.attributeType = .transformableAttributeType
propertyValue.attributeValueClassName = "Any"
propertyValue.isOptional = false
propertyValue.allowsExternalBinaryDataStorage = true
entityPropertyProperties.append(propertyValue.copy() as! NSAttributeDescription)
actionPropertyProperties.append(propertyValue.copy() as! NSAttributeDescription)
relationshipPropertyProperties.append(propertyValue.copy() as! NSAttributeDescription)
let propertyRelationship = NSRelationshipDescription()
propertyRelationship.name = "node"
propertyRelationship.minCount = 1
propertyRelationship.maxCount = 1
propertyRelationship.isOptional = false
propertyRelationship.deleteRule = .noActionDeleteRule
let propertySetRelationship = NSRelationshipDescription()
propertySetRelationship.name = "propertySet"
propertySetRelationship.minCount = 0
propertySetRelationship.maxCount = 0
propertySetRelationship.isOptional = false
propertySetRelationship.deleteRule = .noActionDeleteRule
propertyRelationship.inverseRelationship = propertySetRelationship
propertySetRelationship.inverseRelationship = propertyRelationship
propertyRelationship.destinationEntity = entityDescription
propertySetRelationship.destinationEntity = entityPropertyDescription
entityPropertyProperties.append(propertyRelationship.copy() as! NSRelationshipDescription)
entityProperties.append(propertySetRelationship.copy() as! NSRelationshipDescription)
propertyRelationship.destinationEntity = relationshipDescription
propertySetRelationship.destinationEntity = relationshipPropertyDescription
relationshipPropertyProperties.append(propertyRelationship.copy() as! NSRelationshipDescription)
relationshipProperties.append(propertySetRelationship.copy() as! NSRelationshipDescription)
propertyRelationship.destinationEntity = actionDescription
propertySetRelationship.destinationEntity = actionPropertyDescription
actionPropertyProperties.append(propertyRelationship.copy() as! NSRelationshipDescription)
actionProperties.append(propertySetRelationship.copy() as! NSRelationshipDescription)
// tag
let tagName = NSAttributeDescription()
tagName.name = "name"
tagName.attributeType = .stringAttributeType
tagName.isOptional = false
entityTagProperties.append(tagName.copy() as! NSAttributeDescription)
actionTagProperties.append(tagName.copy() as! NSAttributeDescription)
relationshipTagProperties.append(tagName.copy() as! NSAttributeDescription)
let tagRelationship = NSRelationshipDescription()
tagRelationship.name = "node"
tagRelationship.minCount = 1
tagRelationship.maxCount = 1
tagRelationship.isOptional = false
tagRelationship.deleteRule = .noActionDeleteRule
let tagSetRelationship = NSRelationshipDescription()
tagSetRelationship.name = "tagSet"
tagSetRelationship.minCount = 0
tagSetRelationship.maxCount = 0
tagSetRelationship.isOptional = false
tagSetRelationship.deleteRule = .noActionDeleteRule
tagRelationship.inverseRelationship = tagSetRelationship
tagSetRelationship.inverseRelationship = tagRelationship
tagRelationship.destinationEntity = entityDescription
tagSetRelationship.destinationEntity = entityTagDescription
entityTagProperties.append(tagRelationship.copy() as! NSRelationshipDescription)
entityProperties.append(tagSetRelationship.copy() as! NSRelationshipDescription)
tagRelationship.destinationEntity = relationshipDescription
tagSetRelationship.destinationEntity = relationshipTagDescription
relationshipTagProperties.append(tagRelationship.copy() as! NSRelationshipDescription)
relationshipProperties.append(tagSetRelationship.copy() as! NSRelationshipDescription)
tagRelationship.destinationEntity = actionDescription
tagSetRelationship.destinationEntity = actionTagDescription
actionTagProperties.append(tagRelationship.copy() as! NSRelationshipDescription)
actionProperties.append(tagSetRelationship.copy() as! NSRelationshipDescription)
// group
let groupName = NSAttributeDescription()
groupName.name = "name"
groupName.attributeType = .stringAttributeType
groupName.isOptional = false
entityGroupProperties.append(groupName.copy() as! NSAttributeDescription)
actionGroupProperties.append(groupName.copy() as! NSAttributeDescription)
relationshipGroupProperties.append(groupName.copy() as! NSAttributeDescription)
let groupRelationship = NSRelationshipDescription()
groupRelationship.name = "node"
groupRelationship.minCount = 1
groupRelationship.maxCount = 1
groupRelationship.isOptional = false
groupRelationship.deleteRule = .noActionDeleteRule
let groupSetRelationship = NSRelationshipDescription()
groupSetRelationship.name = "groupSet"
groupSetRelationship.minCount = 0
groupSetRelationship.maxCount = 0
groupSetRelationship.isOptional = false
groupSetRelationship.deleteRule = .noActionDeleteRule
groupRelationship.inverseRelationship = groupSetRelationship
groupSetRelationship.inverseRelationship = groupRelationship
groupRelationship.destinationEntity = entityDescription
groupSetRelationship.destinationEntity = entityGroupDescription
entityGroupProperties.append(groupRelationship.copy() as! NSRelationshipDescription)
entityProperties.append(groupSetRelationship.copy() as! NSRelationshipDescription)
groupRelationship.destinationEntity = relationshipDescription
groupSetRelationship.destinationEntity = relationshipGroupDescription
relationshipGroupProperties.append(groupRelationship.copy() as! NSRelationshipDescription)
relationshipProperties.append(groupSetRelationship.copy() as! NSRelationshipDescription)
groupRelationship.destinationEntity = actionDescription
groupSetRelationship.destinationEntity = actionGroupDescription
actionGroupProperties.append(groupRelationship.copy() as! NSRelationshipDescription)
actionProperties.append(groupSetRelationship.copy() as! NSRelationshipDescription)
// Inverse relationship for Subjects -- B.
let actionSubjectSetRelationship = NSRelationshipDescription()
actionSubjectSetRelationship.name = "subjectSet"
actionSubjectSetRelationship.minCount = 0
actionSubjectSetRelationship.maxCount = 0
actionSubjectSetRelationship.isOptional = false
actionSubjectSetRelationship.deleteRule = .noActionDeleteRule
actionSubjectSetRelationship.destinationEntity = entityDescription
let actionSubjectRelationship = NSRelationshipDescription()
actionSubjectRelationship.name = "actionSubjectSet"
actionSubjectRelationship.minCount = 0
actionSubjectRelationship.maxCount = 0
actionSubjectRelationship.isOptional = false
actionSubjectRelationship.deleteRule = .noActionDeleteRule
actionSubjectRelationship.destinationEntity = actionDescription
actionSubjectRelationship.inverseRelationship = actionSubjectSetRelationship
actionSubjectSetRelationship.inverseRelationship = actionSubjectRelationship
entityProperties.append(actionSubjectRelationship.copy() as! NSRelationshipDescription)
actionProperties.append(actionSubjectSetRelationship.copy() as! NSRelationshipDescription)
// Inverse relationship for Subjects -- E.
// Inverse relationship for Objects -- B.
let actionObjectSetRelationship = NSRelationshipDescription()
actionObjectSetRelationship.name = "objectSet"
actionObjectSetRelationship.minCount = 0
actionObjectSetRelationship.maxCount = 0
actionObjectSetRelationship.isOptional = false
actionObjectSetRelationship.deleteRule = .noActionDeleteRule
actionObjectSetRelationship.destinationEntity = entityDescription
let actionObjectRelationship = NSRelationshipDescription()
actionObjectRelationship.name = "actionObjectSet"
actionObjectRelationship.minCount = 0
actionObjectRelationship.maxCount = 0
actionObjectRelationship.isOptional = false
actionObjectRelationship.deleteRule = .noActionDeleteRule
actionObjectRelationship.destinationEntity = actionDescription
actionObjectRelationship.inverseRelationship = actionObjectSetRelationship
actionObjectSetRelationship.inverseRelationship = actionObjectRelationship
entityProperties.append(actionObjectRelationship.copy() as! NSRelationshipDescription)
actionProperties.append(actionObjectSetRelationship.copy() as! NSRelationshipDescription)
// Inverse relationship for Objects -- E.
// Inverse relationship for Subjects -- B.
let relationshipSubjectSetRelationship = NSRelationshipDescription()
relationshipSubjectSetRelationship.name = "subject"
relationshipSubjectSetRelationship.minCount = 1
relationshipSubjectSetRelationship.maxCount = 1
relationshipSubjectSetRelationship.isOptional = true
relationshipSubjectSetRelationship.deleteRule = .noActionDeleteRule
relationshipSubjectSetRelationship.destinationEntity = entityDescription
let relationshipSubjectRelationship = NSRelationshipDescription()
relationshipSubjectRelationship.name = "relationshipSubjectSet"
relationshipSubjectRelationship.minCount = 0
relationshipSubjectRelationship.maxCount = 0
relationshipSubjectRelationship.isOptional = false
relationshipSubjectRelationship.deleteRule = .noActionDeleteRule
relationshipSubjectRelationship.destinationEntity = relationshipDescription
relationshipSubjectRelationship.inverseRelationship = relationshipSubjectSetRelationship
relationshipSubjectSetRelationship.inverseRelationship = relationshipSubjectRelationship
entityProperties.append(relationshipSubjectRelationship.copy() as! NSRelationshipDescription)
relationshipProperties.append(relationshipSubjectSetRelationship.copy() as! NSRelationshipDescription)
// Inverse relationship for Subjects -- E.
// Inverse relationship for Objects -- B.
let relationshipObjectSetRelationship = NSRelationshipDescription()
relationshipObjectSetRelationship.name = "object"
relationshipObjectSetRelationship.minCount = 1
relationshipObjectSetRelationship.maxCount = 1
relationshipObjectSetRelationship.isOptional = true
relationshipObjectSetRelationship.deleteRule = .noActionDeleteRule
relationshipObjectSetRelationship.destinationEntity = entityDescription
let relationshipObjectRelationship = NSRelationshipDescription()
relationshipObjectRelationship.name = "relationshipObjectSet"
relationshipObjectRelationship.minCount = 0
relationshipObjectRelationship.maxCount = 0
relationshipObjectRelationship.isOptional = false
relationshipObjectRelationship.deleteRule = .noActionDeleteRule
relationshipObjectRelationship.destinationEntity = relationshipDescription
relationshipObjectRelationship.inverseRelationship = relationshipObjectSetRelationship
relationshipObjectSetRelationship.inverseRelationship = relationshipObjectRelationship
entityProperties.append(relationshipObjectRelationship.copy() as! NSRelationshipDescription)
relationshipProperties.append(relationshipObjectSetRelationship.copy() as! NSRelationshipDescription)
// Inverse relationship for Objects -- E.
entityDescription.properties = entityProperties as! [NSPropertyDescription]
entityPropertyDescription.properties = entityPropertyProperties as! [NSPropertyDescription]
entityTagDescription.properties = entityTagProperties as! [NSPropertyDescription]
entityGroupDescription.properties = entityGroupProperties as! [NSPropertyDescription]
actionDescription.properties = actionProperties as! [NSPropertyDescription]
actionPropertyDescription.properties = actionPropertyProperties as! [NSPropertyDescription]
actionTagDescription.properties = actionTagProperties as! [NSPropertyDescription]
actionGroupDescription.properties = actionGroupProperties as! [NSPropertyDescription]
relationshipDescription.properties = relationshipProperties as! [NSPropertyDescription]
relationshipPropertyDescription.properties = relationshipPropertyProperties as! [NSPropertyDescription]
relationshipTagDescription.properties = relationshipTagProperties as! [NSPropertyDescription]
relationshipGroupDescription.properties = relationshipGroupProperties as! [NSPropertyDescription]
Model.managedObjectModel = NSManagedObjectModel()
Model.managedObjectModel?.entities = [
entityDescription,
entityPropertyDescription,
entityTagDescription,
entityGroupDescription,
relationshipDescription,
relationshipPropertyDescription,
relationshipTagDescription,
relationshipGroupDescription,
actionDescription,
actionPropertyDescription,
actionTagDescription,
actionGroupDescription
]
}()
}