コード例 #1
0
ファイル: Simulation.cs プロジェクト: slicol/SpiceSharp
        /// <summary>
        /// Collect behaviors of all circuit entities while also setting them up
        /// </summary>
        /// <typeparam name="T">Base behavior</typeparam>
        /// <returns></returns>
        protected BehaviorList <T> SetupBehaviors <T>(IEnumerable <Entity> entities) where T : Behavior
        {
            if (entities == null)
            {
                throw new ArgumentNullException(nameof(entities));
            }

            // Register all behaviors
            foreach (var o in entities)
            {
                T behavior = o.CreateBehavior <T>(this);
                if (behavior != null)
                {
                    EntityBehaviors.Add(o.Name, behavior);
                }
            }
            return(EntityBehaviors.GetBehaviorList <T>());
        }
コード例 #2
0
ファイル: Simulation.cs プロジェクト: jeason1997/SpiceSharp
        /// <summary>
        /// Set up all behaviors previously created.
        /// </summary>
        /// <param name="entities">The circuit entities.</param>
        private void SetupBehaviors(IEnumerable <Entity> entities)
        {
            var behaviors = new HashSet <IBehavior>();

            foreach (var entity in entities)
            {
                behaviors.Clear();

                // Create the behaviors in the reverse order to allow inheritance
                for (var i = BehaviorTypes.Count - 1; i >= 0; i--)
                {
                    IBehavior behavior = null;
                    var       type     = BehaviorTypes[i];

                    // Try to reuse a behavior first
                    if (EntityBehaviors.TryGetBehaviors(entity.Name, out var ebd))
                    {
                        // If the entity behaviors already contains the type, reuse that object
                        ebd.TryGetValue(type, out behavior);
                    }

                    // If it doesn't exist, request a new behavior
                    if (behavior == null)
                    {
                        behavior = entity.CreateBehavior(type, this);
                    }

                    // Add the behavior to the pool
                    if (behavior != null)
                    {
                        EntityBehaviors.Add(type, entity.Name, behavior);
                        behaviors.Add(behavior);
                    }
                }

                // Setup the distinct behaviors
                foreach (var behavior in behaviors)
                {
                    entity.SetupBehavior(behavior, this);
                }
            }
        }