public void CompareObjects(SchemaMemberSelection SourceSelection, SchemaMemberSelection TargetSelection) { /*T Display.DialogMessage("Comparing...", "Source=[" + (SourceSelection == null ? "<UNMATCHED>" : SourceSelection.MemberNameCaption) + * "], Target=[" + (TargetSelection == null ? "<UNMATCHED>" : TargetSelection.MemberNameCaption) + "]"); */ this.PropertiesGrid.RowDefinitions.Clear(); this.PropertiesGrid.Children.Clear(); if (SourceSelection != null && SourceSelection.RefMember != null && TargetSelection != null && TargetSelection.RefMember != null && SourceSelection.RefMember.GetType() != TargetSelection.RefMember.GetType()) { return; // Instances must have same type! } if ((SourceSelection != null && SourceSelection.RefMember is SchemaMemberGroup) || (TargetSelection != null && TargetSelection.RefMember is SchemaMemberGroup)) { return; } var Properties = SourceSelection.Get(sel => sel.RefMember as IModelEntity) .Get(ins => ins.ClassDefinition) .Get(cld => cld.Properties) .NullDefault(Enumerable.Empty <MModelPropertyDefinitor>()) .Concat(TargetSelection.Get(sel => sel.RefMember as IModelEntity) .Get(ins => ins.ClassDefinition) .Get(cld => cld.Properties) .NullDefault(Enumerable.Empty <MModelPropertyDefinitor>())) .Distinct().OrderBy(prop => prop.Name); var SourceInstance = SourceSelection.Get(sel => sel.RefMember as IModelEntity); var TargetInstance = TargetSelection.Get(sel => sel.RefMember as IModelEntity); foreach (var PropDef in Properties) { var SourceValue = (SourceInstance == null ? null : PropDef.Read(SourceInstance)); var TargetValue = (TargetInstance == null ? null : PropDef.Read(TargetInstance)); if (SourceValue == null && TargetValue == null) { continue; } var ActionCode = MergingManager.MERGE_ACTION_UPDATE; /* Cancelled: No property level merge will be provided yet * var ActionCode = (MergingManager.PreferredComparer(SourceValue, TargetValue) * ? MergingManager.MERGE_ACTION_NONE * : MergingManager.MERGE_ACTION_UPDATE); */ AddComparisonRow(PropDef, ActionCode, SourceValue, TargetValue, SourceSelection != null && TargetSelection != null); } }
// ----------------------------------------------------------------------------------------- public void DetermineSourceChildren(IModelEntity Source) { if (!this.EquivalentEntities.AddNew(Source, null)) { return; } // PENDING: GET ONLY THE MINIMUM OWNED AND DOMINANT OBJECTS foreach (var PropDef in Source.ClassDefinition.Properties) { if (!PropDef.IsStoreBoxBased) { var ValueObject = PropDef.Read(Source); if (ValueObject == null) { continue; } var ValueEntity = ValueObject as IModelEntity; if (ValueEntity == null) { var ValueAssignment = ValueObject as MAssignment; if (ValueAssignment != null) { ValueEntity = ValueAssignment.AssignedValue as IModelEntity; } /* Not required (the owner is outside the owned children and dominant dependencies) * else * { * var ValueOwnership = ValueObject as MOwnership; * if (ValueOwnership != null) * ValueEntity = ValueOwnership.Owner as IModelEntity; * } */ } if (ValueEntity != null) { DetermineSourceChildren(ValueEntity); } else { Console.WriteLine("Prop{{" + PropDef.QualifiedTechName + "}}: [" + ValueObject.ToStringAlways() + "]"); } } } // Collections are typically owned foreach (var CollDef in Source.ClassDefinition.Collections) { var SourceCollection = (EditableCollection)CollDef.Read(Source) as IEnumerable <object>; if (SourceCollection == null) { continue; } var Index = 0; foreach (var Item in SourceCollection) { var EntityItem = Item as IModelEntity; if (EntityItem != null) { DetermineSourceChildren(EntityItem); } else if (Item != null) { Console.WriteLine("CollItem{{" + CollDef.QualifiedTechName + "(" + Index.ToString() + ")}}: [" + Item.ToStringAlways() + "]"); } Index++; } } }
// --------------------------------------------------------------------------------------------------------------------------------------------------------- /// <summary> /// Fixes any Unique-Element duplicated Global-Id. /// </summary> private static bool ModelRev8_FixDuplicatedGlobalIds(Composition TargetCompo, string Route = null, IList <IMModelClass> ExaminedInstances = null, IMModelClass Target = null) { if (Route == null) { Route = "\\\\"; } if (ExaminedInstances == null) { ExaminedInstances = new List <IMModelClass>(); } if (Target == null) { Target = TargetCompo; } if (ExaminedInstances.Contains(Target)) { return(false); } ExaminedInstances.Add(Target); Route = Route + Target.ToHashCodeAndString() + "~"; var PropertyDefs = Target.ClassDefinition.Properties; foreach (var PropDef in PropertyDefs) { var Value = PropDef.Read(Target); var PropValue = (Value is MOwnership ? ((MOwnership)Value).Owner : (Value is MAssignment ? ((MAssignment)Value).AssignedValue : Value)) as IMModelClass; if (PropValue != null) { ModelRev8_FixDuplicatedGlobalIds(TargetCompo, Route + PropDef.TechName + "=", ExaminedInstances, PropValue); } } var CollectionDefs = Target.ClassDefinition.Collections; foreach (var CollDef in CollectionDefs) { var Collection = (EditableCollection)CollDef.Read(Target); var CollList = Collection as IList; if (CollList != null) { var Duplicates = new List <UniqueElement>(); var Index = -1; foreach (var Item in CollList) { Index++; var CollItem = (Item is MOwnership ? ((MOwnership)Item).Owner : (Item is MAssignment ? ((MAssignment)Item).AssignedValue : Item)) as IMModelClass; if (CollItem != null) { var ItemId = CollDef.TechName + "[" + Index.ToString() + "]=" + CollItem.ToHashCodeAndString() + "\\"; if (Item is UniqueElement) { var Items = ((IEnumerable <object>)CollList).CastAs <UniqueElement, object>(); if (Items.Any(item => item != Item && item.GlobalId == ((UniqueElement)Item).GlobalId)) { Duplicates.Add((UniqueElement)Item); //T Console.WriteLine("%" + Route + ItemId + "=" + ((UniqueElement)Item).GlobalId.ToString() + (Item is FormalElement ? (((FormalElement)Item).Version != null ? "::" + ((FormalElement)Item).Version.Creation.ToString("yyyyMMdd.hhmmss") : "") : "")); } } ModelRev8_FixDuplicatedGlobalIds(TargetCompo, ItemId, ExaminedInstances, CollItem); } } foreach (var Duplicate in Duplicates.Skip(1)) { Duplicate.GlobalId = Guid.NewGuid(); } } else { var CollDict = Collection as IDictionary; if (CollDict != null) { var Duplicates = new List <UniqueElement>(); var Index = -1; foreach (var Key in CollDict.Keys) { Index++; var CollKey = (Key is MOwnership ? ((MOwnership)Key).Owner : (Key is MAssignment ? ((MAssignment)Key).AssignedValue : Key)) as IMModelClass; if (CollKey != null) { var ItemId = CollDef.TechName + "[" + Index.ToString() + "]K=" + CollKey.ToHashCodeAndString() + "\\"; if (CollKey is UniqueElement) { var Items = ((IEnumerable <object>)CollList).CastAs <UniqueElement, object>(); if (Items.Any(item => item != CollKey && item.GlobalId == ((UniqueElement)CollKey).GlobalId)) { Duplicates.Add((UniqueElement)CollKey); //T Console.WriteLine("%" + Route + ItemId + "=" + ((UniqueElement)CollKey).GlobalId.ToString() + (CollKey is FormalElement ? (((FormalElement)CollKey).Version != null ? "::" + ((FormalElement)CollKey).Version.Creation.ToString("yyyyMMdd.hhmmss") : "") : "")); } } ModelRev8_FixDuplicatedGlobalIds(TargetCompo, ItemId, ExaminedInstances, CollKey); } } Index = -1; foreach (var Value in CollDict.Values) { Index++; var CollValue = (Value is MOwnership ? ((MOwnership)Value).Owner : (Value is MAssignment ? ((MAssignment)Value).AssignedValue : Value)) as IMModelClass; if (CollValue != null) { var ItemId = CollDef.TechName + "[" + Index.ToString() + "]V=" + CollValue.ToHashCodeAndString() + "\\"; if (CollValue is UniqueElement) { var Items = ((IEnumerable <object>)CollList).CastAs <UniqueElement, object>(); if (Items.Any(item => item != CollValue && item.GlobalId == ((UniqueElement)CollValue).GlobalId)) { Duplicates.Add((UniqueElement)CollValue); //T Console.WriteLine("%" + Route + ItemId + "=" + ((UniqueElement)CollValue).GlobalId.ToString() + (CollValue is FormalElement ? (((FormalElement)CollValue).Version != null ? "::" + ((FormalElement)CollValue).Version.Creation.ToString("yyyyMMdd.hhmmss") : "") : "")); } } ModelRev8_FixDuplicatedGlobalIds(TargetCompo, ItemId, ExaminedInstances, CollValue); } } foreach (var Duplicate in Duplicates.Skip(1)) { Duplicate.GlobalId = Guid.NewGuid(); } } } } return(true); }
private static void ModelRev7_FindLocationsOf(IList <IMModelClass> ExaminedInstances, string Route, ref List <string> Results, IMModelClass Target, IMModelClass SearchedObject) { if (ExaminedInstances == null) { ExaminedInstances = new List <IMModelClass>(); } if (ExaminedInstances.Contains(Target)) { return; } if (Target == SearchedObject) { Results.Add(Route + Target.ToHashCodeAndString()); } ExaminedInstances.Add(Target); Route = Route + Target.ToHashCodeAndString() + "~"; var PropertyDefs = Target.ClassDefinition.Properties; foreach (var PropDef in PropertyDefs) { var Value = PropDef.Read(Target); var PropValue = (Value is MOwnership ? ((MOwnership)Value).Owner : (Value is MAssignment ? ((MAssignment)Value).AssignedValue : Value)) as IMModelClass; if (PropValue != null) { ModelRev7_FindLocationsOf(ExaminedInstances, Route + PropDef.TechName + "=", ref Results, PropValue, SearchedObject); } } var CollectionDefs = Target.ClassDefinition.Collections; foreach (var CollDef in CollectionDefs) { var Collection = (EditableCollection)CollDef.Read(Target); var CollList = Collection as IList; if (CollList != null) { var Index = -1; foreach (var Item in CollList) { Index++; var CollItem = (Item is MOwnership ? ((MOwnership)Item).Owner : (Item is MAssignment ? ((MAssignment)Item).AssignedValue : Item)) as IMModelClass; if (CollItem != null) { ModelRev7_FindLocationsOf(ExaminedInstances, Route + CollDef.TechName + "[" + Index.ToString() + "]=" + CollItem.ToHashCodeAndString() + "\\", ref Results, CollItem, SearchedObject); } } } else { var CollDict = Collection as IDictionary; if (CollDict != null) { var Index = -1; foreach (var Key in CollDict.Keys) { Index++; var CollKey = (Key is MOwnership ? ((MOwnership)Key).Owner : (Key is MAssignment ? ((MAssignment)Key).AssignedValue : Key)) as IMModelClass; if (CollKey != null) { ModelRev7_FindLocationsOf(ExaminedInstances, Route + CollDef.TechName + "[" + Index.ToString() + "]K=" + CollKey.ToHashCodeAndString() + "\\", ref Results, CollKey, SearchedObject); } } Index = -1; foreach (var Value in CollDict.Values) { Index++; var CollValue = (Value is MOwnership ? ((MOwnership)Value).Owner : (Value is MAssignment ? ((MAssignment)Value).AssignedValue : Value)) as IMModelClass; if (CollValue != null) { ModelRev7_FindLocationsOf(ExaminedInstances, Route + CollDef.TechName + "[" + Index.ToString() + "]V=" + CollValue.ToHashCodeAndString() + "\\", ref Results, CollValue, SearchedObject); } } } } } }
/// <summary> /// Fixes references to owner, which are not pointing to the real ones (resulting of old-buggy cloning operation). /// </summary> private static bool ModelRev7_ApplyFixOwnerReferences(Composition TargetCompo, string Route, IList <IMModelClass> ExaminedInstances, List <Tuple <IMModelClass, MModelPropertyDefinitor, object> > Fixes, IMModelClass Target, IMModelClass DirectOwner = null, int Level = 0) { if (ExaminedInstances.Contains(Target)) { return(false); } ExaminedInstances.Add(Target); Route = Route + Target.ToHashCodeAndString() + "~"; var PropertyDefs = Target.ClassDefinition.Properties; foreach (var PropDef in PropertyDefs) { var Value = PropDef.Read(Target); var PropValue = (Value is MOwnership ? ((MOwnership)Value).Owner : (Value is MAssignment ? ((MAssignment)Value).AssignedValue : Value)) as IMModelClass; if (PropValue != null) { if (PropDef.ReferencesOwner.HasValue && DirectOwner != null) { var Change = ModelRev7_EvaluateOwnershipDisconnection(TargetCompo, Target, Route, DirectOwner, PropValue); if (Change) { // PENDING: Ensure there is only one claiming owner // (not needed if THE-WHOLE-PROCESS is invoked by 2nd time without generating new changes) object Referencer = null; var IsValid = true; // just until all type-validations are implemented Referencer = (Value is MOwnership ? (object)(((MOwnership)Value).CreateClone(DirectOwner)) : (Value is MAssignment ? (object)(((MAssignment)Value).CreateClone(DirectOwner)) : DirectOwner)); // Ensure coherence per each treated type var TableOwner = DirectOwner as Table; var TableRefer = PropValue as Table; if (TableOwner != null) { if (TableOwner.AssignedDesignator.Value != TableRefer.AssignedDesignator.Value) { IsValid = false; } } /* var RelDefOwner = DirectOwner as RelationshipDefinition; * var RelDefRefer = PropValue as RelationshipDefinition; * if (IsValid && RelDefOwner != null) * { * // validate for reldef * IsValid = true; * } */ if (Referencer != null && IsValid) { Fixes.Add(Tuple.Create(Target, PropDef, Referencer)); //T Console.WriteLine("TYPES of Target=[{0}], NewRef=[{1}]", Target.GetType().Name, Referencer.GetType().Name); } } } if (PropDef.Membership != EEntityMembership.External) { ModelRev7_ApplyFixOwnerReferences(TargetCompo, Route + PropDef.TechName + "=", ExaminedInstances, Fixes, PropValue, (PropDef.Membership == EEntityMembership.External ? null : Target), Level + 1); } } } var CollectionDefs = Target.ClassDefinition.Collections; foreach (var CollDef in CollectionDefs) { if (CollDef.Membership == EEntityMembership.External) { continue; } var Collection = (EditableCollection)CollDef.Read(Target); var CollList = Collection as IList; if (CollList != null) { var Index = -1; foreach (var Item in CollList) { Index++; var CollItem = (Item is MOwnership ? ((MOwnership)Item).Owner : (Item is MAssignment ? ((MAssignment)Item).AssignedValue : Item)) as IMModelClass; if (CollItem != null) { ModelRev7_ApplyFixOwnerReferences(TargetCompo, Route + CollDef.TechName + "[" + Index.ToString() + "]=", ExaminedInstances, Fixes, CollItem, (CollDef.Membership == EEntityMembership.External ? null : Target), Level + 1); } } } else { var CollDict = Collection as IDictionary; if (CollDict != null) { var Index = -1; foreach (var Key in CollDict.Keys) { Index++; var CollKey = (Key is MOwnership ? ((MOwnership)Key).Owner : (Key is MAssignment ? ((MAssignment)Key).AssignedValue : Key)) as IMModelClass; if (CollKey != null) { ModelRev7_ApplyFixOwnerReferences(TargetCompo, Route + CollDef.TechName + "[" + Index.ToString() + "]K=", ExaminedInstances, Fixes, CollKey, (CollDef.Membership == EEntityMembership.External ? null : Target), Level + 1); } } Index = -1; foreach (var Value in CollDict.Values) { Index++; var CollValue = (Value is MOwnership ? ((MOwnership)Value).Owner : (Value is MAssignment ? ((MAssignment)Value).AssignedValue : Value)) as IMModelClass; if (CollValue != null) { ModelRev7_ApplyFixOwnerReferences(TargetCompo, Route + CollDef.TechName + "[" + Index.ToString() + "]V=", ExaminedInstances, Fixes, CollValue, (CollDef.Membership == EEntityMembership.External ? null : Target), Level + 1); } } } } } return(true); }
private static void PopulateProperties(TModelClass Target, TModelClass Source, IMModelClass TargetOwner, ECloneOperationScope CloningScope, bool IsForCloning, MModelPropertyDefinitor[] TargetProperties) { foreach (var PropDef in TargetProperties) { var SourceValue = PropDef.Read(Source); /*T if (//T PropDef.TechName.EndsWith("LinkRoleDef") || * PropDef.TechName == "OwnerRelationshipDef") * Console.WriteLine("debug!"); */ /*T if (PropDef.TechName == "ForegroundBrush") * { * Console.WriteLine("Populating prop ForegroundBrush >>>>>>>>>>>>>>>>>>>>>"); * Console.WriteLine("SourceEnt: {0}, TargetEnt: {1}", Source.GetHashCode(), Target.GetHashCode()); * if (PropDef.IsStoreBoxBased) * { * Console.WriteLine("StoreBox-Source: {0}", PropDef.GetStoreBoxContainer(Source).GetHashCode()); * Console.WriteLine("StoreBox-Target: {0}", PropDef.GetStoreBoxContainer(Target).GetHashCode()); * } * Console.WriteLine("SourceVal: HC={0}", SourceValue.GetHashCode()); * var TarVal = PropDef.Read(Target); * TarVal = TarVal.NullDefault("<NULL>"); * Console.WriteLine("TargetVal: HC={0} (to be overwritten)", TarVal.GetHashCode()); * Console.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); * } */ var DoClone = PropDef.IsCloneableFor(CloningScope, Source); if (SourceValue is IMModelClass && DoClone.Item1) { //T PopulationLevel++; SourceValue = ((IMModelClass)SourceValue).CreateCopy(DoClone.Item2, Target); //T PopulationLevel--; } if (PropDef.ReferencesOwner.HasValue && TargetOwner != null) { // NOTE: See ModelFixes.ModelRev7_FixOwnerReferences method, which resulted in this piece of code for reassign Owners. var SourceOwnership = SourceValue as MOwnership; var Owner = (SourceOwnership == null ? SourceValue : SourceOwnership.Owner); if (Owner != null && Owner.GetType() == TargetOwner.GetType()) { if (SourceOwnership != null) { SourceValue = SourceOwnership.CreateClone(TargetOwner); } else { SourceValue = TargetOwner; } } } // Refinements... // Needed because the initial clone only has duplicated the root properties (shallow copy) if (IsForCloning) { var SourceAssignment = SourceValue as MAssignment; if (SourceAssignment != null) { if (SourceAssignment.IsLocal && (SourceAssignment.AssignedValue is IMModelClass) && DoClone.Item1) { SourceAssignment = ((IMModelClass)SourceAssignment.AssignedValue).CreateCopy(DoClone.Item2, Target).Assign(true); } else { SourceAssignment = ((MAssignment)SourceValue).CreateClone(); } SourceValue = SourceAssignment; } if (PropDef.IsStoreBoxBased) { var ClonedStore = PropDef.GetStoreBoxContainer(Target).CreateClone(); PropDef.SetStoreBoxBaseContainer(Target, ClonedStore); } } PropDef.Write(Target, SourceValue); } }