/// <summary> /// Handles when a statechage is noticed by the external source /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void ExternalSourceInput(object sender, ConnectionStateChangedEventHandlerArgs args) { //find all the rules that say something this connection. fire then collect the changes to be made to the states. var statechangedirectives = AllRules.Where(r => r.Connections.Any(c => c.Name == args.Connection.Name)) .Select(r => r.Fire(r.Connections.First(r1 => r1.Name == args.Connection.Name), args.NewState)) .SelectMany(s => s); //do not only send the message back to the source it came from also check if others want to know //about it. var externals = ExternalSources.SelectMany(e => e.Connections, (e1, c1) => new { ExternalSource = e1, Connection = c1 }).ToList(); foreach (var stateChangeDirective in statechangedirectives.Where(s => s != StateChangeDirective.NoOperation)) { var directive = stateChangeDirective; foreach (var external in externals.Where(e => e.Connection.Name == directive.Connection.Name).Select(e => e.ExternalSource)) { external.SetState(directive.Connection, directive.NewState.Name); } } if (args.Connection.Copied) { externals.First(t => t.Connection.Name == args.Connection.Name && !t.Connection.Copied).ExternalSource. SetState(args.Connection, args.NewState.Name); } else { ((IExternalSource)sender).SetState(args.Connection, args.NewState.Name); } }
/// <summary> /// resolve the connections to their real ones by their name. /// </summary> /// <param name="connectionNames">connection names</param> /// <returns>a list of connections.</returns> public IEnumerable <Connection> ResolveConnections(IEnumerable <string> connectionNames) { return(ExternalSources.SelectMany(e => e.Connections) .Where(c => !c.Copied) .Join(connectionNames, c => c.Name, s => s, (c, s) => c) .ToList()); }