-
Notifications
You must be signed in to change notification settings - Fork 5
/
DocPropertiesSwitcher.java
160 lines (141 loc) · 6.26 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
package org.genericsystem.ir.app.gui.utils;
import java.io.File;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
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.ZoneGeneric;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
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 {
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();
ObjectProperty<File> file = new SimpleObjectProperty<>(new File(System.getProperty("user.dir") + "/../gs-cv/classes/" + currentDocClass.getValue().toString() + "/zones/zones.json"));
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
return null != file.get() && file.get().exists();
}, file);
return reverse ? binding.not() : binding;
}
public static ObservableValue<Boolean> isClassZoneFilePresent(Context context, boolean reverse) {
DocInstance currentDoc = (DocInstance) context.getGeneric();
DocClassInstance docClassInstance = currentDoc.getDocClass();
ObjectProperty<File> file = new SimpleObjectProperty<>(new File(System.getProperty("user.dir") + "/../gs-cv/classes/" + docClassInstance.getValue().toString() + "/zones/zones.json"));
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
return null != file.get() && file.get().exists();
}, file);
return reverse ? binding.not() : binding;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ObservableValue<Boolean> isDocOcrd(Context context, boolean reverse) {
// TODO: modify the algorithm so each zone needs to be processed before returning true
DocInstance currentDoc = (DocInstance) context.getGeneric();
Root root = currentDoc.getRoot();
ZoneText zoneText = root.find(ZoneText.class);
ZoneGeneric zoneGeneric = root.find(ZoneGeneric.class);
// ObservableList<ZoneGeneric> zoneGenerics = (ObservableList) zoneGeneric.getInstances().toObservableList(); // XXX maybe just a list
ObservableList<ZoneTextInstance> zoneTextInstances = (ObservableList) currentDoc.getHolders(zoneText).toObservableList();
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
// zoneGenerics.stream().allMatch(g -> {
// zoneTextInstances.filtered(zti -> zti.getZoneNum());
// });
return !zoneTextInstances.isEmpty();
}, 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;
}
}