Skip to content

Ensure we keep the order of one-many relations after export/import to JSON/MSE - #1154

Closed
jecisc wants to merge 2 commits into
moosetechnology:developmentfrom
jecisc:relations-order
Closed

jecisc wants to merge 2 commits into
moosetechnology:developmentfrom
jecisc:relations-order

Conversation

@jecisc

@jecisc jecisc commented Sep 23, 2026

Copy link
Copy Markdown
Member

Problem

It's a known problem that multivalued relations do not keep the order.

The problem is that for one-many relations we do not export the many side, only the one side. (The many side is derived). Then, when we load the entities, the one side adds itself in the many side because of the slot keeping bedirectional relations. So they end up added in the order of the entities in the file.

We already got the problem in the past for example for properties such as #parameters and most of the time we add manually a method like #orderedParameters to sort by source anchor. But this has cons: Add a method manually
The user needs to know the difference
Takes more time since it computes it at each call

I see 3 solutions and this change is the implementation of the first solution.

Solution 1: Reverse de derived relation. Instead of deriving the many side, derive the one side. Pro:

  • Keeps the order Cons:
  • This fix the problem on the Famix generated methods side, but not on the Fame side (can still happen if we add relations by hand)
  • Do not fix many-many relations, but we have no such relations for now in Moose known models

Solution 2: Add a hook to have a post import phase. In Famix we could add something that iterates the multivalued relations to reorder entities with a source anchor and reorder based on the source anchor Pro:

  • Keeps the order
  • Works for entities added by hand Cons:
  • Famix specific, not in Fame (but Fame is not used a lot without Famix so it's not that important)
  • Slows down the import (which is sad since people are working to speed it up)
  • Does not work on entities without source anchor
  • Adds complexity to the import process

Solution 3: Add in the exported file a data for the order of derived multivalued properties and use it after the import to reorder the multivalued slots on post load (but in Fame directly since it's not dependent on source anchors) Pros:

  • Keeps the order
  • Fix in Fame directly, works all the time Cons: 
  • Slows down the import (but way less than solution 2 since we already have the order)
  • Makes the process a little more complex
  • Needs more work since it change the format of the MSE/JSON file. This means we need to update VVJ as well

Personally I'd like to fix it at the Fame level. But it requires the most work and I know that Nicolas and BL have ideas to update the export format.

I'd go with solution 1, and note this problem for the change of format planned to try and tackle all our problems at once with this project. The goals been:

  • Improve the speed
  • Decrease the size of the file
  • Be able to add metadatas
  • Keep order of multivalued relations

… JSON/MSE

**Problem**

It's a known problem that multivalued relations do not keep the order. 

The problem is that for one-many relations we do not export the many side, only the one side. (The many side is derived). Then, when we load the entities, the one side adds itself in the many side because of the slot keeping bedirectional relations. So they end up added in the order of the entities in the file. 

We already got the problem in the past for example for properties such as #parameters and most of the time we add manually a method like #orderedParameters to sort by source anchor. But this has cons:
Add a method manually
The user needs to know the difference
Takes more time since it computes it at each call

I see 3 solutions and this change is the implementation of the first solution. 

**Solution 1**: Reverse de derived relation. Instead of deriving the many side, derive the one side. 
Pro:
- Keeps the order
Cons:
- This fix the problem on the Famix generated methods side, but not on the Fame side (can still happen if we add relations by hand)
- Do not fix many-many relations, but we have no such relations for now in Moose known models

**Solution 2**: Add a hook to have a post import phase. In Famix we could add something that iterates the multivalued relations to reorder entities with a source anchor and reorder based on the source anchor
Pro: 
- Keeps the order
- Works for entities added by hand
Cons:
- Famix specific, not in Fame (but Fame is not used a lot without Famix so it's not that important)
- Slows down the import (which is sad since people are working to speed it up)
- Does not work on entities without source anchor
- Adds complexity to the import process

**Solution 3**: Add in the exported file a data for the order of derived multivalued properties and use it after the import to reorder the multivalued slots on post load (but in Fame directly since it's not dependent on source anchors)
Pros:
- Keeps the order
- Fix in Fame directly, works all the time
Cons: 
- Slows down the import (but way less than solution 2 since we already have the order)
- Makes the process a little more complex
- Needs more work since it change the format of the MSE/JSON file. This means we need to update VVJ as well

Personally I'd like to fix it at the Fame level. But it requires the most work and I know that Nicolas and BL have ideas to update the export format. 

I'd go with solution 1, and note this problem for the change of format planned to try and tackle all our problems at once with this project. The goals been:
- Improve the speed
- Decrease the size of the file
- Be able to add metadatas
- Keep order of multivalued relations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this the whole change, and everything else is generated?

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! I should have done 2 commits sorry.

I'm also writing a regression test right now

@badetitou badetitou left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I dislike the idea of modifying the generator to update <derived> so that the export using fame is modified.

We are just pointing out that there is a more fundamental problem of the goal of <derived>, and this is what must be updated.
It does not seem that this modification will break anything so far for me.
But we are just delaying the need of real solution and that's bad

I won't block the PR for that so far.. But that's my strict opinion

@jecisc

jecisc commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

I dislike the idea of modifying the generator to update <derived> so that the export using fame is modified.

We are just pointing out that there is a more fundamental problem of the goal of <derived>, and this is what must be updated. It does not seem that this modification will break anything so far for me. But we are just delaying the need of real solution and that's bad

I won't block the PR for that so far.. But that's my strict opinion

I agree. To me this is a short term fix.

I'd like to have a better fix if we update the export format to split entities/relations. If we do that, we need to update the way to export relations. At that moment, we can chose a way that does not depend on the derived pragma.

@Ducasse

Ducasse commented Sep 23, 2026

Copy link
Copy Markdown

what is the cost of fixing the real cause?

@jecisc

jecisc commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

I am checking today how costly it would be. In fact the fixing the real cause could help me for another feature so I'm trying to do it. I'll fallback to this PR is it's too long to fix the real cause

@jecisc

jecisc commented Sep 25, 2026

Copy link
Copy Markdown
Member Author

I propose to replace this PR by this one: moosetechnology/Fame#131

@jecisc

jecisc commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of the new way I implemented

@jecisc jecisc closed this Sep 28, 2026
@jecisc
jecisc deleted the relations-order branch September 28, 2026 11:47
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.

4 participants