Databases Reference
In-Depth Information
input : eid = entity id, top_used = top used entities
output : snip = an annotated code snippet
1 begin
2
snip = empty string;
/* getUsedPositions(..) looks up SourcererDB and returns all positions in
the code where top_used entities are used. Positions are mapped to a list
of used entities
*/
used_pos_map = getUsedPositions(top_used, eid);
3
forall the position IN used_pos_map do
4
rationale = empty string;
5
forall the used_entity IN used_pos_map[position] do
/* Below, append(a,b) returns a new string by appending string 'b'
to 'a'.createRationale(..) selects relation type and FQN of used
entity and creates a rationale as a comment
6
*/
rationale = append(rationale, createRationale(used_entity, eid));
7
end
/* extractFragment(..) extracts the surrounding expression in a code
entity from position
8
*/
snip_fragment = extractFragment(eid, position);
/* appendSnip(..) works same as append(..) and returns true if
rationale and snip_fragment do not already exist in snip
9
*/
if appendSnip(rationale, snip_fragment)
snip then
10
snip = appendSnip(snip, rationale);
11
snip = appendSnip(snip, snip_fragment);
12
end
13
end
14
15 end
Algorithm 2: Snippet extraction
The second step involves generating code snippet for each entity in the hits.
This is done using the list of top used entities and the 'entity_id' of a given hit.
The algorithm for this process is shown in Algorithm 2 . The procedure first queries
SourcererDB to locate all the positions in the source of an entity where any of the
top APIs are used (Line 3). For all APIs that are used in a position, a rationale com-
ment is generated (Lines 5-8). A rationale comment indicates the type and FQN of
the used API. Then, a few of the surrounding lines of code are extracted from each
starting position (Line 9). Rationale comments are inserted on top of these extracted
lines (Lines 10-13). Finally, a sequence of these commented code fragments is re-
turned as an example code snippet. A sample Java code snippet generated using a
hit returned for a query “write to workbench error log” is shown in Fig. 8.5 .
In Fig. 8.5 , Lines 5 and 14 are two positions in the code where some top APIs
were found to be used. Lines 1-4 show rationale comments for two APIs ( IStatus
 
Search WWH ::




Custom Search