Пример #1
0
        public override IEnumerable <object> Pull(IRequest query, Dictionary <string, object> config = null)
        {
            var response = SpeckleClient.StreamGetObjectsAsync(SpeckleStreamId, "").Result;

            List <IBHoMObject> bHoMObjects = new List <IBHoMObject>();
            List <IObject>     iObjects    = new List <IObject>();
            List <object>      reminder    = new List <object>();

            bool assignSpeckleIdToBHoMObjects = true;

            if (query == null)
            {
                if (!BH.Engine.Speckle.Convert.ToBHoM(response, out bHoMObjects, out iObjects, out reminder, assignSpeckleIdToBHoMObjects))
                {
                    BH.Engine.Reflection.Compute.RecordError("Failed to deserialize and cast the Server response into BHoM objects.");
                }

                return(bHoMObjects.Concat(iObjects).Concat(reminder));
            }
            else
            {
                /// -------------------
                /// Base Pull rewritten
                /// -------------------

                // Make sure this is a FilterQuery
                FilterRequest filter = query as FilterRequest;
                if (filter == null)
                {
                    Engine.Reflection.Compute.RecordWarning("Please specify a FilterQuery");
                    return(new List <object>());
                }

                List <string> speckleIds = QueryToSpeckleIds(filter);

                // Read the IBHoMObjects
                BH.Engine.Speckle.Convert.ToBHoM(response, out bHoMObjects, out iObjects, out reminder, assignSpeckleIdToBHoMObjects, speckleIds);

                // Filter by tag if any
                bHoMObjects = filter.Tag == "" ? bHoMObjects : bHoMObjects.Where(x => x.Tags.Contains(filter.Tag)).ToList();

                // Return stuff
                if (typeof(IBHoMObject).IsAssignableFrom(filter.Type))
                {
                    return(bHoMObjects);
                }
                else if (typeof(IObject).IsAssignableFrom(filter.Type))
                {
                    return(iObjects);
                }
                else
                {
                    return(bHoMObjects.Concat(iObjects).Concat(reminder));
                }
            }

            return(new List <object>());
        }
Пример #2
0
        // Note: setAssignedId is currently not exposed as an option
        //  -- waiting for Adapter refactoring LVL 04 to expose a new CRUDconfig input for the Push
        // CRUDconfig will become available to all CRUD methods
        protected bool CreateAnyObject(List <IObject> objects, bool setAssignedId = true)
        {
            /// Create the objects
            List <SpeckleObject> objs_serialized = SpeckleCore.Converter.Serialise(objects);

            SpeckleLayer.ObjectCount += objects.Count();
            SpeckleStream.Objects.AddRange(objs_serialized);

            /// Assign any other property to the objects before sending them
            var objList = objects.ToList();
            int i       = 0;

            foreach (var o in SpeckleStream.Objects)
            {
                IBHoMObject bhomObj = objList[i] as IBHoMObject;
                if (bhomObj != null)
                {
                    SpeckleStream.Objects[i].Name = string.IsNullOrEmpty(bhomObj.Name) ? bhomObj.GetType().ToString() : bhomObj.Name;
                    //SpeckleStream.Objects[i].Type = string.IsNullOrEmpty(bhomObj.Name) ? bhomObj.GetType().ToString() : bhomObj.Name;
                }
                i++;
            }

            /// Send the objects
            var updateResponse = SpeckleClient.StreamUpdateAsync(SpeckleStreamId, SpeckleStream).Result;

            SpeckleClient.BroadcastMessage("stream", SpeckleStreamId, new { eventType = "update-global" });



            /// Read the IBHoMobjects as exported in speckle
            /// so we can assign the Speckle-generated id into the BHoMobjects
            if (setAssignedId)
            {
                ResponseObject response = SpeckleClient.StreamGetObjectsAsync(SpeckleStreamId, "").Result;

                List <IBHoMObject>        bHoMObjects_inSpeckle = new List <IBHoMObject>();
                IEnumerable <IBHoMObject> iBhomObjsInSpeckle    = BH.Engine.Speckle.Convert.ToBHoM(response, true);

                VennDiagram <IBHoMObject> correspondenceDiagram = Engine.Data.Create.VennDiagram(objects.Where(o => o as IBHoMObject != null).Cast <IBHoMObject>(), iBhomObjsInSpeckle, new IBHoMGUIDComparer());

                if (correspondenceDiagram.Intersection.Count != objects.Count())
                {
                    var gna = 0;
                    //Engine.Reflection.Compute.RecordError("Push failed.\nNumber of objects created in Speckle do not correspond to the number of objects pushed.");
                    //return false;
                }

                correspondenceDiagram.Intersection.ForEach(o => o.Item1.CustomData[AdapterId] = o.Item2.CustomData[AdapterId]);
            }

            return(true);
        }
Пример #3
0
 public ResponseObject MakeGETRequest(List <string> speckleGuids = null)
 {
     if (speckleGuids == null)
     {
         return(SpeckleClient.StreamGetObjectsAsync(SpeckleStreamId, "").Result);
     }
     else
     {
         // GET only specific IDs
         return(SpeckleClient.ObjectGetBulkAsync(speckleGuids.ToArray(), "omit=displayValue").Result);
     }
 }
Пример #4
0
        public override IEnumerable <object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
        {
            // If the request exists, make sure it's a SpeckleRequest
            SpeckleRequest speckleRequest = request as SpeckleRequest;

            if (request != null && request.GetType() != typeof(FilterRequest) && speckleRequest == null)
            {
                Engine.Reflection.Compute.RecordError($"SpeckleAdapter supports only {typeof(SpeckleRequest).Name}.");
                return(new List <object>());
            }

            ResponseObject response     = null;
            string         speckleQuery = "";

            if (speckleRequest != null)
            {
                speckleQuery = Speckle.Convert.ToSpeckleQuery(speckleRequest);
            }

            // Download the objects.
            response = SpeckleClient.StreamGetObjectsAsync(SpeckleClient.Stream.StreamId, speckleQuery).Result;

            // Conversion configuration.
            bool storeSpeckleId = true;

            // Extract the configuations from the ActionConfig.
            // In this case, only SpecklePullConfig contains an option.
            SpecklePullConfig config = actionConfig as SpecklePullConfig;

            if (config != null)
            {
                storeSpeckleId = config.StoreSpeckleId;
            }

            List <object> converted = Speckle.Convert.FromSpeckle(response.Resources, storeSpeckleId);

            return(converted);
        }
Пример #5
0
        /// This method is actually useless --
        /// written assuming IBHoMObjects had static GUID (instead they are re-instantiated at any modification).
        private bool DiffingByBHoMGuid(List <IObject> objectsToBePushed, out List <IObject> objectsCreated)
        {
            objectsCreated = new List <IObject>();
            List <IObject> objectsToBeCreated = new List <IObject>();

            // Dispatch objects to be pushed
            List <IBHoMObject> bhomObjectsToBePushed = null;
            List <IObject>     iObjectsToBePushed    = null;

            BH.Engine.Speckle.Query.DispatchBHoMObjects(objectsToBePushed, out bhomObjectsToBePushed, out iObjectsToBePushed);

            // Receive and dispatch objects already in speckle
            ResponseObject response = SpeckleClient.StreamGetObjectsAsync(SpeckleStreamId, "").Result;

            List <IBHoMObject> bhomObjectsInSpeckle     = null;
            List <IObject>     iObjectsInSpeckle        = null;
            List <object>      reminderObjectsInSpeckle = null;

            BH.Engine.Speckle.Convert.ToBHoM(response, out bhomObjectsInSpeckle, out iObjectsInSpeckle, out reminderObjectsInSpeckle, false);

            // If speckle doesn't contain anything, push everything
            if (response.Resources.Count == 0)
            {
                objectsToBeCreated = bhomObjectsToBePushed.Concat(iObjectsToBePushed).ToList();
                if (CreateAnyObject(objectsToBeCreated))
                {
                    objectsCreated = objectsToBeCreated;
                }

                return(true);
            }

            // Diffing for IBHoMObjects
            VennDiagram <IBHoMObject> guidDiagram = Engine.Data.Create.VennDiagram(objectsToBePushed.Where(o => o as IBHoMObject != null).Cast <IBHoMObject>(), bhomObjectsInSpeckle, new IBHoMGUIDComparer());

            List <IBHoMObject> newObjects           = guidDiagram.OnlySet1.ToList();                          // Not having a counterpart in the Speckle Server
            List <IBHoMObject> toBeDeleted          = guidDiagram.OnlySet2.ToList();                          // Objects in the Speckle server that do not exist anymore locally. Just not push them.
            List <IBHoMObject> toBeDiffedByProperty = guidDiagram.Intersection.Select(o => o.Item1).ToList(); // Objects that already exist on the Speckle Server, based on their BHoM GUID.


            // // - Insert code here for toBeDiffedByProperty to use custom comparers to diff by property.

            // At the moment, not having any custom comparer, the toBeDiffedByProperty are all just re-created (like assuming they've all changed, even if they didn't).
            objectsToBeCreated = newObjects.Concat(toBeDiffedByProperty).Concat(objectsToBePushed.Where(o => o as IBHoMObject == null)).ToList();

            if (objectsToBeCreated.Count == 0)
            {
                BH.Engine.Reflection.Compute.RecordNote("Speckle already contains every BHoM currently being pushed. They have not been pushed.");
            }

            // IObjects always have to be recreated as they have no GUID
            List <int> objectsToPushHashes = new List <int>();

            objectsToPushHashes = objectsToBePushed.Select(o => o.ToString().GetHashCode()).ToList();

            foreach (var o in iObjectsToBePushed) //.Concat(reminderObjectsInSpeckle)
            {
                if (!objectsToPushHashes.Any(hash => hash == o.ToString().GetHashCode()))
                {
                    objectsToBeCreated.Add(o);
                }
            }


            if (CreateAnyObject(objectsToBeCreated))
            {
                objectsCreated = objectsToBeCreated;
            }

            return(true);
        }