-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpUtils.java
More file actions
438 lines (399 loc) · 14 KB
/
OpUtils.java
File metadata and controls
438 lines (399 loc) · 14 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2018 ImageJ developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.ops;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.scijava.ops.matcher.MatchingResult;
import org.scijava.ops.matcher.OpCandidate;
import org.scijava.ops.matcher.OpCandidate.StatusCode;
import org.scijava.ops.matcher.OpInfo;
import org.scijava.ops.matcher.OpMatcher;
import org.scijava.ops.matcher.OpRef;
import org.scijava.param.ParameterMember;
import org.scijava.param.ParameterStructs;
import org.scijava.param.ValidityException;
import org.scijava.param.ValidityProblem;
import org.scijava.struct.Member;
import org.scijava.struct.MemberInstance;
import org.scijava.struct.Struct;
import org.scijava.struct.StructInstance;
import org.scijava.struct.ValueAccessible;
import org.scijava.util.Types;
/**
* Utility methods for working with ops.
*
* @author Curtis Rueden
* @author David Kolb
*/
public final class OpUtils {
private OpUtils() {
// NB: prevent instantiation of utility class.
}
// -- Utility methods --
/**
* Parses op names contained in specified String according to the following
* format:
*
* <pre>
* 'prefix1'.'prefix2' , 'prefix1'.'prefix3'
* </pre>
*
* E.g. "math.add, math.pow". </br>
* The name delimiter is a comma (,). Furthermore, names without prefixes
* are added. The above example will result in the following output:
*
* <pre>
* [math.add, add, math.pow, pow]
* </pre>
*
* @param names
* the string containing the names to parse
* @return
*/
public static String[] parseOpNames(String names) {
return Arrays.stream(names.split(",")).map(s -> s.trim())
.toArray(String[]::new);
}
/**
* Returns an array containing the specified name and the name without
* prefixes. Prefixes are assumed to be separated by a comma (,). E.g.
* "math.add" will result in [math.add, add].
*
* @param name
* the string containing the name to parse
* @return
*/
public static String[] parseOpName(String name) {
if (name == null || name.isEmpty()) {
return new String[]{};
}
if (name.contains(".")) {
String[] split = name.split("\\.");
return new String[] { name, split[split.length - 1] };
} else {
return new String[]{name};
}
}
public static List<MemberInstance<?>> inputs(StructInstance<?> op) {
return op.members().stream() //
.filter(memberInstance -> memberInstance.member().isInput()) //
.collect(Collectors.toList());
}
public static List<Member<?>> inputs(OpCandidate candidate) {
return inputs(candidate.struct());
}
public static List<Member<?>> inputs(final Struct struct) {
return struct.members().stream() //
.filter(member -> member.isInput()) //
.collect(Collectors.toList());
}
public static Type[] inputTypes(OpCandidate candidate) {
return getTypes(inputs(candidate.struct()));
}
public static Member<?> output(OpCandidate candidate) {
return candidate.opInfo().output();
}
public static Type outputType(OpCandidate candidate) {
return output(candidate).getType();
}
public static List<Member<?>> outputs(final Struct struct) {
return struct.members().stream() //
.filter(member -> member.isOutput()) //
.collect(Collectors.toList());
}
public static List<MemberInstance<?>> outputs(StructInstance<?> op) {
return op.members().stream() //
.filter(memberInstance -> memberInstance.member().isOutput()) //
.collect(Collectors.toList());
}
public static void checkHasSingleOutput(Struct struct) throws ValidityException {
final int numOutputs = OpUtils.outputs(struct).size();
if (numOutputs != 1) {
final String error = numOutputs == 0 //
? "No output parameters specified. Must specify exactly one." //
: "Multiple output parameters specified. Only a single output is allowed.";
throw new ValidityException(Collections.singletonList(new ValidityProblem(error)));
}
}
public static List<OpDependencyMember<?>> dependencies(Struct struct) {
return struct.members().stream() //
.filter(member -> member instanceof OpDependencyMember) //
.map(member -> (OpDependencyMember<?>) member) //
.collect(Collectors.toList());
}
public static Type[] types(OpCandidate candidate) {
return getTypes(candidate.struct().members());
}
public static double getPriority(final OpCandidate candidate) {
return candidate.opInfo().priority();
}
public static Type[] padTypes(final OpCandidate candidate, Type[] types) {
final Object[] padded = padArgs(candidate, false, (Object[]) types);
return Arrays.copyOf(padded, padded.length, Type[].class);
}
public static Object[] padArgs(final OpCandidate candidate, final boolean secondary, Object... args) {
List<Member<?>> members;
String argName;
if (secondary) {
members = OpUtils.injectableMembers(candidate.struct());
argName = "secondary args";
} else {
members = OpUtils.inputs(candidate.struct());
argName = "args";
}
int inputCount = 0, requiredCount = 0;
for (final Member<?> item : members) {
inputCount++;
if (isRequired(item))
requiredCount++;
}
if (args.length == inputCount) {
// correct number of arguments
return args;
}
if (args.length > inputCount) {
// too many arguments
candidate.setStatus(StatusCode.TOO_MANY_ARGS,
"\nNumber of " + argName + " given: " + args.length + " > " +
"Number of " + argName + " of op: " + inputCount);
return null;
}
if (args.length < requiredCount) {
// too few arguments
candidate.setStatus(StatusCode.TOO_FEW_ARGS,
"\nNumber of " + argName + " given: " + args.length + " < " +
"Number of required " + argName + " of op: " + requiredCount);
return null;
}
// pad optional parameters with null (from right to left)
final int argsToPad = inputCount - args.length;
final int optionalCount = inputCount - requiredCount;
final int optionalsToFill = optionalCount - argsToPad;
final Object[] paddedArgs = new Object[inputCount];
int argIndex = 0, paddedIndex = 0, optionalIndex = 0;
for (final Member<?> item : members) {
if (!isRequired(item) && optionalIndex++ >= optionalsToFill) {
// skip this optional parameter (pad with null)
paddedIndex++;
continue;
}
paddedArgs[paddedIndex++] = args[argIndex++];
}
return paddedArgs;
}
public static boolean isRequired(final Member<?> item) {
return item instanceof ParameterMember && //
((ParameterMember<?>) item).isRequired();
}
public static List<Member<?>> injectableMembers(Struct struct) {
return struct.members()
.stream()
.filter(m -> m instanceof ValueAccessible)
.collect(Collectors.toList());
}
/**
* Checks if incomplete type matching could have occurred. If we have
* several matches that do not have equal output types, the output type may not
* completely match the request as only raw type assignability will be checked
* at the moment.
* @see OpMatcher#typesMatch(OpCandidate)
* @param matches
* @return
*/
private static boolean typeCheckingIncomplete(List<OpCandidate> matches) {
Type outputType = null;
for (OpCandidate match : matches) {
Type ts = output(match).getType();
if (outputType == null || Objects.equals(outputType, ts)) {
outputType = ts;
continue;
} else {
return true;
}
}
return false;
}
public static Type[] getTypes(List<Member<?>> members) {
return members.stream().map(m -> m.getType()).toArray(Type[]::new);
}
/**
* Gets a string with an analysis of a particular match request failure.
* <p>
* This method is used to generate informative exception messages when no
* matches, or too many matches, are found.
* </p>
*
* @param res
* The result of type matching
* @return A multi-line string describing the situation: 1) the type of
* match failure; 2) the list of matching ops (if any); 3) the
* request itself; and 4) the list of candidates including status
* (i.e., whether it matched, and if not, why not).
*/
public static String matchInfo(final MatchingResult res) {
final StringBuilder sb = new StringBuilder();
List<OpCandidate> candidates = res.getCandidates();
List<OpCandidate> matches = res.getMatches();
final OpRef ref = res.getOriginalQueries().get(0);
if (matches.isEmpty()) {
// no matches
sb.append("No matching '" + ref.getLabel() + "' op\n");
} else {
// multiple matches
final double priority = getPriority(matches.get(0));
sb.append("Multiple '" + ref.getLabel() + "' ops of priority " + priority + ":\n");
if (typeCheckingIncomplete(matches)) {
sb.append("Incomplete output type checking may have occured!\n");
}
int count = 0;
for (final OpCandidate match : matches) {
sb.append(++count + ". ");
sb.append(match.toString() + "\n");
}
}
// fail, with information about the request and candidates
sb.append("\n");
sb.append("Request:\n");
sb.append("-\t" + ref.toString() + "\n");
sb.append("\n");
sb.append("Candidates:\n");
if (candidates.isEmpty()) {
sb.append("-\t No candidates found!");
}
int count = 0;
for (final OpCandidate candidate : candidates) {
sb.append(++count + ". ");
sb.append("\t" + opString(candidate.opInfo(), candidate.getStatusItem()) + "\n");
final String status = candidate.getStatus();
if (status != null)
sb.append("\t" + status + "\n");
if (candidate.getStatusCode() == StatusCode.DOES_NOT_CONFORM) {
// TODO: Conformity not yet implemented
// // show argument values when a contingent op rejects them
// for (final ModuleItem<?> item : inputs(info)) {
// final Object value = item.getValue(candidate.getModule());
// sb.append("\t\t" + item.getName() + " = " + value + "\n");
// }
}
}
return sb.toString();
}
/**
* Gets a string describing the given op, highlighting the specific
* parameter.
*
* @param info
* The {@link OpInfo} metadata which describes the op.
* @param special
* A parameter of particular interest when describing the op.
* @return A string describing the op.
*/
public static String opString(final OpInfo info, final Member<?> special) {
final StringBuilder sb = new StringBuilder();
final String outputString = paramString(outputs(info.struct()), null).trim();
if (!outputString.isEmpty())
sb.append("(" + outputString + ") =\n\t");
sb.append(info.implementationName());
sb.append("(" + paramString(inputs(info.struct()), special) + ")");
return sb.toString();
}
/**
* Helper method of {@link #opString(OpInfo, Member)} which parses a set of
* items with a default delimiter of ","
*/
private static String paramString(final Iterable<Member<?>> items, final Member<?> special) {
return paramString(items, special, ",");
}
/**
* As {@link #paramString(Iterable, Member, String)} with an optional
* delimiter.
*/
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim) {
return paramString(items, special, delim, false);
}
/**
* As {@link #paramString(Iterable, Member, String)} with a toggle to
* control if inputs are types only or include the names.
*/
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim,
final boolean typeOnly) {
final StringBuilder sb = new StringBuilder();
boolean first = true;
for (final Member<?> item : items) {
if (first)
first = false;
else
sb.append(delim);
sb.append("\n");
if (item == special)
sb.append("==>"); // highlight special item
sb.append("\t\t");
sb.append(item.getType().getTypeName());
if (!typeOnly) {
sb.append(" " + item.getKey());
if (!((ParameterMember<?>) item).isRequired())
sb.append("?");
}
}
return sb.toString();
}
public static String opString(final OpInfo info) {
final StringBuilder sb = new StringBuilder();
sb.append(info.implementationName() + "(\n\t Inputs:\n");
for (final Member<?> arg : info.inputs()) {
sb.append("\t\t");
sb.append(arg.getType().getTypeName());
sb.append(" ");
sb.append(arg.getKey());
sb.append("\n");
}
sb.append("\t Outputs:\n");
final Member<?> arg = info.output();
sb.append("\t\t");
sb.append(arg.getType().getTypeName());
sb.append(" ");
sb.append(arg.getKey());
sb.append("\n");
sb.append(")\n");
return sb.toString();
}
public static Class<?> findFirstImplementedFunctionalInterface(final OpRef opRef) {
for (final Type opType : opRef.getTypes()) {
final Class<?> functionalInterface = ParameterStructs.findFunctionalInterface(Types.raw(opType));
if (functionalInterface != null) {
return functionalInterface;
}
}
return null;
}
}