Skip to content

Commit bd23694

Browse files
ctruedenclaude
andcommitted
Support computed parameter choices, closing the DynamicCommand gap
Counting DynamicCommand's uses across the scijava and imagej repositories shows two different needs wearing one coat: about 39 files mutate the metadata of a declared parameter (setChoices, setMinimumValue), and about 31 add parameters that do not exist statically (addInput). SciJava Common serves both by making ModuleInfo mutable and per-instance, which is exactly what makes harvesting hard -- a platform cannot know a command's parameters without constructing it. Adding parameters was already covered by generated groups, and better, because those are declared: @group(membersFrom = ...) says where the dynamic part is and implies that the rest is fixed. This adds the other half. @parameter(choicesFrom = "behavior") names a behavior producing the values a parameter may take, so the parameter stays statically visible while its values follow the state; choices = {...} covers the static case; and ParameterNode.choices() hands a user interface the resolved list without it caring which kind it was. Being a behavior, it works for scripts, which DynamicCommand never did. There is therefore no DynamicCommand base class and no mutable description. If min and max want the same treatment, minFrom and maxFrom follow the identical pattern -- worth adding when a widget needs it. NB: one full-reactor run failed with twelve ops-image suites erroring in SCIFIO's format discovery (Context.inject on a null context), in code this branch does not touch. It did not reproduce on re-run or in isolation, and the same suites passed in the surrounding runs. Recorded here in case it recurs in CI. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013gJpzGd3ksbME4CN6Tu2ni
1 parent ae3cf2f commit bd23694

7 files changed

Lines changed: 162 additions & 1 deletion

File tree

‎docs/migration-plan.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,25 @@ after it.
802802
come from whatever object the parameter holds; where that object came from
803803
is a separate question, for a chooser widget's candidate list. It is one
804804
source among several, so the harvester is not blocked on it.
805+
- **`DynamicCommand` is supported in use, not in form.** Counting its uses
806+
across the scijava and imagej repositories shows two different needs
807+
wearing one coat: about 39 files mutate the metadata of a *declared*
808+
parameter (`setChoices`, `setMinimumValue`), and about 31 add parameters
809+
that do not exist statically (`addInput`). SciJava Common serves both by
810+
making `ModuleInfo` mutable and per-instance, which is precisely what makes
811+
harvesting hard: a platform cannot know a command's parameters without
812+
constructing it.
813+
- Adding parameters is the generated group above — and better, because it
814+
is *declared*: `@Group(membersFrom = ...)` tells a platform exactly where
815+
the dynamic part is and that everything else is fixed.
816+
- Computing metadata is `@Parameter(choicesFrom = "behavior")`, which
817+
leaves the parameter statically visible while its values follow the
818+
state. `@Parameter(choices = {...})` covers the static case, and
819+
`ParameterNode.choices()` hands a UI the resolved list either way. Being
820+
a behavior, it works for scripts too.
821+
- So there is no `DynamicCommand` base class and no mutable description.
822+
If `min` and `max` want the same treatment, `minFrom`/`maxFrom` follow
823+
the identical pattern — worth adding when a widget needs it, not before.
805824
- **Known rough edges**: group placement is provisional (a group appears
806825
where its first member would, and a generated group needs an explicit
807826
`after` anchor); `ParameterModel` decides the tree changed by comparing

‎scijava-execute/src/main/java/org/scijava/execute/FieldParameterMember.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public Map<String, String> attrs() {
131131
put(attrs, VALIDATOR, parameter.validator());
132132
put(attrs, VISIBLE_WHEN, parameter.visibleWhen());
133133
put(attrs, GROUP, parameter.group());
134+
put(attrs, CHOICES, String.join(",", parameter.choices()));
135+
put(attrs, CHOICES_FROM, parameter.choicesFrom());
134136
return attrs;
135137
}
136138

‎scijava-execute/src/main/java/org/scijava/execute/Parameter.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,26 @@
100100

101101
/** The group this parameter belongs to, if any. */
102102
String group() default "";
103+
104+
/**
105+
* The values this parameter may take, where they are known in advance.
106+
*
107+
* @see #choicesFrom()
108+
*/
109+
String[] choices() default {};
110+
111+
/**
112+
* A behavior producing the values this parameter may take, where they
113+
* depend on runtime state - the axes of the open image, the fields of the
114+
* chosen table.
115+
* <p>
116+
* NB: this is what SciJava Common needed {@code DynamicCommand} for. There,
117+
* a command mutated its own {@code ModuleInfo} to call {@code setChoices},
118+
* which made the description itself mutable and per-instance: a platform
119+
* could not know a command's parameters without constructing it. Declaring
120+
* that the choices come from a behavior keeps the parameter statically
121+
* visible while its values stay dynamic.
122+
* </p>
123+
*/
124+
String choicesFrom() default "";
103125
}

‎scijava-execute/src/main/java/org/scijava/execute/ParameterMember.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public interface ParameterMember<T> extends Member<T> {
6666
/** Metadata key: a human-readable label. */
6767
String LABEL = "label";
6868

69+
/** Metadata key: the values this parameter may take, comma-separated. */
70+
String CHOICES = "choices";
71+
72+
/** Metadata key: the behavior producing the values it may take. */
73+
String CHOICES_FROM = "choicesFrom";
74+
6975
/** Gets this parameter's metadata. */
7076
Map<String, String> attrs();
7177

‎scijava-harvest/src/main/java/org/scijava/harvest/ParameterNode.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ default boolean isCollapsible() {
6565
default boolean isCollapsed() {
6666
return false;
6767
}
68+
69+
/**
70+
* Gets the values this parameter may take, as resolved right now.
71+
* <p>
72+
* A user interface renders a chooser when this is non-empty and does not
73+
* care whether the values were declared or computed.
74+
* </p>
75+
*/
76+
default List<Object> choices() {
77+
return List.of();
78+
}
6879
}

‎scijava-harvest/src/main/java/org/scijava/harvest/ParameterTree.java‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,49 @@ ParameterTree build() {
187187
/** Builds the node for one parameter, expanding it if it is a struct. */
188188
private ParameterNode nodeFor(final MemberInstance<?> member) {
189189
final DefaultNode node = new DefaultNode(label(member), member);
190+
node.choices = choices(member);
190191
for (final MemberInstance<?> child : subMembers(member))
191192
node.children.add(nodeFor(child));
192193
return node;
193194
}
194195

196+
/**
197+
* Gets the values a parameter may take, declared or computed.
198+
* <p>
199+
* NB: computing them is what SciJava Common used {@code DynamicCommand}
200+
* for, by mutating the command's own description. Here the parameter
201+
* stays statically visible and only its values are dynamic.
202+
* </p>
203+
*/
204+
private List<Object> choices(final MemberInstance<?> member) {
205+
final Optional<String> from = attr(member,
206+
ParameterMember.CHOICES_FROM);
207+
if (from.isPresent()) {
208+
final Optional<Behavior> behavior = executable.behavior(from.get());
209+
if (behavior.isPresent()) return asList(behavior.get().invoke());
210+
}
211+
return attr(member, ParameterMember.CHOICES) //
212+
.map(csv -> (List<Object>) new ArrayList<Object>(List.of(csv.split(
213+
",")))) //
214+
.orElse(List.of());
215+
}
216+
217+
private static List<Object> asList(final Object result) {
218+
if (result == null) return List.of();
219+
if (result instanceof List) {
220+
return new ArrayList<>((List<?>) result);
221+
}
222+
if (result instanceof Object[]) {
223+
return new ArrayList<>(List.of((Object[]) result));
224+
}
225+
if (result instanceof Iterable) {
226+
final List<Object> list = new ArrayList<>();
227+
((Iterable<?>) result).forEach(list::add);
228+
return list;
229+
}
230+
return List.of(result);
231+
}
232+
195233
/**
196234
* Gets the parameters <em>inside</em> a parameter, if it has any.
197235
* <p>
@@ -206,7 +244,14 @@ private List<MemberInstance<?>> subMembers(
206244
final MemberInstance<?> member)
207245
{
208246
final Object value = member.isReadable() ? member.get() : null;
209-
if (value == null) return List.of();
247+
if (value == null) {
248+
// NB: with no value there is nothing to read or write, but the
249+
// group's shape is still known from the declared type, so a dialog
250+
// can show it rather than an unexplained gap. Only a concrete type
251+
// has fields; an unset `Joke` shows nothing until one is chosen,
252+
// which is correct.
253+
return List.of();
254+
}
210255
final Struct struct = Executables.struct(value.getClass());
211256
final List<Member<?>> members = struct.members();
212257
if (members.isEmpty()) return List.of();
@@ -315,6 +360,7 @@ private static class DefaultNode implements ParameterNode {
315360
private final List<ParameterNode> children = new ArrayList<>();
316361
private boolean collapsible;
317362
private boolean collapsed;
363+
private List<Object> choices = List.of();
318364

319365
DefaultNode(final String label, final MemberInstance<?> member) {
320366
this.label = label;
@@ -346,6 +392,11 @@ public boolean isCollapsed() {
346392
return collapsed;
347393
}
348394

395+
@Override
396+
public List<Object> choices() {
397+
return choices;
398+
}
399+
349400
@Override
350401
public String toString() {
351402
return isGroup() ? "[" + label + "]" : label;

‎scijava-harvest/src/test/java/org/scijava/harvest/ParameterModelTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,56 @@ public Optional<Behavior> behavior(final String name) {
289289
assertEquals(Map.of("name", "A name is required"), model.problems());
290290
}
291291

292+
// -- Choices, static and computed --
293+
294+
public static class PickAxis implements Runnable {
295+
296+
/** Which axes exist depends on the image, so the values are computed. */
297+
@Parameter(choicesFrom = "axisChoices")
298+
private String axis = "X";
299+
300+
@Parameter(choices = { "Mean", "Median", "Max" })
301+
private String statistic = "Mean";
302+
303+
/** Stands in for "the axes of the currently open image". */
304+
private List<String> axes = List.of("X", "Y", "Z");
305+
306+
@SuppressWarnings("unused")
307+
private List<String> axisChoices() {
308+
return axes;
309+
}
310+
311+
@Override
312+
public void run() {}
313+
}
314+
315+
/**
316+
* Computed choices are what SciJava Common needed DynamicCommand for: a
317+
* command mutating its own description to call setChoices. Here the
318+
* parameter stays statically visible and only its values are dynamic.
319+
*/
320+
@Test
321+
public void testComputedChoices() {
322+
final PickAxis command = new PickAxis();
323+
final ParameterModel model = model(command);
324+
325+
assertEquals(List.of("X", "Y", "Z"), model.tree().find("axis")
326+
.orElseThrow().choices());
327+
328+
// A different image: different axes, same declared parameter.
329+
command.axes = List.of("Channel", "Time");
330+
model.set("axis", "X");
331+
assertEquals(List.of("Channel", "Time"), model.tree().find("axis")
332+
.orElseThrow().choices());
333+
}
334+
335+
@Test
336+
public void testDeclaredChoices() {
337+
final ParameterModel model = model(new PickAxis());
338+
assertEquals(List.of("Mean", "Median", "Max"), model.tree().find(
339+
"statistic").orElseThrow().choices());
340+
}
341+
292342
private static ParameterModel model(final Runnable object) {
293343
final Executable executable = Executables.executableOf(object);
294344
return new ParameterModel(executable.create());

0 commit comments

Comments
 (0)