The Code Analyzer is an essential tool for working with Source Code. You can use it to perform very
complex queries on Source Code repositories at lightning speed either locally or on a
Sparx Intel Cloud Service. The Queries use a language with a small but powerful vocabulary that permits
Code Metrics to be queried much faster than conventional methods. Where this is essential
is in the large scale Code base. Allowing you to stringently check any small changes using a
distinct Cross Reference Analysis of the Code to ensure that all effects are clearly discernible.
In this demo we'll look at running over some simple examples. How to use results in the
Abstract Syntax Tree Pane to create specific Queries. Look at examples using the Find in
Files Window and the Pencil Icon. Then we will look at the quick Setup Process. Finally we will
run over the new Sparx Intel Service with the Code Analyzer. It consists of the Query Panel,
the Source Panel, Line Address Panel and the Abstract Syntax Tree Panel displayed on the right.
The Query Panel is where we add short Queries, the Magnifier Icon is used to execute these.
Let's run over some examples to get familiar with the usefulness of this feature and the possible
depth it can be used for. We will start with a simple Query to find a Variable in some C++ Code.
Let's do a simple find of a String. This gives us the results in the Line Address Panel,
where we can double click to open the Source Code at the line in the Source Panel.
For another simple case let's try to find the Definition for the Operation called Embark.
This gives us back the Definition of the Function.
For a more complex process that calls this. Let's use the Ancestor Function to find the Calls to the
Operation Embark. This returns purely the Cause of this Function not the actual Definition.
Where you can get familiar with the power of this feature is on reading the Abstract Syntax Pane.
This provides an Abstraction Tree of the Code Syntax to give you the course structure. One step
above the Code Syntax this window provides a Tree that allows for navigation of the structure based
on Parent and Child Syntax Statements. This not only clarifies exactly what the typing the string
usage is, but helps you. Figuring out what you want to search for, when doing explicit Searches.
Let's have a look at an example of searching for a String used in a variety of different contexts.
To give more meaning to this, we will first do a simple Find on Ctrain. This gives us
a number of different Returns. Although this is searching a simple set of Code. A simple
Find returns a long set of results for any complex Code Base, this can be unmanageable.
Let's refine this to only find its usage as a Parameter Type. Now given what we found
in the AST Pane for a Parameter Type. Let's create a Query to find just that for Ctrain.
For this we use the Parameter Type as shown earlier. On running it we then see.
This is narrowed down to two references, which are much more quickly accessible.
Let's have a look at a much more complex Abstract Syntax result using the AST Tree.
Here we are using the Ancestor to find a Procedure GetNextStation for each entry in the list. This
highlights the whole Operation as per the Ancestor Call. However there is more detail on the AST Pane
to help sift through the complex Code. In the first case the Call to GetNextStation
Operation is fairly simple. It is not subject to any conditions or iterative indentation.
In the third case the Call to GetNextStation Operation is subject to a wild condition
and is set within a Try and an If Condition. Let's see from
this Tree a higher perspective of the general conditions needed to be met before this call.
We can then go back up the Tree Clause and see
quickly the selected Code Block that this Try Statement covers.
With the Find Panel at the bottom this uses predefined Queries based on a Block
Select in the Code Editor such as for a Variable Name. For an example of
this we can open a Source Code file of a Class using F12 and select a Variable.
Then we can simply do this Search from that selection using the Find in Files.
This returns a selection of lines, where this is used.
Now we do the same Query to find a Call of the Operation Embark.
This finds the Calls to this Operation called Embark.
Included with the installation is a set of predefined Searches that you
can use as a starter for writing your own Queries. This gives some examples
of how to use the Query Language that is further covered in the Help Files.
Let's have a look at the Quick Setup Process. For the Setup you just need to define
a Directory Tree for where the Code is located. The location of where to place the Database
containing the Dictionary. The specific Code Language you want to be parsed and also set
the Reference on the default Macro List. Then we compile this ready to be used in the Code Miner.
You can also use this Database. We just set up as a Cloud-based Repository for group access
using the Sparx Intel Service, which we will cover in the next section. A large Code Base usually
involves a large Team. The Code Base Intel Service allows multiple Enterprise Architect Clients
to access and Query. The same information from many different software domains and frameworks.
For a simple run over the features we can firstly look at getting the Service running.
It can be installed on a server as a Windows Service or run as a standalone process.
Let's run it locally as a standalone process. Firstly we have the Config File defining the Name,
the Port and the Database location as the prime settings. In this case the Config File was placed
in the directory with the existing Database. We were using earlier on. With the Service being a
local instance, we can simply open a Windows Command and set the Directory to reference
the location of the Services Executable. We then start this process with two Parameters the Name as
defined in the Config. There can be many defined and the reference to the Configuration File.
We see that this is now running and waiting on a command. Let's connect EA's Code Analyzer to this
On the hamburger we select “Connect to Service”. In the Server Field we add the
Machine Address, where the Server is located as well as the Port. Then we click on “Connect”.
If this was on a server then the Server Name and Port will be used.
Okay with that connected just for a background check we see, the Client Connection has occurred.
On running a Parameter Type Find. We get the results as two rows returned. We see,
it was processed as a Server Log Update in the Command Window. So it is clearly now operating
through the Service. We can now try an Ancestor Search and see this is done by the Server as well.
Of course this is far better, when set up on a server machine to make it accessible to the Team,
but it gives a general overview of the simple process to do this locally.
Hopefully this gives you a good starting point for getting familiar with the new Code Miner Features,
which are available in the new version 16 Release of Enterprise Architect.
For more information on this see sparxsystems.com/ea16