예제 #1
0
        // ---------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <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);
        }
예제 #2
0
        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);
                            }
                        }
                    }
                }
            }
        }