-
Notifications
You must be signed in to change notification settings - Fork 5
/
DocPropertiesSwitcher.java
177 lines (156 loc) · 6.98 KB
/
DocPropertiesSwitcher.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
package org.genericsystem.ir.app.gui.utils;
import java.io.File;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.comparator.ImgFilterFunction;
import org.genericsystem.cv.model.Doc;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass.DocClassInstance;
import org.genericsystem.cv.model.ImgFilter;
import org.genericsystem.cv.model.ImgFilter.ImgFilterInstance;
import org.genericsystem.cv.model.ZoneGeneric;
import org.genericsystem.cv.model.ZoneGeneric.ZoneInstance;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
import org.genericsystem.ir.DistributedVerticle;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.context.TagSwitcher;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
public class DocPropertiesSwitcher {
private static final String ZONES_FILE_BASE_PATH = DistributedVerticle.BASE_PATH + "/classes/";
public static class DOC_CLASS_EMPTY implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isDocClassEmpty(context, false);
}
}
public static class DOC_CLASS_NOT_EMPTY implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isDocClassEmpty(context, true);
}
}
public static class SUPERVISION_AVAILABLE implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isSupervisionAvailable(context, false);
}
}
public static class SUPERVISION_NOT_AVAILABLE implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isSupervisionAvailable(context, true);
}
}
public static class DOC_DEZONED implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isClassZoneFilePresent(context, false);
}
}
public static class DOC_NOT_DEZONED implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isClassZoneFilePresent(context, true);
}
}
public static class DOC_OCRD implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isDocOcrd(context, false);
}
}
public static class DOC_NOT_OCRD implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isDocOcrd(context, true);
}
}
public static class DOC_SUPERVISED implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isDocSupervised(context, false);
}
}
public static class DOC_NOT_SUPERVISED implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return isDocSupervised(context, true);
}
}
public static ObservableValue<Boolean> isDocClassEmpty(Context context, boolean reverse) {
DocClassInstance currentDocClass = (DocClassInstance) context.getGeneric();
Doc doc = context.getGeneric().getRoot().find(Doc.class);
ObservableList<Generic> docInstances = currentDocClass.getHolders(doc).toObservableList();
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
return null == docInstances || docInstances.isEmpty();
}, docInstances);
return reverse ? binding.not() : binding;
}
public static ObservableValue<Boolean> isSupervisionAvailable(Context context, boolean reverse) {
DocClassInstance currentDocClass = (DocClassInstance) context.getGeneric();
BooleanBinding binding = getBooleanBinding(currentDocClass.getValue().toString());
return reverse ? binding.not() : binding;
}
public static ObservableValue<Boolean> isClassZoneFilePresent(Context context, boolean reverse) {
DocInstance currentDoc = (DocInstance) context.getGeneric();
DocClassInstance docClassInstance = currentDoc.getDocClass();
BooleanBinding binding = getBooleanBinding(docClassInstance.getValue().toString());
return reverse ? binding.not() : binding;
}
private static BooleanBinding getBooleanBinding(String docClass) {
ObjectProperty<File> file = new SimpleObjectProperty<>(new File(ZONES_FILE_BASE_PATH + docClass + "/zones/zones.json"));
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
return null != file.get() && file.get().exists();
}, file);
return binding;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ObservableValue<Boolean> isDocOcrd(Context context, boolean reverse) {
DocInstance currentDoc = (DocInstance) context.getGeneric();
Root root = currentDoc.getRoot();
ZoneText zoneText = root.find(ZoneText.class);
ZoneGeneric zoneGeneric = root.find(ZoneGeneric.class);
// The original image should ALWAYS exist
ImgFilterInstance imgFilterInstance = ((ImgFilter) root.find(ImgFilter.class)).getImgFilter(ImgFilterFunction.ORIGINAL.getName());
ObservableList<ZoneTextInstance> zoneTextInstances = (ObservableList) currentDoc.getHolders(zoneText).toObservableList();
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
Snapshot<ZoneInstance> zoneInstances = (Snapshot) currentDoc.getDocClass().getHolders(zoneGeneric);
// Consider the document as not OCR'd when the class was not de-zoned
if (zoneInstances.isEmpty())
return false;
else { // Otherwise, return true only when all the zones have been processed
return zoneInstances.stream().allMatch(zoneInstance -> {
ZoneTextInstance zti = zoneText.getZoneText(currentDoc, zoneInstance, imgFilterInstance);
// System.out.println(String.format("doc: %s | zone: %s | filter: %s | zti: %s", currentDoc, zoneInstance, imgFilterInstance, zti));
return null != zti;
});
}
}, zoneTextInstances);
return reverse ? binding.not() : binding;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static ObservableValue<Boolean> isDocSupervised(Context context, boolean reverse) {
// TODO: will a document be considered as not supervised if a field needs to be left empty?
DocInstance currentDoc = (DocInstance) context.getGeneric();
Root root = currentDoc.getRoot();
ZoneText zoneText = root.find(ZoneText.class);
ObservableList<ZoneTextInstance> zoneTextInstances = (ObservableList) currentDoc.getHolders(zoneText).toObservableList().filtered(zt -> "reality".equals(((ZoneTextInstance) zt).getImgFilter().getValue()));
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
if (zoneTextInstances.isEmpty()) {
return false;
} else {
// If any field is empty, return false otherwise true
return !zoneTextInstances.stream().anyMatch(g -> "".equals(g.getValue().toString()));
}
}, zoneTextInstances);
return reverse ? binding.not() : binding;
}
}