Implemented the cluster group repositories

This commit is contained in:
Lennart Hensler committed 2014-03-15 16:39:12 +01:00
1 parent a6dbf58d7e
commit be0ad56062
15 files changed
+276 -31

No files matched your search

@@ -6,4 +6,6 @@ public interface Cluster<ElementType> {
public boolean isInRange(ElementType value);
public Class<ElementType> getClusterElementsType();
}
@@ -8,4 +8,6 @@ public interface ClusterBoundary<ElementType> {
public ComparisonStrategy getStrategy();
public Class<ElementType> getClusterElementsType();
}
@@ -2,6 +2,10 @@ package com.sap.sse.datamining.data;
public interface ClusterGroup<ElementType> {
public String getName();
public Cluster<ElementType> getClusterFor(ElementType value);
public Class<ElementType> getClusterElementsType();
}
@@ -0,0 +1,11 @@
package com.sap.sse.datamining.data;
import java.util.Collection;
public interface ClusterGroupRepository {
public void add(ClusterGroup<?> clusterGroup);
public <T> Collection<ClusterGroup<T>> getClusterGroupsFor(Class<T> type);
}
@@ -1,13 +1,34 @@
package com.sap.sse.datamining.impl.data;
import java.util.ArrayList;
import java.util.Collection;
import com.sap.sse.datamining.data.Cluster;
import com.sap.sse.datamining.data.ClusterBoundary;
public abstract class AbstractCluster<ElementType> implements Cluster<ElementType> {
protected final String name;
private Collection<ClusterBoundary<ElementType>> boundaries;
public AbstractCluster(String name) {
public AbstractCluster(String name, Collection<ClusterBoundary<ElementType>> boundaries) {
this.name = name;
this.boundaries = new ArrayList<>(boundaries);
}
@Override
public boolean isInRange(ElementType value) {
for (ClusterBoundary<ElementType> boundary : boundaries) {
if (!boundary.contains(value)) {
return false;
}
}
return true;
}
@Override
public Class<ElementType> getClusterElementsType() {
return boundaries.iterator().next().getClusterElementsType();
}
@Override
@@ -17,7 +38,11 @@ public abstract class AbstractCluster<ElementType> implements Cluster<ElementTyp
@Override
public String toString() {
return getName();
return getName() + boundaries;
}
public Collection<ClusterBoundary<ElementType>> getClusterBoundaries() {
return boundaries;
}
}
@@ -1,27 +1,23 @@
package com.sap.sse.datamining.impl.data;
import java.util.Arrays;
import java.util.Iterator;
import com.sap.sse.datamining.data.ClusterBoundary;
public class ClusterWithBoundaries<ElementType> extends AbstractCluster<ElementType> {
private final ClusterBoundary<ElementType> lowerBound;
private final ClusterBoundary<ElementType> upperBound;
public ClusterWithBoundaries(String name, ClusterBoundary<ElementType> lowerBound,
ClusterBoundary<ElementType> upperBound) {
super(name);
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
@Override
public boolean isInRange(ElementType value) {
return lowerBound.contains(value) && upperBound.contains(value);
super(name, Arrays.asList(lowerBound, upperBound));
}
@Override
public String toString() {
return super.toString() + " " + lowerBound + " - " + upperBound;
Iterator<ClusterBoundary<ElementType>> boundariesIterator = super.getClusterBoundaries().iterator();
ClusterBoundary<ElementType> lowerBound = boundariesIterator.next();
ClusterBoundary<ElementType> upperBound = boundariesIterator.next();
return super.getName() + " " + lowerBound + " - " + upperBound;
}
}
@@ -1,25 +1,21 @@
package com.sap.sse.datamining.impl.data;
import java.util.Arrays;
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) {
super(name);
this.boundary = boundary;
}
@Override
public boolean isInRange(ElementType value) {
return boundary.contains(value);
super(name, Arrays.asList(boundary));
}
@Override
public String toString() {
ClusterBoundary<ElementType> boundary = super.getClusterBoundaries().iterator().next();
StringBuilder builder = new StringBuilder();
if (boundary.getStrategy() == ComparisonStrategy.LOWER_EQUALS_THAN ||
boundary.getStrategy() == ComparisonStrategy.LOWER_THAN) {
@@ -30,7 +26,7 @@ public class ClusterWithSingleBoundary<ElementType> extends AbstractCluster<Elem
boundary.getStrategy() == ComparisonStrategy.GREATER_THAN) {
builder.append(" - " + INFINITE);
}
return super.toString() + " " + builder.toString();
return super.getName() + " " + builder.toString();
}
}
@@ -0,0 +1,10 @@
package com.sap.sse.datamining.impl.data;
public class ComparableClusterBoundary<ElementType extends Comparable<ElementType>> extends ComparatorClusterBoundary<ElementType> {
public ComparableClusterBoundary(ElementType boundaryValue,
ComparisonStrategy strategy) {
super(new ComparableComparator<ElementType>(), boundaryValue, strategy);
}
}
@@ -39,5 +39,11 @@ public class ComparatorClusterBoundary<ElementType> implements ClusterBoundary<E
public ComparisonStrategy getStrategy() {
return strategy;
}
@SuppressWarnings("unchecked") // Necessary because you can't use instanceof with generics
@Override
public Class<ElementType> getClusterElementsType() {
return (Class<ElementType>) boundaryValue.getClass();
}
}
@@ -0,0 +1,35 @@
package com.sap.sse.datamining.impl.data;
import java.util.Collection;
import java.util.HashSet;
import com.sap.sse.datamining.data.ClusterGroup;
import com.sap.sse.datamining.data.ClusterGroupRepository;
public class CompoundClusterGroupRepository implements ClusterGroupRepository {
private Collection<ClusterGroupRepository> repositories;
private SimpleClusterGroupRepository internalRepository;
public CompoundClusterGroupRepository(Collection<ClusterGroupRepository> repositories) {
this.repositories = new HashSet<>(repositories);
this.internalRepository = new SimpleClusterGroupRepository();
this.repositories.add(internalRepository);
}
@Override
public void add(ClusterGroup<?> clusterGroup) {
internalRepository.add(clusterGroup);
}
@Override
public <T> Collection<ClusterGroup<T>> getClusterGroupsFor(Class<T> type) {
Collection<ClusterGroup<T>> allClusterGroups = new HashSet<>();
for (ClusterGroupRepository clusterGroupRepository : repositories) {
allClusterGroups.addAll(clusterGroupRepository.getClusterGroupsFor(type));
}
return allClusterGroups;
}
}
@@ -8,9 +8,11 @@ import com.sap.sse.datamining.data.ClusterGroup;
public class FixClusterGroup<ElementType> implements ClusterGroup<ElementType> {
private final Iterable<Cluster<ElementType>> clusters;
private final String name;
private final Collection<Cluster<ElementType>> clusters;
public FixClusterGroup(Collection<Cluster<ElementType>> clusters) {
public FixClusterGroup(String name, Collection<Cluster<ElementType>> clusters) {
this.name = name;
this.clusters = new HashSet<>(clusters);
}
@@ -23,5 +25,20 @@ public class FixClusterGroup<ElementType> implements ClusterGroup<ElementType> {
}
return null;
}
@Override
public Class<ElementType> getClusterElementsType() {
return clusters.iterator().next().getClusterElementsType();
}
@Override
public String getName() {
return name;
}
@Override
public String toString() {
return getName() + clusters;
}
}
@@ -0,0 +1,47 @@
package com.sap.sse.datamining.impl.data;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sap.sse.datamining.data.ClusterGroup;
import com.sap.sse.datamining.data.ClusterGroupRepository;
public class SimpleClusterGroupRepository implements ClusterGroupRepository {
private static final Logger LOGGER = Logger.getLogger(SimpleClusterGroupRepository.class.getSimpleName());
private HashMap<Class<?>, Collection<ClusterGroup<?>>> clusterGroupsMappedByType;
public SimpleClusterGroupRepository() {
clusterGroupsMappedByType = new HashMap<>();
}
@Override
public void add(ClusterGroup<?> clusterGroup) {
Class<?> clusterElementsType = clusterGroup.getClusterElementsType();
if (!clusterGroupsMappedByType.containsKey(clusterElementsType)) {
clusterGroupsMappedByType.put(clusterElementsType, new HashSet<ClusterGroup<?>>());
}
clusterGroupsMappedByType.get(clusterElementsType).add(clusterGroup);
}
@SuppressWarnings("unchecked") // Necessary because you can't use instanceof with generics
@Override
public <T> Collection<ClusterGroup<T>> getClusterGroupsFor(Class<T> type) {
Collection<ClusterGroup<T>> specificClusterGroups = new HashSet<>();
if (clusterGroupsMappedByType.containsKey(type)) {
for (ClusterGroup<?> clusterGroup : clusterGroupsMappedByType.get(type)) {
try {
specificClusterGroups.add((ClusterGroup<T>) clusterGroup);
} catch (ClassCastException e) {
LOGGER.log(Level.FINER, "Error casting the stored cluster groups:", e);
}
}
}
return specificClusterGroups;
}
}