Tested and refactored cluster groups and clusters

This commit is contained in:
Lennart Hensler committed 2014-03-11 10:12:14 +01:00
1 parent e0a9f552b6
commit 5601a00b4e
9 files changed
+165 -26

No files matched your search

@@ -9,7 +9,6 @@ import org.junit.Test;
import com.sap.sse.datamining.data.Cluster;
import com.sap.sse.datamining.data.ClusterBoundary;
import com.sap.sse.datamining.data.ClusterBoundary.ComparisonStrategy;
public class TestClustersWithBoundaries {
@@ -8,7 +8,6 @@ import java.util.Comparator;
import org.junit.Test;
import com.sap.sse.datamining.data.ClusterBoundary;
import com.sap.sse.datamining.data.ClusterBoundary.ComparisonStrategy;
public class TestComparatorClusterBoundary {
@@ -1,28 +1,92 @@
package com.sap.sse.datamining.impl.data;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import org.junit.Ignore;
import org.junit.Before;
import org.junit.Test;
import com.sap.sse.datamining.data.Cluster;
import com.sap.sse.datamining.data.ClusterBoundary;
import com.sap.sse.datamining.data.ClusterGroup;
public class TestFixClusterGroup {
public static final Comparator<Integer> comparator = new ComparableComparator<Integer>();
@Ignore
private Cluster<Integer> clusterLowerThanZero;
private Cluster<Integer> clusterFromZeroToTen;
private Cluster<Integer> clusterFromElevenToTwenty;
private Cluster<Integer> clusterFromTwentyOneToThirty;
private Cluster<Integer> clusterGreaterThanThirty;
@Test
public void testClusterGroupWithClosedBoundaries() {
ClusterGroup<Integer> groupWithClosedBoundaries = createGroupWithClosedBoundaries();
fail("Not yet implemented");
assertThat(groupWithClosedBoundaries.getClusterFor(0), is(clusterFromZeroToTen));
assertThat(groupWithClosedBoundaries.getClusterFor(10), is(clusterFromZeroToTen));
assertThat(groupWithClosedBoundaries.getClusterFor(11), is(clusterFromElevenToTwenty));
assertThat(groupWithClosedBoundaries.getClusterFor(20), is(clusterFromElevenToTwenty));
assertThat(groupWithClosedBoundaries.getClusterFor(21), is(clusterFromTwentyOneToThirty));
assertThat(groupWithClosedBoundaries.getClusterFor(30), is(clusterFromTwentyOneToThirty));
assertThat(groupWithClosedBoundaries.getClusterFor(-1), nullValue());
assertThat(groupWithClosedBoundaries.getClusterFor(31), nullValue());
}
private ClusterGroup<Integer> createGroupWithClosedBoundaries() {
// TODO Auto-generated method stub
return null;
Collection<Cluster<Integer>> clusters = new ArrayList<>();
clusters.add(clusterFromZeroToTen);
clusters.add(clusterFromElevenToTwenty);
clusters.add(clusterFromTwentyOneToThirty);
return new FixClusterGroup<>(clusters);
}
@Test
public void testClusterGroupWithOpenBoundaries() {
ClusterGroup<Integer> groupWithOpenBoundaries = createGroupWithOpenBoundaries();
assertThat(groupWithOpenBoundaries.getClusterFor(-1), is(clusterLowerThanZero));
assertThat(groupWithOpenBoundaries.getClusterFor(31), is(clusterGreaterThanThirty));
}
private ClusterGroup<Integer> createGroupWithOpenBoundaries() {
Collection<Cluster<Integer>> clusters = new ArrayList<>();
clusters.add(clusterLowerThanZero);
clusters.add(clusterFromZeroToTen);
clusters.add(clusterFromElevenToTwenty);
clusters.add(clusterFromTwentyOneToThirty);
clusters.add(clusterGreaterThanThirty);
return new FixClusterGroup<>(clusters);
}
@Before
public void initializeClusters() {
ClusterBoundary<Integer> lowerThanZero = new ComparatorClusterBoundary<Integer>(comparator, 0, ComparisonStrategy.LOWER_THAN);
clusterLowerThanZero = new ClusterWithSingleBoundary<>("Test", lowerThanZero);
ClusterBoundary<Integer> greaterEqualsThanZero = new ComparatorClusterBoundary<Integer>(new ComparableComparator<Integer>(), 0, ComparisonStrategy.GREATER_EQUALS_THAN);
ClusterBoundary<Integer> lowerEqualsThanTen = new ComparatorClusterBoundary<Integer>(new ComparableComparator<Integer>(), 10, ComparisonStrategy.LOWER_EQUALS_THAN);
clusterFromZeroToTen = new ClusterWithBoundaries<>("Test", greaterEqualsThanZero, lowerEqualsThanTen);
ClusterBoundary<Integer> greaterThanTen = new ComparatorClusterBoundary<Integer>(new ComparableComparator<Integer>(), 10, ComparisonStrategy.GREATER_THAN);
ClusterBoundary<Integer> lowerEqualsThanTwenty = new ComparatorClusterBoundary<Integer>(new ComparableComparator<Integer>(), 20, ComparisonStrategy.LOWER_EQUALS_THAN);
clusterFromElevenToTwenty = new ClusterWithBoundaries<>("Test", greaterThanTen, lowerEqualsThanTwenty);
ClusterBoundary<Integer> greaterThanTwenty = new ComparatorClusterBoundary<Integer>(new ComparableComparator<Integer>(), 20, ComparisonStrategy.GREATER_THAN);
ClusterBoundary<Integer> lowerEqualsThanThirty = new ComparatorClusterBoundary<Integer>(new ComparableComparator<Integer>(), 30, ComparisonStrategy.LOWER_EQUALS_THAN);
clusterFromTwentyOneToThirty = new ClusterWithBoundaries<>("Test", greaterThanTwenty, lowerEqualsThanThirty);
ClusterBoundary<Integer> greaterThanThirty = new ComparatorClusterBoundary<Integer>(comparator, 30, ComparisonStrategy.GREATER_THAN);
clusterGreaterThanThirty = new ClusterWithSingleBoundary<>("Test", greaterThanThirty);
}
}
@@ -1,11 +1,11 @@
package com.sap.sse.datamining.data;
import com.sap.sse.datamining.impl.data.ComparisonStrategy;
public interface ClusterBoundary<ElementType> {
public enum ComparisonStrategy {
LOWER_THAN, LOWER_EQUALS_THAN, GREATER_THAN, GREATER_EQUALS_THAN
}
public boolean contains(ElementType value);
public ComparisonStrategy getStrategy();
}
@@ -14,5 +14,10 @@ public abstract class AbstractCluster<ElementType> implements Cluster<ElementTyp
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
@@ -18,5 +18,10 @@ public class ClusterWithBoundaries<ElementType> extends AbstractCluster<ElementT
public boolean isInRange(ElementType value) {
return lowerBound.contains(value) && upperBound.contains(value);
}
@Override
public String toString() {
return super.toString() + " " + lowerBound + " - " + upperBound;
}
}
@@ -4,6 +4,8 @@ import com.sap.sse.datamining.data.ClusterBoundary;
public class ClusterWithSingleBoundary<ElementType> extends AbstractCluster<ElementType> {
private static final char INFINITE = '\u221e';
private final ClusterBoundary<ElementType> boundary;
public ClusterWithSingleBoundary(String name, ClusterBoundary<ElementType> boundary) {
@@ -16,4 +18,19 @@ public class ClusterWithSingleBoundary<ElementType> extends AbstractCluster<Elem
return boundary.contains(value);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (boundary.getStrategy() == ComparisonStrategy.LOWER_EQUALS_THAN ||
boundary.getStrategy() == ComparisonStrategy.LOWER_THAN) {
builder.append("-" + INFINITE + " - ");
}
builder.append(boundary);
if (boundary.getStrategy() == ComparisonStrategy.GREATER_EQUALS_THAN ||
boundary.getStrategy() == ComparisonStrategy.GREATER_THAN) {
builder.append(" - " + INFINITE);
}
return super.toString() + " " + builder.toString();
}
}
@@ -19,21 +19,25 @@ public class ComparatorClusterBoundary<ElementType> implements ClusterBoundary<E
@Override
public boolean contains(ElementType value) {
return verifyComparisonResult(comparator.compare(value, boundaryValue));
return strategy.verifyComparisonResult(comparator.compare(value, boundaryValue));
}
private boolean verifyComparisonResult(int comparisonResult) {
switch (strategy) {
case GREATER_EQUALS_THAN:
return comparisonResult == 0 || comparisonResult >= 1;
case GREATER_THAN:
return comparisonResult >= 1;
case LOWER_EQUALS_THAN:
return comparisonResult == 0 || comparisonResult <= -1;
case LOWER_THAN:
return comparisonResult <= -1;
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (strategy == ComparisonStrategy.GREATER_EQUALS_THAN || strategy == ComparisonStrategy.GREATER_THAN) {
builder.append(strategy.getSignifier());
}
throw new UnsupportedOperationException("No implementation for the strategy " + strategy);
builder.append(boundaryValue);
if (strategy == ComparisonStrategy.LOWER_EQUALS_THAN || strategy == ComparisonStrategy.LOWER_THAN) {
builder.append(strategy.getSignifier());
}
return builder.toString();
}
@Override
public ComparisonStrategy getStrategy() {
return strategy;
}
}
@@ -0,0 +1,46 @@
package com.sap.sse.datamining.impl.data;
public enum ComparisonStrategy {
LOWER_THAN("[") {
@Override
public boolean verifyComparisonResult(int comparisonResult) {
return comparisonResult <= -1;
}
},
LOWER_EQUALS_THAN("]") {
@Override
public boolean verifyComparisonResult(int comparisonResult) {
return comparisonResult == 0 || comparisonResult <= -1;
}
},
GREATER_THAN("]") {
@Override
public boolean verifyComparisonResult(int comparisonResult) {
return comparisonResult >= 1;
}
},
GREATER_EQUALS_THAN("[") {
@Override
public boolean verifyComparisonResult(int comparisonResult) {
return comparisonResult == 0 || comparisonResult >= 1;
}
};
private String signifier = null;
private ComparisonStrategy(String signifier) {
this.signifier = signifier;
}
public String getSignifier() {
return signifier;
}
public boolean verifyComparisonResult(int comparisonResult) {
throw new UnsupportedOperationException("No implementation for the strategy " + this);
}
}