コード例 #1
0
        public static Dictionary <string, Tuple <object, object> > DifferentProperties(this IBHoMObject obj1, IBHoMObject obj2, IsEqualConfig config = null)
        {
            var dict = new Dictionary <string, Tuple <object, object> >();

            //Use default config if null
            config = config ?? new IsEqualConfig();

            CompareLogic comparer = new CompareLogic();

            comparer.Config.MaxDifferences  = 1000;
            comparer.Config.MembersToIgnore = config.PropertiesToIgnore;
            comparer.Config.DoublePrecision = config.NumericTolerance;

            if (config.IgnoreCustomData)
            {
                comparer.Config.MembersToIgnore.Add("CustomData");
            }

            if (config.IgnoreGuid)
            {
                comparer.Config.TypesToIgnore.Add(typeof(Guid));
            }

            ComparisonResult result = comparer.Compare(obj1, obj2);

            dict = result.Differences.ToDictionary(diff => diff.PropertyName, diff => new Tuple <object, object>(diff.Object1, diff.Object2));

            if (dict.Count == 0)
            {
                return(null);
            }

            return(dict);
        }
コード例 #2
0
        public static Output <bool, List <string>, List <string>, List <string> > IsEqual(this IBHoMObject obj1, IBHoMObject obj2, IsEqualConfig config = null)
        {
            //Use default config if null
            config = config ?? new IsEqualConfig();

            CompareLogic comparer = new CompareLogic();

            comparer.Config.MaxDifferences = 1000;

            comparer.Config.MembersToIgnore = config.PropertiesToIgnore;

            if (config.IgnoreCustomData)
            {
                comparer.Config.MembersToIgnore.Add("CustomData");
            }

            if (config.IgnoreGuid)
            {
                comparer.Config.TypesToIgnore.Add(typeof(Guid));
            }

            comparer.Config.DoublePrecision = config.NumericTolerance;


            ComparisonResult result = comparer.Compare(obj1, obj2);

            List <string> propsDifferent = result.Differences.Select(x => x.PropertyName).ToList();
            List <string> obj1DiffValues = result.Differences.Select(x => x.Object1Value).ToList();
            List <string> obj2DiffValues = result.Differences.Select(x => x.Object2Value).ToList();

            return(new Output <bool, List <string>, List <string>, List <string> >
            {
                Item1 = result.AreEqual,
                Item2 = propsDifferent,
                Item3 = obj1DiffValues,
                Item4 = obj2DiffValues
            });
        }
コード例 #3
0
        public static Delta Diffing(Stream previousStream, Stream currentStream, bool enablePropertyDiff = true, List <string> exceptions = null, bool useDefaultExceptions = true)
        {
            // Take the Stream's objects
            List <IBHoMObject> currentObjs = currentStream.Objects.ToList();
            List <IBHoMObject> readObjs    = previousStream.Objects.ToList();

            // Make dictionary with object hashes to speed up the next lookups
            Dictionary <string, IBHoMObject> readObjs_dict = readObjs.ToDictionary(obj => obj.GetHashFragment().Hash, obj => obj);

            // Set exceptions
            if (useDefaultExceptions)
            {
                Compute.SetDefaultExceptions(ref exceptions);
            }

            // Dispatch the objects: new, modified or old
            List <IBHoMObject> toBeCreated = new List <IBHoMObject>();
            List <IBHoMObject> toBeUpdated = new List <IBHoMObject>();
            List <IBHoMObject> toBeDeleted = new List <IBHoMObject>();
            var objModifiedProps           = new Dictionary <string, Dictionary <string, Tuple <object, object> > >();

            foreach (var obj in currentObjs)
            {
                var hashFragm = obj.GetHashFragment();

                if (hashFragm?.PreviousHash == null)
                {
                    toBeCreated.Add(obj); // It's a new object
                }

                else if (hashFragm.PreviousHash == hashFragm.Hash)
                {
                    // It's NOT been modified
                }

                else if (hashFragm.PreviousHash != hashFragm.Hash)
                {
                    toBeUpdated.Add(obj); // It's been modified

                    if (enablePropertyDiff)
                    {
                        // Determine changed properties
                        IBHoMObject oldObjState = null;
                        readObjs_dict.TryGetValue(hashFragm.PreviousHash, out oldObjState);

                        if (oldObjState == null)
                        {
                            continue;
                        }

                        IsEqualConfig config = new IsEqualConfig();
                        config.PropertiesToIgnore = exceptions;

                        var differentProps = BH.Engine.Testing.Query.DifferentProperties(obj, oldObjState, config);

                        objModifiedProps.Add(hashFragm.Hash, differentProps);
                    }
                }
                else
                {
                    throw new Exception("Could not find hash information to perform Diffing on some objects.");
                }
            }

            // If no modified property was found, set the field to null (otherwise will get empty list)
            objModifiedProps = objModifiedProps.Count == 0 ? null : objModifiedProps;

            // All ReadObjs that cannot be found by hash in the previousHash of the CurrentObjs are toBeDeleted
            Dictionary <string, IBHoMObject> CurrentObjs_withPreviousHash_dict = currentObjs
                                                                                 .Where(obj => obj.GetHashFragment().PreviousHash != null)
                                                                                 .ToDictionary(obj => obj.GetHashFragment().PreviousHash, obj => obj);

            toBeDeleted = readObjs_dict.Keys.Except(CurrentObjs_withPreviousHash_dict.Keys)
                          .Where(k => readObjs_dict.ContainsKey(k)).Select(k => readObjs_dict[k]).ToList();

            return(new Delta(toBeCreated, toBeDeleted, toBeUpdated, objModifiedProps));
        }