Exemplo n.º 1
0
        private static List <string> topoSortVertices(ModelClassNS.ModelClass modelStruct)
        {
            List <string>    topToBottom        = new List <string>();
            HashSet <string> remainingVertexIds = new HashSet <string>(modelStruct.graph.vertices.Keys);

            while (remainingVertexIds.Count != 0)
            {
                List <string> roots = remainingVertexIds.Where((string vtxId) => {
                    foreach (string edgeId in modelStruct.edgesByVertex[vtxId].edgesIn)
                    {
                        NetworkContainersNS.Edge edge = modelStruct.graph.edges[edgeId];
                        if (remainingVertexIds.Contains(edge.sourceVertexId))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }).ToList();

                foreach (string rootId in roots)
                {
                    topToBottom.Add(rootId);
                    remainingVertexIds.Remove(rootId);
                }
            }

            return(topToBottom);
        }
Exemplo n.º 2
0
 public static ResponseJson.EdgeData getEdgeJsonData(NetworkContainersNS.Edge edge)
 {
     return(new ResponseJson.EdgeData {
         consistency = GraphUtils.getEdgeConsistencyString(edge.consistency),
         sourceVertexId = edge.sourceVertexId,
         sourcePortId = edge.sourcePortId,
         targetVertexId = edge.targetVertexId,
         targetPortId = edge.targetPortId
     });
 }
Exemplo n.º 3
0
        public static void deleteEdge(
            NetworkContainersNS.Graph graph,
            Dictionary <string, ModelClassNS.VertexEdgesInfo> edgesByVertex,
            string edgeId
            )
        {
            if (!graph.edges.ContainsKey(edgeId))
            {
                throw new System.Exception("Edge with given id does not exist");
            }

            NetworkContainersNS.Edge edge = graph.edges[edgeId];
            graph.edges.Remove(edgeId);

            edgesByVertex[edge.sourceVertexId].edgesOut.Remove(edgeId);
            edgesByVertex[edge.targetVertexId].edgesIn.Remove(edgeId);
        }
Exemplo n.º 4
0
        private static void propagateEdge(ModelClassNS.ModelClass modelStruct, string edgeId)
        {
            NetworkContainersNS.Edge edge        = modelStruct.graph.edges[edgeId];
            Layers.Layer             sourceLayer = modelStruct.layerDict.layers[edge.sourceVertexId];
            Layers.Layer             targetLayer = modelStruct.layerDict.layers[edge.targetVertexId];
            string sourceFieldName = sourceLayer.getValueNameOfPort(edge.sourcePortId);
            string targetFieldName = targetLayer.getValueNameOfPort(edge.targetPortId);

            bool edgeIsConsistent = targetLayer.compareFieldValue(targetFieldName, sourceLayer.getValueString(sourceFieldName));

            if (edgeIsConsistent)
            {
                edge.consistency = NetworkContainersNS.ConsistencyType.Consistent;
                return;
            }

            string validateFieldValue = targetLayer.validateFieldString(targetFieldName, sourceLayer.getValueString(sourceFieldName));

            if (validateFieldValue != null)
            {
                edge.consistency = NetworkContainersNS.ConsistencyType.Inconsistent;
                return;
            }

            Layers.LayersValidated validatedUpdate = targetLayer.validateSetFields(new Dictionary <string, string>()
            {
                { targetFieldName, sourceLayer.getValueString(sourceFieldName) },
            });
            if (validatedUpdate.errors.Count != 0)
            {
                edge.consistency = NetworkContainersNS.ConsistencyType.Inconsistent;
                return;
            }

            targetLayer.setFields(new Dictionary <string, string>()
            {
                { targetFieldName, sourceLayer.getValueString(sourceFieldName) },
            });
            edge.consistency = NetworkContainersNS.ConsistencyType.Consistent;
        }
Exemplo n.º 5
0
        public static string validateEdge(
            NetworkContainersNS.Graph graph,
            Dictionary <string, ModelClassNS.VertexEdgesInfo> edgesByVertex,
            string newEdgeId,
            string sourceVertexId,
            string sourcePortId,
            string targetVertexId,
            string targetPortId
            )
        {
            // check vertices and ports
            if (!graph.vertices.ContainsKey(sourceVertexId))
            {
                return("Vertex with given source id does not exist");
            }
            if (!graph.vertices.ContainsKey(targetVertexId))
            {
                return("Vertex with given target id does not exist");
            }
            if (!graph.vertices[sourceVertexId].ports.ContainsKey(sourcePortId))
            {
                return("Source vertex does not have port with given id");
            }
            if (!graph.vertices[targetVertexId].ports.ContainsKey(targetPortId))
            {
                return("Target vertex does not have port with given id");
            }
            if (graph.vertices[sourceVertexId].ports[sourcePortId].type != NetworkContainersNS.PortType.Output)
            {
                return("Source port is not an output port");
            }
            if (graph.vertices[targetVertexId].ports[targetPortId].type != NetworkContainersNS.PortType.Input)
            {
                return("Target port is not an input port");
            }

            // check if an edge to the same port already exists
            foreach (string edgeId in edgesByVertex[targetVertexId].edgesIn)
            {
                NetworkContainersNS.Edge edge = graph.edges[edgeId];
                if (edge.targetPortId == targetPortId)
                {
                    return("Target port is already occupied");
                }
            }

            // search for loop
            HashSet <string> vertexIdsToInvestigate = new HashSet <string>()
            {
                sourceVertexId
            };
            HashSet <string> ancestorsOfSourceVtx = new HashSet <string>();

            while (vertexIdsToInvestigate.Count != 0)
            {
                string investigateVertexId = vertexIdsToInvestigate.ElementAt(0);
                vertexIdsToInvestigate.Remove(investigateVertexId);
                ancestorsOfSourceVtx.Add(investigateVertexId);

                foreach (string edgeInId in edgesByVertex[investigateVertexId].edgesIn)
                {
                    string edgeSourceVertex = graph.edges[edgeInId].sourceVertexId;

                    if (
                        !vertexIdsToInvestigate.Contains(edgeSourceVertex) &&
                        !ancestorsOfSourceVtx.Contains(edgeSourceVertex)
                        )
                    {
                        vertexIdsToInvestigate.Add(edgeSourceVertex);
                    }
                }
            }

            if (ancestorsOfSourceVtx.Contains(targetVertexId))
            {
                return("Loop detected");
            }

            return(null);
        }