--- /dev/null
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+public class WeightCalculator{
+
+ public int calculateWeight(int[][] wcs){
+ int weight = 0;
+ boolean[][] availableFlag = new boolean[wcs.length][wcs[0].length];
+ for(int i = 0; i < wcs.length; i++){
+ for(int j = 0; j < wcs[i].length; j++){
+ availableFlag[i][j] = true;
+ }
+ }
+
+ int length = wcs.length;
+ if(length < wcs[0].length){
+ length = wcs[0].length;
+ }
+ for(int k = 0; k < length; k++){
+ int max = Integer.MIN_VALUE;
+ int column = -1;
+ int row = -1;
+ for(int i = 0; i < wcs.length; i++){
+ for(int j = 0; j < wcs[i].length; j++){
+ if(max < wcs[i][j] && availableFlag[i][j]){
+ max = wcs[i][j];
+ row = i;
+ column = j;
+ }
+ }
+ }
+ if(column >= 0 && row >= 0){
+ for(int i = 0; i < wcs.length; i++){
+ availableFlag[i][column] = false;
+ }
+ for(int j = 0; j < wcs[0].length; j++){
+ availableFlag[row][j] = false;
+ }
+ weight += wcs[column][row];
+ }
+ }
+
+ return weight;
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+/*
+ * $Id$
+ */
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class StackPatternBasedBirthmarkElementTest{
+ private StackPatternBasedBirthmarkElement element;
+
+ @Before
+ public void setup(){
+ CurrentDepth[] depthList = new CurrentDepth[7];
+
+ depthList[0] = new CurrentDepth(1, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[1] = new CurrentDepth(2, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[2] = new CurrentDepth(3, new Opcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
+ depthList[3] = new CurrentDepth(2, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
+ depthList[4] = new CurrentDepth(2, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
+ depthList[5] = new CurrentDepth(1, new Opcode(104, "imul", 0, -1, Opcode.Category.NORMAL, 6));
+ depthList[6] = new CurrentDepth(0, new Opcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
+
+ element = new StackPatternBasedBirthmarkElement(depthList);
+ }
+
+ @Test
+ public void testBasic() throws Exception{
+ Assert.assertEquals(7, element.getLength());
+ Assert.assertEquals(28, element.getWeight());
+
+ Assert.assertEquals(1, element.getDepth(0).getDepth());
+ Assert.assertEquals(2, element.getDepth(1).getDepth());
+ Assert.assertEquals(3, element.getDepth(2).getDepth());
+ Assert.assertEquals(2, element.getDepth(3).getDepth());
+ Assert.assertEquals(2, element.getDepth(4).getDepth());
+ Assert.assertEquals(1, element.getDepth(5).getDepth());
+ Assert.assertEquals(0, element.getDepth(6).getDepth());
+ }
+
+ @Test
+ public void testBuildFromString() throws Exception{
+ StackPatternBasedBirthmarkElement element2 = new StackPatternBasedBirthmarkElement(element.toString());
+
+ Assert.assertEquals(7, element2.getLength());
+ Assert.assertEquals(28, element2.getWeight());
+
+ Assert.assertEquals(1, element2.getDepth(0).getDepth());
+ Assert.assertEquals(2, element2.getDepth(1).getDepth());
+ Assert.assertEquals(3, element2.getDepth(2).getDepth());
+ Assert.assertEquals(2, element2.getDepth(3).getDepth());
+ Assert.assertEquals(2, element2.getDepth(4).getDepth());
+ Assert.assertEquals(1, element2.getDepth(5).getDepth());
+ Assert.assertEquals(0, element2.getDepth(6).getDepth());
+ }
+
+ @Test
+ public void testCalculateWeightedCommonSubsequence(){
+ CurrentDepth[] depthList = new CurrentDepth[10];
+ depthList[0] = new CurrentDepth(1, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[1] = new CurrentDepth(2, new Opcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
+ depthList[2] = new CurrentDepth(1, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
+ depthList[3] = new CurrentDepth(1, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
+ depthList[4] = new CurrentDepth(2, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[5] = new CurrentDepth(3, new Opcode( 5, "iconst_2", 0, 1, Opcode.Category.NORMAL, 1));
+ depthList[6] = new CurrentDepth(2, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
+ depthList[7] = new CurrentDepth(2, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
+ depthList[8] = new CurrentDepth(1, new Opcode( 96, "iadd", 0, -1, Opcode.Category.NORMAL, 3));
+ depthList[9] = new CurrentDepth(0, new Opcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
+ StackPatternBasedBirthmarkElement pattern2 = new StackPatternBasedBirthmarkElement(depthList);
+
+ Assert.assertEquals(21, element.getWeight(pattern2));
+ Assert.assertEquals(21, pattern2.getWeight(element));
+ }
+}
+++ /dev/null
-package jp.sourceforge.stigmata.birthmarks.wsp;
-
-/*
- * $Id$
- */
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- *
- * @author Haruaki Tamada
- * @version $Revision$
- */
-public class StackPatternTest{
- private StackPattern pattern;
-
- @Before
- public void setup(){
- pattern = new StackPattern();
-
- pattern.update(new CurrentDepth(1, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7)));
- pattern.update(new CurrentDepth(2, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7)));
- pattern.update(new CurrentDepth(3, new Opcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1)));
- pattern.update(new CurrentDepth(2, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4)));
- pattern.update(new CurrentDepth(2, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1)));
- pattern.update(new CurrentDepth(1, new Opcode(104, "imul", 0, -1, Opcode.Category.NORMAL, 6)));
- pattern.update(new CurrentDepth(0, new Opcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2)));
- }
-
- @Test
- public void testBasic() throws Exception{
- Assert.assertEquals(7, pattern.getLength());
- Assert.assertEquals(28, pattern.getWeight());
-
- Assert.assertEquals(1, pattern.getDepth(0).getDepth());
- Assert.assertEquals(2, pattern.getDepth(1).getDepth());
- Assert.assertEquals(3, pattern.getDepth(2).getDepth());
- Assert.assertEquals(2, pattern.getDepth(3).getDepth());
- Assert.assertEquals(2, pattern.getDepth(4).getDepth());
- Assert.assertEquals(1, pattern.getDepth(5).getDepth());
- Assert.assertEquals(0, pattern.getDepth(6).getDepth());
- }
-
- @Test
- public void testCalculateWeightedCommonSubsequence(){
- StackPattern pattern2 = new StackPattern();
-
- pattern2.update(1, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
- pattern2.update(2, new Opcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
- pattern2.update(1, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
- pattern2.update(1, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
- pattern2.update(2, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
- pattern2.update(3, new Opcode( 5, "iconst_2", 0, 1, Opcode.Category.NORMAL, 1));
- pattern2.update(2, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
- pattern2.update(2, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
- pattern2.update(1, new Opcode( 96, "iadd", 0, -1, Opcode.Category.NORMAL, 3));
- pattern2.update(0, new Opcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
-
- Assert.assertEquals(21, pattern.calculateWeight(pattern2));
- Assert.assertEquals(21, pattern2.calculateWeight(pattern));
- }
-}