Skip to content

Commit 26506ff

Browse files
Name only the state sets a suggested call sequence changes (#484)
Closes #482. ## What With more than one `@StateSet`, the call-sequence hint named every state set, including the ones the suggested calls leave as they are. It now names only the sets whose states differ before and after the calls. Only the displayed hint changes; no solver query changes. Reproducer from the issue: ``` before: Call s.mark() before this call to move s from open and unmarked to open and marked after: Call s.mark() before this call to move s from unmarked to marked ``` Existing fixture `ErrorStateHintMultipleSets`: ``` before: Call d.unlock() before this call to move d from closed and locked to closed and unlocked after: Call d.unlock() before this call to move d from locked to unlocked ``` ## How - `StateMachine.stateSetsChanged(before, after)`: the sets whose states differ. If the sets it would leave out, taken together, differ too (how states of different sets are combined changed), every set is named, as before. - `StateMachine.describeOnly(subset, sets)`: names `subset` only in the given sets, reusing `StateCover` (the naming from #474/#475) on the states that agree with `subset` in those sets. - `StateHints.callSequence` names both sides with these. `from any state` is unchanged. ## Not changed, on purpose - **`In state open and unmarked, s can call: mark()`** still names the full state: which calls are allowed depends on every set, so dropping `open` would make the hint less accurate. - **Found/expected** (`found open(s) && unmarked(s) but expected marked(s) && open(s)`) is left as it is. These are the verifier's real predicates (the simplified premise chain and the `from` disjunction, with arguments, ghosts and aliases). The IDE reads them too. Dropping or reordering conjuncts that hold on both sides would need a semantic check on arbitrary predicates. Doing that only for display is riskier than this PR should be. It could be a follow-up if the study needs it. ## Tests - `TestStateHints.callSequenceNamesOnlyTheStateSetsItChanges` (the renamed multi-set test; fails on main). - `StateMachineTest`: `statesAreNamedOnlyInTheSetsShown`, `setsChangedAreThoseWhoseStatesDiffer` (including the cases where how the sets are combined changes). - `mvn test`: 692 tests, 0 failures. No other expectation changed. The dcm4che hint (`from startCompression to compressionExplicit`) already named a single set. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 03b2d8b commit 26506ff

4 files changed

Lines changed: 66 additions & 11 deletions

File tree

‎liquidjava-verifier/src/main/java/liquidjava/diagnostics/hints/StateHints.java‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static List<Hint> forCall(StateMachine machine, RefinedFunction call, Set
7474
if (category == ErrorCategory.UNKNOWN || checkedElsewhere)
7575
return hints.anyState(current, call, checkedElsewhere);
7676
boolean pathDependent = category == ErrorCategory.PATH_DEPENDENT_STATE;
77-
List<Hint> sequences = hints.howToReach(current, call, hints.namesHere(current),
77+
List<Hint> sequences = hints.howToReach(current, call, false,
7878
() -> machine.complete() ? List.of(hints.unreachable(current, call, pathDependent)) : List.of());
7979
// in a known state, a check is suggested only when no sequence of calls reaches a state that allows the call
8080
boolean reachable = sequences.stream().anyMatch(h -> h.kind() == Kind.CALL_SEQUENCE);
@@ -112,19 +112,19 @@ private List<Hint> anyState(Set<State> current, RefinedFunction call, boolean ch
112112
List<Hint> hints = new ArrayList<>();
113113
hints.add(new Hint(Kind.NEEDED_STATE, message));
114114
hints.addAll(guard(current, call));
115-
hints.addAll(howToReach(current, call, "any state", List::of));
115+
hints.addAll(howToReach(current, call, true, List::of));
116116
return hints;
117117
}
118118

119119
/**
120120
* The shortest sequences of calls that are allowed in every state of {@code current} and lead to {@code call}
121121
*
122-
* @param from
123-
* how to name {@code current}
122+
* @param fromAnyState
123+
* whether to name {@code current} as any state
124124
* @param unreachable
125125
* the hints to give if no sequence exists
126126
*/
127-
private List<Hint> howToReach(Set<State> current, RefinedFunction call, String from,
127+
private List<Hint> howToReach(Set<State> current, RefinedFunction call, boolean fromAnyState,
128128
Supplier<List<Hint>> unreachable) {
129129
Optional<List<List<CallVariant>>> search = machine.shortestPaths(current, call, MAX_SEQUENCES);
130130
if (search.isEmpty())
@@ -134,7 +134,7 @@ private List<Hint> howToReach(Set<State> current, RefinedFunction call, String f
134134
return unreachable.get();
135135
if (sequences.get(0).size() > MAX_SEQUENCE_LENGTH)
136136
return List.of();
137-
return sequences.stream().map(sequence -> callSequence(current, sequence, from)).toList();
137+
return sequences.stream().map(sequence -> callSequence(current, sequence, fromAnyState)).toList();
138138
}
139139

140140
/**
@@ -193,13 +193,16 @@ private boolean namesCheckedState(RefinedFunction method) {
193193
return refinements.anyMatch(p -> StateMachine.mayName(p, checked));
194194
}
195195

196-
private Hint callSequence(Set<State> current, List<CallVariant> sequence, String from) {
196+
/** The states before and after {@code sequence} are named only in the state sets it changes */
197+
private Hint callSequence(Set<State> current, List<CallVariant> sequence, boolean fromAnyState) {
197198
Set<State> reached = current;
198199
for (CallVariant step : sequence)
199200
reached = machine.after(reached, step);
201+
Set<Integer> changed = machine.stateSetsChanged(current, reached);
202+
String from = fromAnyState ? "any state" : join(machine.describeOnly(current, changed));
200203
String calls = sequence.stream().map(this::call).collect(Collectors.joining(", then "));
201204
return new Hint(Kind.CALL_SEQUENCE, String.format("Call %s before this call to move %s from %s to %s", calls,
202-
receiver, from, names(reached)));
205+
receiver, from, join(machine.describeOnly(reached, changed))));
203206
}
204207

205208
private Hint unreachable(Set<State> current, RefinedFunction call, boolean pathDependent) {

‎liquidjava-verifier/src/main/java/liquidjava/processor/state_machine/StateMachine.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Map;
1212
import java.util.Optional;
1313
import java.util.Set;
14+
import java.util.TreeSet;
1415
import java.util.stream.Collectors;
1516
import java.util.stream.IntStream;
1617

@@ -243,6 +244,39 @@ public List<List<String>> describe(Collection<State> subset, Set<Integer> named)
243244
return StateCover.of(states, Set.copyOf(subset), named);
244245
}
245246

247+
/**
248+
* {@code subset} named only by its states in the sets {@code shown}, whatever they are in the other sets, e.g.
249+
* {@code [[unmarked]]} for the state {@code open and unmarked} if only the second set is shown
250+
*/
251+
public List<List<String>> describeOnly(Collection<State> subset, Set<Integer> shown) {
252+
List<State> agreeing = states.stream()
253+
.filter(s -> subset.stream().anyMatch(
254+
member -> shown.stream().allMatch(k -> s.names().get(k).equals(member.names().get(k)))))
255+
.toList();
256+
return describe(agreeing, shown);
257+
}
258+
259+
/**
260+
* The state sets, as indexes into {@link State#names()}, whose states in {@code after} differ from those in
261+
* {@code before}. Every set if the other sets, taken together, differ too: then how the states of different sets
262+
* are combined changed, which naming the changed sets alone would hide.
263+
*/
264+
public Set<Integer> stateSetsChanged(Collection<State> before, Collection<State> after) {
265+
Set<Integer> every = IntStream.range(0, states.get(0).names().size()).boxed()
266+
.collect(Collectors.toCollection(TreeSet::new));
267+
Set<Integer> changed = every.stream()
268+
.filter(k -> !statesIn(before, Set.of(k)).equals(statesIn(after, Set.of(k))))
269+
.collect(Collectors.toCollection(TreeSet::new));
270+
Set<Integer> kept = every.stream().filter(k -> !changed.contains(k)).collect(Collectors.toSet());
271+
return !changed.isEmpty() && statesIn(before, kept).equals(statesIn(after, kept)) ? changed : every;
272+
}
273+
274+
/** The states of the sets {@code sets} that {@code subset} has, combined as in its states */
275+
private static Set<List<String>> statesIn(Collection<State> subset, Set<Integer> sets) {
276+
return subset.stream().map(s -> sets.stream().sorted().map(s.names()::get).toList())
277+
.collect(Collectors.toSet());
278+
}
279+
246280
/**
247281
* The state sets that {@code subset} depends on, as indexes into {@link State#names()}: those its
248282
* {@link #describe(Collection) description} names

‎liquidjava-verifier/src/test/java/liquidjava/api/tests/TestStateHints.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ void missingCallsAreSuggestedInOrder() {
3737
}
3838

3939
@Test
40-
void statesOfMultipleStateSetsAreCombined() {
40+
void callSequenceNamesOnlyTheStateSetsItChanges() {
4141
assertHints("ErrorStateHintMultipleSets.java",
42-
new Hint(Kind.CALL_SEQUENCE,
43-
"Call d.unlock() before this call to move d from closed and locked to closed and unlocked"),
42+
new Hint(Kind.CALL_SEQUENCE, "Call d.unlock() before this call to move d from locked to unlocked"),
4443
new Hint(Kind.AVAILABLE_CALLS, "In state closed and locked, d can call: unlock()"));
4544
}
4645

‎liquidjava-verifier/src/test/java/liquidjava/processor/state_machine/StateMachineTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ void statesAreNamedInEverySetAsked() {
7070
MACHINE.describe(Set.copyOf(MACHINE.states()), Set.of(0)));
7171
}
7272

73+
@Test
74+
void statesAreNamedOnlyInTheSetsShown() {
75+
assertEquals(List.of(List.of("locked")), MACHINE.describeOnly(Set.of(OPEN_LOCKED), Set.of(1)));
76+
assertEquals(List.of(List.of("locked"), List.of("unlocked")),
77+
MACHINE.describeOnly(Set.of(OPEN_LOCKED, CLOSED_UNLOCKED), Set.of(1)));
78+
}
79+
80+
@Test
81+
void setsChangedAreThoseWhoseStatesDiffer() {
82+
assertEquals(Set.of(1), MACHINE.stateSetsChanged(Set.of(OPEN_LOCKED), Set.of(OPEN_UNLOCKED)));
83+
// each set has the same states on both sides, only how they are combined changed
84+
assertEquals(Set.of(0, 1),
85+
MACHINE.stateSetsChanged(Set.of(OPEN_LOCKED, CLOSED_UNLOCKED), Set.of(OPEN_UNLOCKED, CLOSED_LOCKED)));
86+
// the third set changed, and so did how the first two are combined
87+
List<State> before = List.of(state("a", "x", "old"), state("b", "y", "old"));
88+
List<State> after = List.of(state("a", "y", "new"), state("b", "x", "new"));
89+
assertEquals(Set.of(0, 1, 2), machine(before).stateSetsChanged(before, after));
90+
}
91+
7392
@Test
7493
void setsNamedAreThoseTheDescriptionFixes() {
7594
assertEquals(Set.of(1), MACHINE.stateSetsNamed(Set.of(OPEN_UNLOCKED, CLOSED_UNLOCKED)));

0 commit comments

Comments
 (0)