Reputation: 67
I am looking into calculating the cyclomatic complexity of java methods using Rascal.
One approach to this would be:
case
, catch
, do
, while
, if
, for
, foreach
Another one is using graph theory and using the formula e-n+2.
both e and n can be obtained quite easily using rascal functionality. My problem is how do I go about constructing the control flow graph, I found the following module:
analysis::flow::ControlFlow
which seems to be a step in the right direction but I am totally lost on where to go from there.
Upvotes: 4
Views: 1434
Reputation: 15439
The easiest way is indeed counting the forking nodes on the AST.
In our publication that explained SLOC and CC do not have a strong correlation to each other (preprint), we have also shared our rascal code to calculate CC (see Figure 2).
Here is the code extracted from the article, first create the file's AST with m3, and search for all methods/code blocks in the file. Per method you call this rascal function that visits the AST and counts certain nodes.
int calcCC(Statement impl) {
int result = 1;
visit (impl) {
case \if(_,_) : result += 1;
case \if(_,_,_) : result += 1;
case \case(_) : result += 1;
case \do(_,_) : result += 1;
case \while(_,_) : result += 1;
case \for(_,_,_) : result += 1;
case \for(_,_,_,_) : result += 1;
case \foreach(_,_,_) : result += 1;
case \catch(_,_): result += 1;
case \conditional(_,_,_): result += 1;
case \infix(_,"&&",_) : result += 1;
case \infix(_,"||",_) : result += 1;
}
return result;
}
Upvotes: 5
Reputation: 6696
For construction control flow graphs in Rascal, there is a paper which explains how to do it in pure Rascal and how to raise the abstraction level using a declarative language called DCFlow: http://link.springer.com/chapter/10.1007%2F978-3-319-11245-9_18
Upvotes: 4