/// <inheritdoc/> public virtual bool TryGetBehaviors(string name, out IBehaviorContainer ebd) { _lock.EnterUpgradeableReadLock(); try { if (_dictionary.TryGetValue(name, out ebd)) return true; // Try asking our event var args = new BehaviorsNotFoundEventArgs(name); OnBehaviorsNotFound(args); if (args.Behaviors != null) { ebd = args.Behaviors; return true; } // Nothing found ebd = null; return false; } finally { _lock.ExitUpgradeableReadLock(); } }
/// <inheritdoc/> public void Add(IBehaviorContainer behaviors) { behaviors.ThrowIfNull(nameof(behaviors)); _lock.EnterUpgradeableReadLock(); try { if (_dictionary.ContainsKey(behaviors.Name)) { throw new ArgumentException(Properties.Resources.Behaviors_BehaviorsAlreadyExist.FormatString(behaviors.Name)); } _lock.EnterWriteLock(); try { _dictionary.Add(behaviors.Name, behaviors); _values.Add(behaviors); } finally { _lock.ExitWriteLock(); } } finally { _lock.ExitUpgradeableReadLock(); } }
/// <summary> /// Initializes a new instance of the <see cref="BindingContext"/> class. /// </summary> /// <param name="entity">The entity creating the behavior.</param> /// <param name="simulation">The simulation for which a behavior is created.</param> /// <param name="behaviors">The behavior container.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entity"/> or <paramref name="simulation"/> is <c>null</c>.</exception> public BindingContext(IEntity entity, ISimulation simulation, IBehaviorContainer behaviors) { Entity = entity.ThrowIfNull(nameof(entity)); Simulation = simulation.ThrowIfNull(nameof(simulation)); Behaviors = behaviors; _cloned = entity.LinkParameters ? null : new Dictionary <IParameterSet, IParameterSet>(); }
/// <inheritdoc/> public void Resolve(ISimulation simulation, IBehaviorContainer container, TContext context) { var builder = container.Build(simulation, context); foreach (var action in _builders) { action(builder); } }
/// <summary> /// Initializes a new instance of the <see cref="CurrentControlledBindingContext" /> class. /// </summary> /// <param name="component">The component that creates the behavior.</param> /// <param name="simulation">The simulation for which the behavior is created.</param> /// <param name="behaviors">The created behaviors.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="component"/> or <paramref name="simulation"/> is <c>null</c>.</exception> public CurrentControlledBindingContext(ICurrentControllingComponent component, ISimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { // Gets the current controlling behaviors if (component.ControllingSource != null) { ControlBehaviors = simulation.EntityBehaviors[component.ControllingSource]; } }
/// <inheritdoc/> public void Resolve(ISimulation simulation, IEntity entity, IBehaviorContainer container) { // Reading is not really a problem var context = ContextFactory.Get <TContext>(simulation, entity, container); var builder = container.Build(simulation, context); foreach (var action in _builders) { action(builder); } }
/// <summary> /// Creates a new binding context. /// </summary> /// <param name="simulation">The simulation.</param> /// <param name="entity">The entity.</param> /// <param name="container">The behavior container.</param> /// <returns>The new context.</returns> public static TContext Get(ISimulation simulation, IEntity entity, IBehaviorContainer container) { if (_contextFactory == null) { lock (_lock) { if (_contextFactory == null) { CreateFactory(); } } } return(_contextFactory(simulation, entity, container)); }
/// <summary> /// Initializes a new instance of the <see cref="BindingContext"/> class. /// </summary> /// <param name="component">The component creating the behavior.</param> /// <param name="simulation">The simulation for which a behavior is created.</param> /// <param name="behaviors">The behaviors created by the entity.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="component"/> or <paramref name="simulation"/> is <c>null</c>.</exception> public ComponentBindingContext(IComponent component, ISimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { // Get the nodes of the component var nodes = component.Nodes; string[] myNodes; if (nodes != null && nodes.Count > 0) { myNodes = new string[nodes.Count]; for (var i = 0; i < nodes.Count; i++) { if (nodes[i] == null) { myNodes[i] = Constants.Ground; SpiceSharpWarning.Warning(this, Properties.Resources.Nodes_NullToGround.FormatString(component.Name, i)); } else { myNodes[i] = nodes[i]; } } } else { myNodes = Array <string> .Empty(); } Nodes = myNodes; // Get the model of the component if (component.Model != null) { ModelBehaviors = simulation.EntityBehaviors[component.Model]; } else { ModelBehaviors = null; } }
public static ICodeBlock GetMethodCallForBehavior(BehaviorSave behavior, IBehaviorContainer container) { return GetMethodCallForBehavior(behavior.Name, container); }
/// <summary> /// Initializes a new instance of the <see cref="BindingContext" /> class. /// </summary> /// <param name="component">The component that creates the behavior.</param> /// <param name="simulation">The simulation for which the behavior is created.</param> /// <param name="behaviors">The created behaviors.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="component"/>, <paramref name="simulation"/> or <paramref name="behaviors"/> is <c>null</c>.</exception> public BindingContext(MutualInductance component, ISimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { Inductor1Behaviors = simulation.EntityBehaviors[component.InductorName1.ThrowIfNull(nameof(component.InductorName1))]; Inductor2Behaviors = simulation.EntityBehaviors[component.InductorName2.ThrowIfNull(nameof(component.InductorName2))]; }
/// <summary> /// Initializes a new instance of the <see cref="BehavioralBindingContext" /> class. /// </summary> /// <param name="component">The component creating the behavior.</param> /// <param name="simulation">The simulation for which a behavior is created.</param> /// <param name="behaviors">The created behaviors.</param> public BehavioralBindingContext(IComponent component, ISimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { }
/// <summary> /// Initializes a new instance of the <see cref="SubcircuitBindingContext"/> class. /// </summary> /// <param name="component">The component creating the behavior.</param> /// <param name="simulation">The simulation for which behaviors are created.</param> /// <param name="behaviors">The behaviors created by the entity.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="component"/>, <paramref name="simulation"/> or <paramref name="behaviors"/> is <c>null</c>.</exception> public SubcircuitBindingContext(IComponent component, SubcircuitSimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { }
/// <summary> /// Initializes a new instance of the <see cref="CurrentSwitchBindingContext"/> class. /// </summary> /// <param name="component">The component that creates the behavior.</param> /// <param name="simulation">The simulation for which the behavior is created.</param> /// <param name="behaviors">The created behaviors.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="component"/>, <paramref name="simulation"/> or <paramref name="behaviors"/> is <c>null</c>.</exception> public CurrentSwitchBindingContext(ICurrentControllingComponent component, ISimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { var branch = ControlBehaviors.GetValue <IBranchedBehavior <double> >().Branch; ControlValue = () => branch.Value; }
/// <summary> /// Initializes a new instance of the <see cref="BehaviorContainerBuilder{TContext}"/> class. /// </summary> /// <param name="container">The container.</param> /// <param name="simulation">The simulation.</param> /// <param name="context">The context.</param> public BehaviorContainerBuilder(IBehaviorContainer container, ISimulation simulation, TContext context) { _container = container.ThrowIfNull(nameof(container)); _simulation = simulation.ThrowIfNull(nameof(simulation)); _context = context; }
/// <summary> /// Gets a binding context of the specified type. /// </summary> /// <typeparam name="TContext">The type of the context.</typeparam> /// <param name="simulation">The simulation.</param> /// <param name="entity">The entity.</param> /// <param name="container">The container.</param> /// <returns>The binding context.</returns> public static TContext Get <TContext>(ISimulation simulation, IEntity entity, IBehaviorContainer container) where TContext : IBindingContext { var factory = _factories.GetOrAdd(typeof(TContext), CreateFactory); return((TContext)factory(simulation, entity, container)); }
/// <summary> /// Initializes a new instance of the <see cref="VoltageSwitchBindingContext"/> class. /// </summary> /// <param name="component">The component creating the behavior.</param> /// <param name="simulation">The simulation for which a behavior is created.</param> /// <param name="behaviors">The behaviors created by the entity.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="simulation"/>, <paramref name="component"/> or <paramref name="behaviors"/> is <c>null</c>.</exception> public VoltageSwitchBindingContext(IComponent component, ISimulation simulation, IBehaviorContainer behaviors) : base(component, simulation, behaviors) { var state = GetState <IBiasingSimulationState>(); var a = state.GetSharedVariable(Nodes[2]); var b = state.GetSharedVariable(Nodes[3]); ControlValue = () => a.Value - b.Value; }
/// <summary> /// Initializes a new instance of the <see cref="BindingContext"/> class. /// </summary> /// <param name="entity">The entity.</param> /// <param name="simulation">The simulation.</param> /// <param name="behaviors">The behavior.</param> public SamplerBindingContext(IEntity entity, ISimulation simulation, IBehaviorContainer behaviors) : base(entity, simulation, behaviors) { }
public static List<BehaviorRequirement> GetBehaviorRequirementsForElement(IBehaviorContainer behaviorContainer) { List<BehaviorRequirement> requirements = new List<BehaviorRequirement>(); for (int i = 0; i < behaviorContainer.Behaviors.Count; i++) { requirements.AddRange(GetBehaviorRequirementsForBehavior(behaviorContainer.Behaviors[i])); } return requirements; }
public static ICodeBlock GetMethodCallForBehavior(string behaviorName, IBehaviorContainer container) { ICodeBlock codeBlock = new CodeDocument(); string returnString = GetRawBehaviorMethodHeader(behaviorName); if (returnString.StartsWith("//")) { codeBlock.Line(returnString); return codeBlock; } else { returnString = returnString.Trim(); returnString = returnString.Replace("public ", ""); returnString = returnString.Replace("private ", ""); returnString = returnString.Replace("protected ", ""); returnString = returnString.Replace("internal ", ""); returnString = returnString.Replace("override ", ""); returnString = returnString.Replace("virtual ", ""); returnString = returnString.Replace("static ", ""); // The first word is going to be the return value returnString = returnString.Substring(returnString.IndexOf(' ') + 1); List<BehaviorRequirement> requirements = BehaviorManager.GetBehaviorRequirementsForBehavior(behaviorName); if (requirements.Count != 0) { // Let's clear out the argments and fill them with the objects that fulfill the requirements int start = returnString.IndexOf('(') + 1; int indexOfClose = returnString.IndexOf(')'); string argumentList = returnString.Substring(start, indexOfClose - start) ; returnString = returnString.Replace(argumentList, ""); argumentList = ""; for (int i = 0; i < requirements.Count; i++) { //string requirementFulfiller = string requirementFulfiller = container.GetFulfillerName(requirements[i]); argumentList += requirementFulfiller; if (i != requirements.Count - 1) { argumentList += ", "; } } returnString = returnString.Insert(start, argumentList); } codeBlock.Line(returnString + ";"); return codeBlock; } }
/// <summary> /// Initializes a new instance of the <see cref="ParallelBindingContext"/> class. /// </summary> /// <param name="entity">The entity.</param> /// <param name="simulation">The simulation for which behaviors are created.</param> /// <param name="behaviors">The behaviors created by the entity.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entity"/>, <paramref name="simulation"/> or <paramref name="behaviors"/> is <c>null</c>.</exception> public ParallelBindingContext(IEntity entity, ParallelSimulation simulation, IBehaviorContainer behaviors) : base(entity, simulation, behaviors) { }