Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEMI algorithm chooses 1 line to extract #90

Open
lyriccoder opened this issue Oct 29, 2020 · 2 comments
Open

SEMI algorithm chooses 1 line to extract #90

lyriccoder opened this issue Oct 29, 2020 · 2 comments

Comments

@lyriccoder
Copy link
Member

lyriccoder commented Oct 29, 2020

SEMI algorithm chooses 1 line to extract.
Seems we consider extracting more than 3 lines, don't we?

 DocumentElement getDocumentElement(int startOffset, int endOffset) throws BadLocationException {
        readLock();
        checkDocumentDirty();
        try {
            for(int i = 0; i < elements.size(); i++) {
                DocumentElement de = elements.get(i);
                if(de.getStartOffset() == startOffset &&
                        de.getEndOffset() == endOffset)
                    return de;
                if(de.getStartOffset() > startOffset) break;
            }
            return null;
        }finally{
            readUnlock();
        }
    }
    List<DocumentElement> getDocumentElements(int startOffset) throws BadLocationException {
        readLock();
        if(documentDirty) {
            writeLock(); // This line is suggested to extract
            try {
                doc.readLock();
                try {
                    elements.resort();
                } finally {
                    doc.readUnlock();
                }
            }finally {
                writeUnlock();
            }
            documentDirty = false;
        }
        try {
            int elementIndex = elements.binarySearchForOffset(startOffset);
            if(elementIndex < 0) {
                return Collections.emptyList();
            } else {
                ArrayList<DocumentElement> found = new ArrayList<DocumentElement>();
                found.add(elements.get(elementIndex));
                int eli = elementIndex;
                while(--eli >= 0) {
                    DocumentElement previous = elements.get(eli);
                    if(previous.getStartOffset() == startOffset) {
                        found.add(0, previous);
                    } else {
                        break;
                    }
                }
                while(++elementIndex < elements.size()) {
                    DocumentElement next = elements.get(elementIndex);
                    if(next.getStartOffset() == startOffset) {
                        found.add(next);
                    } else {
                        break;
                    }
                }
                return found;
            }
        }finally{
            readUnlock();
        }
    }

writeLock(); // This line is suggested to extract

Is it ok?
DocumentModel_618deb49bbf23fe675cea45f2e2cf3f69e8231df8d81a2f47123c696ac11dbf1_getDocumentElements_199.zip

@aravij
Copy link
Contributor

aravij commented Nov 2, 2020

For the given file

1. class Example {
2.      DocumentElement getDocumentElement(int startOffset, int endOffset) throws BadLocationException {
3.         readLock();
4.         checkDocumentDirty();
5.         try {
6.             for(int i = 0; i < elements.size(); i++) {
7.                 DocumentElement de = elements.get(i);
8.                 if(de.getStartOffset() == startOffset &&
9.                         de.getEndOffset() == endOffset)
10.                     return de;
11.                 if(de.getStartOffset() > startOffset) break;
12.             }
13.             return null;
14.         }finally{
15.             readUnlock();
16.         }
17.     }
18.     List<DocumentElement> getDocumentElements(int startOffset) throws BadLocationException {
19.         readLock();
20.         if(documentDirty) {
21.             writeLock(); // This line is suggested to extract
22.             try {
23.                 doc.readLock();
24.                 try {
25.                     elements.resort();
26.                 } finally {
27.                     doc.readUnlock();
28.                 }
29.             }finally {
30.                 writeUnlock();
31.             }
32.             documentDirty = false;
33.         }
34.         try {
35.             int elementIndex = elements.binarySearchForOffset(startOffset);
36.             if(elementIndex < 0) {
37.                 return Collections.emptyList();
38.             } else {
39.                 ArrayList<DocumentElement> found = new ArrayList<DocumentElement>();
40.                 found.add(elements.get(elementIndex));
41.                 int eli = elementIndex;
42.                 while(--eli >= 0) {
43.                     DocumentElement previous = elements.get(eli);
44.                     if(previous.getStartOffset() == startOffset) {
45.                         found.add(0, previous);
46.                     } else {
47.                         break;
48.                     }
49.                 }
50.                 while(++elementIndex < elements.size()) {
51.                     DocumentElement next = elements.get(elementIndex);
52.                     if(next.getStartOffset() == startOffset) {
53.                         found.add(next);
54.                     } else {
55.                         break;
56.                     }
57.                 }
58.                 return found;
59.             }
60.         }finally{
61.             readUnlock();
62.         }
63.     }
64. }

The result of creating extraction opportunities step is following:

8 opportunities found in method getDocumentElement in class Example in file Example.java:
0th extraction opportunity:
        First statement: Statement expression on line 3
        Last statement: Statement expression on line 3

1th extraction opportunity:
        First statement: Statement expression on line 4
        Last statement: Statement expression on line 4

2th extraction opportunity:
        First statement: For statement on line 6
        Last statement: Return statement on line 10

3th extraction opportunity:
        First statement: If statement on line 11
        Last statement: If statement on line 11

4th extraction opportunity:
        First statement: Break statement on line 11
        Last statement: Break statement on line 11

5th extraction opportunity:
        First statement: Return statement on line 13
        Last statement: Return statement on line 13

6th extraction opportunity:
        First statement: Statement expression on line 15
        Last statement: Statement expression on line 15

7th extraction opportunity:
        First statement: For statement on line 6
        Last statement: If statement on line 11

20 opportunities found in method getDocumentElements in class Example in file Example.java:
0th extraction opportunity:
        First statement: Statement expression on line 19
        Last statement: Statement expression on line 19

1th extraction opportunity:
        First statement: If statement on line 20
        Last statement: If statement on line 20

2th extraction opportunity:
        First statement: Statement expression on line 21
        Last statement: Statement expression on line 21

3th extraction opportunity:
        First statement: Statement expression on line 23
        Last statement: Statement expression on line 23

4th extraction opportunity:
        First statement: Statement expression on line 25
        Last statement: Statement expression on line 25

5th extraction opportunity:
        First statement: Statement expression on line 27
        Last statement: Statement expression on line 27

6th extraction opportunity:
        First statement: Statement expression on line 30
        Last statement: Statement expression on line 30

7th extraction opportunity:
        First statement: Statement expression on line 32
        Last statement: Statement expression on line 32

8th extraction opportunity:
        First statement: Local variable declaration on line 35
        Last statement: If statement on line 36

9th extraction opportunity:
        First statement: Return statement on line 37
        Last statement: Return statement on line 37

10th extraction opportunity:
        First statement: Local variable declaration on line 39
        Last statement: Statement expression on line 45

11th extraction opportunity:
        First statement: Break statement on line 47
        Last statement: Break statement on line 47

12th extraction opportunity:
        First statement: While statement on line 50
        Last statement: Statement expression on line 53

13th extraction opportunity:
        First statement: Break statement on line 55
        Last statement: Break statement on line 55

14th extraction opportunity:
        First statement: Return statement on line 58
        Last statement: Return statement on line 58

15th extraction opportunity:
        First statement: Statement expression on line 61
        Last statement: Statement expression on line 61

16th extraction opportunity:
        First statement: Local variable declaration on line 35
        Last statement: Statement expression on line 45

17th extraction opportunity:
        First statement: Statement expression on line 19
        Last statement: Statement expression on line 27

18th extraction opportunity:
        First statement: Local variable declaration on line 35
        Last statement: Statement expression on line 53

19th extraction opportunity:
        First statement: Statement expression on line 19
        Last statement: Statement expression on line 61

After filtering the kept opportunities are:

4 opportunities found in method getDocumentElement in class Example in file Example.java:
0th extraction opportunity:
        First statement: Statement expression on line 3
        Last statement: Statement expression on line 3

1th extraction opportunity:
        First statement: Statement expression on line 4
        Last statement: Statement expression on line 4

2th extraction opportunity:
        First statement: Return statement on line 13
        Last statement: Return statement on line 13

3th extraction opportunity:
        First statement: Statement expression on line 15
        Last statement: Statement expression on line 15

10 opportunities found in method getDocumentElements in class Example in file Example.java:
0th extraction opportunity:
        First statement: Statement expression on line 19
        Last statement: Statement expression on line 19

1th extraction opportunity:
        First statement: Statement expression on line 21
        Last statement: Statement expression on line 21

2th extraction opportunity:
        First statement: Statement expression on line 23
        Last statement: Statement expression on line 23

3th extraction opportunity:
        First statement: Statement expression on line 25
        Last statement: Statement expression on line 25

4th extraction opportunity:
        First statement: Statement expression on line 27
        Last statement: Statement expression on line 27

5th extraction opportunity:
        First statement: Statement expression on line 30
        Last statement: Statement expression on line 30

6th extraction opportunity:
        First statement: Statement expression on line 32
        Last statement: Statement expression on line 32

7th extraction opportunity:
        First statement: Return statement on line 37
        Last statement: Return statement on line 37

8th extraction opportunity:
        First statement: Return statement on line 58
        Last statement: Return statement on line 58

9th extraction opportunity:
        First statement: Statement expression on line 61
        Last statement: Statement expression on line 61

All kept extraction opportunities are single statements ones.

Ranking kept extraction opportunities we get:

Extraction opportunities groups of method getDocumentElement in class Example in file Example.java:
        Extraction opportunities group with scope 15:
                Extraction opportunity with score 15:
                        Statement expression on line 3
        Extraction opportunities group with scope 15:
                Extraction opportunity with score 15:
                        Statement expression on line 4
        Extraction opportunities group with scope 15:
                Extraction opportunity with score 15:
                        Return statement on line 13
        Extraction opportunities group with scope 15:
                Extraction opportunity with score 15:
                        Statement expression on line 15
Extraction opportunities groups of method getDocumentElements in class Example in file Example.java:
        Extraction opportunities group with scope 43:
                Extraction opportunity with score 43:
                        Statement expression on line 21
        Extraction opportunities group with scope 43:
                Extraction opportunity with score 43:
                        Statement expression on line 30
        Extraction opportunities group with scope 43:
                Extraction opportunity with score 43:
                        Return statement on line 37
        Extraction opportunities group with scope 41:
                Extraction opportunity with score 41:
                        Statement expression on line 19
        Extraction opportunities group with scope 41:
                Extraction opportunity with score 41:
                        Statement expression on line 32
        Extraction opportunities group with scope 41:
                Extraction opportunity with score 41:
                        Statement expression on line 61
        Extraction opportunities group with scope 39:
                Extraction opportunity with score 39:
                        Statement expression on line 23
        Extraction opportunities group with scope 39:
                Extraction opportunity with score 39:
                        Statement expression on line 27
        Extraction opportunities group with scope 35:
                Extraction opportunity with score 35:
                        Return statement on line 58
        Extraction opportunities group with scope 33:
                Extraction opportunity with score 33:
                        Statement expression on line 25

Each group consist of a single opportunity. The one with highest score in getDocumentElements method suggests to extract the statement you marked.

@aravij
Copy link
Contributor

aravij commented Nov 3, 2020

Issues based on findings:

  • Find structure/syntactic statements connections instead of semantic connection
  • Instead of filtering transform opportunities by adding statements
  • Instead of greedy clustering start opportunity from all statements
  • Instead of start search from lines start from variables/objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants