CalculateGraphMetricsAsync ( Microsoft.Office.Interop.Excel.Workbook workbook, GraphMetricUserSettings graphMetricUserSettings ) { Debug.Assert(workbook != null); Debug.Assert(graphMetricUserSettings != null); AssertValid(); // Create the default list of graph metrics. IGraphMetricCalculator2[] aoAllGraphMetricCalculators = new IGraphMetricCalculator2[] { new VertexDegreeCalculator2(), new BrandesFastCentralityCalculator2(), new EigenvectorCentralityCalculator2(), new PageRankCalculator2(), new ClusteringCoefficientCalculator2(), new OverallMetricCalculator2(), new GroupMetricCalculator2(), }; this.CalculateGraphMetricsAsync( workbook, aoAllGraphMetricCalculators, graphMetricUserSettings); }
CalculateGraphMetricsAsync ( IGraph graph, Microsoft.Office.Interop.Excel.Workbook workbook ) { Debug.Assert(workbook != null); AssertValid(); // Create the default list of graph metrics. IGraphMetricCalculator2[] aoAllGraphMetricCalculators = new IGraphMetricCalculator2[] { new VertexDegreeCalculator2(), new BrandesFastCentralityCalculator2(), new EigenvectorCentralityCalculator2(), new PageRankCalculator2(), new ClusteringCoefficientCalculator2(), new EdgeReciprocationCalculator2(), new GroupMetricCalculator2(), new IntergroupEdgeCalculator2(), new OverallMetricCalculator2(), new WordMetricCalculator2(), new TwitterSearchNetworkTopItemsCalculator2( (new PerWorkbookSettings(workbook)).GraphHistory), new ReciprocatedVertexPairRatioCalculator2(), }; this.CalculateGraphMetricsAsync(graph, workbook, aoAllGraphMetricCalculators); }
//************************************************************************* // Constructor: CalculateGraphMetricsDialog() // /// <overloads> /// Initializes a new instance of the <see /// cref="CalculateGraphMetricsDialog" /> class. /// </overloads> /// /// <summary> /// Initializes a new instance of the <see /// cref="CalculateGraphMetricsDialog" /> class with a specified list of /// graph metric calculators. /// </summary> /// /// <param name="graph"> /// Graph to calculate the graph metrics for, or null. If null, the graph /// is read from <paramref name="workbook" />. /// </param> /// /// <param name="workbook"> /// Workbook where the graph metrics will get written. If <paramref /// name="graph" /> is null, the graph is read from this workbook, too. /// </param> /// /// <param name="graphMetricCalculators"> /// An array of <see cref="IGraphMetricCalculator2" /> implementations, one /// for each set of graph metrics that should be calculated, or null to use /// a default list of graph metric calculators. /// </param> /// /// <param name="dialogTitle"> /// Title for the dialog, or null to use a default title. /// </param> /// /// <param name="activateRelevantWorksheetWhenDone"> /// true to activate one of the worksheets to let the user know that graph /// metrics have been calculated. The worksheet to activate is determined /// by which columns were calculated. /// </param> //************************************************************************* public CalculateGraphMetricsDialog ( IGraph graph, Microsoft.Office.Interop.Excel.Workbook workbook, IGraphMetricCalculator2 [] graphMetricCalculators, String dialogTitle, Boolean activateRelevantWorksheetWhenDone ) : this(graph, workbook) { m_oGraphMetricCalculators = graphMetricCalculators; m_bActivateRelevantWorksheetWhenDone = activateRelevantWorksheetWhenDone; if (dialogTitle != null) { this.Text = dialogTitle; } AssertValid(); }
//************************************************************************* // Method: CalculateGraphMetricsAsync() // /// <summary> /// Asynchronously calculates one or more sets of specified graph metrics /// and returns the results as an array of <see /// cref="GraphMetricColumn" /> objects. /// </summary> /// /// <param name="workbook"> /// Workbook containing the graph contents. /// </param> /// /// <param name="graphMetricCalculators"> /// An array of <see cref="IGraphMetricCalculator2" /> implementations, one /// for each set of graph metrics that should be calculated. This method /// sorts the array in place, so its contents will likely be in a different /// order when the method returns. /// </param> /// /// <param name="graphMetricUserSettings"> /// User settings for calculating graph metrics. /// </param> /// /// <remarks> /// For each <see cref="IGraphMetricCalculator2" /> implementation in the /// <paramref name="graphMetricCalculators" /> array, this method calls the /// implementation's <see /// cref="IGraphMetricCalculator2.TryCalculateGraphMetrics" /> method. The /// <see cref="GraphMetricColumn" /> objects returned by each /// implementation are aggregated. When graph metric calculations /// complete, the <see cref="GraphMetricCalculationCompleted" /> event /// fires and the aggregated results can be obtained via the <see /// cref="RunWorkerCompletedEventArgs.Result" /> property. /// /// <para> /// To cancel the calculations, call <see cref="CancelAsync" />. /// </para> /// /// <para> /// If <paramref name="workbook" /> contains invalid graph data, a <see /// cref="WorkbookFormatException" /> is thrown on the caller's thread /// before asynchronous calculations begin. /// </para> /// /// </remarks> //************************************************************************* public void CalculateGraphMetricsAsync( Microsoft.Office.Interop.Excel.Workbook workbook, IGraphMetricCalculator2 [] graphMetricCalculators, GraphMetricUserSettings graphMetricUserSettings ) { Debug.Assert(workbook != null); Debug.Assert(graphMetricCalculators != null); Debug.Assert(graphMetricUserSettings != null); AssertValid(); const String MethodName = "CalculateGraphMetricsAsync"; if (this.IsBusy) { throw new InvalidOperationException( String.Format( "{0}:{1}: An asynchronous operation is already in progress." , this.ClassName, MethodName ) ); } // Read the workbook into a graph. Do this from the calling thread to // avoid reading the Excel UI from a background thread. IGraph oGraph = ReadWorkbook(workbook, graphMetricUserSettings); // Sort the calculators so that those that can handle duplicate edges // are grouped at the beginning of the array. When the workbook is // read into a graph, duplicate edges are included and that graph is // passed to the first group of calculators. When the second group is // reached -- those that cannot handle duplicate edges -- the // duplicates are removed from the graph before passing the graph to // the second group. Array.Sort(graphMetricCalculators, (x, y) => y.HandlesDuplicateEdges.CompareTo(x.HandlesDuplicateEdges) ); // Wrap the arguments in an object that can be passed to // BackgroundWorker.RunWorkerAsync(). CalculateGraphMetricsAsyncArgs oCalculateGraphMetricsAsyncArgs = new CalculateGraphMetricsAsyncArgs(); oCalculateGraphMetricsAsyncArgs.Graph = oGraph; oCalculateGraphMetricsAsyncArgs.SortedGraphMetricCalculators = graphMetricCalculators; oCalculateGraphMetricsAsyncArgs.GraphMetricUserSettings = graphMetricUserSettings; // Create a BackgroundWorker and handle its events. m_oBackgroundWorker = new BackgroundWorker(); m_oBackgroundWorker.WorkerReportsProgress = true; m_oBackgroundWorker.WorkerSupportsCancellation = true; m_oBackgroundWorker.DoWork += new DoWorkEventHandler( BackgroundWorker_DoWork); m_oBackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged); m_oBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( BackgroundWorker_RunWorkerCompleted); m_oBackgroundWorker.RunWorkerAsync(oCalculateGraphMetricsAsyncArgs); }
//************************************************************************* // Method: CalculateGraphMetricsAsync() // /// <overloads> /// Asynchronously calculates one or more sets of graph metrics and returns /// the results as an array of <see cref="GraphMetricColumn" /> objects. /// </overloads> /// /// <summary> /// Asynchronously calculates one or more sets of a default list of graph /// metrics and returns the results as an array of <see /// cref="GraphMetricColumn" /> objects. /// </summary> /// /// <param name="workbook"> /// Workbook containing the graph contents. /// </param> /// /// <param name="graphMetricUserSettings"> /// User settings for calculating graph metrics. /// </param> /// /// <remarks> /// (See the other overload for more details.) /// </remarks> //************************************************************************* public void CalculateGraphMetricsAsync( Microsoft.Office.Interop.Excel.Workbook workbook, GraphMetricUserSettings graphMetricUserSettings ) { Debug.Assert(workbook != null); Debug.Assert(graphMetricUserSettings != null); AssertValid(); // Create the default list of graph metrics. IGraphMetricCalculator2[] aoAllGraphMetricCalculators = new IGraphMetricCalculator2[] { new VertexDegreeCalculator2(), new BrandesFastCentralityCalculator2(), new EigenvectorCentralityCalculator2(), new PageRankCalculator2(), new ClusteringCoefficientCalculator2(), new OverallMetricCalculator2(), new GroupMetricCalculator2(), }; this.CalculateGraphMetricsAsync( workbook, aoAllGraphMetricCalculators, graphMetricUserSettings); }
ReadWorkbook ( Microsoft.Office.Interop.Excel.Workbook oWorkbook, IGraphMetricCalculator2 [] oGraphMetricCalculators ) { Debug.Assert(oWorkbook != null); Debug.Assert(oGraphMetricCalculators != null); AssertValid(); ReadWorkbookContext oReadWorkbookContext = new ReadWorkbookContext(); oReadWorkbookContext.FillIDColumns = true; oReadWorkbookContext.PopulateVertexWorksheet = true; oReadWorkbookContext.ReadGroups = true; // Some of the graph metric calculators, such as // TopItemsCalculatorBase2, need to rank vertices or edges by values in // a specified column. Make those values available on the graph. oReadWorkbookContext.ReadAllEdgeAndVertexColumns = true; WorkbookReader oWorkbookReader = new WorkbookReader(); return ( oWorkbookReader.ReadWorkbook( oWorkbook, oReadWorkbookContext) ); }
CalculateGraphMetricsAsync ( IGraph graph, Microsoft.Office.Interop.Excel.Workbook workbook ) { Debug.Assert(workbook != null); AssertValid(); // Create the default list of graph metrics. IGraphMetricCalculator2[] aoAllGraphMetricCalculators = new IGraphMetricCalculator2[] { new VertexDegreeCalculator2(), new BrandesFastCentralityCalculator2(), new EigenvectorCentralityCalculator2(), new PageRankCalculator2(), new ClusteringCoefficientCalculator2(), new EdgeReciprocationCalculator2(), new GroupMetricCalculator2(), new IntergroupEdgeCalculator2(), new OverallMetricCalculator2(), new WordMetricCalculator2(), new TwitterSearchNetworkTopItemsCalculator2( ( new PerWorkbookSettings(workbook) ).GraphHistory), new ReciprocatedVertexPairRatioCalculator2(), }; this.CalculateGraphMetricsAsync(graph, workbook, aoAllGraphMetricCalculators); }
//************************************************************************* // Constructor: CalculateGraphMetricsDialog() // /// <overloads> /// Initializes a new instance of the <see /// cref="CalculateGraphMetricsDialog" /> class. /// </overloads> /// /// <summary> /// Initializes a new instance of the <see /// cref="CalculateGraphMetricsDialog" /> class with a specified list of /// graph metric calculators. /// </summary> /// /// <param name="workbook"> /// Workbook containing the graph contents. /// </param> /// /// <param name="graphMetricUserSettings"> /// User settings for calculating graph metrics. /// </param> /// /// <param name="graphMetricCalculators"> /// An array of <see cref="IGraphMetricCalculator2" /> implementations, one /// for each set of graph metrics that should be calculated, or null to use /// a default list of graph metric calculators. /// </param> /// /// <param name="dialogTitle"> /// Title for the dialog, or null to use a default title. /// </param> //************************************************************************* public CalculateGraphMetricsDialog( Microsoft.Office.Interop.Excel.Workbook workbook, GraphMetricUserSettings graphMetricUserSettings, IGraphMetricCalculator2 [] graphMetricCalculators, String dialogTitle ) : this(workbook, graphMetricUserSettings) { m_oGraphMetricCalculators = graphMetricCalculators; if (dialogTitle != null) { this.Text = dialogTitle; } AssertValid(); }
//************************************************************************* // Method: CalculateGroups() // /// <summary> /// Group's the graph's vertices using a graph metric calculator. /// </summary> /// /// <param name="oGraphMetricCalculator"> /// The IGraphMetricCalculator2 to use. /// </param> /// /// <param name="sDialogTitle"> /// Title for the CalculateGraphMetricsDialog, or null to use a default /// title. /// </param> /// /// <returns> /// true if the graph metrics were successfully calculated, or false if /// they were cancelled or the application isn't ready. /// </returns> //************************************************************************* private Boolean CalculateGroups( IGraphMetricCalculator2 oGraphMetricCalculator, String sDialogTitle ) { Debug.Assert(oGraphMetricCalculator != null); Debug.Assert( !String.IsNullOrEmpty(sDialogTitle) ); AssertValid(); if ( !this.ExcelApplicationIsReady(true) ) { return (false); } // The CalculateGraphMetricsDialog does the work. CalculateGraphMetricsDialog oCalculateGraphMetricsDialog = new CalculateGraphMetricsDialog( this.InnerObject, new GraphMetricUserSettings(), new IGraphMetricCalculator2[] {oGraphMetricCalculator}, sDialogTitle ); if (oCalculateGraphMetricsDialog.ShowDialog() == DialogResult.OK) { // Check the "read groups" checkbox in the ribbon. this.Ribbon.ReadGroups = true; return (true); } return (false); }