Skip to content

Comments

#2640 implement merge behavior on Reactions#2641

Closed
benjaperez1983 wants to merge 7 commits intodevelopfrom
2640-implement-merge-behavior-for-reactions
Closed

#2640 implement merge behavior on Reactions#2641
benjaperez1983 wants to merge 7 commits intodevelopfrom
2640-implement-merge-behavior-for-reactions

Conversation

@benjaperez1983
Copy link
Contributor

@benjaperez1983 benjaperez1983 commented Sep 16, 2025

Fixes #2640

Description

Merge behavior for reactions

Type of change

Please mark relevant options with an x in the brackets.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires documentation changes (link at least one user or developer documentation issue):
  • Algorithm update - updates algorithm documentation/questions/answers etc.
  • Other (please describe):

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Integration tests
  • Unit tests
  • Manual tests
  • No tests required

Reviewer checklist

Mark everything that needs to be checked before merging the PR.

  • Check if the code is well documented
  • Check if the behavior is what is expected
  • Check if the code is well tested
  • Check if the code is readable and well formatted
  • Additional checks (document below if any)
  • Check if documentation update issue(s) are created if the option This change requires a documentation update above is selected

Screenshots (if appropriate):

Questions (if appropriate):

@benjaperez1983 benjaperez1983 marked this pull request as draft September 16, 2025 15:16
@benjaperez1983 benjaperez1983 requested review from msevestre and rwmcintosh and removed request for msevestre September 16, 2025 16:13
Copy link
Member

@msevestre msevestre left a comment

Choose a reason for hiding this comment

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

ok some feedback. This will change completely once the merge stuff that I am doing is implemented. For nwo my suggestgion would be to have one method that takes two reaction, a merge and returns the merge results.. This should be tested and this shoudl be enough.

overall the code is veyr hard to read. Did you generate this code with AI somehow?

@benjaperez1983
Copy link
Contributor Author

ok some feedback. This will change completely once the merge stuff that I am doing is implemented. For nwo my suggestgion would be to have one method that takes two reaction, a merge and returns the merge results.. This should be tested and this shoudl be enough.

overall the code is veyr hard to read. Did you generate this code with AI somehow?

I have resolved the comments how, I have some remarks anyways.

On the Cloning, you are right there is a whole logic for it (which I`m reusing right now) but nevertheless there are some things that are not being fully cloned (such as formula and parameters), so I added that to it.

Regarding your other question, funny thing I had a very large and difficult to read nested methods which I asked the AI to simplify, turned out to be even harder to read, so I rolled that back and try refactoring in a way it is better now.
So please feel free to go through it again and let me know.

Thanks!

@msevestre
Copy link
Member

@benjaperez1983 Let's meet when you have time this week. Most of this code is obsolete I believe. I am going to comment on the PR what I believe will be kept

Copy link
Member

@msevestre msevestre left a comment

Choose a reason for hiding this comment

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

I suggest to close this PR and to make one towards #2603 implementing the mergeReactions method

     private void mergeReactions(ReactionBuilder target, BuilderSource<ReactionBuilder> source)
     {
        //TODO add reaction merge behavior
     }

integrations tests should be written by modifying the ModuleHelperForTests ensuring that we have the right behavior for OVERWRITE (is given normally since the code is generic) and EXTEND (is bb specific)

private void performMerge()
{
cacheBuilders(x => x.Reactions, _reactions);
var mergedReactions = mergeReactions(ReactionAndMergeBehaviors);
Copy link
Member

Choose a reason for hiding this comment

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

remove when implemented in #2603

internal IReadOnlyList<(EventGroupBuildingBlock eventGroupBuildingBlock, MergeBehavior mergeBehavior)> EventGroupAndMergeBehaviors =>
_simulationConfiguration.ModuleConfigurations.Where(x => x.Module.EventGroups != null).Select(x => (x.Module.EventGroups, x.MergeBehavior)).ToList();

internal IReadOnlyList<(ReactionBuildingBlock reactionBuildingBlock, MergeBehavior mergeBehavior)> ReactionAndMergeBehaviors =>
Copy link
Member

Choose a reason for hiding this comment

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

remove when implemented in #2603

}
else
{
extendReaction(current, incoming);
Copy link
Member

Choose a reason for hiding this comment

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

this is the method that we need to probably port over (the merge). The rest is done dynamically using the clone as I suggested (yes it also clones the formula)

Copy link
Contributor Author

@benjaperez1983 benjaperez1983 Oct 7, 2025

Choose a reason for hiding this comment

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

Well, if I dont copy the formula as Im doing now. I get this over many tests.
Message: 
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.NullReferenceException : Object reference not set to an instance of an object.

All related with the formula.
So maybe I`m not doing this as I should. Will mark it for conversation


private static void extendReaction(ReactionBuilder target, ReactionBuilder incoming)
{
var byNameParam = target.Parameters.ToDictionary(x => x.Name, StringComparer.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

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

none of thos management of parmaeter stuff is required. We have the contaienr task that merge container structure already for us. It's being used in #2603

target.Dimension = incoming.Dimension ?? target.Dimension;

if (incoming.ContainerCriteria != null)
target.ContainerCriteria = incoming.ContainerCriteria;
Copy link
Member

Choose a reason for hiding this comment

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

I am not srue about this line to be honest.


private static void extendReaction(ReactionBuilder target, ReactionBuilder incoming)
{
var byNameParam = target.Parameters.ToDictionary(x => x.Name, StringComparer.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

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

this feels a bit heavy considering we have a functiont aht returns null already
the code is already implemented for this in the merge taks so we can ignroe

      public void AddOrReplaceInContainer(IContainer container, IEntity entityToAddOrReplace)
      {
         var existingChild = container.GetSingleChildByName(entityToAddOrReplace.Name);
         if (existingChild != null)
            container.RemoveChild(existingChild);

         container.Add(entityToAddOrReplace);
      }


copy.Formula = incoming.Formula;

copy.CreateProcessRateParameter = incoming.CreateProcessRateParameter;
Copy link
Member

Choose a reason for hiding this comment

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

this should alreayd be taken over from the updatePropertiesFrom

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, some failing tests if I exclude this in relation with the createprocessrate.


copy.CreateProcessRateParameter = incoming.CreateProcessRateParameter;
copy.ProcessRateParameterPersistable = incoming.ProcessRateParameterPersistable;
copy.Id = incoming.Id;
Copy link
Member

Choose a reason for hiding this comment

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

that's the important bit. that is missing in #2612

@rwmcintosh
Copy link
Member

@benjaperez1983 do you need this PR or branch anymore?

@rwmcintosh
Copy link
Member

@benjaperez1983 close this one?

@benjaperez1983
Copy link
Contributor Author

@rwmcintosh closed, was waiting until the other branch (based on michael`s) was reviewed, but I believe we can close it now.

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.

Implement "Merge" behavior for reactions.

3 participants