private ExecutionPath GetDefaultExecutionPath() { lock (_graph) { return(_defaultExecutionPath ?? (_defaultExecutionPath = new ExecutionPath(_graph.TopologicalSort().ToArray()))); } }
public void InternalCompute(Action <BidirectionalGraph <TVertex, TEdge>, TVertex, TVertex, TEdge> action) { // Iterate in topo order, track indirect ancestors and remove edges from them to the visited vertex var ancestorsOfVertices = new Dictionary <TVertex, HashSet <TVertex> >(); foreach (var vertexId in _graph.TopologicalSort().ToList()) //making sure we do not mess enumerator or smthn { //TODO think of some heuristic value here. Like (verticesCount / 2) or (verticesCount / 3) var thisVertexPredecessors = new List <TVertex>(); var thisVertexAncestors = new HashSet <TVertex>(); ancestorsOfVertices[vertexId] = thisVertexAncestors; // Get indirect ancestors foreach (var inEdge in _graph.InEdges(vertexId)) { var predecessor = inEdge.Source; thisVertexPredecessors.Add(predecessor); // Add all the ancestors of the predeccessors thisVertexAncestors.UnionWith(ancestorsOfVertices[predecessor]); } // Add indirect edges foreach (var indirectAncestor in thisVertexAncestors) { TEdge foundIndirectEdge; _graph.TryGetEdge(indirectAncestor, vertexId, out foundIndirectEdge); action(_graph, indirectAncestor, vertexId, foundIndirectEdge); } // Add predecessors to ancestors list thisVertexAncestors.UnionWith(thisVertexPredecessors); } }
/// <summary> /// Sort a list of ColumnOrigins based on the references between the tables. /// </summary> /// <param name="columnOrigins">ColumnOrigins to sort</param> /// <returns>Sorted ColumnOrigins</returns> private List <ColumnOrigin> SortTablesFromReferences(List <ColumnOrigin> columnOrigins) { var referenceGraph = new BidirectionalGraph <Table, Edge <Table> >(); var tables = columnOrigins.Select(c => c.Table).ToList(); // List of tables that is used in the columnorigins HashSet <Table> relevantTables = new HashSet <Table>(tables); // Add vertices to graph referenceGraph.AddVertexRange(tables); // Add edges to graph // Edges are added between T1 and T2, iff. T1 has a linking reference to T2 foreach (var table in tables) { foreach (var outRelation in table.Relations.Where(r => r.LinkTable == table && r.LinkTable != r.AnchorTable)) { if (!relevantTables.Contains(outRelation.AnchorTable)) { continue; } referenceGraph.AddEdge(new Edge <Table>(outRelation.LinkTable, outRelation.AnchorTable)); } } // Topological sort var topologicalSort = referenceGraph.TopologicalSort(); // Associate tables with their ordering var tableOrderings = topologicalSort.Select((table, index) => new KeyValuePair <Table, int>(table, index)).ToDictionary(pair => pair.Key, pair => pair.Value); // Order column origins by the ordering in tableOrderings return(columnOrigins.OrderBy(origin => tableOrderings[origin.Table]).ToList()); }
/// <summary> /// Runs through the graph and calls <paramref name="action"/> /// for each couple of indirect ancestor vertex of a given vertex. /// </summary> public void InternalCompute( [NotNull, InstantHandle] Action < BidirectionalGraph <TVertex, TEdge>, TVertex, TVertex, bool, TEdge > action) { // Iterate in topological order, track indirect ancestors and remove edges from them to the visited vertex var verticesAncestors = new Dictionary <TVertex, HashSet <TVertex> >(); foreach (TVertex vertexId in _graph.TopologicalSort().ToArray()) // Making sure we do not mess enumerator or something { var vertexPredecessors = new List <TVertex>(); var vertexAncestors = new HashSet <TVertex>(); verticesAncestors[vertexId] = vertexAncestors; // Get indirect ancestors foreach (TEdge inEdge in _graph.InEdges(vertexId)) { TVertex predecessor = inEdge.Source; vertexPredecessors.Add(predecessor); // Add all the ancestors of the predecessors vertexAncestors.UnionWith(verticesAncestors[predecessor]); } // Add indirect edges foreach (TVertex indirectAncestor in vertexAncestors) { bool found = _graph.TryGetEdge( indirectAncestor, vertexId, out TEdge foundIndirectEdge); action(_graph, indirectAncestor, vertexId, found, foundIndirectEdge); } // Add predecessors to ancestors list vertexAncestors.UnionWith(vertexPredecessors); } }
public void CommitFlow() { _updateForwardOrder = _graphOfComponents.TopologicalSort().ToArray(); }