1
2
3
4
5
6
7
8
9
10
11
12
13
14 package eu.fbk.rdfpro;
15
16 import java.util.function.Predicate;
17
18 import javax.annotation.Nullable;
19
20 import org.openrdf.model.Statement;
21 import org.openrdf.model.Value;
22 import org.openrdf.rio.RDFHandler;
23 import org.openrdf.rio.RDFHandlerException;
24
25 import eu.fbk.rdfpro.util.Scripting;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @FunctionalInterface
42 public interface Reducer {
43
44
45
46
47 Reducer IDENTITY = new Reducer() {
48
49 @Override
50 public void reduce(final Value key, final Statement[] statements, final RDFHandler handler)
51 throws RDFHandlerException {
52 for (final Statement statement : statements) {
53 handler.handleStatement(statement);
54 }
55 }
56
57 };
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 static Reducer filter(final Reducer reducer,
75 @Nullable final Predicate<Statement> existsPredicate,
76 @Nullable final Predicate<Statement> forallPredicate) {
77
78 if (existsPredicate != null) {
79 if (forallPredicate != null) {
80 return new Reducer() {
81
82 @Override
83 public void reduce(final Value key, final Statement[] statements,
84 final RDFHandler handler) throws RDFHandlerException {
85 boolean exists = false;
86 for (final Statement statement : statements) {
87 if (!forallPredicate.test(statement)) {
88 return;
89 }
90 exists = exists || existsPredicate.test(statement);
91 }
92 if (exists) {
93 reducer.reduce(key, statements, handler);
94 }
95 }
96
97 };
98 } else {
99 return new Reducer() {
100
101 @Override
102 public void reduce(final Value key, final Statement[] statements,
103 final RDFHandler handler) throws RDFHandlerException {
104 for (final Statement statement : statements) {
105 if (existsPredicate.test(statement)) {
106 reducer.reduce(key, statements, handler);
107 return;
108 }
109 }
110 }
111
112 };
113 }
114 } else {
115 if (forallPredicate != null) {
116 return new Reducer() {
117
118 @Override
119 public void reduce(final Value key, final Statement[] statements,
120 final RDFHandler handler) throws RDFHandlerException {
121 for (final Statement statement : statements) {
122 if (!forallPredicate.test(statement)) {
123 return;
124 }
125 }
126 reducer.reduce(key, statements, handler);
127 }
128
129 };
130 } else {
131 return reducer;
132 }
133 }
134 }
135
136
137
138
139
140
141
142
143
144 static Reducer concat(final Reducer... reducers) {
145 return new Reducer() {
146
147 @Override
148 public void reduce(final Value key, final Statement[] statements,
149 final RDFHandler handler) throws RDFHandlerException {
150 for (final Reducer reducer : reducers) {
151 reducer.reduce(key, statements, handler);
152 }
153 }
154
155 };
156 }
157
158
159
160
161
162
163
164
165
166 @Nullable
167 static Mapper parse(@Nullable final String expression) {
168 return expression == null ? null
169 : Scripting.compile(Mapper.class, expression, "k", "p", "h");
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 void reduce(@Nullable Value key, Statement[] statements, RDFHandler handler)
186 throws RDFHandlerException;
187
188 }