Skip to content

ST6RI-982 Move transition structure preparation out of semantic getters - #830

Draft
seidewitz wants to merge 8 commits into
masterfrom
ST6RI-982
Draft

seidewitz wants to merge 8 commits into
masterfrom
ST6RI-982

Conversation

@seidewitz

@seidewitz seidewitz commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

This PR continues the line of changes in previous PRs #814 (ST6RI-947), #818 (ST6RI-979) and #822 (ST6RI-981) to separate model preparation from model queries.

Background

Previously, the various textual notations for transitions were all parsed into a TransitionUsage without a required transitionLink feature. This was physically inserted into the abstract syntax tree (AST) later by overriding the method addAdditionalMembers in class TransitionUsageAdapter. Further, the shorthand transition notation in which the source is determined lexically (e.g, of the form "then...") was parsed into a TransitionUsage without the initial owned membership that usually determines the transition source. The source was determined later, also as part of the addAdditionalMembers functionality.

Normally, addAdditionalMembers is called from Namespace::doTransform as part of the semantic transformation of a model. However, it was also possible to call TransactionUsage.getSource() before a full transformation was done, in which case the implementation called TransitionUsageAdapter.addAdditionalMembers on-demand. This coupled model queries with structural model mutations and adapter initialization. Such couplings are being systematically removed from the implementation.

For example, consider following model, with a transition that explicitly declares both the source and target states:

state def S {
    state s1;
    transition s1_s2 first s1 accept T then s2;
    state s2;
}

A transition declaration in this form was parsed as referencing it's source state using an alias membership and containing a succession whose target feature was the transition target. Transforming the transition then resulted in an implied reference subsetting from the source end of the contained succession to the alias member, establishing that member as the succession source feature. The method TransitionUsage.getSource returned the alias member reference to the transition source state.

Now consider a version of this model that uses the implied-source shorthand notaion. In this shorthand, the source state is not explicitly denoted in the transaction, but is determined as the lexically nearest previous state before the transition.

state def S {
    state s1;
    then s2;
    state s2;
}

This was parsed similarly to the previous version of the model, but without an alias membership for the source state. The correct source state was computed during transformation and an alias membership referencing this state was physically added to the TransitionUsage. At this point, the TransitionUsage in the AST has the same form as when parsed from the full notation.

So, during semantic transformation of the model, calling TransitionUsageAdapter.transform included an initial call to TransitionUsageAdapter.addAdditionalMembers. The method TransitionUsageAdapter.addAdditionalMembers did two things:

  1. Called computeSource, which added a source alias membership to the TransitionUsage if it had been missing.
  2. Called computeTransitionLinkConnectors, which (among other things) added a transitionLinkFeature to the TransitionUsage as necessary.

The property getter getSource on the TransitionUsage was also implemented to call addAdditionalMembers on the TransitionUsage before actually getting the source element. This ensured that a TransitionUsage parsed from the shorthand notation actually had its source added to the parse tree, so it could be returned by the getter, in the case that the getter was called before transform. However, in this case, the getSource query could result in the physical mutations to the parsed model as described above.

Considerations

In the previous PRs mentioned at the beginning of this description, the general approach was to move functionality from the adapter class for an element to the post-processor class for the element. This is not the approach taken for the present PR for two reasons:

  1. Adding owned elements during post-processing interferes with the post-processing element iteration (done using a TreeIterator), so some elements may get skipped.
  2. The computation of the source of a shorthand TransitionUsage is not parser-specific and, therefore, should stay in the adapter.

Changes

  1. Parsing.
    • Added an initial Membership to the SysML grammar productions representing the shorthand TransitionUsage notation. For the shorthand, this Membership is parsed with a null memberElement.
    • Added a final owned transitionLinkFeature to all the productions that produce TransitionUsages.
  2. Transformation.
    • Removed adding the (alias) Membership for the source feature and adding the transitionLinkFeature from the TransitionUsageAdapter, but left other processing. This includes (in computeSource) finding the source for a shorthand TransitionUsage parsed with an initial Membership with a null memberElement and setting the memberElement to this source value.
    • Moved calls of computeSource and computeTransitionLinkConnections from addAdditionalMembers to doTransform.
    • Removed the flag guarding against recursion, since it is no longer necessary.
  3. Query.
    • Updated UsageUtil.getSourceFeatureOf(Transition) to remove the call to addAdditionalMembers. This means that the source of a TransitionUsage parsed from the shorthand notation remains null until the TransitionUsage is tansformed.

Tests

Added org.omg.sysml.interactive.tests.TransitionUsageTest:

  1. testTransitionSource – Tests that the source (and target) of a longhand transition declaration are computed correctly.
  2. testTransitionSourceWithFeatureChain - Tests that the source feature and source of a transition usage are computed correctly when the source is a feature chain.
  3. testTransitionShorthandNotation – Tests:
    • A TransitionUsage parsed from the shorthand notation has its source set correctly.
    • Once the source is set for a TransitionUsage parsed from the shorthand notation, that TransitionUsage can change position in the abstract syntax tree for a model, and it's source feature won't change.
    • Moving the shorthand transition declaration in the model text and re-parsing can result in a different transition source.
  4. testSuccessionInTransitionUsage – Tests:
    • For a transition usage that is parsed and not transformed, the referenced feature of the first end of the succession is null.
    • Once the transition usage is transformed, it succession gets the proper source feature.

- Revised productions for textual notation shorthand TransitionUsages to
add EmptyTransitionSourceMember at the beginning.

- Revised all TransitionUsage productions to have TransitionLinkMember
at the end.

- Removed adding these as additional members in the
TransitionUsageAdapter.

- Revised computation of UsageUtil.getSourceFeatureOf(transition).
Currently contains one test method testTransitionShorthandNotation,
which checks that, when a transition is parsed from the shorthand
notation, and then moved programmatically in the model, it retains its
already computed source and target values.

The test passes.
@seidewitz seidewitz added this to the 2026-10 milestone Nov 16, 2026
@seidewitz
seidewitz requested review from adaussy and himi November 16, 2026 07:13
@seidewitz seidewitz self-assigned this Nov 16, 2026
@seidewitz

seidewitz commented Nov 17, 2026 •

Copy link
Copy Markdown
Member Author

@adaussy @himi
Please review by tomorrow (Wed, 17 Nov), so I can include this PR in the 2026-10 release, or let me know you won't be able to. Thanks.

findFirst().orElse(null);
if (sourceFeatureMembership != null && sourceFeatureMembership.getMemberElement() == null) {
Feature source = UsageUtil.getPreviousFeature(transition);
sourceFeatureMembership.setMemberElement(source);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a mutation that can be called from a getter:

getSource / sourceFeature
  → UsageUtil.getSourceFeatureOf
  → TransitionUsageAdapter.computeSource
  → membership.setMemberElement(source)

@seidewitz seidewitz Nov 18, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a trade-off. However, it is essentially the same level of mutation as resolving a proxy, which happens all the time in MF getters. Indeed, I was thinking of possibly using a proxy instead of a null for the un-transformed source member, but I think this would be hard to get to work with the Xtext (which we still need to do). Also, the source feature resolution is entirely reference, so there is no change to the AST composition (like there was before).

That said, I am not sure it is necessary that getSource returns the correct value before transformation. I set it up that way because the code for getSourceFeatureOf previously had a call to addAdditionalMembers, to ensure the source feature was computed. I would need to do some research to determine whether this is still necessary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to remove the mutation by getSource and getSourceFeature and still get all tests to pass, though it took a bit more changes than I had expected.


// Note: Needs to come before clearing and recomputation of inheritance cache.
//checkTransitionUsageSuccessionBindingConnector
computeTransitionLinkConnectors();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this computation into doTransform raises a lifecycle question for long-lived editable models.

After a transition has been transformed, reconnecting its source changes the source reference, but the previously computed binding still refers to the old source.

The inherited binding-enumeration methods only return the stored candidates, and calling ElementUtil.transform again skips doTransform on an already-transformed adapter.

Our prototype refreshed the transition binding candidates when they were requested, replacing candidates whose endpoints had changed. This also allowed reconstruction after clearing the binding lists or replacing the adapter.

What operation should an editor use to refresh these candidates after an edit or cleanup, without requiring a global transformation and neither a global cash invalidation? Could we add a regression test covering a source reconnection followed by binding enumeration?

@seidewitz seidewitz Nov 18, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This computation was always in doTransform, because Namespace.doTransform calls addAdditionalMembers. It was just moved out of TransitionUsageAdapter.addAdditionalMembers (which is a goal of this PR) directly into doTransform.

Also, the point you are making is not specific to this PR. It is really just a variant of the "when to clear caches?" question. For example, the shorthand for SuccessionAsUsage requires the same lexical source search as the shorthand for TransitionUsage (indeed, the source and target for a TransitionUsage really get set in its nested succession).

Now, these shorthands are really intended for use in the textual notation. They are not available in the graphical notation. When a segment of a textual notation is re-parsed, then new abstract syntax objects are created for the resulting parse subtree, with new adapters ready for a new round of transformation.

But, if you want to get this same effect without re-parsing an element subtree, you could use ElementUtil.clean(Element), which removes the current adapter object from an element. Then, the next time the adapter for that element is retrieved, a new one is created that is untransformed. (This also effectively drops any caches implemented in the adapter.)

If, instead, you want to have a specific method for clearing the isTransformed flag or an option to not set this flag, that would not be very hard to add. But it should probably have its own issue separate from (and more generally than) what is being addressed in this PR.

findFirst().orElse(null);
Membership sourceFeatureMembership = computeSourceFeatureMembershipOf(transition);
if (sourceFeatureMembership != null) {
Feature sourceFeature = FeatureUtil.getBasicFeatureOf((Feature)sourceFeatureMembership.getMemberElement());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the returned feature for a chained source. Previously, the basic feature was used to check whether the source represented an ActionUsage, but the method returned the original feature referenced by the membership.

For first off.nested, the previous implementation returned the chain, whereas this implementation returns its final feature, nested.

Is this change intentional for the contract of sourceFeature()? The final getSource() getter already normalizes its result, so these two APIs should be checked separately. Could we add a test covering the source membership, sourceFeature() and getSource() for a chained source?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mistake. I'll fix it. Thanks for the catch.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


ownedMembers.remove(transition);
ownedMembers.add(transition);
ElementUtil.transform(transition);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this call actually execute doTransform again? ElementAdapter.transform() skips it when the adapter is already marked as transformed.

The endpoint-stability assertions are useful, but this call does not appear to exercise a second transformation. Could we clarify the test comment and add separate coverage for the intended binding-refresh lifecycle after a programmatic edit?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I was kind of thinking that the idempotency of transform was part of the point here. However, I agree the test would be stronger if an ElementUtil.clean(transition) call is added, so that the transformation really has to run again. I have confirmed that the test still passes with this modification.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- Added tests for longhand transitions with and without a feature chain
as source. (Test with source feature chain fails.)

- Updated testTransitionShorthandNotation to clean element before
transforming again.

/**
* This flag records if the source or target of a SuccessionAsUsage that is the succession of a TransitionUsage
* have been set correctly. If not, a recomputation of implicit general types later my resolve the problem.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling ? may ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a mistype. I corrected it by amending the head commit and doing a force push. This is a case in which it really didn't seem worth devoting a commit to a one character correction!

* This flag records if the source or target of a SuccessionAsUsage that is the succession of a TransitionUsage
* have been set correctly. If not, a recomputation of implicit general types later my resolve the problem.
*/
protected boolean isRecomputeImplicitGeneralTypes = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this flag may complicated a long-lived editable model. See my general comment review.

@seidewitz seidewitz Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field isRecomputeImplicitGeneralTypes is not persistent state. It is simply a way to communicate between the methods addDefaultGeneralType and computeImplicitGeneralType on how isComputeImplicitGeneralTypes (inherited from TypeAdapter) needs to be set. This is necessary, because isComputeImplicitGeneralTypes is set to false after the call from TypeAdapter.computeImplicitGeneralType() to addDefaultGeneralType(). I tried moving this set to before the call, but this resulted in a few test failures (one KerML test involving metadata and a couple virtual container tests).

However, I will admit that it is rather poor style to implement this kind of local communication between methods using a class field. So I have re-implemented it without this field by splitting the part of TypeAdapter.computeImplicitGeneralType() the makes the call to addDefaultGeneralType() and sets isComputeImplicitGeneralTypes to false into a separately overridable method (see below). This shows that no new persistent state is required for the proposed implementation.

What is persistent state, though, is the TypeAdapter.isComputeImplicitGeneralTypes flag, which already existed before this PR. I tried removing this flag, but that resulted in two to three times worse performance on the batch parse/transform/validate performance tests. So the flag is necessary for the current Xtext-based pilot implementation.

While not clearly delineated in the code (this is one of the older parts of the codebase), the isComputeImplicitGeneralTypes flag basically singles the caching of the implicit general types. So this flag should be handled together with caching in other adapters. But this is beyond the scope of the present PR. The important thing is that the implementation proposed here doesn't make the caching situation any worse.

@adaussy

adaussy commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Hi Ed,

Thank you for addressing the review comments. Removing source completion from the getters, preserving chained sources and strengthening the transformation test are useful improvements.

Regarding the new isRecomputeImplicitGeneralTypes field in ReferenceUsageAdapter, I understand the problem it solves: an endpoint may be queried before the transition’s shorthand source has been established, and that incomplete computation must not prevent a later attempt from finding the source.

My concern is about its lifecycle in a long-lived editable model. The flag handles “the source is not available yet”, but it does not appear to handle “the source was successfully computed and has subsequently changed”.

For example:

state def Machine {
    state off;
    state on;
    transition t first off then on;
}

After processing this model, the succession’s source end has an implicit reference subsetting to off. If a graphical command reconnects the transition source to on, the retry flag is already false. Re-enabling computation alone would not seem sufficient either, since the existing implicit reference subsetting prevents a replacement from being added.

I checked the proposed ElementUtil.clean(transition) approach. It only removes the adapter from the transition itself; it does not reset adapters on its owned elements. Cleaning and transforming the transition can therefore rebuild its binding candidates while leaving the succession source end’s previously computed implicit reference subsetting unchanged.

Calling transformAll after cleaning only the transition would not necessarily resolve this: the owned elements retain their adapters and their isTransformed flags. Also, if implied relationships have already been materialized, removing adapters does not remove those relationships.

The updated movement test is useful, but it preserves the source, so the results retained by the succession ends remain valid. It does not exercise a reconnection after a successful initial computation.

I take your point that this belongs to the broader invalidation question and is not entirely introduced by this PR. However, this additional flag illustrates the difficulty we encountered in ST6RI-969: stored candidates, computation control and the completeness of previous results are managed together across the adapter hierarchy. Clients cannot reliably refresh one result without understanding the state held by several related adapters.

I am not suggesting that adapters should never contain state, or that this flag should be removed without an equivalent mechanism. Could we document the intended lifecycle and add a test that:

  1. Computes the transition and succession endpoints successfully.
  2. Changes the transition’s explicit source from off to on.
  3. Applies the recommended invalidation and preparation operations.
  4. Verifies that both the binding candidates and the succession source end follow on, without retaining the previous reference to off.

We can address the general lifecycle in a separate issue, but identifying and testing the required invalidation scope here would give downstream applications a concrete integration contract.

Longer term, I would prefer to represent result completeness explicitly and separate request-local evaluation state from optional persistent caching. That direction does not depend on adopting the proposed rule engine.

- Updated UsageUtil.getSourceFeatureOf() so it no longer calls
TransitionUsageAdapter.computeSource().

- Updated ReferenceUsageAdapter so implied SuccessionAsUsage
related features can be recomputed later if null now.
TypeAdapter now provides an overridable method "addImplicitGeneralTypes"
with an parameter for setting the value of "isComputeImplicitTypes".
- Add cleanAll methods to ElementUtil.
- Updated test to use cleanAll and transformAll in second step.
@seidewitz

Copy link
Copy Markdown
Member Author

In the last couple of commits, I have implemented the following:

  1. Updated TypeAdapter so it now provides an overridable method addImplicitGeneralTypes
    with a parameter for setting the value ofisComputeImplicitTypes. This is overridden in ReferenceUsageAdapter, eliminating the need for the isRecomputeImplicitGeneralTypes field.
  2. Added cleanAll methods to ElementUtil, which cleans the AST(s) routed in a specific element, resource or resource set. The second step of testTransitionUsageShorthandNotation now calls ElementUtil.cleanAll(stateDef) followed by transformAll(stateDef). The test still passes.

As necessary:
- Adds Membership with no member element as the first ownedRelationship
of the TranstionUsage.
- Adds an empty transitionLinkFeature.
@seidewitz

Copy link
Copy Markdown
Member Author

@adaussy
I have added methods to UsageUtil to programmatically support the transition of TransitionUsages persisted in XMI or JSON before the inclusion of the changes in this PR. There should also be a CLI utility class, but I have not done that.

@seidewitz
seidewitz marked this pull request as draft November 25, 2026 23:08
@seidewitz seidewitz modified the milestones: 2026-10, 2026-11 Nov 25, 2026
@seidewitz

Copy link
Copy Markdown
Member Author

This PR has been moved to the 2026-11 milestone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants