Ejemplo n.º 1
0
        /// <summary>
        /// Return all models within scope of the specified relative model.
        /// </summary>
        /// <param name="relativeTo">The model calling this method</param>
        /// <returns>The found models or an empty array if not found.</returns>
        public Model[] FindAll(Model relativeTo)
        {
            // Look in cache first.
            string cacheKey = "ALLINSCOPE";
            object value    = GetFromCache(cacheKey, relativeTo);

            if (value != null)
            {
                return(value as Model[]);
            }

            // Get all children first.
            List <Model> modelsInScope = new List <Model>();

            foreach (Model child in Apsim.ChildrenRecursively(relativeTo))
            {
                modelsInScope.Add(child);
            }

            // Add relativeTo.
            //modelsInScope.Add(relativeTo);

            // Get siblings and parents siblings and parents, parents siblings etc
            // until we reach a Simulations or Simulation model.
            if (!(relativeTo is Simulations))
            {
                Model relativeToParent = relativeTo;
                do
                {
                    foreach (IModel model in Apsim.Siblings(relativeToParent))
                    {
                        modelsInScope.Add(model as Model);
                    }
                    relativeToParent = relativeToParent.Parent as Model;

                    // Add in the top level model that we stopped on.
                    if (relativeToParent != null)
                    {
                        modelsInScope.Add(relativeToParent);
                    }
                }while (relativeToParent != null && !(relativeToParent is Simulation) && !(relativeToParent is Simulations));
            }

            // Add the in scope models to the cache and return them
            AddToCache(cacheKey, relativeTo, modelsInScope.ToArray());
            return(modelsInScope.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Give the specified model a unique name
        /// </summary>
        /// <param name="modelToCheck">The model to check the name of</param>
        public static void EnsureNameIsUnique(IModel modelToCheck)
        {
            string        originalName = modelToCheck.Name;
            string        newName      = originalName;
            int           counter      = 0;
            List <IModel> siblings     = Apsim.Siblings(modelToCheck);
            IModel        child        = siblings.Find(m => m.Name == newName);

            while (child != null && child != modelToCheck && counter < 10000)
            {
                counter++;
                newName = originalName + counter.ToString();
                child   = siblings.Find(m => m.Name == newName);
            }

            if (counter == 1000)
            {
                throw new Exception("Cannot create a unique name for model: " + originalName);
            }

            modelToCheck.Name = newName;
        }