|
32 | 32 | import java.util.List; |
33 | 33 | import java.util.stream.Collectors; |
34 | 34 |
|
| 35 | +import org.scijava.ops.base.OpCandidate.StatusCode; |
| 36 | +import org.scijava.param.ParameterMember; |
35 | 37 | import org.scijava.struct.Member; |
36 | 38 | import org.scijava.struct.MemberInstance; |
37 | 39 | import org.scijava.struct.Struct; |
@@ -95,10 +97,163 @@ public static List<MemberInstance<?>> outputs(StructInstance<?> op) { |
95 | 97 | .filter(memberInstance -> memberInstance.member().isOutput()) // |
96 | 98 | .collect(Collectors.toList()); |
97 | 99 | } |
98 | | - |
| 100 | + |
99 | 101 | public static double getPriority(final OpCandidate candidate) { |
100 | 102 | // TODO: Think about what to do about non @Plugin-based ops...? |
101 | 103 | // What if there is no annotation? How to discern a priority? |
102 | 104 | return candidate.opInfo().getAnnotation().priority(); |
103 | 105 | } |
| 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 | + } |
104 | 259 | } |
0 commit comments