public static string RelateNode(BuildingGraphAPIClient client, BGNode fromNode, BGNode toNode, string relationshipType)
    {
        var _client = client.Client;


        if (string.IsNullOrEmpty(fromNode.Id))
        {
            return("From node must have an ID");
        }
        if (string.IsNullOrEmpty(toNode.Id))
        {
            return("To node must have an ID");
        }

        var fromPn = new PendingNode(fromNode.Name, fromNode.Id);
        var toPn   = new PendingNode(toNode.Name, toNode.Id);

        fromNode.intPendingNode = fromPn;
        toNode.intPendingNode   = toPn;

        //variables in relationships not supported yet
        _client.Relate(fromPn, toPn, relationshipType, null);
        _client.Commit();

        return("OK");
    }
Esempio n. 2
0
 internal BGNode(PendingNode pendingNode)
 {
     intPendingNode = pendingNode;
     Name           = pendingNode.NodeName;
     Id             = pendingNode.TempId;
     WasCommited    = pendingNode.WasCommited;
 }
Esempio n. 3
0
        /// <summary>
        ///  MATCH(a),(b)
        ///  WHERE ID(a) = $fromNodeId AND ID(b) = $toNodeId
        ///  CREATE (a)-[r: $relType $variables ]->(b)
        /// </summary>
        /// <param name="fromNodeId"></param>
        /// <param name="toNodeId"></param>
        /// <param name="relType"></param>
        /// <param name="variables"></param>
        public void Relate(PendingNode fromNode, PendingNode toNode, string relType, Dictionary <string, object> variables)
        {
            var qlSafeVariables = new Dictionary <string, object>();

            if (variables != null)
            {
                foreach (var kvp in variables)
                {
                    var qlSafeName = Utils.GetGraphQLCompatibleFieldName(kvp.Key);
                    if (!qlSafeVariables.ContainsKey(qlSafeName))
                    {
                        qlSafeVariables.Add(qlSafeName, Convert.ToNeo4jType(kvp.Value));
                    }
                }
            }


            Dictionary <string, object> props = new Dictionary <string, object>();

            props.Add("frid", fromNode.Id);
            props.Add("toid", toNode.Id);

            string query = string.Empty;

            if (qlSafeVariables != null && qlSafeVariables.Count > 0)
            {
                props.Add("cvar", qlSafeVariables);
                query =
                    string.Format("MATCH(a: {0} {{Id: $frid}}),(b:{1} {{Id: $toid}})", fromNode.NodeName, toNode.NodeName) +
                    string.Format("CREATE (a)-[r:{0} $cvar]->(b) ", relType);
            }
            else
            {
                query =
                    string.Format("MATCH(a: {0} {{Id: $frid}}),(b:{1} {{Id: $toid}})", fromNode.NodeName, toNode.NodeName) +
                    string.Format("CREATE (a)-[r:{0}]->(b) ", relType);
            }

            var pec = new PendingNodeRelate();

            pec.Query = query;
            pec.Props = props;

            pec.Completed = (IResultCursor result) =>
            {
                var rs = result;
            };

            pec.FromNode = fromNode;
            pec.ToNode   = toNode;
            pec.RelType  = relType;

            relateStack.Enqueue(pec);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            this.Phase = GH_SolutionPhase.Collecting;


            var fromElmName = string.Empty;

            DA.GetData("From Node", ref fromElmName);

            var fromElmId = string.Empty;

            DA.GetData("From Id", ref fromElmId);

            var toElmName = string.Empty;

            DA.GetData("To Node", ref toElmName);

            var toElmId = string.Empty;

            DA.GetData("To Id", ref toElmId);

            var relName = string.Empty;

            DA.GetData("Relationship Name", ref relName);

            var endpoint = string.Empty;

            DA.GetData("Endpoint", ref endpoint);

            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            if (_client == null || _client.EndPointUrl != endpoint)
            {
                _client = new BuildingGraphClient(endpoint);
            }

            var fromPn = new PendingNode(fromElmName, fromElmId);
            var toPn   = new PendingNode(toElmName, toElmId);

            //variables in relationships not supported yet
            _client.Relate(fromPn, toPn, relName, null);
            _client.Commit();

            DA.SetData("From Node ID", fromElmId);
            DA.SetData("To Node ID", toElmId);
            DA.IncrementIteration();

            this.Phase = GH_SolutionPhase.Computed;
        }
Esempio n. 5
0
        public PendingNode Push(Node node, Dictionary <string, object> variables)
        {
            var qlSafeVariables = new Dictionary <string, object>();

            if (variables != null)
            {
                foreach (var kvp in variables)
                {
                    var qlSafeName = Utils.GetGraphQLCompatibleFieldName(kvp.Key);
                    if (!qlSafeVariables.ContainsKey(qlSafeName))
                    {
                        qlSafeVariables.Add(qlSafeName, Convert.ToNeo4jType(kvp.Value));
                    }
                }
            }

            var pendingNode = new PendingNode(node);

            if (qlSafeVariables.ContainsKey(pendingNode.Id))
            {
                qlSafeVariables.Add("Id", pendingNode.Id);
            }
            else
            {
                qlSafeVariables["Id"] = pendingNode.Id;
            }

            if (!qlSafeVariables.ContainsKey("SessionId"))
            {
                qlSafeVariables.Add("SessionId", "");
            }

            Dictionary <string, object> props = new Dictionary <string, object>();

            props.Add("props", qlSafeVariables);

            //todo: sort out multiple labels
            var nodeLabel     = node.Labels.First();
            var allNodeLabels = node.Labels.First();

            if (_labelMapping.ContainsKey(nodeLabel))
            {
                allNodeLabels = nodeLabel + ":" + string.Join(":", _labelMapping[nodeLabel]);
            }

            var query = string.Format("CREATE (nn:{0} $props)", allNodeLabels);

            if (_applyIDConstraints && !constrained.Contains(nodeLabel))
            {
                var pecCs = new PendingNodePush();
                pecCs.Query = string.Format("CREATE CONSTRAINT ON(nc:{0}) ASSERT nc.Id IS UNIQUE", nodeLabel);
                schemaStack.Enqueue(pecCs);
                constrained.Add(nodeLabel);
            }

            var pec = new PendingNodePush();

            pec.Query    = query;
            pec.Props    = props;
            pec.Node     = node;
            pec.PushNode = pendingNode;
            pushStack.Enqueue(pec);

            pec.Completed = (IResultCursor result) =>
            {
                var rs = result;
                if (pec.PushNode != null)
                {
                    pec.PushNode.SetCommited(pec.PushNode.Id);
                }
            };

            return(pendingNode);
        }
        public void Write(MEPRevitGraph mepGraph, Autodesk.Revit.DB.Document rootDoc)
        {
            var track = new Dictionary <MEPRevitNode, PendingNode>();

            var models = new Dictionary <string, PendingNode>();
            var types  = new Dictionary <string, PendingNode>();
            var levels = new Dictionary <string, PendingNode>();

            var rootModelNode  = new Model.Model();
            var rootModelIdent = DocUtils.GetDocumentIdent(rootDoc);

            rootModelNode.Name = rootDoc.PathName;
            rootModelNode.ExtendedProperties.Add("Identity", rootModelIdent);
            rootModelNode.ExtendedProperties.Add("DateTimeStamp", System.DateTime.Now.ToShortDateString());
            var rootparams = MEPGraphUtils.GetNodePropsWithElementProps(rootModelNode, rootDoc.ProjectInformation);
            var seid       = _gdbClient.Push(rootModelNode, rootparams);

            models.Add(rootModelIdent, seid);

            //add the nodes
            foreach (var mepNode in mepGraph.Nodes)
            {
                var npNode      = mepNode.AsAbstractNode;
                var elmAbParams = npNode.GetAllProperties();


                if (!string.IsNullOrEmpty(mepNode.OrginDocIdent))
                {
                    var elmNode  = mepNode.AsElementNode;
                    var elmParms = elmNode.GetAllProperties();

                    var elmdoc = DocUtils.GetDocument(mepNode.OrginDocIdent, rootDoc.Application);
                    var elm    = elmdoc.GetElement(new Autodesk.Revit.DB.ElementId(mepNode.OriginId));
                    if (elm != null)
                    {
                        elmParms    = MEPGraphUtils.GetNodePropsWithElementProps(elmNode, elm);
                        elmAbParams = MEPGraphUtils.GetNodePropsWithElementProps(npNode, elm);
                    }

                    var atid = _gdbClient.Push(npNode, elmAbParams);
                    track.Add(mepNode, atid);

                    var elmid = _gdbClient.Push(elmNode, elmParms);

                    //relate the element node to the abstracted model node
                    _gdbClient.Relate(atid, elmid, Model.MEPEdgeTypes.REALIZED_BY.ToString(), null);


                    PendingNode modelId = null;
                    //create up model nodes
                    if (models.ContainsKey(mepNode.OrginDocIdent))
                    {
                        modelId = models[mepNode.OrginDocIdent];
                    }
                    else
                    {
                        var modelNode = new Model.Model();
                        modelNode.ExtendedProperties.Add("Identity", mepNode.OrginDocIdent);
                        var mparams = modelNode.GetAllProperties();

                        var ldoc = DocUtils.GetDocument(mepNode.OrginDocIdent, rootDoc.Application);
                        if (ldoc != null)
                        {
                            mparams = MEPGraphUtils.GetNodePropsWithElementProps(modelNode, ldoc.ProjectInformation);
                        }

                        modelId = _gdbClient.Push(modelNode, mparams);
                        models.Add(mepNode.OrginDocIdent, modelId);
                    }

                    var elmedgeProps = MEPGraphUtils.GetEdgeProps(elm);
                    //connect up model node to element node
                    _gdbClient.Relate(elmid, modelId, Model.MEPEdgeTypes.IS_IN.ToString(), elmedgeProps);

                    Autodesk.Revit.DB.Element typeElm = null;


                    if (elm is Autodesk.Revit.DB.FamilyInstance)
                    {
                        typeElm = (elm as Autodesk.Revit.DB.FamilyInstance).Symbol;
                    }
                    else
                    {
                        var mpType = elm.GetTypeId();
                        typeElm = elmdoc.GetElement(mpType);
                    }



                    //create type nodes
                    if (typeElm != null)
                    {
                        PendingNode tsId = null;
                        if (!types.ContainsKey(typeElm.UniqueId))
                        {
                            var edgeProps = MEPGraphUtils.GetEdgeProps(typeElm);
                            var tsNode    = new Model.ElementType();
                            tsNode.Name = typeElm.Name;
                            var tsprops1 = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm);

                            tsId = _gdbClient.Push(tsNode, tsprops1);
                            types.Add(typeElm.UniqueId, tsId);

                            var tselmNode = new Model.ModelElement();
                            var tsprops2  = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm);
                            var tselmId   = _gdbClient.Push(tselmNode, tsprops2);

                            _gdbClient.Relate(tsId, tselmId, Model.MEPEdgeTypes.REALIZED_BY.ToString(), null);
                            _gdbClient.Relate(tselmId, modelId, Model.MEPEdgeTypes.IS_IN.ToString(), edgeProps);
                        }
                        else
                        {
                            tsId = types[typeElm.UniqueId];
                        }
                        _gdbClient.Relate(atid, tsId, Model.MEPEdgeTypes.IS_OF.ToString(), null);
                    }

                    //create level nodes
                    var lvl = elmdoc.GetElement(new Autodesk.Revit.DB.ElementId(mepNode.LevelId));
                    if (lvl != null)
                    {
                        var         edgeProps = MEPGraphUtils.GetEdgeProps(lvl);
                        PendingNode lvlId     = null;
                        if (!levels.ContainsKey(lvl.UniqueId))
                        {
                            var lvlNode = new Model.Level();
                            lvlNode.Name = lvl.Name;
                            var lvlprops = MEPGraphUtils.GetNodePropsWithElementProps(lvlNode, lvl);
                            lvlId = _gdbClient.Push(lvlNode, lvlprops);
                            levels.Add(lvl.UniqueId, lvlId);
                            _gdbClient.Relate(lvlId, modelId, Model.MEPEdgeTypes.IS_IN.ToString(), edgeProps);
                        }
                        else
                        {
                            lvlId = levels[lvl.UniqueId];
                        }

                        _gdbClient.Relate(atid, lvlId, Model.MEPEdgeTypes.IS_ON.ToString(), null);
                    }
                }
                else
                {
                    var modelId = _gdbClient.Push(npNode, elmAbParams);
                    track.Add(mepNode, modelId);
                }
            }

            //now add the adjacencies
            foreach (var mepEdge in mepGraph.Edges)
            {
                if (!track.ContainsKey(mepEdge.ThisNode))
                {
                    continue;
                }

                if (!track.ContainsKey(mepEdge.NextNode))
                {
                    continue;
                }

                var nid1 = track[mepEdge.ThisNode];
                var nid2 = track[mepEdge.NextNode];

                var edPArams = new Dictionary <string, object>();
                foreach (var wkvp in mepEdge.Weights)
                {
                    edPArams.Add(wkvp.Key, wkvp.Value);
                }


                _gdbClient.Relate(nid1, nid2, mepEdge.AsNodeEdge.EdgeType.ToString(), edPArams);
            }


            //add systems and connections
            foreach (var system in mepGraph.Systems)
            {
                var sysNode = new Model.System();

                var syselm    = rootDoc.GetElement(new Autodesk.Revit.DB.ElementId(system));
                var srops     = MEPGraphUtils.GetNodePropsWithElementProps(sysNode, syselm);
                var sysNodeId = _gdbClient.Push(sysNode, srops);

                var tselmNode = new Model.ModelElement();
                tselmNode.ExtendedProperties.Add("UniqueId", syselm.UniqueId);

                var emprops = MEPGraphUtils.GetNodePropsWithElementProps(tselmNode, syselm);
                var tselmId = _gdbClient.Push(tselmNode, emprops);
                _gdbClient.Relate(sysNodeId, tselmId, Model.MEPEdgeTypes.REALIZED_BY.ToString(), null);
                var edgeProps = MEPGraphUtils.GetEdgeProps(syselm);
                _gdbClient.Relate(tselmId, seid, Model.MEPEdgeTypes.IS_IN.ToString(), edgeProps);


                var stypeId = syselm.GetTypeId();
                var typeElm = rootDoc.GetElement(stypeId);
                if (typeElm != null)
                {
                    PendingNode tsId = null;
                    if (!types.ContainsKey(typeElm.UniqueId))
                    {
                        var stypeedgeProps = MEPGraphUtils.GetEdgeProps(typeElm);
                        var tsNode         = new Model.ElementType();
                        tsNode.Name = typeElm.Name;
                        var tsprops1 = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm);

                        tsId = _gdbClient.Push(tsNode, tsprops1);
                        types.Add(typeElm.UniqueId, tsId);

                        var sysTypeelmNode = new Model.ModelElement();
                        var tsprops2       = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm);
                        var sysTypeelmId   = _gdbClient.Push(sysTypeelmNode, tsprops2);

                        _gdbClient.Relate(tsId, sysTypeelmId, Model.MEPEdgeTypes.REALIZED_BY.ToString(), null);
                        _gdbClient.Relate(tselmId, seid, Model.MEPEdgeTypes.IS_IN.ToString(), stypeedgeProps);
                    }
                    else
                    {
                        tsId = types[typeElm.UniqueId];
                    }

                    _gdbClient.Relate(sysNodeId, tsId, Model.MEPEdgeTypes.IS_OF.ToString(), null);
                }

                var snodes = mepGraph.GetAllNodesForSystem(system);
                foreach (var snd in snodes)
                {
                    if (!track.ContainsKey(snd))
                    {
                        continue;
                    }

                    var rid = track[snd];
                    _gdbClient.Relate(rid, sysNodeId, Model.MEPEdgeTypes.ABSTRACTED_BY.ToString(), null);
                }
            }

            AsyncContext.Run(() => _gdbClient.CommitAsync());
            //var task = Task.Run(() => AsyncContext.Run(() => _gdbClient.CommitAsync()));
            //task.Wait();
        }
Esempio n. 7
0
        public void Write(MEPRevitGraph mepGraph, BuildingGraphMapping clientMapping, Autodesk.Revit.DB.Document rootDoc)
        {
            var track  = new Dictionary <MEPRevitNode, PendingNode>();
            var models = new Dictionary <string, PendingNode>();
            var types  = new Dictionary <string, PendingNode>();
            var levels = new Dictionary <string, PendingNode>();

            var rootModelNode  = new Model.RevitModel();
            var rootModelIdent = DocUtils.GetDocumentIdent(rootDoc);

            rootModelNode.Name = rootDoc.PathName;
            rootModelNode.ExtendedProperties.Add("Identity", rootModelIdent);
            rootModelNode.ExtendedProperties.Add("DateTimeStamp", System.DateTime.Now.ToShortDateString());

            var schema = _gdbClient.GetSchema();

            //gather data from the graph about nodes which already exist
            string dGather = @"query($modelIdent:String){
  Model (Identity:$modelIdent){
    Identity
    ModelElements {
      UniqueId
      AbstractElement {
        NodeType: __typename
        Name
        ... on Space{
          Number
          Name
          Area
          Id
        }
        ... on Level{
          Name
          Elevation
        }
      }
    }
  }
}";


            var vars = new Dictionary <string, object>();

            vars.Add("modelIdent", rootModelIdent);
            var res = _gdbClient.ExecuteQuery(dGather, vars);

            var     dbModels    = res.Result.Model;
            dynamic dbRootModel = null;
            Dictionary <string, dynamic> modElmCache = new Dictionary <string, dynamic>();

            if (models != null)
            {
                foreach (var model in models)
                {
                    dbRootModel = model;
                    foreach (var modelElement in dbModels.ModelElements)
                    {
                        var meID = modelElement.UniqueId.Value;
                        if (meID == null)
                        {
                            continue;
                        }
                        if (!modElmCache.ContainsKey(meID))
                        {
                            modElmCache.Add(modelElement.UniqueId.Value, modelElement);
                        }
                        else
                        {
                            //there shouldn't be multiple
                            modElmCache[meID] = modelElement;
                        }
                    }
                }
            }



            var         rootparams = MEPGraphUtils.GetNodePropsWithElementProps(rootModelNode, rootDoc.ProjectInformation, schema, clientMapping, true);
            PendingNode seid       = null;

            if (dbRootModel == null)
            {
                seid = _gdbClient.Push(rootModelNode.Label, rootparams);
            }
            else
            {
                seid = _gdbClient.Push(rootModelNode.Label, rootparams);
            }

            models.Add(rootModelIdent, seid);

            var exprops = new Dictionary <string, object>();

            exprops.Add("test", 1);


            //add the nodes
            foreach (var mepNode in mepGraph.Nodes)
            {
                var npNode      = mepNode.AsAbstractNode;
                var elmAbParams = npNode.GetAllProperties();


                if (!string.IsNullOrEmpty(mepNode.OrginDocIdent))
                {
                    var elmNode  = mepNode.AsElementNode;
                    var elmParms = elmNode.GetAllProperties();

                    var elmdoc = DocUtils.GetDocument(mepNode.OrginDocIdent, rootDoc.Application);
                    var elm    = elmdoc.GetElement(new Autodesk.Revit.DB.ElementId(mepNode.OriginId));
                    if (elm != null)
                    {
                        elmParms    = MEPGraphUtils.GetNodePropsWithElementProps(elmNode, elm, schema, clientMapping, true);
                        elmAbParams = MEPGraphUtils.GetNodePropsWithElementProps(npNode, elm, schema, clientMapping, true);
                    }

                    var atid = _gdbClient.Push(npNode.Label, elmAbParams);
                    track.Add(mepNode, atid);

                    var elmid = _gdbClient.Push(elmNode.Label, elmParms);

                    //relate the element node to the abstracted model node
                    _gdbClient.Relate(atid, elmid, Model.MEPEdgeTypes.REALIZED_BY, null);


                    PendingNode modelId = null;
                    //create up model nodes
                    if (models.ContainsKey(mepNode.OrginDocIdent))
                    {
                        modelId = models[mepNode.OrginDocIdent];
                    }
                    else
                    {
                        var modelNode = new Model.RevitModel();
                        modelNode.ExtendedProperties.Add("Identity", mepNode.OrginDocIdent);
                        var mparams = modelNode.GetAllProperties();

                        var ldoc = DocUtils.GetDocument(mepNode.OrginDocIdent, rootDoc.Application);
                        if (ldoc != null)
                        {
                            mparams = MEPGraphUtils.GetNodePropsWithElementProps(modelNode, ldoc.ProjectInformation, schema, clientMapping, true);
                        }

                        modelId = _gdbClient.Push(modelNode.Label, mparams);
                        models.Add(mepNode.OrginDocIdent, modelId);
                    }

                    var elmedgeProps = MEPGraphUtils.GetEdgeProps(elm);
                    //connect up model node to element node
                    _gdbClient.Relate(elmid, modelId, Model.MEPEdgeTypes.IS_IN, elmedgeProps);

                    Autodesk.Revit.DB.Element typeElm = null;


                    if (elm is Autodesk.Revit.DB.FamilyInstance)
                    {
                        typeElm = (elm as Autodesk.Revit.DB.FamilyInstance).Symbol;
                    }
                    else
                    {
                        var mpType = elm.GetTypeId();
                        typeElm = elmdoc.GetElement(mpType);
                    }



                    //create type nodes
                    if (typeElm != null)
                    {
                        PendingNode tsId = null;
                        if (!types.ContainsKey(typeElm.UniqueId))
                        {
                            var edgeProps = MEPGraphUtils.GetEdgeProps(typeElm);
                            var tsNode    = new Model.ElementType();
                            tsNode.Name = typeElm.Name;
                            var tsprops1 = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm, schema, clientMapping, true);

                            tsId = _gdbClient.Push(tsNode.Label, tsprops1);
                            types.Add(typeElm.UniqueId, tsId);

                            var tselmNode = new Model.ModelElement();
                            var tsprops2  = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm, schema, clientMapping, true);
                            var tselmId   = _gdbClient.Push(tselmNode.Label, tsprops2);

                            _gdbClient.Relate(tsId, tselmId, Model.MEPEdgeTypes.REALIZED_BY, exprops);
                            _gdbClient.Relate(tselmId, modelId, Model.MEPEdgeTypes.IS_IN, edgeProps);
                        }
                        else
                        {
                            tsId = types[typeElm.UniqueId];
                        }
                        _gdbClient.Relate(atid, tsId, Model.MEPEdgeTypes.IS_OF, exprops);
                    }

                    //create level nodes
                    var lvl = elmdoc.GetElement(new Autodesk.Revit.DB.ElementId(mepNode.LevelId));
                    if (lvl != null)
                    {
                        var         edgeProps = MEPGraphUtils.GetEdgeProps(lvl);
                        PendingNode lvlId     = null;
                        if (!levels.ContainsKey(lvl.UniqueId))
                        {
                            var lvlNode = new Model.Level();
                            lvlNode.Name = lvl.Name;
                            var lvlprops = MEPGraphUtils.GetNodePropsWithElementProps(lvlNode, lvl, schema, clientMapping, true);
                            lvlId = _gdbClient.Push(lvlNode.Label, lvlprops);
                            levels.Add(lvl.UniqueId, lvlId);
                            _gdbClient.Relate(lvlId, modelId, Model.MEPEdgeTypes.IS_IN, edgeProps);
                        }
                        else
                        {
                            lvlId = levels[lvl.UniqueId];
                        }

                        _gdbClient.Relate(atid, lvlId, Model.MEPEdgeTypes.IS_ON, exprops);
                    }
                }
                else
                {
                    var modelId = _gdbClient.Push(npNode.Label, elmAbParams);
                    track.Add(mepNode, modelId);
                }
            }

            //now add the adjacencies
            foreach (var mepEdge in mepGraph.Edges)
            {
                if (!track.ContainsKey(mepEdge.ThisNode))
                {
                    continue;
                }

                if (!track.ContainsKey(mepEdge.NextNode))
                {
                    continue;
                }

                var nid1 = track[mepEdge.ThisNode];
                var nid2 = track[mepEdge.NextNode];

                var edPArams = new Dictionary <string, object>();
                foreach (var wkvp in mepEdge.Weights)
                {
                    edPArams.Add(wkvp.Key, wkvp.Value);
                }


                _gdbClient.Relate(nid1, nid2, mepEdge.AsNodeEdge.EdgeType, edPArams);
            }


            //add systems and connections
            foreach (var system in mepGraph.Systems)
            {
                var sysNode = new Model.System();

                var syselm    = rootDoc.GetElement(new Autodesk.Revit.DB.ElementId(system));
                var srops     = MEPGraphUtils.GetNodePropsWithElementProps(sysNode, syselm, schema, clientMapping, true);
                var sysNodeId = _gdbClient.Push(sysNode.Label, srops);

                var tselmNode = new Model.ModelElement();
                tselmNode.ExtendedProperties.Add("UniqueId", syselm.UniqueId);

                var emprops = MEPGraphUtils.GetNodePropsWithElementProps(tselmNode, syselm, schema, clientMapping, true);
                var tselmId = _gdbClient.Push(tselmNode.Label, emprops);
                _gdbClient.Relate(sysNodeId, tselmId, Model.MEPEdgeTypes.REALIZED_BY, null);
                var edgeProps = MEPGraphUtils.GetEdgeProps(syselm);
                _gdbClient.Relate(tselmId, seid, Model.MEPEdgeTypes.IS_IN, edgeProps);


                var stypeId = syselm.GetTypeId();
                var typeElm = rootDoc.GetElement(stypeId);
                if (typeElm != null)
                {
                    PendingNode tsId = null;
                    if (!types.ContainsKey(typeElm.UniqueId))
                    {
                        var stypeedgeProps = MEPGraphUtils.GetEdgeProps(typeElm);
                        var tsNode         = new Model.ElementType();
                        tsNode.Name = typeElm.Name;
                        var tsprops1 = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm, schema, clientMapping, true);

                        tsId = _gdbClient.Push(tsNode.Label, tsprops1);
                        types.Add(typeElm.UniqueId, tsId);

                        var sysTypeelmNode = new Model.ModelElement();
                        var tsprops2       = MEPGraphUtils.GetNodePropsWithElementProps(tsNode, typeElm, schema, clientMapping, true);
                        var sysTypeelmId   = _gdbClient.Push(sysTypeelmNode.Label, tsprops2);

                        _gdbClient.Relate(tsId, sysTypeelmId, Model.MEPEdgeTypes.REALIZED_BY, null);
                        _gdbClient.Relate(tselmId, seid, Model.MEPEdgeTypes.IS_IN, stypeedgeProps);
                    }
                    else
                    {
                        tsId = types[typeElm.UniqueId];
                    }

                    _gdbClient.Relate(sysNodeId, tsId, Model.MEPEdgeTypes.IS_OF, null);
                }

                var snodes = mepGraph.GetAllNodesForSystem(system);
                foreach (var snd in snodes)
                {
                    if (!track.ContainsKey(snd))
                    {
                        continue;
                    }

                    var rid = track[snd];
                    _gdbClient.Relate(rid, sysNodeId, Model.MEPEdgeTypes.ABSTRACTED_BY, null);
                }
            }

            _gdbClient.Commit();
        }
Esempio n. 8
0
        public PendingNode Push(Model.Node node, Dictionary <string, object> variables)
        {
            var qlSafeVariables = new Dictionary <string, object>();

            if (variables != null)
            {
                foreach (var kvp in variables)
                {
                    var qlSafeName = Utils.GetGraphQLCompatibleFieldName(kvp.Key);
                    if (!qlSafeVariables.ContainsKey(qlSafeName))
                    {
                        qlSafeVariables.Add(qlSafeName, kvp.Value);
                    }
                }
            }

            Dictionary <string, object> props = new Dictionary <string, object>();

            props.Add("props", qlSafeVariables);

            var pendingNode = new PendingNode(node);

            if (qlSafeVariables.ContainsKey(pendingNode.TempId))
            {
                qlSafeVariables.Add("TempId", pendingNode.TempId);
            }
            else
            {
                qlSafeVariables["TempId"] = pendingNode.TempId;
            }

            var nodeLabel = node.Label;
            var query     = string.Format("CREATE (nn:{0} $props)", nodeLabel);


            if (!constrained.Contains(nodeLabel))
            {
                var pecCs = new PendingCypher();
                pecCs.Query = string.Format("CREATE CONSTRAINT ON(nc:{0}) ASSERT nc.TempId IS UNIQUE", nodeLabel);
                pushStack.Enqueue(pecCs);
                constrained.Add(nodeLabel);
            }

            var pec = new PendingCypher();

            pec.Query    = query;
            pec.Props    = props;
            pec.Node     = node;
            pec.FromNode = pendingNode;
            pushStack.Enqueue(pec);

            pec.Committed = (IStatementResult result) =>
            {
                var rs = result;
                if (pec.FromNode != null)
                {
                    pec.FromNode.SetCommited(pec.FromNode.TempId);
                }
            };

            return(pendingNode);
        }
Esempio n. 9
0
        public void ElecticalService_AddSocketsToSpaces()
        {
            var client   = new BuildingGraphClient(@"https://*****:*****@"query ($projectName:String, $buidingName: String, $rootDbName: String) {
  Space(filter: {BaseLevel: {Building: {Name: $buidingName, Projects_some: {Name: $projectName}}}}) {
    Id
    Name    
    Number
    Number_of_230V_Single_Sockets_Non_Essential
    Number_of_230V_Twin_Sockets_Non_Essential
    ElectricalOutlets{
      Name
      Is_Essential
      Number_Of_Outlets
    }
    BaseLevel{
      Id
      Abbreviation
    }
    DBPanelElements{
      Name
      Id
      OutgoingCircuits{
        Name
        Id
      }
    }
  }
  rootPanel : DBPanel (Name:$rootDbName){
    Id
    OutgoingCircuits
    {
      Id
      Name
      Number
      levelPanels : Panels{
        Id
        Name
        Level{
          Id
          Name
          Abbreviation
        }
        OutgoingCircuits{
          Id
          Name
          Number
          spacePanels : Panels{
            Id
            Name
            Number_of_Ways
            OutgoingCircuits{
              Id
              Name
              Number
            }
            Space{
              Id
              Name
              Number
            }
          }
        }
      }
    }
  }
}

";

            Dictionary <string, object> vars = new Dictionary <string, object>();

            vars.Add("projectName", project);
            vars.Add("buidingName", building);
            vars.Add("rootDbName", "DB-CORE1");
            var result = client.ExecuteQuery(eleQuery, vars);

            //if (result is Exception) return result;
            //if (result == null) return "Error querying API";
            //if (result.Space == null) return "No spaces found";

            //find root db and build connection tree
            var rootPanelId = string.Empty;
            var levelPanels = new Dictionary <string, PendingNode>();

            if (result.rootPanel != null && result.rootPanel.Count > 0)
            {
                var rootPanel = result.rootPanel[0];
                rootPanelId = rootPanel.Id.Value;
                foreach (var levelCircuit in rootPanel.OutgoingCircuits)
                {
                    //assum each circuit from root to level goes to only one level
                    var    levelPanel = levelCircuit.levelPanels[0];
                    string levelId    = levelPanel.Level.Id.Value;
                    levelPanels.Add(levelId, new PendingNode("Level", levelId));
                }
            }

            PendingNode rootDbNode = null;

            if (string.IsNullOrEmpty(rootPanelId))
            {
                var rootDbVars = new Dictionary <string, object>();
                rootDbVars.Add("Name", "DB-CORE1");
                rootDbNode = client.Push("DBPanel", rootDbVars);
                client.Commit();
            }
            else
            {
                rootDbNode = new PendingNode("DBPanel", rootPanelId);
            }

            foreach (var space in result.Space)
            {
                var no_230SingleNonEssReq   = space.Number_of_230V_Single_Sockets_Non_Essential.Value;
                var no_230TwinNonEssReg     = space.Number_of_230V_Twin_Sockets_Non_Essential.Value;
                var no_230SingleNonEssCount = 0;
                var no_230TwinNonEssCount   = 0;


                if (space.ElectricalOutlets != null)
                {
                    foreach (var elecLoad in space.ElectricalOutlets)
                    {
                        if (elecLoad.Is_Essential == null || !elecLoad.Is_Essential.Value)
                        {
                            if (elecLoad.Number_Of_Outlets != null && elecLoad.Number_Of_Outlets.Value == 1)
                            {
                                no_230SingleNonEssCount = ++no_230SingleNonEssCount;
                            }
                            else if (elecLoad.Number_Of_Outlets != null && elecLoad.Number_Of_Outlets.Value == 2)
                            {
                                no_230TwinNonEssCount = ++no_230TwinNonEssCount;
                            }
                        }
                    }
                }
                var no_230SingleNonEssDelta = no_230SingleNonEssReq - no_230SingleNonEssCount;
                var no_230TwinNonEssDelta   = no_230TwinNonEssReg - no_230TwinNonEssCount;

                var newNodesCirc1 = new List <PendingNode>();
                var newNodesCirc2 = new List <PendingNode>();
                if (no_230SingleNonEssDelta > 0)
                {
                    for (int skc = 0; skc < no_230SingleNonEssDelta; skc++)
                    {
                        var sckTwinVars = new Dictionary <string, object>();
                        sckTwinVars.Add("Name", "Single Non-Essential Socket");
                        sckTwinVars.Add("Is_Essential", false);
                        sckTwinVars.Add("Number_Of_Outlets", 1);
                        sckTwinVars.Add("Apparent_Load", 5);
                        sckTwinVars.Add("Diversity", 0.1);
                        newNodesCirc1.Add(client.Push("ElectricalOutlet", sckTwinVars));
                    }

                    for (int skc = 0; skc < no_230TwinNonEssReg; skc++)
                    {
                        var sckTwinVars = new Dictionary <string, object>();
                        sckTwinVars.Add("Name", "Twin Non-Essential Socket");
                        sckTwinVars.Add("Is_Essential", false);
                        sckTwinVars.Add("Number_Of_Outlets", 2);
                        sckTwinVars.Add("Apparent_Load", 10);
                        sckTwinVars.Add("Diversity", 0.1);
                        newNodesCirc2.Add(client.Push("ElectricalOutlet", sckTwinVars));
                    }
                }

                client.Commit();


                if (newNodesCirc2.Count <= 0 && newNodesCirc2.Count <= 0)
                {
                    return;
                }

                var         spaceNode      = new PendingNode("Space", space.Id.Value);
                PendingNode spacePanelNode = null;
                PendingNode twinCircNode   = null;
                PendingNode singleCircNode = null;
                if (space.DBPanelElements != null && space.DBPanelElements.Count > 1)
                {
                    var spacePanel = space.DBPanelElements[0];
                    spacePanelNode = new PendingNode("DBPanel", spacePanel.Id.Value);

                    foreach (var circ in spacePanel.OutgoingCircuits)
                    {
                        if (circ.Name.Value == "Twin")
                        {
                            twinCircNode = new PendingNode("Circuit", circ.Id.Value);
                        }
                        if (circ.Name.Value == "Single")
                        {
                            singleCircNode = new PendingNode("Circuit", circ.Id.Value);
                        }
                    }
                }
                else
                {
                    var spaceDbVars = new Dictionary <string, object>();
                    spaceDbVars.Add("Name", "DB-" + space.Number.Value);
                    spacePanelNode = client.Push("DBPanel", spaceDbVars);
                    client.Commit();
                    client.Relate(spacePanelNode, spaceNode, Model.MEPEdgeTypes.IS_IN_SPACE, null);
                    client.Commit();
                }

                if (twinCircNode == null)
                {
                    var circVars = new Dictionary <string, object>();
                    circVars.Add("Name", "Twin");
                    circVars.Add("Voltage", 230);
                    twinCircNode = client.Push("Circuit", circVars);
                    client.Commit();
                    client.Relate(spacePanelNode, twinCircNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Commit();
                }

                if (singleCircNode == null)
                {
                    var circVars = new Dictionary <string, object>();
                    circVars.Add("Name", "Single");
                    circVars.Add("Voltage", 230);
                    singleCircNode = client.Push("Circuit", circVars);
                    client.Commit();
                    client.Relate(spacePanelNode, singleCircNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Commit();
                }



                foreach (var outletNode in newNodesCirc1)
                {
                    client.Relate(outletNode, spaceNode, Model.MEPEdgeTypes.IS_IN_SPACE, null);
                    client.Relate(singleCircNode, outletNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                }


                foreach (var outletNode in newNodesCirc2)
                {
                    client.Relate(outletNode, spaceNode, Model.MEPEdgeTypes.IS_IN_SPACE, null);
                    client.Relate(twinCircNode, outletNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                }

                client.Commit();
                //create db panel for level
                var         levelId     = space.BaseLevel.Id.Value;
                PendingNode levelDbNode = null;
                if (levelPanels.ContainsKey(levelId))
                {
                    levelDbNode = levelPanels[levelId];


                    var spaceCircVars = new Dictionary <string, object>();
                    spaceCircVars.Add("Name", space.Number);
                    var spaceCirc = client.Push("Circuit", spaceCircVars);
                    client.Relate(spaceCirc, spacePanelNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Relate(levelDbNode, spaceCirc, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Commit();
                }
                else
                {
                    var levelDbVars = new Dictionary <string, object>();
                    levelDbVars.Add("Name", "DB-" + space.BaseLevel.Abbreviation.Value);
                    levelDbNode = client.Push("DBPanel", levelDbVars);
                    client.Relate(levelDbNode, new PendingNode("Level", levelId), Model.MEPEdgeTypes.IS_ON, null);


                    var levelCircVars = new Dictionary <string, object>();
                    levelCircVars.Add("Name", space.BaseLevel.Abbreviation.Value);
                    levelCircVars.Add("Voltage", 230);
                    var levelCirc = client.Push("Circuit", levelCircVars);
                    client.Relate(levelCirc, levelDbNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Relate(rootDbNode, levelCirc, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);

                    levelPanels.Add(levelId, levelDbNode);
                    client.Commit();

                    var spaceCircVars = new Dictionary <string, object>();
                    spaceCircVars.Add("Name", space.Number);
                    spaceCircVars.Add("Voltage", 230);
                    var spaceCirc = client.Push("Circuit", spaceCircVars);
                    client.Relate(spaceCirc, spacePanelNode, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Relate(levelDbNode, spaceCirc, Model.MEPEdgeTypes.ELECTRICAL_FLOW_TO, null);
                    client.Commit();
                }
            }

            client.Commit();
        }