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,
            GraphMetricUserSettings oGraphMetricUserSettings
        )
        {
            Debug.Assert(oWorkbook != null);
            Debug.Assert(oGraphMetricUserSettings != null);
            AssertValid();

            ReadWorkbookContext oReadWorkbookContext = new ReadWorkbookContext();

            oReadWorkbookContext.FillIDColumns           = true;
            oReadWorkbookContext.PopulateVertexWorksheet = true;

            if ((oGraphMetricUserSettings.GraphMetricsToCalculate &
                 GraphMetrics.GroupMetrics) != 0)
            {
                oReadWorkbookContext.ReadGroups        = true;
                oReadWorkbookContext.SaveGroupVertices = true;
            }

            WorkbookReader oWorkbookReader = new WorkbookReader();

            return(oWorkbookReader.ReadWorkbook(
                       oWorkbook, oReadWorkbookContext));
        }
예제 #3
0
        //*************************************************************************
        //  Constructor: GraphMetricsDialog()
        //
        /// <overloads>
        /// Initializes a new instance of the <see cref="GraphMetricsDialog" />
        /// class.
        /// </overloads>
        ///
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphMetricsDialog" />
        /// class with a GraphMetricUserSettings object.
        /// </summary>
        ///
        /// <param name="workbook">
        /// Workbook containing the graph contents.
        /// </param>
        ///
        /// <param name="graphMetricUserSettings">
        /// The object being edited.
        /// </param>
        ///
        /// <param name="mode">
        /// Indicates the mode in which the dialog is being used.
        /// </param>
        //*************************************************************************

        public GraphMetricsDialog
        (
            Microsoft.Office.Interop.Excel.Workbook workbook,
            GraphMetricUserSettings graphMetricUserSettings,
            DialogMode mode
        )
            : this()
        {
            Debug.Assert(workbook != null);
            Debug.Assert(graphMetricUserSettings != null);

            m_oWorkbook = workbook;
            m_oGraphMetricUserSettings = graphMetricUserSettings;
            m_eMode = mode;

            if (m_eMode == DialogMode.EditOnly)
            {
                this.Text += " Options";
                btnOK.Text = "OK";
            }

            // Instantiate an object that saves and retrieves the position of this
            // dialog.  Note that the object automatically saves the settings when
            // the form closes.

            m_oGraphMetricsDialogUserSettings =
                new GraphMetricsDialogUserSettings(this);

            DoDataExchange(false);

            AssertValid();
        }
예제 #4
0
        //*************************************************************************
        //  Constructor: GraphMetricsDialog()
        //
        /// <overloads>
        /// Initializes a new instance of the <see cref="GraphMetricsDialog" />
        /// class.
        /// </overloads>
        ///
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphMetricsDialog" />
        /// class with a GraphMetricUserSettings object.
        /// </summary>
        ///
        /// <param name="workbook">
        /// Workbook containing the graph contents.
        /// </param>
        ///
        /// <param name="graphMetricUserSettings">
        /// The object being edited.
        /// </param>
        ///
        /// <param name="mode">
        /// Indicates the mode in which the dialog is being used.
        /// </param>
        //*************************************************************************
        public GraphMetricsDialog(
            Microsoft.Office.Interop.Excel.Workbook workbook,
            GraphMetricUserSettings graphMetricUserSettings,
            DialogMode mode
            )
            : this()
        {
            Debug.Assert(workbook != null);
            Debug.Assert(graphMetricUserSettings != null);

            m_oWorkbook = workbook;
            m_oGraphMetricUserSettings = graphMetricUserSettings;
            m_eMode = mode;

            if (m_eMode == DialogMode.EditOnly)
            {
            this.Text += " Options";
            btnOK.Text = "OK";
            }

            // Instantiate an object that saves and retrieves the position of this
            // dialog.  Note that the object automatically saves the settings when
            // the form closes.

            m_oGraphMetricsDialogUserSettings =
            new GraphMetricsDialogUserSettings(this);

            DoDataExchange(false);

            AssertValid();
        }
        //*************************************************************************
        //  Constructor: CalculateGraphMetricsContext()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="CalculateGraphMetricsContext" /> class.
        /// </summary>
        ///
        /// <param name="graphMetricUserSettings">
        /// The user's settings for calculating graph metrics.
        /// </param>
        ///
        /// <param name="backgroundWorker">
        /// The BackgroundWorker object that is performing all graph metric
        /// calculations.
        /// </param>
        //*************************************************************************
        public CalculateGraphMetricsContext(
            GraphMetricUserSettings graphMetricUserSettings,
            BackgroundWorker backgroundWorker
            )
        {
            m_oGraphMetricUserSettings = graphMetricUserSettings;
            m_oBackgroundWorker = backgroundWorker;

            AssertValid();
        }
        //*************************************************************************
        //  Constructor: CalculateGraphMetricsContext()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="CalculateGraphMetricsContext" /> class.
        /// </summary>
        ///
        /// <param name="graphMetricUserSettings">
        /// The user's settings for calculating graph metrics.
        /// </param>
        ///
        /// <param name="backgroundWorker">
        /// The BackgroundWorker object that is performing all graph metric
        /// calculations.
        /// </param>
        //*************************************************************************

        public CalculateGraphMetricsContext
        (
            GraphMetricUserSettings graphMetricUserSettings,
            BackgroundWorker backgroundWorker
        )
        {
            m_oGraphMetricUserSettings = graphMetricUserSettings;
            m_oBackgroundWorker        = backgroundWorker;

            AssertValid();
        }
예제 #7
0
        //*************************************************************************
        //  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();
        }
예제 #8
0
        //*************************************************************************
        //  Constructor: CalculateGraphMetricsDialog()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="CalculateGraphMetricsDialog" /> class with a default 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>
        //*************************************************************************

        public CalculateGraphMetricsDialog
        (
            Microsoft.Office.Interop.Excel.Workbook workbook,
            GraphMetricUserSettings graphMetricUserSettings
        )
            : this()
        {
            // Instantiate an object that saves and retrieves the position of this
            // dialog.  Note that the object automatically saves the settings when
            // the form closes.

            m_oCalculateGraphMetricsDialogUserSettings =
                new CalculateGraphMetricsDialogUserSettings(this);

            m_oWorkbook = workbook;
            m_oGraphMetricCalculators  = null;
            m_oGraphMetricUserSettings = graphMetricUserSettings;

            m_oGraphMetricCalculationManager = new GraphMetricCalculationManager();

            m_oGraphMetricCalculationManager.GraphMetricCalculationProgressChanged
                += new ProgressChangedEventHandler(
                       GraphMetricCalculationManager_GraphMetricCalculationProgressChanged);

            m_oGraphMetricCalculationManager.GraphMetricCalculationCompleted +=
                new RunWorkerCompletedEventHandler(
                    GraphMetricCalculationManager_GraphMetricCalculationCompleted);

            DoDataExchange(false);

            // Assume that calculations will not succeed.

            this.DialogResult = DialogResult.Cancel;

            AssertValid();
        }
예제 #9
0
        TryCalculateGraphMetrics
        (
            IGraph graph,
            CalculateGraphMetricsContext calculateGraphMetricsContext,
            out GraphMetricColumn [] graphMetricColumns
        )
        {
            Debug.Assert(graph != null);
            Debug.Assert(calculateGraphMetricsContext != null);
            AssertValid();

            graphMetricColumns = new GraphMetricColumn[0];

            GraphMetricUserSettings oGraphMetricUserSettings =
                calculateGraphMetricsContext.GraphMetricUserSettings;

            if (!oGraphMetricUserSettings.ShouldCalculateGraphMetrics(
                    GraphMetrics.Degree |
                    GraphMetrics.InDegree |
                    GraphMetrics.OutDegree
                    ))
            {
                return(true);
            }

            IVertexCollection oVertices = graph.Vertices;
            Int32             iVertices = oVertices.Count;

            // The following lists correspond to vertex worksheet columns.

            List <GraphMetricValueWithID> oInDegreeGraphMetricValues =
                new List <GraphMetricValueWithID>();

            List <GraphMetricValueWithID> oOutDegreeGraphMetricValues =
                new List <GraphMetricValueWithID>();

            List <GraphMetricValueWithID> oDegreeGraphMetricValues =
                new List <GraphMetricValueWithID>();

            // Create a dictionary to keep track of vertex degrees.  The key is the
            // IVertex.ID and the value is a zero-based index into the above lists.

            Dictionary <Int32, Int32> oVertexIDDictionary =
                new Dictionary <Int32, Int32>();

            // Calculate the degrees for each vertex using the
            // VertexDegreeCalculator class in the Algorithms namespace, which
            // knows nothing about Excel.
            //
            // For simplicity, all degree metrics (in-degree, out-degree, and
            // degree) are calculated regardless of whether the graph is directed
            // or undirected.  After all metrics are calculated,
            // FilterGraphMetricColumns() filters out the metrics that don't apply
            // to the graph, based on its directedness.

            Dictionary <Int32, VertexDegrees> oVertexDegreeDictionary;

            if (!(new Algorithms.VertexDegreeCalculator()).
                TryCalculateGraphMetrics(graph,
                                         calculateGraphMetricsContext.BackgroundWorker,
                                         out oVertexDegreeDictionary))
            {
                // The user cancelled.

                return(false);
            }

            Int32 iRowID;

            foreach (IVertex oVertex in oVertices)
            {
                if (!TryGetRowID(oVertex, out iRowID))
                {
                    continue;
                }

                VertexDegrees oVertexDegrees;

                if (!oVertexDegreeDictionary.TryGetValue(oVertex.ID,
                                                         out oVertexDegrees))
                {
                    Debug.Assert(false);
                }

                oInDegreeGraphMetricValues.Add(new GraphMetricValueWithID(
                                                   iRowID, oVertexDegrees.InDegree));

                oOutDegreeGraphMetricValues.Add(new GraphMetricValueWithID(
                                                    iRowID, oVertexDegrees.OutDegree));

                oDegreeGraphMetricValues.Add(new GraphMetricValueWithID(
                                                 iRowID, oVertexDegrees.Degree));

                Debug.Assert(oInDegreeGraphMetricValues.Count ==
                             oOutDegreeGraphMetricValues.Count);

                oVertexIDDictionary.Add(oVertex.ID,
                                        oInDegreeGraphMetricValues.Count - 1);
            }

            // Figure out which columns to add.

            graphMetricColumns = FilterGraphMetricColumns(graph,
                                                          calculateGraphMetricsContext, oInDegreeGraphMetricValues,
                                                          oOutDegreeGraphMetricValues, oDegreeGraphMetricValues);

            return(true);
        }
예제 #10
0
        FilterGraphMetricColumns
        (
            IGraph oGraph,
            CalculateGraphMetricsContext oCalculateGraphMetricsContext,
            List <GraphMetricValueWithID> oInDegreeGraphMetricValues,
            List <GraphMetricValueWithID> oOutDegreeGraphMetricValues,
            List <GraphMetricValueWithID> oDegreeGraphMetricValues
        )
        {
            AssertValid();

            Debug.Assert(oGraph != null);
            Debug.Assert(oCalculateGraphMetricsContext != null);
            Debug.Assert(oInDegreeGraphMetricValues != null);
            Debug.Assert(oOutDegreeGraphMetricValues != null);
            Debug.Assert(oDegreeGraphMetricValues != null);

            GraphMetricUserSettings oGraphMetricUserSettings =
                oCalculateGraphMetricsContext.GraphMetricUserSettings;

            Boolean bGraphIsDirected =
                (oGraph.Directedness == GraphDirectedness.Directed);

            Boolean bCalculateInDegree = bGraphIsDirected &&
                                         oGraphMetricUserSettings.ShouldCalculateGraphMetrics(
                GraphMetrics.InDegree);

            Boolean bCalculateOutDegree = bGraphIsDirected &&
                                          oGraphMetricUserSettings.ShouldCalculateGraphMetrics(
                GraphMetrics.OutDegree);

            Boolean bCalculateDegree = !bGraphIsDirected &&
                                       oGraphMetricUserSettings.ShouldCalculateGraphMetrics(
                GraphMetrics.Degree);

            // Figure out which columns to add.

            List <GraphMetricColumn> oGraphMetricColumns =
                new List <GraphMetricColumn>();

            if (bCalculateInDegree)
            {
                oGraphMetricColumns.Add(new GraphMetricColumnWithID(
                                            WorksheetNames.Vertices, TableNames.Vertices,
                                            VertexTableColumnNames.InDegree,
                                            VertexTableColumnWidths.InDegree,
                                            NumericFormat, CellStyleNames.GraphMetricGood,
                                            oInDegreeGraphMetricValues.ToArray()));
            }

            if (bCalculateOutDegree)
            {
                oGraphMetricColumns.Add(new GraphMetricColumnWithID(
                                            WorksheetNames.Vertices, TableNames.Vertices,
                                            VertexTableColumnNames.OutDegree,
                                            ExcelUtil.AutoColumnWidth,
                                            NumericFormat, CellStyleNames.GraphMetricGood,
                                            oOutDegreeGraphMetricValues.ToArray()));
            }

            if (bCalculateDegree)
            {
                oGraphMetricColumns.Add(new GraphMetricColumnWithID(
                                            WorksheetNames.Vertices, TableNames.Vertices,
                                            VertexTableColumnNames.Degree,
                                            ExcelUtil.AutoColumnWidth,
                                            NumericFormat, CellStyleNames.GraphMetricGood,
                                            oDegreeGraphMetricValues.ToArray()));
            }

            return(oGraphMetricColumns.ToArray());
        }
        //*************************************************************************
        //  Method: ReadWorkbook()
        //
        /// <summary>
        /// Reads the workbook contents into a NodeXL graph.
        /// </summary>
        ///
        /// <param name="oWorkbook">
        /// Workbook containing the graph contents.
        /// </param>
        ///
        /// <param name="oGraphMetricUserSettings">
        /// User settings for calculating graph metrics.
        /// </param>
        ///
        /// <returns>
        /// The <see cref="IGraph" /> read from the workbook.
        /// </returns>
        ///
        /// <remarks>
        /// If <paramref name="oWorkbook" /> contains valid graph data, a new <see
        /// cref="IGraph" /> is created from the workbook contents and returned.
        /// Otherwise, a <see cref="WorkbookFormatException" /> is thrown.
        /// </remarks>
        //*************************************************************************
        protected IGraph ReadWorkbook(
            Microsoft.Office.Interop.Excel.Workbook oWorkbook,
            GraphMetricUserSettings oGraphMetricUserSettings
            )
        {
            Debug.Assert(oWorkbook != null);
            Debug.Assert(oGraphMetricUserSettings != null);
            AssertValid();

            ReadWorkbookContext oReadWorkbookContext = new ReadWorkbookContext();
            oReadWorkbookContext.FillIDColumns = true;
            oReadWorkbookContext.PopulateVertexWorksheet = true;

            if ( (oGraphMetricUserSettings.GraphMetricsToCalculate &
            GraphMetrics.GroupMetrics) != 0)
            {
            oReadWorkbookContext.ReadGroups = true;
            oReadWorkbookContext.SaveGroupVertices = true;
            }

            WorkbookReader oWorkbookReader = new WorkbookReader();

            return ( oWorkbookReader.ReadWorkbook(
            oWorkbook, oReadWorkbookContext) );
        }
        //*************************************************************************
        //  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);
        }
        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);
        }
        //*************************************************************************
        //  Constructor: CalculateGraphMetricsDialog()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="CalculateGraphMetricsDialog" /> class with a default 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>
        //*************************************************************************
        public CalculateGraphMetricsDialog(
            Microsoft.Office.Interop.Excel.Workbook workbook,
            GraphMetricUserSettings graphMetricUserSettings
            )
            : this()
        {
            // Instantiate an object that saves and retrieves the position of this
            // dialog.  Note that the object automatically saves the settings when
            // the form closes.

            m_oCalculateGraphMetricsDialogUserSettings =
            new CalculateGraphMetricsDialogUserSettings(this);

            m_oWorkbook = workbook;
            m_oGraphMetricCalculators = null;
            m_oGraphMetricUserSettings = graphMetricUserSettings;

            m_oGraphMetricCalculationManager = new GraphMetricCalculationManager();

            m_oGraphMetricCalculationManager.GraphMetricCalculationProgressChanged
            += new ProgressChangedEventHandler(
            GraphMetricCalculationManager_GraphMetricCalculationProgressChanged);

            m_oGraphMetricCalculationManager.GraphMetricCalculationCompleted +=
            new RunWorkerCompletedEventHandler(
                GraphMetricCalculationManager_GraphMetricCalculationCompleted);

            DoDataExchange(false);

            // Assume that calculations will not succeed.

            this.DialogResult = DialogResult.Cancel;

            AssertValid();
        }
        //*************************************************************************
        //  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: 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);
        }
예제 #17
0
        //*************************************************************************
        //  Method: ShowGraphMetrics()
        //
        /// <summary>
        /// Shows the dialog that lists available graph metrics and calculates them
        /// if requested by the user.
        /// </summary>
        ///
        /// <param name="mode">
        /// Indicates the mode in which the GraphMetricsDialog is being used.
        /// </param>
        //*************************************************************************
        public void ShowGraphMetrics(
            GraphMetricsDialog.DialogMode mode
            )
        {
            AssertValid();

            if ( !this.ExcelApplicationIsReady(true) )
            {
            return;
            }

            // Create the object that might be edited by the dialog.

            GraphMetricUserSettings oGraphMetricUserSettings =
            new GraphMetricUserSettings();

            GraphMetricsDialog oGraphMetricsDialog = new GraphMetricsDialog(
            this.InnerObject, oGraphMetricUserSettings, mode);

            if (oGraphMetricsDialog.ShowDialog() == DialogResult.OK)
            {
            // Save the edited object.

            oGraphMetricUserSettings.Save();
            }
        }