OSDN Git Service

cflib は plugins プロジェクトから,Stigmata直下のプロジェクトに移行したため,このリポジトリからは削除した.
[stigmata/stigmata-plugins.git] / wsp / src / main / java / jp / sourceforge / stigmata / birthmarks / wsp / StackPatternBasedBirthmarkComparator.java
1 package jp.sourceforge.stigmata.birthmarks.wsp;
2
3 import jp.sourceforge.stigmata.Birthmark;
4 import jp.sourceforge.stigmata.BirthmarkContext;
5 import jp.sourceforge.stigmata.BirthmarkElement;
6 import jp.sourceforge.stigmata.birthmarks.comparators.AbstractBirthmarkComparator;
7 import jp.sourceforge.stigmata.spi.BirthmarkService;
8
9 /**
10  * 
11  * @author Haruaki Tamada
12  */
13 public class StackPatternBasedBirthmarkComparator
14         extends AbstractBirthmarkComparator{
15     public StackPatternBasedBirthmarkComparator(BirthmarkService spi){
16         super(spi);
17     }
18
19     @Override
20     public double compare(Birthmark b1, Birthmark b2,
21                           BirthmarkContext context){
22         int[][] wcs = createMatrix(b1, b2);
23         int weightOfWcs = new WeightCalculator().calculateWeight(wcs);
24         int weightOfBirthmark1 = 0;
25         for(BirthmarkElement element: b1){
26             weightOfBirthmark1 +=
27                 ((StackPatternBasedBirthmarkElement)element).getWeight();
28         }
29
30         return (double)weightOfWcs / (double)weightOfBirthmark1;
31     }
32
33     private int[][] createMatrix(Birthmark b1, Birthmark b2){
34         BirthmarkElement[] elementsA = b1.getElements();
35         BirthmarkElement[] elementsB = b2.getElements();
36
37         int[][] matrix = new int[elementsA.length][elementsB.length];
38         for(int i = 0; i < elementsA.length; i++){
39             for(int j = 0; j < elementsB.length; j++){
40                 StackPatternBasedBirthmarkElement wsp1 = 
41                     (StackPatternBasedBirthmarkElement)elementsA[i];
42                 StackPatternBasedBirthmarkElement wsp2 =
43                     (StackPatternBasedBirthmarkElement)elementsB[j];
44                 matrix[i][j] = wsp1.getWeight(wsp2);
45             }
46         }
47         return matrix;
48     }
49 }