-
Notifications
You must be signed in to change notification settings - Fork 5
/
DocPropertiesCheckerSwitcher.java
114 lines (98 loc) · 3.95 KB
/
DocPropertiesCheckerSwitcher.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
package org.genericsystem.watch.gui;
import java.io.File;
import org.genericsystem.common.Root;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass.DocClassInstance;
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 DocPropertiesCheckerSwitcher {
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> 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);
if (reverse)
return binding.not();
else
return binding;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ObservableValue<Boolean> isDocOcrd(Context context, boolean reverse) {
// TODO: verify / test
DocInstance currentDoc = (DocInstance) context.getGeneric();
Root root = currentDoc.getRoot();
ObservableList<ZoneTextInstance> zoneTextInstances = (ObservableList) currentDoc.getHolders(root.find(ZoneText.class)).toObservableList();
BooleanBinding binding = Bindings.createBooleanBinding(() -> null != zoneTextInstances, zoneTextInstances);
if (reverse)
return binding.not();
else
return 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();
ObservableList<ZoneTextInstance> zoneTextInstances = (ObservableList) currentDoc.getHolders(root.find(ZoneText.class)).toObservableList().filtered(zt -> "reality".equals(((ZoneTextInstance) zt).getImgFilter().getValue()));
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
if (zoneTextInstances == null) {
return false;
} else {
// If any field is empty, return false otherwise true
return !zoneTextInstances.stream().anyMatch(g -> "".equals(g.getValue().toString()));
}
}, zoneTextInstances);
if (reverse)
return binding.not();
else
return binding;
}
}