Add :root and :in selectors, fix variable bug #1690
Merged
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.
This commit adds support for the
:in
and:root
functions. The:in
function is a simpler way to check if a shape is in an expression, typically used to test if a variable contains a shape or if a root expression contains a shape.:root
is used to create rooted common subexpressions that are evaluated once against every shape in the model.:root
expressions are evaluate in an isolated context, so any variables used or stored by them are not accessible outside the root selector.:root
selectors allows selection to be broken into multiple steps and evaluate globally.Let's say you want all number shapes that are used in operation inputs, but not used in operation outputs. This can be done today using the following expression:
With the addition of the ``:in` selector, this gets easier because we can avoid using a scoped attribute selector:
(Note: to make this work, I had to uncover and fix a bug in the implementation of how we store variables. We previously used
Collection#add
as aReceiver
, but that method will return false if it's already seen a shape, which is wrong.)With the addition of
:root
, you can use a much simpler expression:(Note: the result of root expressions are run once and cached. No need to store them in a variable)
These expressions seem to be exactly the same, however, the
:root
expression gives a different result when working with models that contain multiple services. In the first two expressions, if any service uses shape X in input and not output, then X is a result. However, in the:root
expression, X is only part of the result if no service uses it in their output shape closures.Issue #, if available:
Description of changes:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.