Book a Demo
Prev Next

Code Miner Example Script

The following example script is designed to demonstrate the use all of the methods available for all of the Code Miner API interface objects.

Example Script

!INC Local Scripts.EAConstants-JScript
/*
* Script Name:           CodeMiner API Test
* Author:                    Sparx Systems
* Purpose:                 Exercise all of the functions available within the Code Miner API
* Date:                       May 2024
*/
var q1 = "{\
          byItem(\"OPERATION\",\"NAME\",\"LoadBinary\"),\
          byItem(\"OPERATION\",\"NAMESPACE\",\"C2DShapeEditDlg\")\
}";
function PrintFacetInfo(indent, facet, k)
{
          indent = indent + "    ";
          // print out the Name/Value data pair, for the specified CMFacet object
          Session.Output(indent + "---  Facet " + k + " Info  ---");
          Session.Output(indent + "Facet name: " + facet.GetName() );
          Session.Output(indent + "Facet value: " + facet.GetValue() );
          Session.Output("   ");
}
function PrintDataNode(indent, dataNode, n, level)
{
           indent = indent + "=   ";
           level++;
//       print out the details for the specified CMDataNode object.
           Session.Output("  ");
           Session.Output(indent + " DataNode " + level + " - " + n + "  ----------");
           Session.Output(indent + "DataNode name: " + dataNode.GetName() );
           Session.Output(indent + "Line: " + dataNode.GetLine() );
           Session.Output(indent + "Column: " + dataNode.GetColumn() );
           Session.Output(indent + "- - - - - - -");
           Session.Output(indent + "Facet count: " + dataNode.GetFacetCount() );
//        iterate through the array of CMFacet objects and print out their contents
           var facet as EA.CMFacet;
           var facetCount;
           facetCount = dataNode.GetFacetCount();
           var k;
           for(k=0; k < facetCount; k++)
           {
                           facet = dataNode.GetFacet(k);
                          PrintFacetInfo(indent, facet, k+1);
            }
//         iterate through the array of CMDataNode child objects and print out their contents
             var nextLevel = level + 1;
             Session.Output("   ");
             Session.Output(indent + "Level " + nextLevel + " DataNode count: " + dataNode.GetChildCount() );
             var childDataNodeCount;
             childDataNodeCount = dataNode.GetChildCount();
             var l;
             for(l=0; l < childDataNodeCount; l++)
             {
                               childDataNode = dataNode.GetChildNode(l);
                               PrintDataNode(indent, childDataNode, l+1, level);
              }
              Session.Output(indent + "---------------------");
}
function PrintResultSetElement(indent, resultset, i)
{
//          print out the details of the specified result set element
              indent = indent + "    ";
              var elementNumber = i + 1;
              Session.Output(indent + "ResultSet - Element(" + elementNumber + ")");
              Session.Output(indent + "-------------------------");
              Session.Output(indent + "  Filename : " + resultset.GetFilename(i) );
              Session.Output(indent + "  Address : " + resultset.GetAddress(i) );
              Session.Output(indent + "  Position : " + resultset.GetPosition(i) );
              Session.Output(indent + "  Start : " + resultset.GetPositionStart(i) );
              Session.Output(indent + "  End : " + resultset.GetPositionEnd(i) );
              Session.Output(indent + "  Top Level DataNode count: " + resultset.GetChildCount()
);
              var childCount;
              childCount = resultset.GetChildCount();
              var level = 0;
              var dataNode as EA.CMDataNode;
              var j;
              for(j=0; j < childCount; j++)
              {
                               dataNode = resultset.GetChildNode(0);
                               PrintDataNode(indent, dataNode, j+1, level);
               }
                                Session.Output(indent + "---------------------");
}
function main()
{
              var minerService as EA.CMService;
              minerService = Repository.CodeMinerService;
//         Connect to a Code Miner database
              if( minerService.CMLoadDatabase("C:\\CodeMiner Example\\Project1.cdb") )
              {
                   var resultset as EA.CMDataSet;
//              Execute a query against the Code Miner database
                   Session.Output("Query: " + q1);
                   resultset = minerService.ExecuteMFQL(q1);
                   Session.Output("Results Count: " + resultset.Count);
                   Session.Output("Child count : " + resultset.GetChildCount() );
                   var indent;
                   indent = "  ";
//               Iterate through the result set, printing out the details of each 'match' returned by the query.
                   var i;
                   for(i=0; i < resultset.Count; i++)
                   {
                                       PrintResultSetElement(indent, resultset, i);
                    }

                   /* Out of Range Test */
                   var x = resultset.GetFilename(8);
                   Session.Output(indent + "Node Filename: " + x );
           }
           else
                   Session.Output(indent + "Failed to Open Database: " + minerService.GetLastError() );
}
main();

LearnMore