Exemple #1
0
 internal static PropertyGraphModel SortGraph(PropertyGraphModel graph)
 {
     graph.CreateLinks();
     if (graph.Vertices != null)
     {
         graph.Vertices = graph.Vertices.OrderBy(v => v.Id).ToList();
     }
     if (graph.Edges != null)
     {
         graph.Edges = graph.Edges.OrderBy(e => new Tuple <Guid, Guid, string>(e.SourceVertex.Id, e.TargetVertex.Id, e.Name)).ToList();
     }
     if (graph.Vertices != null)
     {
         foreach (var vertex in graph.Vertices)
         {
             vertex.Props = vertex.Props != null ? new SortedDictionary <string, object>(vertex.Props) : new SortedDictionary <string, object>();
             vertex.Edges = new List <PropertyEdgeModel>();
         }
     }
     if (graph.Edges != null && graph.Vertices != null)
     {
         foreach (var edge in graph.Edges)
         {
             edge.Source = graph.Vertices.IndexOf(edge.SourceVertex);
             edge.Target = graph.Vertices.IndexOf(edge.TargetVertex);
             edge.SourceVertex.Edges.Add(edge);
             edge.SourceVertex.Edges.Add(edge);
             edge.Props = edge.Props != null ? new SortedDictionary <string, object>(edge.Props) : new SortedDictionary <string, object>();
         }
     }
     return(graph);
 }
Exemple #2
0
        public PropertyGraphModel Transform(PropertyGraphModel graph)
        {
            // no vertices to filter
            if (graph.Vertices == null)
            {
                return(graph);
            }

            var edges = new List <PropertyEdgeModel>();

            // evaluate each vertex
            var vertices = graph.Vertices
                           .Where(vertex => Expression.Evaluate(vertex, null))
                           .ToList();

            // index new set of vertices
            var vertexIndexer = new Dictionary <Guid, int>();

            for (var i = 0; i < vertices.Count; i++)
            {
                vertexIndexer.Add(vertices[i].Id, i);
            }

            // add all edges that have both ends in vertices list
            foreach (var vertex in vertices)
            {
                if (vertex.Edges == null)
                {
                    continue;
                }

                var newVertexEdges = new List <PropertyEdgeModel>();
                foreach (var edge in vertex.Edges)
                {
                    if (edge.SourceVertex.Id != vertex.Id)
                    {
                        continue;
                    }

                    int sourceIndex;
                    int targetIndex;
                    if (vertexIndexer.TryGetValue(edge.SourceVertex.Id, out sourceIndex) &&
                        vertexIndexer.TryGetValue(edge.TargetVertex.Id, out targetIndex))
                    {
                        var newEdge = new PropertyEdgeModel()
                        {
                            SourceVertex = edge.SourceVertex,
                            TargetVertex = edge.TargetVertex,
                            Source       = sourceIndex,
                            Target       = targetIndex,
                            Name         = edge.Name,
                            Props        = edge.Props
                        };
                        edges.Add(newEdge);
                        newVertexEdges.Add(newEdge);
                    }
                }
                vertex.Edges = newVertexEdges;
            }

            var result = new PropertyGraphModel()
            {
                Vertices = vertices,
                Edges    = edges,
                Data     = graph.Data
            };

            result.CreateLinks();
            return(result);
        }
Exemple #3
0
        public PropertyGraphModel Transform(PropertyGraphModel graph)
        {
            // calculate groups and memberships
            var groupKeyToIndexSet = new Dictionary <object, HashSet <int> >();  // groupKey to vertex index set
            var lpToGroupKeySet    = new Dictionary <int, HashSet <object> >();

            for (int index = 0; index < graph.Vertices.Count; index++)
            {
                var vertex        = graph.Vertices[index];
                var groupKeyArray = GroupKeyArray(vertex);
                if (groupKeyArray == null)
                {
                    continue;
                }

                foreach (var key in groupKeyArray)
                {
                    if (key == null)
                    {
                        continue;
                    }
                    if (!groupKeyToIndexSet.ContainsKey(key))
                    {
                        groupKeyToIndexSet[key] = new HashSet <int>();
                    }
                    groupKeyToIndexSet[key].Add(index);
                    if (!lpToGroupKeySet.ContainsKey(index))
                    {
                        lpToGroupKeySet[index] = new HashSet <object>();
                    }
                    lpToGroupKeySet[index].Add(key);
                }
            }

            // transform groups to vertices
            var groupVertices        = new List <PropertyVertexModel>();
            var groupKeyToGroupIndex = new Dictionary <object, int>();
            var i = 0;

            foreach (var group in groupKeyToIndexSet)
            {
                groupVertices.Add(new PropertyVertexModel()
                {
                    Id    = new Guid(i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                    Props = new Dictionary <string, object>()
                    {
                        { "label", group.Key },
                        { "size", group.Value.Count },
                        { "members", group.Value }
                    }
                });
                groupKeyToGroupIndex[group.Key] = i;
                i++;
            }

            // transform edges
            List <PropertyEdgeModel> groupEdgesList = null;

            if (graph.Edges != null)
            {
                var groupEdges = new Dictionary <Tuple <int, int, string>, PropertyEdgeModel>();
                foreach (var edge in graph.Edges)
                {
                    if (!lpToGroupKeySet.ContainsKey(edge.Source))
                    {
                        continue;
                    }
                    if (!lpToGroupKeySet.ContainsKey(edge.Target))
                    {
                        continue;
                    }

                    var sourceGroupSet = lpToGroupKeySet[edge.Source];
                    var targetGroupSet = lpToGroupKeySet[edge.Target];

                    foreach (var sourceGroup in sourceGroupSet)
                    {
                        var source = groupKeyToGroupIndex[sourceGroup];
                        foreach (var targetGroup in targetGroupSet)
                        {
                            var target = groupKeyToGroupIndex[targetGroup];

                            var name = edge.Name;
                            var key  = new Tuple <int, int, string>(source, target, name);
                            if (!groupEdges.ContainsKey(key))
                            {
                                groupEdges[key] = new PropertyEdgeModel()
                                {
                                    Name         = name,
                                    Source       = source,
                                    Target       = target,
                                    SourceVertex = groupVertices[source],
                                    TargetVertex = groupVertices[target],
                                    Props        = new Dictionary <string, object>()
                                    {
                                        { "size", 0 },
                                        { "connectors", new Dictionary <int, HashSet <int> >() } // member index => other members set
                                    }
                                };
                            }
                            groupEdges[key].Props["size"] = (int)groupEdges[key].Props["size"] + 1;
                            var connectors = groupEdges[key].Props["connectors"] as Dictionary <int, HashSet <int> >;
                            if (!connectors.ContainsKey(edge.Source))
                            {
                                connectors[edge.Source] = new HashSet <int>()
                                {
                                    edge.Target
                                }
                            }
                            ;
                            else
                            {
                                connectors[edge.Source].Add(edge.Target);
                            }
                        }
                    }
                }

                groupEdgesList = groupEdges.Select(kv => kv.Value).ToList();
            }

            var result = new PropertyGraphModel()
            {
                Vertices = groupVertices,
                Edges    = groupEdgesList
            };

            result.CreateLinks();

            result.Data["grouped_vertices"] = graph.Vertices;

            return(result);
        }