1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+
7
+ package software_testing_meaures ;
8
+
9
+ import java .io .BufferedReader ;
10
+ import java .io .FileReader ;
11
+ import java .util .ArrayList ;
12
+ import java .util .Scanner ;
13
+ import java .util .Stack ;
14
+ import java .util .regex .Matcher ;
15
+ import java .util .regex .Pattern ;
16
+
17
+ /**
18
+ *
19
+ * @author Guarav Shah
20
+ */
21
+ class graph
22
+ {
23
+ graph [] adjacents = new graph [2 ]; // each node can have two outgoing branches out of it.
24
+ String type ; // will store the line numbers which this node comprise
25
+ int start ;
26
+ int end ;
27
+ int index =0 ; // will represent the number of outgoing edges out of a node.
28
+ public String toString ()
29
+ {
30
+ return " N" +this .index +" " +this .start +" " +this .end ;
31
+ }
32
+ }
33
+ class CyclomaticComplexity_Using_GraphMatrix {
34
+ static ArrayList <graph > nodes ;
35
+ static int number ;
36
+ public static void print_Graph_Matrix ()
37
+ {
38
+ graph node ,left ,right ;
39
+ int graphMatrix [][] = new int [nodes .size ()][nodes .size ()],value [] = new int [nodes .size ()],verticalSum =0 ;
40
+ System .out .println ("\n " );
41
+ System .out .println ("******************** CONNECTION MATRIX ********************" );
42
+ System .out .print (" " );
43
+ for (int i =0 ;i <nodes .size ();i ++)
44
+ {
45
+ System .out .print ("N" +(i +1 )+" " );
46
+ }
47
+ System .out .println ();
48
+ for (int i =0 ;i <nodes .size ();i ++)
49
+ {
50
+ value [i ]=0 ;
51
+ node = nodes .get (i );
52
+ // left link is not null
53
+ if (node .adjacents [0 ]!=null )
54
+ {
55
+ left = node .adjacents [0 ];
56
+ graphMatrix [i ][left .index -1 ]++;
57
+ value [i ]++;
58
+ }
59
+ if (node .adjacents [1 ]!=null )
60
+ {
61
+ right = node .adjacents [1 ];
62
+ graphMatrix [i ][right .index -1 ]++;
63
+ value [i ]++;
64
+ }
65
+ }
66
+ for (int i =0 ;i <nodes .size ();i ++)
67
+ {
68
+ System .out .print ("N" +nodes .get (i ).index +" " );
69
+ for (int j =0 ;j <nodes .size ();j ++)
70
+ {
71
+ System .out .print (graphMatrix [i ][j ]+" " );
72
+ }
73
+ if (value [i ]!=0 )
74
+ {
75
+ verticalSum += value [i ]-1 ;
76
+ System .out .print (value [i ]+"-1=" +(value [i ]-1 ));
77
+ }
78
+ else
79
+ {
80
+ System .out .print (value [i ]+"-0=0" );
81
+ }
82
+ System .out .println ();
83
+ }
84
+ System .out .println ("\n CYCLOMATIC COMPLEXITY USING CONNECTION MATRIX " +verticalSum +"+1=" +(verticalSum +1 ));
85
+ }
86
+ public static void print_Nodes_Lines ()
87
+ {
88
+ graph node ;
89
+ System .out .println ("\n ********* Mapping Flow Graph Node To DD Graph Nodes *********\n " );
90
+ System .out .println ("Program DD Path Adjacent Nodes" );
91
+ System .out .println ("Graph Graph Left Right" );
92
+ System .out .println ("Nodes Nodes Edge Edge" );
93
+ System .out .println ("----------------------------------" );
94
+ for (int i =0 ;i <nodes .size ();i ++)
95
+ {
96
+ node = nodes .get (i );
97
+ System .out .printf ("%2s-%2s %6s" ,node .start ,node .end ,"N" +node .index );
98
+ if (node .adjacents [0 ]!=null )
99
+ //System.out.print(" "+node.adjacents[0].toString()+" ");
100
+ System .out .printf ("%10s" ,"N" +node .adjacents [0 ].index );
101
+ if (node .adjacents [0 ]!=null && node .adjacents [1 ]!=null )
102
+ System .out .printf ("%10s" ,"N" +node .adjacents [1 ].index );
103
+ else if (node .adjacents [1 ]!=null )
104
+ System .out .print ("Adjacent Nodes " +node .adjacents [1 ].toString ()+" " );
105
+ System .out .println ();
106
+ }
107
+ }
108
+ public static void main (String arfs [])
109
+ {
110
+ Scanner s = new Scanner (System .in );
111
+ String line ,path ;
112
+ // alternateNode will take care of statements which are part of one directional output of conditional statements.
113
+ boolean conditionFound =false ,alternateNode =false ,mergeNode =false ;
114
+ StringBuffer sb = new StringBuffer ();
115
+ Stack <Integer > scope = new Stack <Integer >(); // 1 means top element is opening bracket, 2 means closing bracket
116
+ Stack <graph > stack = new Stack <graph >();
117
+ nodes = new ArrayList <graph >();
118
+
119
+ int counter =0 ,lastConditionPoint =1 ; // lastConditionPoint will store the last time before a condition node
120
+ // Pattern definition section
121
+ Pattern decision = Pattern .compile ("\\ b(if|for|while|do)\\ b(.*)" ); // decision
122
+ // left node will store the node on left of conditional node, right node will store the node on right side of the conditional node.
123
+ graph node ,last =null ,left =null ,right =null ,temp =null ; // last node will store the last condition node found in order to trace the path along two outgoing edges.
124
+ // Matcher definition section
125
+ Matcher dM ,bM ;
126
+ do
127
+ {
128
+ System .out .print ("Enter file name : " );
129
+ path = "C:\\ Users\\ Suraj Singh\\ Desktop\\ " +s .next ()+".txt" ;
130
+ System .out .println ("\n \n ******************** FILE CONTENT ********************" );
131
+ try
132
+ {
133
+ BufferedReader r = new BufferedReader (new FileReader (path ));
134
+ while ( (line =r .readLine ())!=null )
135
+ {
136
+ counter ++;// store the line number in the sampled program.
137
+ dM = decision .matcher (line );
138
+ System .out .print (counter +" " );
139
+ System .out .println (line );
140
+ if (dM .find ()) // We found a conditional node
141
+ {
142
+ // conditionFound = true;
143
+ node = new graph ();
144
+ node .start =lastConditionPoint ;
145
+ node .end = counter ;// will store the starting and ending line numbers for which this node is defined.
146
+ lastConditionPoint = counter +1 ;
147
+ node .index = ++number ;
148
+ if (stack .size ()!=0 ) // nested conditional statements
149
+ {
150
+ temp = stack .peek ();
151
+ temp .adjacents [0 ] = node ; // set the left outgoing edge
152
+ }
153
+ // last = node; // last node is stored in order to set the pointers when scope of this condition ends
154
+ stack .push (node );
155
+ nodes .add (node );
156
+ conditionFound = true ;
157
+ //nodes.add(new graph())
158
+ // Add code for this node
159
+ }
160
+ else if (line .contains ("{" ) && conditionFound ) // beginning of new block scope
161
+ {
162
+ conditionFound =false ;
163
+ scope .add (1 );
164
+ }
165
+ else if (line .contains ("else" ))
166
+ {
167
+ alternateNode = true ;
168
+ }
169
+ else if (line .contains ("}" ) && scope .size ()!=0 && !alternateNode ) // we found the complete block scope with open and closing brackets
170
+ {
171
+ //inside the scope we need to create one single graph node
172
+ node = new graph ();
173
+ node .start = lastConditionPoint ;
174
+ node .end = counter ;
175
+ node .index = ++number ;
176
+ last = stack .peek ();
177
+ if (left !=null && right !=null )
178
+ left .adjacents [0 ] = right .adjacents [0 ] = node ;
179
+ if (last .adjacents [0 ]==null )
180
+ last .adjacents [0 ] = node ; // if condition is true then statements inside scope are executed and form the sibling of last node.
181
+ left = node ;
182
+ nodes .add (node );
183
+ mergeNode = false ;
184
+ lastConditionPoint = counter +1 ;
185
+ scope .pop ();
186
+ }
187
+ else if (line .contains ("}" ) && alternateNode ) // take care of else part
188
+ {
189
+ node = new graph ();
190
+ node .start = lastConditionPoint ;
191
+ node .end = counter ;
192
+ node .index = ++number ;
193
+ nodes .add (node );
194
+ last = stack .pop ();
195
+ right = node ;
196
+ //last.adjacents[1] = new graph();
197
+ if (last .adjacents [1 ]==null )
198
+ last .adjacents [1 ] = node ;
199
+ lastConditionPoint = counter +1 ;
200
+ alternateNode = false ;
201
+ mergeNode = true ;
202
+ last = null ; // both left and right are set of the conditional node
203
+ }
204
+ } // end of while loop
205
+
206
+ // To handle the case when last statements are part of some condition statements
207
+ node = new graph ();
208
+ node .start = lastConditionPoint ;
209
+ node .end = counter ;
210
+ node .index = ++number ;
211
+ nodes .add (node );
212
+ if (!mergeNode ) // alternateNode = true, both outgoing edges hasn't been taken care of
213
+ {
214
+ last .adjacents [1 ] = new graph ();
215
+ last .adjacents [1 ] = node ;
216
+ }
217
+ else // if alternateNode is present, then this is the merge node
218
+ {
219
+ // set the link of the nodes inside the conditional statements.
220
+ left .adjacents [0 ] = node ;
221
+ right .adjacents [0 ] = node ;
222
+ }
223
+ }
224
+ catch (Exception e )
225
+ {
226
+ e .printStackTrace ();
227
+ System .out .println (e .toString ());
228
+ }
229
+ }while (false ); // run only once :)
230
+ print_Nodes_Lines ();
231
+ print_Graph_Matrix ();
232
+ }
233
+ }
234
+
235
+
236
+
237
+
238
+
239
+
0 commit comments