PropertyModification[] GetModifications(IdentifiedObject identifiedObject) { var type = identifiedObject.GetType(); var propertyNames = GetPropertyNames(type).Except(new[] { nameof(IdentifiedObject.mRID) }); var accessor = TypeAccessor.Create(type); return(propertyNames.Select(property => new PropertyModification { Name = property, Value = accessor[identifiedObject, property] }).ToArray()); }
public void GeneratorOn(long generatorGid, Dictionary <long, DerForecastDayAhead> prod) { networkModel = CalculationEngineCache.Instance.GetNMSModel(); foreach (long gid in networkModel.Keys) { IdentifiedObject io = networkModel[gid]; var type = io.GetType(); if (type.Name.Equals("Substation")) { Substation substation = (Substation)networkModel[gid]; if (substation.Equipments.Contains(generatorGid)) { prod[gid].Production += prod[generatorGid].Production; SubGeographicalRegion subgr = (SubGeographicalRegion)networkModel[substation.SubGeoReg]; prod[subgr.GlobalId].Production += prod[generatorGid].Production; prod[subgr.GeoReg].Production += prod[generatorGid].Production; } } } }
static IdentifiedObject InnerApplyDiffNew(IdentifiedObject currentState, PropertyModification[] modifications) { var type = currentState.GetType(); var accessor = TypeAccessor.Create(type); var newState = currentState.CloneNew(); // set all non-modified properties var propertyNames = GetPropertyNames(type).Except(modifications.Select(m => m.Name)); foreach (var property in propertyNames) { accessor[newState, property] = accessor[currentState, property]; } // apply modified properties foreach (var modification in modifications) { var property = modification.Name ?? throw new ArgumentException($@"Tried to get value from modification without a name: {FormatModification(modification)}"); try { accessor[newState, property] = modification.Value; var valuePresenceMember = ValuePresenceMembers.GetOrAdd(type, _ => new ConcurrentDictionary <string, Member>()) .GetOrAdd(property, _ => accessor.GetMembers().FirstOrDefault(m => m.Name == $"{property}Specified")); if (valuePresenceMember != null) { accessor[newState, valuePresenceMember.Name] = !ReferenceEquals(modification.Value, null); } } catch (Exception exception) { throw new ApplicationException($"Could not set the value of the '{property}' property on {type} from modification {FormatModification(modification)}", exception); } } return((IdentifiedObject)newState); }
void CheckIt(IdentifiedObject currentState, bool verbose) { var type = currentState.GetType(); var newState = _factory.Create(type); var typeAccessor = GetTypeAccessor(newState); // only change some of the properties foreach (var property in GetProperties(typeAccessor)) { try { // 20% chance we will change the value if (_random.Next(5) == 0) { continue; } // 80% chance we will keep the previous value typeAccessor[newState, property] = typeAccessor[currentState, property]; } catch (Exception exception) { throw new ApplicationException($"Could not set property '{property}'", exception); } } // it's the same object newState.mRID = currentState.mRID; if (verbose) { Console.WriteLine($@"Getting diff for this object modification: {currentState.ToPrettyCson()} => {newState.ToPrettyCson()} "); } var dataSetMembers = _differ.GetDiff(new[] { currentState }, new[] { newState }).ToList(); if (dataSetMembers.Count == 0) { Assert.That(newState.ToPrettyCson(), Is.EqualTo(currentState.ToPrettyCson()), "Didn't get a result from the differ, so the two states should be equal"); return; } Assert.That(dataSetMembers.Count, Is.EqualTo(1)); var dataSetMember = dataSetMembers.First(); if (verbose) { Console.WriteLine($@"Got this diff: {dataSetMember.ToPrettyCson()} "); } var roundtrippedSequence = _differ.ApplyDiff(new[] { currentState }, dataSetMembers).ToList(); Assert.That(roundtrippedSequence.Count, Is.EqualTo(1)); var roundtrippedState = roundtrippedSequence.First(); if (verbose) { Console.WriteLine($@"Got this roundtripped state: {roundtrippedState.ToPrettyCson()}"); } Assert.That(roundtrippedState.ToPrettyJson(), Is.EqualTo(newState.ToPrettyJson()), $@"This state change (CURRENT): {currentState.ToPrettyCson()} => {newState.ToPrettyCson()} yielded this diff: {dataSetMember.ToPrettyCson()} which, when applied to CURRENT, yielded THIS result: {roundtrippedState.ToPrettyCson()}"); }
static TypeAccessor GetTypeAccessor(IdentifiedObject newState) { return(TypeAccessors.GetOrAdd(newState.GetType(), TypeAccessor.Create)); }