APSIMInitiative / ApsimX

ApsimX is the next generation of APSIM
http://www.apsim.info
Other
130 stars 160 forks source link

Duplicate name checking should be extended to folders #8884

Open HamishBrownPFR opened 4 months ago

HamishBrownPFR commented 4 months ago

Describe the new feature

In the example on the screen shot below (attached .apsimx file also) we have a lifecycle organism called Septoria and an Infestation object called AscosporeInfestation which sets up spore populations in the Septoria model. However it throws an error because there is a manager script in the folder above also named Septoria. Renaming the manager module to something else fixes the problem. In the back ground the infestation model uses .FindInScope to find a LifeCycle object of name Septoria and cast it to a LifeCycle object. But that does not work because the first thing FindInScope finds called Septoria is a manager.

It is not possible to have duplicate names for children of an object but this rule is not extented to children within folders. Would it be possible to enforce non-duplicate naming into folders to stop this error occurring?

The problem is partially fixed by including a LifeCylce type in the find (i.e FindInScope(InfestingOranisumName)) in Infestation.cs. However, the report model also suffers the same problem. i.e when trying to report Septoria.Spores.TotalPopulation the report model finds the manager script named Septoria and throws an error because it has not child called Spores

image image BaseDevelopment.zip

HamishBrownPFR commented 4 months ago

To show this is not an obscure issue limited to the case I have put up here. I renamed the manager from "Septoria" to "Fertiliser" (that is what it is actaully doing) and that causes errors because report finds the manager script called fertilizer instead of the Fertiliser object

par456 commented 4 months ago

The reason for nodes not being able to share a name is due to the full path being used as the ID for referencing a specific node. That obviously doesn't apply if the two nodes are under different parents as their full paths are different. Part of the problem also is that Folders were originally supposed for scope limiting, but that only happens sometimes because the locator methods aren't consistent.

The problem you are running up against here is that the locator (FindInScope) doesn't care about the type of model you are looking for. It goes purely by name and will return the first model that it can find with that name, even if it's not the one you actually need.

I think you should be able to use FindInScope<LifeCycle>() to give it a type to look for and that should at least force it to keep searching until it finds what you need.

In the reports when I've seen this conflict, I've just added parent in as well. So instead of [Septoria].Variable I'd have [Block].Septoria.Variable so that it knows to start at the block level instead of in the folder.

HamishBrownPFR commented 4 months ago

The simplest work around is just to not give things duplicate names and then we don't get these clashes. The problem is when duplicate names are given inadvertently.