テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の...

28
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka Univ テテテテテテテテテテテテテテテテ テテテテテテテテテテテテテテテ テテ テテ 1 , テテ テテ 2 , テ テテ 1 , テテ テテ 1 1 テテテテ 2 テテテテテテテテテテテテテ

description

テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案. 〇 山中 裕樹 1 , 吉田 則 裕 2 , 崔 恩瀞 1 , 井上 克郎 1. 1 大阪大学  2 奈良先端科学 技術大学院大学. メソッドクローン. メソッド単位のコードクローン 互いに一致または類似した処理を行うメソッド ソースコードのコピーアンドペーストなどによって発生. クローン ペア. メソッドクローンの定義 [1]. - PowerPoint PPT Presentation

Transcript of テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の...

Page 1: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

テキストマイニング技術を応用した

メソッドクローン検出手法の提案〇山中 裕樹 1, 吉田 則裕 2, 崔 恩瀞 1, 井上 克郎 1

1 大阪大学  2 奈良先端科学技術大学院大学

Page 2: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

2Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

メソッドクローン

メソッド単位のコードクローン►互いに一致または類似した処理を行うメソッド►ソースコードのコピーアンドペーストなどによって

発生

クローンペア

Page 3: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

3Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

メソッドクローンの定義 [1]

種類 意味

タイプ 1

レイアウト・空白・コメントの違いを除き完全に一致している

タイプ 2

タイプ 1 に加え変数名・型の違いを除き構文的に一致している

タイプ 3 タイプ 2 に加え文が挿入・削除・変更されている

タイプ 4 構文上異なる実装だが,同一処理を実行している[1]C. K. Roy, J. R. Cordy, R. Koschke. Comparison and evaluation of code clone detection techniques and tools: a qualitative approach. Science of Computer Programming, Vol. 74, No. 7, pp. 470–495, 2009.

Page 4: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

4Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

種類 意味

タイプ 1

レイアウト・空白・コメントの違いを除き完全に一致している

タイプ 2

タイプ 1 に加え変数名・型の違いを除き構文的に一致している

タイプ 3 タイプ 2 に加え文が挿入・削除・変更されている

タイプ 4 構文上異なる実装だが,同一処理を実行している

int sum(int[] data){ int sum = 0; for(int i=0; i<data.length; i++){ sum = sum + data[i] ;

} return sum;}

int sum(int[] data){ int sum = 0; for(int i=0; i<data.length; i++){ sum = sum + data[i] ;

} return sum;}メソッド 1 メソッド 2

メソッドクローン:タイプ 1

Page 5: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

5Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

種類 意味

タイプ 1

レイアウト・空白・コメントの違いを除き完全に一致している

タイプ 2

タイプ 1に加え変数名・型の違いを除き構文的に一致している

タイプ 3 タイプ 2 に加え文の挿入・削除・変更されている

タイプ 4 構文上異なる実装だが,同一処理を実行している

メソッド 1 メソッド 2

int sum(int[] data){ int sum = 0; for(int i=0; i<data.length; i++){ sum = sum + data[i] ;

} return sum;}

double sum(double[] data){ double sum = 0; for(int j=0; j<data.length; j++){ sum = sum + data[j] ;

} return sum;}

メソッドクローン:タイプ 2

Page 6: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

6Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

種類 意味

タイプ 1

レイアウト・空白・コメントの違いを除き完全に一致している

タイプ 2

タイプ 1 に加え変数名・型の違いを除き構文的に一致している

タイプ 3 タイプ 2に加え文が挿入・削除・変更されている

タイプ 4 構文上異なる実装だが,同一処理を実行している

メソッド 1 メソッド 2

int sum(int[] data){ int sum = 0; for(int i=0; i<data.length; i++){ sum = sum + data[i];

} return sum;}

int sum(int[] data){ int sum = 0; for(int i=0; i<data.length; i++){ sum += data[i]; } return sum;}

メソッドクローン:タイプ 3

Page 7: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

7Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

種類 意味

タイプ 1

レイアウト・空白・コメントの違いを除き完全に一致している

タイプ 2

タイプ 1 に加え変数名・型の違いを除き構文的に一致している

タイプ 3 タイプ 2 に加え文が挿入・削除・変更されている

タイプ 4 構文上異なる実装だが,同一処理を実行している

int sum(int[] data){ int sum = 0; for(int i=0; i<data.length; i++){ sum = sum + data[i];

} return sum;}

int sum(int[] data){ int sum = 0, i=0; while(i<data.length){ sum += data[i];   i++; } return sum;}

メソッドクローン:タイプ 4

メソッド 1 メソッド 2

Page 8: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

8Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

本研究の概要タイプ 1- タイプ 4 のメソッドクローン検出

手法の提案►テキストマイニング技術を利用►メソッドを特徴ベクトルに変換し,類似度を計

処理が類似したメソッドを高速に検出

Page 9: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

9Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

メソッドクローン検出の目的リファクタリング支援

一貫した修正漏れによるバグの検出(タイプ 3,4 )

Class A

hoge()

Class B

hoge()Class A

Class S

hoge()

Class Bクローン

メソッドの引上げ

int sum(int[] data){ if ( data == null) return null; ・・・}

int sum(int[] data){ // 例外処理漏れ ・・・}

例外処理の追加漏れ

Page 10: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

10Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

既存のクローン検出手法構文的な類似性に着目した手法 [2][3]

►メリット:タイプ 1, タイプ 2 のクローンを高速に検出可能

►デメリット:タイプ 3,タイプ 4のクローンを検出が困難

意味的な類似性に着目した手法 [4]►メリット:タイプ 1- タイプ 4 のクローンを検出

可能►デメリット:検出時間に膨大な時間がかかる

[2] T. Kamiya, S. Kusumoto, K. Inoue. CCFinder: a multilinguistic token-based code clone detection system for large scale source code. IEEE Trans. Softw. Eng., Vol. 28, No. 7, pp. 654–670, 2002. [3] L. Jiang, G. Misherghi, Z. Su, S. Glondu. DECKARD: scalable and accurate tree-based detection of code clones. In Proc. of ICSE ’07, pp. 96-105, 2007.[4] R. Komondoor, S. Horwitz. Using slicing to identify duplication in source code. In Proc. of SAS ’01, pp. 40–56, 2001.

Page 11: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

11Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

提案手法の概要メソッド中のワードに重みを付け特徴ベクトル

を計算►識別子名を構成する単語 ( 関数名,変数名 )►予約語に含まれる単語 ( if, while )

近似アルゴリズムを用いて特徴ベクトルをクラスタリング• タイプ 1 からタイプ 4 のメソッドクロー

ンを検出• 近似アルゴリズムにより高速な検出を実

Page 12: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

12Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

検出アルゴリズム

ワード 回数xxx 3yyy 2

・・・・・・

メソッドA

メソッドB

ワード 回数xxx 2yyy 4

・・・ ・・・

メソッドC

メソッドD

メソッドE

類似度 メソッド対クロー

0.95メソッド A ✓メソッド B

0.70メソッド C

 メソッド D

0.70メソッド C

 メソッド E

0.90メソッド D ✓メソッド E

・・・ ・・・ ・・・

},,,{ 321  aaa

},,,{ 321  bbb

メソッド A

メソッド B メソッド B

メソッド A

STEP1 :各メソッドからワードの抽出STEP2: ワードに対して重みを計算し特徴ベクトルに変換STEP3: 各メソッドの特徴ベクトルをクラスタリングSTEP4: クラスタ中の特徴ベクトル間の類似度を計算STEP1 STEP2 STEP3 STEP4

ソースコード

ワードリスト

特徴ベクトル

クラスタ クローンペアリスト

Page 13: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

13Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

STEP1 :ワードの抽出各メソッドから識別子名・予約語を抽出

►2 文字以下の識別子は 1 つのメタワードとして扱う

►複数の単語の場合,区切り文字や大文字で分割例: dataSize data + size⇒

ワード 出現回数

予約語int 4for 1

return 1

分割した識別子名

data 4sum 4size 2

メタワード 4

int sum(int[] data, int dataSize){ int sum = 0; for(int i=0; i<dataSize; i++) sum += data[i]; return sum;}

メソッド A のワードリストメソッド A のソースコード

Page 14: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

14Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

STEP1 :ワードの抽出各メソッドから識別子名・予約語を抽出

►2文字以下の識別子は 1つのメタワードとして扱う

►複数の単語の場合,区切り文字や大文字で分割例: dataSize data + size⇒

ワード 出現回数

予約語int 4for 1

return 1

分割した識別子名

data 4sum 4size 2

メタワード 4

int sum(int[] data, int dataSize){ int sum = 0; for(int i=0; i<dataSize; i++) sum += data[i]; return sum;}

メソッド A のワードリストメソッド A のソースコード

Page 15: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

15Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

STEP1 :ワードの抽出各メソッドから識別子名・予約語を抽出

►2 文字以下の識別子は 1 つのメタワードとして扱う

►複数の単語の場合,区切り文字や大文字で分割例: dataSize data + size⇒

ワード 出現回数

予約語int 4for 1

return 1

分割した識別子名

data 4sum 4size 2

メタワード 4

int sum(int[] data, int dataSize){ int sum = 0; for(int i=0; i<dataSize); i++) sum += data[i]; return sum;}

メソッド A のワードリストメソッド A のソースコード

Page 16: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

16Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

STEP2 :特徴ベクトルの計算

TF-IDF 法を利用►文書中の単語に関する重み付けの手法►TF値と IDF値の積で表される

各ワードの重みを特徴量として各メソッドを特徴ベクトルに変換

メソッド中のワードの出現頻度

ソースコード全体のワードの希少さ×

Page 17: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

17Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

TF-IDF の計算例

ワード 出現回数

予約語int 4for 1

return 1

分割した識別子名

data 4sum 4size 2

メタワード 4

ワード メソッド A

メソッドB

メソッドC

メソッド D

・・・

・・・

・・・

・・・

・・・

sum ✔ ✔・・・

・・・

・・・

・・・

・・・

20.0204

sumTF 30.024log

sumIDF

06.0- sumIDFTF

×

ワード sum の特徴量

メソッド A におけるワード sum の TF-IDF 値を求める

メソッド A のワードリスト

全メソッドのワード含有のチェック

sum の出現回数

全ワードの出現回数

全メソッド数

sum を含むメソッド数

Page 18: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

18Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

STEP3 :特徴ベクトルのクラスタリング

LSH(Locality-Sensitive Hashing)[5] を利用►近似最近傍探索アルゴリズムの一つ

ハッシュ関数を用いて高速にクラスタリング可能►クローンペアと成りうる候補を絞ることが目的

メソッド名 特徴ベクトル

メソッド A (5,4,2,1, ・・・ )

メソッド B (0,0,2,2, ・・・ )

メソッド C (0,0,2,2, ・・・ )

メソッド D (3,4,2,1, ・・・ )

・・・ ・・・各メソッドの特徴ベク

トル

クラスタリング

メソッドのクラスタ

[5] P. Indyk, R. Motwani. Approximate nearest neighbors: towards removing the curse of dimensionality. In Proc. of STOC ’98, pp. 604-613, 1998.

メソッド Aメソッド D

メソッド Bメソッド C

Page 19: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

19Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

STEP4: ベクトル間の類似度計算各クラスタ内で特徴ベクトル間の類似度を

計算►コサイン類似度を利用►特徴ベクトル 間の類似度の計算方法

ba

bababasim

,cos,

閾値( 0.9)以上であればクローンとして検出

ba ,

Page 20: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

20Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

評価実験:リサーチクエッション

RQ1:►タイプ 1- タイプ 4 のクローンを検出でき

るかRQ2 :

►既存手法と比較して本手法は有用であるかRQ3:

►どのようなメソッドクローンを検出できるか

Page 21: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

21Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

RQ1: 検出性の評価Roy らのベンチマーク [6] を利用

►タイプ 1- タイプ 4 の 16 個のクローンペアが用意

全タイプのクローンを検出することを確認

タイプ1

タイプ2

タイプ3

タイプ4

ベンチマーク

3 4 5 4

検出結果 3 2 5 4

[6] C. K. Roy, J. R. Cordy, R. Koschke. Comparison and evaluation of code clone detection techniques and tools: a qualitative approach. Science of Computer Programming, Vol. 74, No. 7, pp.470-495, 2009.

Page 22: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

22Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

RQ2: 既存手法との比較メモリベースの検出ツール MeCC[7] と比較

►タイプ 1- タイプ 4 の手続き(関数)単位のクローン検出

MeCC の評価実験で利用された OSS に適用►検出精度と検出時間を比較

プロジェクト名 言語 サイズApacheHTTPD C 343KLOC

Python C 435KLOCPostgreSQL C 937KLOC

[7] H. Kim, Y. Jung, S. Kim, K. Yi. MeCC: memory comparison-based clone detector. In Proc. of ICSE ’11, pp. 301–310, 2011.

Page 23: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

23Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

RQ2: 検出精度の比較結果適合率と各タイプの正解検出クローン数を

比較 プロジェクト名

適合率検出クローン

タイプ1

タイプ2

タイプ3

タイプ4

本手法

Python 94.5% 19 103 159 21ApacheHTTPD 95.4% 71 100 190 11

PostgreSQL 94.7% 57 230 341 17合計 94.6% 147 433 690 49

MeCC

Python 85.3% 3 127 82 13ApacheHTTPD 87.5% 2 84 71 10

PostgreSQL 83.1% 9 120 88 14合計 85.00% 14 331 241 37• MeCC よりも高い適合率でクローンを検

出• 各タイプのクローン検出数も MeCC より

多い

Page 24: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

24Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

RQ2: 検出時間の比較結果

ApacheHTTPD Python PostgreSQL

本手法 1m43s 2m13s 4m39s

MeCC 310m34s 65m26s 428m32s

MeCC よりも高速にクローン検出を行うことが可能

MeCC の実行環境と同一のものを利用►Ubuntu 64-bit ( RAM: 8.0GB, CPU: Intel Xeon 2.4GHz)

Page 25: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

25Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

RQ3: 検出クローンの例 (1/2)繰り返し処理の置き換え

static const XML_Char * FASTCALLpoolCopyString(STRING_POOL *pool, const   XML_Char *s){ // 例外処理漏れの可能性がある ! do { if (!poolAppendChar(pool, *s)) return NULL; }while (*s++); s = pool->start; poolFinish(pool); return s;}

static const XML_Char *poolCopyStringN(STRING_POOL *pool, const     XML_Char *s, int n){ if (!pool->ptr && !poolGrow(pool)) return NULL; for (; n > 0; --n, s++) { if (!poolAppendChar(pool, *s)) return NULL; } s = pool->start; poolFinish(pool); return s;}for 文を用いた繰り返し do-while 文を用いた繰り返

Page 26: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

26Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

RQ3: 検出クローンの例 (2/2)文の並び替え static PyObject *

dequereviter_next(dequeiterobject *it){ PyObject *item; if (it->counter == 0) return NULL; if (it->deque->state != it->state) { it->counter = 0; PyErr_SetString(PyExc_RuntimeError,

"deque mutated during iteration"); return NULL; } assert (!(it->b == it->deque->leftblock && it->index < it->deque->leftindex));

・・・

}

static PyObject *dequeiter_next(dequeiterobject *it){ PyObject *item; if (it->deque->state != it->state) { it->counter = 0; PyErr_SetString(PyExc_RuntimeError, "deque mutated during iteration"); return NULL; } if (it->counter == 0) return NULL; assert (!(it->b == it->deque->rightblock &&

it->index > it->deque->rightindex));

・・・

}

例外処理の位置が異なる

Page 27: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

27Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

まとめと今後の課題まとめ

►テキストマイニング技術を用いたタイプ 4 のクローン検出手法を提案

►既存手法と比較して高精度・高速なクローン検出が可能

今後の課題►他のクローン検出ツールとの比較►タイプ 2 のクローン検出精度の向上

辞書を用いたワードのクラスタリングなどを利用

Page 28: テキストマイニング 技術 を 応用 した メソッドクローン検出 手法 の 提案

Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University

ご清聴ありがとうございました