Skip to content

Commit 4ff1f92

Browse files
Treiblesschorlegselzer
authored andcommitted
Add analysis
1 parent 615f4ea commit 4ff1f92

3 files changed

Lines changed: 159 additions & 7 deletions

File tree

‎src/main/java/org/scijava/ops/base/MatchingResult.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public OpCandidate singleMatch() {
4343

4444
return matches.get(0);
4545
}
46-
47-
// final String analysis = OpUtils.matchInfo(candidates, matches);
48-
// TODO
49-
throw new IllegalArgumentException("TODO dump analysis");
46+
final String analysis = OpUtils.matchInfo(this);
47+
throw new IllegalArgumentException(analysis);
5048
}
5149
}

‎src/main/java/org/scijava/ops/base/OpRef.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ public String getLabel() {
142142
append(sb, name);
143143
if (types != null) {
144144
for (final Type t : types) {
145-
// FIXME: Use better Type-to-string approach, once it exists.
146-
append(sb, t instanceof Class ? ((Class<?>) t).getName() : t.toString());
145+
append(sb, Types.name(t));
147146
}
148147
}
149148
return sb.toString();

‎src/main/java/org/scijava/ops/base/OpUtils.java‎

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.List;
3333
import java.util.stream.Collectors;
3434

35+
import org.scijava.ops.base.OpCandidate.StatusCode;
36+
import org.scijava.param.ParameterMember;
3537
import org.scijava.struct.Member;
3638
import org.scijava.struct.MemberInstance;
3739
import org.scijava.struct.Struct;
@@ -95,10 +97,163 @@ public static List<MemberInstance<?>> outputs(StructInstance<?> op) {
9597
.filter(memberInstance -> memberInstance.member().isOutput()) //
9698
.collect(Collectors.toList());
9799
}
98-
100+
99101
public static double getPriority(final OpCandidate candidate) {
100102
// TODO: Think about what to do about non @Plugin-based ops...?
101103
// What if there is no annotation? How to discern a priority?
102104
return candidate.opInfo().getAnnotation().priority();
103105
}
106+
107+
/**
108+
* Gets a string with an analysis of a particular match request failure.
109+
* <p>
110+
* This method is used to generate informative exception messages when no
111+
* matches, or too many matches, are found.
112+
* </p>
113+
*
114+
* @param res
115+
* The result of type matching
116+
* @return A multi-line string describing the situation: 1) the type of
117+
* match failure; 2) the list of matching ops (if any); 3) the
118+
* request itself; and 4) the list of candidates including status
119+
* (i.e., whether it matched, and if not, why not).
120+
*/
121+
public static String matchInfo(final MatchingResult res) {
122+
final StringBuilder sb = new StringBuilder();
123+
124+
List<OpCandidate> candidates = res.getCandidates();
125+
List<OpCandidate> matches = res.getMatches();
126+
127+
final OpRef ref = candidates.get(0).getRef();
128+
if (matches.isEmpty()) {
129+
// no matches
130+
sb.append("No matching '" + ref.getLabel() + "' op\n");
131+
} else {
132+
// multiple matches
133+
final double priority = getPriority(matches.get(0));
134+
sb.append("Multiple '" + ref.getLabel() + "' ops of priority " + priority + ":\n");
135+
int count = 0;
136+
for (final OpCandidate match : matches) {
137+
sb.append(++count + ". ");
138+
sb.append(match.toString() + "\n");
139+
}
140+
}
141+
142+
// fail, with information about the request and candidates
143+
sb.append("\n");
144+
sb.append("Request:\n");
145+
sb.append("-\t" + opString(ref.getLabel(), (Object[]) ref.getArgs()) + "\n");
146+
sb.append("\n");
147+
sb.append("Candidates:\n");
148+
int count = 0;
149+
for (final OpCandidate candidate : candidates) {
150+
sb.append(++count + ". ");
151+
sb.append("\t" + opString(candidate.opInfo(), candidate.getStatusItem()) + "\n");
152+
final String status = candidate.getStatus();
153+
if (status != null)
154+
sb.append("\t" + status + "\n");
155+
if (candidate.getStatusCode() == StatusCode.DOES_NOT_CONFORM) {
156+
// TODO: Conformity not yet implemented
157+
// // show argument values when a contingent op rejects them
158+
// for (final ModuleItem<?> item : inputs(info)) {
159+
// final Object value = item.getValue(candidate.getModule());
160+
// sb.append("\t\t" + item.getName() + " = " + value + "\n");
161+
// }
162+
}
163+
}
164+
return sb.toString();
165+
}
166+
167+
/**
168+
* Gets a string describing the given op request.
169+
*
170+
* @param name
171+
* The op's name.
172+
* @param args
173+
* The op's input arguments.
174+
* @return A string describing the op request.
175+
*/
176+
public static String opString(final String name, final Object... args) {
177+
final StringBuilder sb = new StringBuilder();
178+
sb.append(name + "(\n\t\t");
179+
boolean first = true;
180+
for (final Object arg : args) {
181+
if (first)
182+
first = false;
183+
else
184+
sb.append(",\n\t\t");
185+
if (arg == null)
186+
sb.append("null");
187+
else if (arg instanceof Class) {
188+
// NB: Class instance used to mark argument type.
189+
sb.append(((Class<?>) arg).getSimpleName());
190+
} else
191+
sb.append(arg.getClass().getSimpleName());
192+
}
193+
sb.append(")");
194+
return sb.toString();
195+
}
196+
197+
/**
198+
* Gets a string describing the given op, highlighting the specific
199+
* parameter.
200+
*
201+
* @param info
202+
* The {@link OpInfo} metadata which describes the op.
203+
* @param special
204+
* A parameter of particular interest when describing the op.
205+
* @return A string describing the op.
206+
*/
207+
public static String opString(final OpInfo info, final Member<?> special) {
208+
final StringBuilder sb = new StringBuilder();
209+
final String outputString = paramString(outputs(info.struct()), null).trim();
210+
if (!outputString.isEmpty())
211+
sb.append("(" + outputString + ") =\n\t");
212+
sb.append(info.opClass().getName());
213+
sb.append("(" + paramString(inputs(info.struct()), special) + ")");
214+
return sb.toString();
215+
}
216+
217+
/**
218+
* Helper method of {@link #opString(OpInfo, Member)} which parses a
219+
* set of items with a default delimiter of ","
220+
*/
221+
private static String paramString(final Iterable<Member<?>> items, final Member<?> special) {
222+
return paramString(items, special, ",");
223+
}
224+
225+
/**
226+
* As {@link #paramString(Iterable, Member, String)} with an optional delimiter.
227+
*/
228+
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim) {
229+
return paramString(items, special, delim, false);
230+
}
231+
232+
/**
233+
* As {@link #paramString(Iterable, Member, String)} with a toggle to
234+
* control if inputs are types only or include the names.
235+
*/
236+
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim,
237+
final boolean typeOnly) {
238+
final StringBuilder sb = new StringBuilder();
239+
boolean first = true;
240+
for (final Member<?> item : items) {
241+
if (first)
242+
first = false;
243+
else
244+
sb.append(delim);
245+
sb.append("\n");
246+
if (item == special)
247+
sb.append("==>"); // highlight special item
248+
sb.append("\t\t");
249+
sb.append(item.getType().getTypeName());
250+
251+
if (!typeOnly) {
252+
sb.append(" " + item.getKey());
253+
if (!((ParameterMember<?>) item).isRequired())
254+
sb.append("?");
255+
}
256+
}
257+
return sb.toString();
258+
}
104259
}

0 commit comments

Comments
 (0)