public int calculateDiff(HollowEffigy comparison, int maxDiff) { runId++; simDiffCount.reset(); traverseComparisonFields(comparison, maxDiff); if(simDiffCount.diffCount >= maxDiff) return HollowEffigyCollectionPairer.MAX_MATRIX_ELEMENT_FIELD_VALUE; return score(); }
public HollowEffigyDiffRecord(HollowEffigy basedOn) { this.map = new HashMap<HollowEffigy.Field, FieldDiffCount>(); traverseOriginalFields(basedOn); }
public long[] pair(final BitSet pairedFromIndices, final BitSet pairedToIndices, final int maxDiff) { final long diffMatrixElements[] = new long[from.getFields().size() * to.getFields().size()]; int matrixElementIdx = 0; for(int i=0;i<from.getFields().size();i++) { final int fromIdx = i; if(pairedFromIndices.get(fromIdx)) { for(int j=0;j<to.getFields().size();j++) { diffMatrixElements[matrixElementIdx++] = getDiffMatrixElement(fromIdx, j, MAX_MATRIX_ELEMENT_FIELD_VALUE); } } else { HollowEffigy fromElement = getComparisonEffigy((HollowEffigy) from.getFields().get(fromIdx).getValue()); HollowEffigyDiffRecord diffRecord = new HollowEffigyDiffRecord(fromElement); for(int j=0;j<to.getFields().size();j++) { if(pairedToIndices.get(j)) { diffMatrixElements[matrixElementIdx++] = getDiffMatrixElement(fromIdx, j, MAX_MATRIX_ELEMENT_FIELD_VALUE); } else { HollowEffigy toElement = getComparisonEffigy((HollowEffigy) to.getFields().get(j).getValue()); int diffScore = diffRecord.calculateDiff(toElement, maxDiff); diffMatrixElements[matrixElementIdx++] = getDiffMatrixElement(fromIdx, j, diffScore); } } } } return diffMatrixElements; }
public void traverseComparisonFields(HollowEffigy comparison, int maxDiff) { for(Field field : comparison.getFields()) { if(field.isLeafNode()) { FieldDiffCount fieldCount = map.get(field); if(fieldCount == null) { if(simDiffCount.diffCount+1 >= maxDiff) { simDiffCount.diffCount++; return; } fieldCount = new FieldDiffCount(); map.put(field, fieldCount); } if(fieldCount.incrementComparisonCount(runId)) { if(++simDiffCount.diffCount >= maxDiff) return; } else { simDiffCount.simCount++; } } else { traverseComparisonFields((HollowEffigy) field.getValue(), maxDiff); if(simDiffCount.diffCount >= maxDiff) return; } } }
@Test public void test() throws IOException { HollowDiff diff = new FakeHollowDiffGenerator().createFakeDiff(); HollowEffigyFactory effigyFactory = new HollowEffigyFactory(); HollowEffigy fromEffigy = effigyFactory.effigy(diff.getFromStateEngine(), "TypeA", 0); HollowEffigy toEffigy = effigyFactory.effigy(diff.getToStateEngine(), "TypeA", 0); HollowEffigyDiffRecord diffRecord = new HollowEffigyDiffRecord(fromEffigy); Assert.assertEquals(8, diffRecord.calculateDiff(toEffigy, 8)); }
private void traverseOriginalFields(HollowEffigy effigy) { for(Field field : effigy.getFields()) { if(field.isLeafNode()) { FieldDiffCount fieldCount = map.get(field); if(fieldCount == null) { fieldCount = new FieldDiffCount(); map.put(field, fieldCount); } fieldCount.incrementOriginalCount(); totalOriginalFieldCount++; } else { traverseOriginalFields((HollowEffigy) field.getValue()); } } }