Skip navigation links
www.sdmetrics.com

Package com.sdmetrics.app

Supports the integration of SDMetrics' measurement capabilities into applications.

See: Description

Package com.sdmetrics.app Description

Supports the integration of SDMetrics' measurement capabilities into applications. The classes in this package simplify the calculation of all metric data, rule violations, and relation matrices. In addition, the classes organize the data in a tabular layout for presentation purposes:

Tutorial - How to calculate and use the metric and rule violation data tables.

The following code snippets show how to use the classes to calculate all metrics, rule violations, and relation matrices, and write the data tables to the console.

  1. Required imports
    import sdmetrics.app.ConsoleMessageHandler;
    import sdmetrics.app.DataTables;
    import sdmetrics.app.MessageHandler;
    import sdmetrics.app.MetricData;
    import sdmetrics.app.ModelData;
    import sdmetrics.app.RelationMatrices;
    import sdmetrics.app.RuleData;
    import sdmetrics.metrics.MatrixEngine;
    import sdmetrics.metrics.MetricsEngine;
    import sdmetrics.metrics.RuleEngine;
    
  2. Have your UML model an metrics engine ready
    The tutorials for packages com.sdmetrics.model and com.sdmetrics.metrics show how to parse an XMI file with the UML model and initialize a metrics engine.
    MetricsEngine metricsEngine = new MetricsEngine(metricStore, model);
    RuleEngine ruleEngine = new RuleEngine(metricsEngine);
    MatrixEngine matrixEngine = new MatrixEngine(metricsEngine);
    
  3. Provide a message handler for progress and output messages
    For larger models, calculations to be carried out can take some time. Therefore, the classes in the package regularly emit progress messages. You can display these messages to indicate that the application is still running and what it is currently doing. In this example, we write the progress messages to the console, showing progress as a percentage:
    MessageHandler msgHandler = new ConsoleMessageHandler() {
       /* Override output formatting. */
       public void message(String msg, int progress) {
          if (msg != null && progress >= 0)
          out.println(msg + " (" + progress + "% complete)");
       }
    };
    
    For applications with a GUI, you could write a message handler that updates the status and message of a progress bar.

  4. Calculate the data
    We can now calculate the data. For the progress indication, we assign 70% of the total progress to metrics calculation, 20% to rule checking, and 10% to the calculation of relation matrices.
    MetricData metrics = new MetricData(metricsEngine);
    metrics.precalculateMetrics(msgHandler, 0, 70);
    
    RuleData rules = new RuleData(ruleEngine);
    rules.checkRules(msgHandler, 70, 90);
    
    RelationMatrices matrices = new RelationMatrices(matrixEngine);
    matrices.calculateMatrices(msgHandler, 90, 100);
    msgHandler.message("Done.", 100);
    
    For class MetricData, pre-calculation of metric data is optional. For classes RuleData and RelationMatrices, you first must check the rules/calculate the matrices before you can access the data.

  5. Access the data
    In this example, we want to write the data tables of metrics, rule violations, relation matrices, and the UML model itself to the console. To this end, we define a method printTables as follows:
    public static void printTables(DataTables data) {
       System.out.println("Writing "+data.getTablesDescription());
       for (int table = 0; table < data.getNumberOfTables(); table++) {
          // write table number and name
          System.out.println("Table " + (table + 1) + ": "
                + data.getTableName(table));
                
          // write column headers
          System.out.print("Name\t");
          for (int col = 0; col < data.getNumberOfColumns(table); col++) {
             System.out.print(data.getColumnName(table, col) + "\t");
          }
          System.out.println();
    
          // write table body
          for (int row = 0; row < data.getNumberOfRows(table); row++) {
             System.out.print(data.getRowName(table, row) + "\t");
             for (int col = 0; col < data.getNumberOfColumns(table); col++) {
                System.out.print(data.getValueAt(table, row, col) + "\t");
             }
             System.out.println();
          }
          System.out.println();
       }
    }
    
    With this method in place, we can dump all the data and the UML model itself to the console:
    printTables(metrics);
    printTables(rules);
    printTables(matrices);
    printTables(new ModelData(model));
    
More practical applications of these classes include:
Skip navigation links
www.sdmetrics.com