OSDN Git Service

787c2487dbcb55a040b65073b555827667ca13a4
[stigmata/stigmata-plugins.git] / wsp / src / main / java / jp / sourceforge / stigmata / birthmarks / wsp / StackPatternBasedBirthmarkComparator.java
1 package jp.sourceforge.stigmata.birthmarks.wsp;
2
3 /*
4  * $Id$
5  */
6
7 import jp.sourceforge.stigmata.Birthmark;
8 import jp.sourceforge.stigmata.BirthmarkContext;
9 import jp.sourceforge.stigmata.BirthmarkElement;
10 import jp.sourceforge.stigmata.birthmarks.comparators.AbstractBirthmarkComparator;
11 import jp.sourceforge.stigmata.spi.BirthmarkSpi;
12
13 /**
14  * 
15  * @author Haruaki Tamada
16  * @version $Revision$
17  */
18 public class StackPatternBasedBirthmarkComparator extends AbstractBirthmarkComparator{
19     public StackPatternBasedBirthmarkComparator(BirthmarkSpi spi){
20         super(spi);
21     }
22
23     @Override
24     public double compare(Birthmark b1, Birthmark b2, BirthmarkContext context){
25         int[][] wcs = createMatrix(b1, b2);
26         int weightOfWcs = new WeightCalculator().calculateWeight(wcs);
27         int weightOfBirthmark1 = 0;
28         for(BirthmarkElement element: b1){
29             weightOfBirthmark1 += ((StackPatternBasedBirthmarkElement)element).getWeight();
30         }
31
32         return (double)weightOfWcs / (double)weightOfBirthmark1;
33     }
34
35     private int[][] createMatrix(Birthmark b1, Birthmark b2){
36         BirthmarkElement[] elementsA = b1.getElements();
37         BirthmarkElement[] elementsB = b2.getElements();
38
39         int[][] matrix = new int[elementsA.length][elementsB.length];
40         for(int i = 0; i < elementsA.length; i++){
41             for(int j = 0; j < elementsB.length; j++){
42                 StackPatternBasedBirthmarkElement wsp1 = (StackPatternBasedBirthmarkElement)elementsA[i];
43                 StackPatternBasedBirthmarkElement wsp2 = (StackPatternBasedBirthmarkElement)elementsB[j];
44                 matrix[i][j] = wsp1.getWeight(wsp2);
45             }
46         }
47         return matrix;
48     }
49 }