-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Modified calculating order for DefaultStateTransitionComparator #4509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,42 +37,98 @@ void testSimpleOrderingEqual() { | |
|
||
@Test | ||
void testSimpleOrderingMoreGeneral() { | ||
StateTransition transition = StateTransition.createStateTransition(state, "CONTIN???LE", "start"); | ||
StateTransition other = StateTransition.createStateTransition(state, "CONTINUABLE", "start"); | ||
assertEquals(1, comparator.compare(transition, other)); | ||
assertEquals(-1, comparator.compare(other, transition)); | ||
StateTransition generic = StateTransition.createStateTransition(state, "CONTIN???LE", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CONTINUABLE", "start"); | ||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this! A simple naming change makes the tests immediately more readable. |
||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testSimpleOrderingMostGeneral() { | ||
StateTransition transition = StateTransition.createStateTransition(state, "*", "start"); | ||
StateTransition other = StateTransition.createStateTransition(state, "CONTINUABLE", "start"); | ||
assertEquals(1, comparator.compare(transition, other)); | ||
assertEquals(-1, comparator.compare(other, transition)); | ||
StateTransition generic = StateTransition.createStateTransition(state, "*", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CONTINUABLE", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testSubstringAndWildcard() { | ||
StateTransition transition = StateTransition.createStateTransition(state, "CONTIN*", "start"); | ||
StateTransition other = StateTransition.createStateTransition(state, "CONTINUABLE", "start"); | ||
assertEquals(1, comparator.compare(transition, other)); | ||
assertEquals(-1, comparator.compare(other, transition)); | ||
StateTransition generic = StateTransition.createStateTransition(state, "CONTIN*", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CONTINUABLE", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testSimpleOrderingMostToNextGeneral() { | ||
StateTransition transition = StateTransition.createStateTransition(state, "*", "start"); | ||
StateTransition other = StateTransition.createStateTransition(state, "C?", "start"); | ||
assertEquals(1, comparator.compare(transition, other)); | ||
assertEquals(-1, comparator.compare(other, transition)); | ||
StateTransition generic = StateTransition.createStateTransition(state, "*", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "C?", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testSimpleOrderingAdjacent() { | ||
StateTransition transition = StateTransition.createStateTransition(state, "CON*", "start"); | ||
StateTransition other = StateTransition.createStateTransition(state, "CON?", "start"); | ||
assertEquals(1, comparator.compare(transition, other)); | ||
assertEquals(-1, comparator.compare(other, transition)); | ||
StateTransition generic = StateTransition.createStateTransition(state, "CON*", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CON?", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testOrderByNumberOfGenericWildcards() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "*", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "**", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testOrderByNumberOfSpecificWildcards() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "CONTI??ABLE", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CONTI?UABLE", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testOrderByLengthWithAsteriskEquality() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "CON*", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CONTINUABLE*", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testOrderByLengthWithWildcardEquality() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "CON??", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CONTINUABLE??", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testOrderByAlphaWithAsteriskEquality() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "DOG**", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CAT**", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testOrderByAlphaWithWildcardEquality() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "DOG??", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CAT??", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
@Test | ||
void testPriorityOrderingWithAlphabeticComparison() { | ||
StateTransition generic = StateTransition.createStateTransition(state, "DOG", "start"); | ||
StateTransition specific = StateTransition.createStateTransition(state, "CAT", "start"); | ||
assertEquals(1, comparator.compare(generic, specific)); | ||
assertEquals(-1, comparator.compare(specific, generic)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is true that this example sorts the patterns from most generic to most specific (ie in ascending order of specificity), but the comparator itself returns the most specific pattern first and the least specific pattern last. This means the specificity is decreasing, not increasing. I know this is subtle, but I think the initial javadoc
Sorts by decreasing specificity of pattern
is correct (which I seem to have missed when I reviewed #4504). Do you agree?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you said this is subtle so I'm OK with leaving it as is. I've been confused by this many times and I think the more important part is the example in the documentation than the word ascending/descending.
However to detail my thinking for anyone who stumbles on this later:
The way I read it the comparator considers the more generic patterns to be greater than the more specific. So the comparator would return * > ?foo > foo, which I would call ascending order of specificity (most specific last).
This was the confusing part for me (quoting myself from a separate issue):
In short I think the
DefaultStateTransitionComparator
sorts in ascending order of specificity, butFlowJobBuilder
sorts in descending order of specificity.FlowJobBuilder
and descending order of specificity is what the end user interacts with, so maybe it's best to keep the documentation as 'descending' throughout.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the clarification, @robertmcnees ! I see, my interpretation of the natural ordering of elements (ie the meaning/definition of the
>
symbol) in theDefaultStateTransitionComparator
was inaccurate.I believe this is the root cause of the confusion. There is probably a good reason for that (probably the use of
TreeSet
inSimpleFlow
?), but my thinking is that since we want transitions to be sorted by the most specific first (like in the issue's examplefoo*
should be fired before*
), then the comparator should result infoo*
being lower than*
. Similar to the natural ordering of numbers, the analogy I like to compare to is that if I assign weights to transitions (likefoo* -> 1
and* -> 2
), the "natural" ordering coming out of the comparator itself would rankfoo*
lower than*
(that's the "natural" order I was expecting, not the opposite).It would be great if we try to avoid the ordering to be opposite between the comparator and the flow implementation. This is important because as a Batch user, if I want to provide a custom comparator, I would not have to do this mental effort to think backward in the comparator's implementation. Otherwise, we can keep the changes in this PR and add a clear definition of what ">" means in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great point about someone wanting to create their own comparator. I was only thinking about it from my narrow perspective of debugging the code. I think making the
DefaultStateTransitionComparator
matchFlowJobBuilder
makes even more sense with that consideration.I created a new issue #4527 so that we can track the solution there. It will likely involve changes that are out of scope to this PR and issue #3996.