-
Notifications
You must be signed in to change notification settings - Fork 5
/
SimpleModel.java
457 lines (364 loc) · 14.1 KB
/
SimpleModel.java
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package org.genericsystem.cv.classifier.newmodel;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.api.core.annotations.Components;
import org.genericsystem.api.core.annotations.InstanceClass;
import org.genericsystem.api.core.annotations.SystemGeneric;
import org.genericsystem.api.core.annotations.constraints.InstanceValueClassConstraint;
import org.genericsystem.api.core.annotations.constraints.PropertyConstraint;
import org.genericsystem.api.core.annotations.constraints.SingularConstraint;
import org.genericsystem.common.Generic;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ConsolidatedType.ConsolidatedInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.DocClassType.DocClassInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.DocType.DocInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ImgDocRel.ImgDocLink;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ImgPathType.ImgPathInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ImgRefreshTimestampType.ImgRefreshTimestampInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ImgTimestampType.ImgTimestampInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ImgType.ImgInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.LayoutType.LayoutInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.SupervisedType.SupervisedInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ZoneNumType.ZoneNumInstance;
import org.genericsystem.cv.classifier.newmodel.SimpleModel.ZoneType.ZoneInstance;
import org.opencv.core.Rect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Simple Model to store the data extracted from documents in Generic System.
*
* @author Pierrik Lassalas
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class SimpleModel {
private static final ObjectMapper mapper = new ObjectMapper();
/**
* DocClassType represents the type of all the classes.
*
* @author Pierrik Lassalas
*/
@SystemGeneric
@InstanceClass(DocClassInstance.class)
public static class DocClassType implements Generic {
@SystemGeneric
public static class DocClassInstance implements Generic {
public Snapshot<DocInstance> getAllDocInstances() {
return (Snapshot) getHolders(getRoot().find(DocType.class));
}
public Snapshot<LayoutInstance> getAllLayouts() {
return (Snapshot) getComposites().filter(composite -> getRoot().find(LayoutType.class).equals(composite.getMeta()));
}
public Snapshot<LayoutInstance> getAllLayoutLeaves() {
return getAllLayouts().filter(layout -> layout.getInheritings().isEmpty());
}
public LayoutInstance getLayoutRoot() {
return getAllLayouts().filter(layout -> layout.getSupers().isEmpty()).first();
}
public DocInstance addDocInstance(String name) {
return (DocInstance) addHolder(getRoot().find(DocType.class), name);
}
public DocInstance getDocInstance(String name) {
return (DocInstance) getHolder(getRoot().find(DocType.class), name);
}
public LayoutInstance addLayout(String json) {
return (LayoutInstance) addHolder(getRoot().find(LayoutType.class), Collections.emptyList(), json);
}
public LayoutInstance addLayout(String json, LayoutInstance... parents) {
return (LayoutInstance) addHolder(getRoot().find(LayoutType.class), Arrays.asList(parents), json);
}
public LayoutInstance getLayout(String json) {
return (LayoutInstance) getHolder(getRoot().find(LayoutType.class), json);
}
}
public Snapshot<DocClassInstance> getAllDocClasses() {
return (Snapshot) getInstances();
}
public DocClassInstance addDocClass(String name) {
return (DocClassInstance) addInstance(name);
}
public DocClassInstance getDocClass(String name) {
return (DocClassInstance) getInstance(name);
}
}
/**
* LayoutType represents the type of the attribute Layout on the {@link DocClassType}. Each element of the layout is a holder of type LayoutType. </br>
* The children/parents relationships are mapped using inheritance.
*
* @author Pierrik Lassalas
*/
@SystemGeneric
@Components(DocClassType.class)
@InstanceClass(LayoutInstance.class)
public static class LayoutType implements Generic {
@SystemGeneric
public static class LayoutInstance implements Generic {
public DocClassInstance getDocClassInstance() {
return (DocClassInstance) getBaseComponent();
}
}
}
/**
* ImgDocRel represents the relation (many to one) between an abstract {@link DocType} and a concrete {@link ImgType}.
*
* @author Pierrik Lassalas
*/
@SystemGeneric
@Components({ ImgType.class, DocType.class })
@SingularConstraint(value = 0)
@InstanceClass(ImgDocLink.class)
public static class ImgDocRel implements Generic {
@SystemGeneric
public static class ImgDocLink implements Generic {
public ImgInstance getImgInstance() {
return (ImgInstance) getComponent(0);
}
public DocInstance getDocInstance() {
return (DocInstance) getComponent(1);
}
}
public Snapshot<ImgDocLink> getAllImgDocLinks() {
return (Snapshot) getInstances();
}
public ImgDocLink addImgDocLink(String name, ImgInstance imgInstance, DocInstance docInstance) {
return (ImgDocLink) addInstance(name, imgInstance, docInstance);
}
public ImgDocLink getImgDocLink(ImgInstance imgInstance, DocInstance docInstance) {
return (ImgDocLink) getInstance(imgInstance, docInstance);
}
}
/**
* A DocType represents the "abstract" document, which can be linked to one or more {@link ImgType} through {@link ImgDocRel}. At this stage, duplicates of ImgType are removed.
*
* @author Pierrik Lassalas
*/
@SystemGeneric
@Components(DocClassType.class)
@InstanceClass(DocInstance.class)
public static class DocType implements Generic {
@SystemGeneric
public static class DocInstance implements Generic {
public DocClassInstance getDocClassInstance() {
return (DocClassInstance) getBaseComponent();
}
public Snapshot<ImgDocLink> getAllImgDocLinks() {
return (Snapshot) getLinks(getRoot().find(ImgDocRel.class));
}
public Snapshot<ImgInstance> getAllLinkedImgs() {
return getAllImgDocLinks().map(link -> link.getImgInstance());
}
public ImgDocLink addImgDocLink(String name, ImgInstance imgInstance) {
return (ImgDocLink) addLink(getRoot().find(ImgDocRel.class), name, imgInstance);
}
public ImgDocLink getImgDocLink(ImgInstance imgInstance) {
return (ImgDocLink) getLink(getRoot().find(ImgDocRel.class), imgInstance);
}
}
}
/**
* An ImgType represents the actual image of the processed document. Duplicates of the same document can exist as multiple instances of this class.
*
* @author Pierrik Lassalas
*/
@SystemGeneric
@InstanceClass(ImgInstance.class)
@InstanceValueClassConstraint(String.class)
public static class ImgType implements Generic {
@SystemGeneric
public static class ImgInstance implements Generic {
public Snapshot<ZoneInstance> getZoneInstances() {
return (Snapshot) getHolders(getRoot().find(ZoneType.class));
}
public Snapshot<ZoneInstance> getEmptyZoneInstances() {
return getZoneInstances().filter(zone -> {
ConsolidatedInstance consolidated = zone.getConsolidated();
return !isConsolidated(consolidated);
});
}
public Snapshot<ZoneInstance> getConsolidatedZoneInstances() {
return getZoneInstances().filter(zone -> {
ConsolidatedInstance consolidated = zone.getConsolidated();
return isConsolidated(consolidated);
});
}
private boolean isConsolidated(ConsolidatedInstance consolidated) {
return consolidated != null && consolidated.getValue() != null && !"".equals(consolidated.getValue());
}
public ZoneInstance addZone(Rect rect) {
try {
String json = mapper.writeValueAsString(rect);
return (ZoneInstance) addHolder(getRoot().find(ZoneType.class), json);
} catch (JsonProcessingException e) {
throw new IllegalStateException("An error has occured while converting the rectangle to a json string", e);
}
}
public ZoneInstance getZone(Rect rect) {
try {
String json = mapper.writeValueAsString(rect);
return getZone(json);
} catch (JsonProcessingException e) {
throw new IllegalStateException("An error has occured while converting the rectangle to a json string", e);
}
}
public ZoneInstance addZone(String json) {
return (ZoneInstance) addHolder(getRoot().find(ZoneType.class), json);
}
public ZoneInstance getZone(String json) {
return (ZoneInstance) getHolder(getRoot().find(ZoneType.class), json);
}
public ImgPathInstance setImgPath(String relativePath) {
return (ImgPathInstance) setHolder(getRoot().find(ImgPathType.class), relativePath);
}
public ImgPathInstance getImgPath() {
return (ImgPathInstance) getHolder(getRoot().find(ImgPathType.class));
}
public ImgTimestampInstance setImgTimestamp(Long timestamp) {
return (ImgTimestampInstance) setHolder(getRoot().find(ImgTimestampType.class), timestamp);
}
public ImgTimestampInstance getImgTimestamp() {
return (ImgTimestampInstance) getHolder(getRoot().find(ImgTimestampType.class));
}
public ImgRefreshTimestampInstance setImgRefreshTimestamp(Long timestamp) {
return (ImgRefreshTimestampInstance) setHolder(getRoot().find(ImgRefreshTimestampType.class), timestamp);
}
public ImgRefreshTimestampInstance getImgRefreshTimestamp() {
return (ImgRefreshTimestampInstance) getHolder(getRoot().find(ImgRefreshTimestampType.class));
}
public ImgDocLink addImgDocLink(String name, DocInstance docInstance) {
return (ImgDocLink) addLink(getRoot().find(ImgDocRel.class), name, docInstance);
}
public ImgDocLink getImgDocLink() {
return (ImgDocLink) getLink(getRoot().find(ImgDocRel.class));
}
public DocInstance getLinkedDoc() {
return getImgDocLink().getDocInstance();
}
}
public Snapshot<ImgInstance> getImgInstances() {
return (Snapshot) getInstances();
}
public ImgInstance addImg(String name) {
return (ImgInstance) addInstance(name);
}
public ImgInstance getImg(String name) {
return (ImgInstance) getInstance(name);
}
}
@SystemGeneric
@Components(ImgType.class)
@InstanceClass(ZoneInstance.class)
@InstanceValueClassConstraint(String.class)
public static class ZoneType implements Generic {
// TODO: might need to change the value of the instance: String not ideal, since there is no direct comparison of the json objects
@SystemGeneric
public static class ZoneInstance implements Generic {
public ImgInstance getImgInstance() {
return (ImgInstance) getBaseComponent();
}
public ZoneNumInstance setZoneNum(int num) {
return (ZoneNumInstance) setHolder(getRoot().find(ZoneNumType.class), num);
}
public ZoneNumInstance getZoneNum() {
return (ZoneNumInstance) getHolder(getRoot().find(ZoneNumType.class));
}
public ConsolidatedInstance setConsolidated(String consolidated) {
return (ConsolidatedInstance) setHolder(getRoot().find(ConsolidatedType.class), consolidated);
}
public ConsolidatedInstance getConsolidated() {
return (ConsolidatedInstance) getHolder(getRoot().find(ConsolidatedType.class));
}
public SupervisedInstance setSupervised(String supervised) {
return (SupervisedInstance) setHolder(getRoot().find(SupervisedType.class), supervised);
}
public SupervisedInstance getSupervised() {
return (SupervisedInstance) getHolder(getRoot().find(SupervisedType.class));
}
public Rect getZoneRect() {
try {
Rect rect = mapper.readValue(getValue().toString(), Rect.class);
return rect;
} catch (IOException e) {
throw new IllegalStateException("An error has occured while converting the json to a rectangle", e);
}
}
}
}
@SystemGeneric
@Components(ZoneType.class)
@PropertyConstraint
@InstanceClass(ZoneNumInstance.class)
@InstanceValueClassConstraint(Integer.class)
public static class ZoneNumType implements Generic {
@SystemGeneric
public static class ZoneNumInstance implements Generic {
public ZoneInstance getZoneInstance() {
return (ZoneInstance) getBaseComponent();
}
}
}
@SystemGeneric
@Components(ZoneType.class)
@PropertyConstraint
@InstanceClass(ConsolidatedInstance.class)
@InstanceValueClassConstraint(String.class)
public static class ConsolidatedType implements Generic {
@SystemGeneric
public static class ConsolidatedInstance implements Generic {
public ZoneInstance getZoneInstance() {
return (ZoneInstance) getBaseComponent();
}
}
}
@SystemGeneric
@Components(ZoneType.class)
@PropertyConstraint
@InstanceClass(SupervisedInstance.class)
@InstanceValueClassConstraint(String.class)
public static class SupervisedType implements Generic {
@SystemGeneric
public static class SupervisedInstance implements Generic {
public ZoneInstance getZoneInstance() {
return (ZoneInstance) getBaseComponent();
}
}
}
@SystemGeneric
@Components(ImgType.class)
@PropertyConstraint
@InstanceClass(ImgPathInstance.class)
@InstanceValueClassConstraint(String.class)
public static class ImgPathType implements Generic {
@SystemGeneric
public static class ImgPathInstance implements Generic {
public ImgInstance getImgInstance() {
return (ImgInstance) getBaseComponent();
}
}
}
@SystemGeneric
@Components(ImgType.class)
@PropertyConstraint
@InstanceClass(ImgTimestampInstance.class)
@InstanceValueClassConstraint(Long.class)
public static class ImgTimestampType implements Generic {
@SystemGeneric
public static class ImgTimestampInstance implements Generic {
public ImgInstance getImgInstance() {
return (ImgInstance) getBaseComponent();
}
}
}
@SystemGeneric
@Components(ImgType.class)
@PropertyConstraint
@InstanceClass(ImgRefreshTimestampInstance.class)
@InstanceValueClassConstraint(Long.class)
public static class ImgRefreshTimestampType implements Generic {
@SystemGeneric
public static class ImgRefreshTimestampInstance implements Generic {
public ImgInstance getImgInstance() {
return (ImgInstance) getBaseComponent();
}
}
}
}