-
Notifications
You must be signed in to change notification settings - Fork 5
/
ObservableListExtractorCustom.java
177 lines (157 loc) · 6.65 KB
/
ObservableListExtractorCustom.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.gui.utils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.model.Doc;
import org.genericsystem.cv.model.DocClass;
import org.genericsystem.cv.model.DocClass.DocClassInstance;
import org.genericsystem.cv.model.ImgFilter;
import org.genericsystem.cv.model.Score;
import org.genericsystem.cv.model.Score.ScoreInstance;
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.reactor.context.ObservableListExtractor;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
/**
* This class contains all the {@link ObservableListExtractor} needed across the app.
*
* @author Pierrik Lassalas
*/
public class ObservableListExtractorCustom {
/*
* === DOC CLASS AND DOC ===
*/
public static class DOC_CLASS_SELECTOR implements ObservableListExtractor {
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Root root = generics[0].getRoot();
DocClass docClass = root.find(DocClass.class);
Snapshot<Generic> docClassInstances = docClass.getInstances();
if (null == docClassInstances)
return FXCollections.emptyObservableList();
return docClassInstances.toObservableList();
}
}
public static class DOC_SELECTOR implements ObservableListExtractor {
@Override
public ObservableList<Generic> apply(Generic[] generics) {
DocClassInstance currentDocClass = (DocClassInstance) generics[0];
Doc doc = generics[0].getRoot().find(Doc.class);
if (null == currentDocClass)
return FXCollections.emptyObservableList();
System.out.println("Current doc class : " + currentDocClass.info());
Snapshot<Generic> docInstances = currentDocClass.getHolders(doc);
if (null == docInstances)
return FXCollections.emptyObservableList();
return docInstances.toObservableList();
}
}
/*
* === ZONES ===
*/
public static class ZONE_SELECTOR implements ObservableListExtractor {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Generic currentDocClass = generics[0];
Root root = currentDocClass.getRoot();
System.out.println("Current docClass: " + currentDocClass.info());
Snapshot<ZoneInstance> zones = (Snapshot) currentDocClass.getHolders(root.find(ZoneGeneric.class));
if (zones == null)
return FXCollections.emptyObservableList();
return (ObservableList) zones.toObservableList().sorted();
}
}
public static class ZONE_SELECTOR_BEST implements ObservableListExtractor {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Generic currentDoc = generics[0];
Root root = currentDoc.getRoot();
System.out.println("Document: " + currentDoc.info());
Snapshot<ZoneTextInstance> zoneTextInstances = (Snapshot) currentDoc.getHolders(root.find(ZoneText.class));
if (zoneTextInstances == null)
return FXCollections.emptyObservableList();
return (ObservableList) zoneTextInstances.toObservableList().filtered(zt -> "best".equals(zt.getImgFilter().getValue())).sorted((g1, g2) -> Integer.compare(g1.getZoneNum(), g2.getZoneNum()));
}
}
public static class ZONE_SELECTOR_REALITY implements ObservableListExtractor {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Generic currentDoc = generics[0];
Root root = currentDoc.getRoot();
System.out.println("Document: " + currentDoc.info());
Snapshot<ZoneTextInstance> zoneTextInstances = (Snapshot) currentDoc.getHolders(root.find(ZoneText.class));
if (zoneTextInstances == null)
return FXCollections.emptyObservableList();
long start = System.nanoTime();
ObservableList ol = zoneTextInstances.toObservableList().filtered(zt -> "reality".equals(zt.getImgFilter().getValue())).sorted((g1, g2) -> Integer.compare(g1.getZoneNum(), g2.getZoneNum()));
long stop = System.nanoTime();
System.out.println("--------- zone selector: " + (stop - start) / 1_000_000 + "ms");
return ol;
}
}
/*
* === DATALIST ===
*/
public static class DATALIST_SELECTOR implements ObservableListExtractor {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObservableList<Generic> apply(Generic[] generics) {
ZoneTextInstance zti = (ZoneTextInstance) generics[0];
Generic currentDoc = generics[1];
Root root = currentDoc.getRoot();
Predicate<ZoneTextInstance> filterByZone = z -> z.getZoneNum() == zti.getZoneNum() && !z.getValue().toString().isEmpty();
Snapshot<ZoneTextInstance> zoneTextInstances = (Snapshot) currentDoc.getHolders(root.find(ZoneText.class));
return (ObservableList) zoneTextInstances.toObservableList().filtered(filterByZone.and(distinctByKey(g -> g.getValue()))).sorted((g1, g2) -> Integer.compare(g1.getZoneNum(), g2.getZoneNum()));
}
/**
* Utility function that can be used to filter a stream to get distinct values, using a personalized function.
*
* @param keyExtractor - lambda expression that will provide the filter criteria
* @return a predicate
*/
public <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
/*
* === OCR TEXT ===
*/
public static class OCR_SELECTOR implements ObservableListExtractor {
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Root root = generics[0].getRoot();
Snapshot<Generic> filters = root.find(ImgFilter.class).getInstances();
if (filters == null)
return FXCollections.emptyObservableList();
return filters.toObservableList();
}
}
/*
* === SCORE ===
*/
public static class SCORE_SELECTOR implements ObservableListExtractor {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObservableList<Generic> apply(Generic[] generics) {
ZoneInstance zoneInstance = (ZoneInstance) generics[0];
Root root = zoneInstance.getRoot();
System.out.println("Current zone: " + zoneInstance.info());
Snapshot<ScoreInstance> scores = (Snapshot) zoneInstance.getHolders(root.find(Score.class));
// scores.forEach(g -> System.out.println(g.info()));
if (scores == null)
return FXCollections.emptyObservableList();
return (ObservableList) scores.toObservableList();
}
}
}