Implemented a Grouper, that groups by a dimension

This commit is contained in:
Lennart Hensler committed 2013-08-06 10:12:48 +02:00
1 parent 431b71ee1b
commit f1e43b1f83
8 files changed
+146 -20

No files matched your search

@@ -1,25 +1,43 @@
package com.sap.sailing.datamining;
import com.sap.sailing.datamining.shared.Dimension;
import com.sap.sailing.datamining.shared.WindStrength;
import com.sap.sailing.domain.base.BoatClass;
import com.sap.sailing.domain.base.Competitor;
import com.sap.sailing.domain.base.Nationality;
import com.sap.sailing.domain.base.RaceDefinition;
import com.sap.sailing.domain.base.Regatta;
import com.sap.sailing.domain.common.LegType;
import com.sap.sailing.domain.tracking.GPSFixMoving;
import com.sap.sailing.domain.tracking.TrackedRace;
import com.sap.sailing.domain.tracking.Wind;
public interface GPSFixWithContext extends GPSFixMoving {
public Competitor getCompetitor();
public int getLegNumber();
public RaceDefinition getRace();
public Regatta getRegatta();
public BoatClass getBoatClass();
public Nationality getNationality();
/**
* The year of the start of the race.
*/
public Integer getYear();
/**
* The leg type of the leg, which contains this gps fix. Can be <code>null</code> if there's no wind in the race.
*/
public LegType getLegType();
public int getLegNumber();
public TrackedRace getTrackedRace();
public Regatta getRegatta();
/**
* The wind at the position and the time of this gps fix. Can be <code>null</code> if there's no wind in the race.
* The wind strength at the position and the time of this gps fix. Can be <code>null</code> if there's no wind in the race.
*/
public Wind getWind();
public WindStrength getWindStrength();
/**
* A string representation of the value of the given dimension of this GPS-Fix (for example the name of
* the competitor of this fix, if the dimension is {@link Dimension#CompetitorName}). Can be <code>null</code>,
* if the value is <code>null</code> (possible for wind or leg type).
*
* @return A string representation of the value of the given dimension of this GPS-Fix.
*/
public String getStringRepresentation(Dimension dimension);
}
@@ -3,8 +3,8 @@ package com.sap.sailing.datamining;
import java.util.Collection;
import java.util.Map;
public interface Grouper<K> {
public interface Grouper {
public Map<K, Collection<GPSFixWithContext>> group(Collection<GPSFixWithContext> data);
public Map<String, Collection<GPSFixWithContext>> group(Collection<GPSFixWithContext> data);
}
@@ -41,7 +41,7 @@ public class SelectionCriteriaFactory {
switch (dimension) {
case RegattaName:
return new RegattaSelectionCriteria((Collection<String>) selection);
case BoatClass:
case BoatClassName:
return new BoatClassSelectionCriteria((Collection<String>) selection);
case CompetitorName:
return new CompetitorNameSelectionCriteria((Collection<String>) selection);
@@ -1,11 +1,19 @@
package com.sap.sailing.datamining.impl;
import java.util.Calendar;
import com.sap.sailing.datamining.GPSFixWithContext;
import com.sap.sailing.datamining.shared.Dimension;
import com.sap.sailing.datamining.shared.WindStrength;
import com.sap.sailing.domain.base.BoatClass;
import com.sap.sailing.domain.base.Competitor;
import com.sap.sailing.domain.base.Leg;
import com.sap.sailing.domain.base.Nationality;
import com.sap.sailing.domain.base.RaceDefinition;
import com.sap.sailing.domain.base.Regatta;
import com.sap.sailing.domain.common.LegType;
import com.sap.sailing.domain.common.NoWindException;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.tracking.GPSFixMoving;
import com.sap.sailing.domain.tracking.TrackedLeg;
import com.sap.sailing.domain.tracking.TrackedRace;
@@ -26,12 +34,12 @@ public class GPSFixWithContextImpl extends GPSFixMovingImpl implements GPSFixWit
this.trackedRace = trackedRace;
this.legNumber = legNumber;
this.competitor = competitor;
this.wind = getTrackedRace().getWind(getPosition(), getTimePoint());
this.wind = this.trackedRace.getWind(getPosition(), getTimePoint());
setLegType(leg);
}
private void setLegType(Leg leg) {
TrackedLeg trackedLeg = getTrackedRace().getTrackedLeg(leg);
TrackedLeg trackedLeg = trackedRace.getTrackedLeg(leg);
try {
legType = trackedLeg == null ? null : trackedLeg.getLegType(getTimePoint());
} catch (NoWindException e) {
@@ -50,13 +58,13 @@ public class GPSFixWithContextImpl extends GPSFixMovingImpl implements GPSFixWit
}
@Override
public TrackedRace getTrackedRace() {
return trackedRace;
public RaceDefinition getRace() {
return trackedRace.getRace();
}
@Override
public Regatta getRegatta() {
return getTrackedRace().getTrackedRegatta().getRegatta();
return trackedRace.getTrackedRegatta().getRegatta();
}
@Override
@@ -65,8 +73,63 @@ public class GPSFixWithContextImpl extends GPSFixMovingImpl implements GPSFixWit
}
@Override
public Wind getWind() {
return wind;
public BoatClass getBoatClass() {
return getCompetitor().getBoat().getBoatClass();
}
@Override
public Nationality getNationality() {
return getCompetitor().getTeam().getNationality();
}
@Override
public WindStrength getWindStrength() {
if (wind == null) {
return null;
}
return WindStrength.valueOf(wind.getBeaufort());
}
@Override
public Integer getYear() {
TimePoint time = trackedRace.getStartOfRace() != null ? trackedRace.getStartOfRace() : trackedRace.getStartOfTracking();
if (time == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(time.asDate());
return calendar.get(Calendar.YEAR);
}
@Override
public String getStringRepresentation(Dimension dimension) {
switch (dimension) {
case BoatClassName:
return getBoatClass().getName();
case CompetitorName:
return getCompetitor().getName();
case LegNumber:
return getLegNumber() + "";
case LegType:
LegType legType = getLegType();
return legType != null ? legType.toString() : null;
case Nationality:
return getNationality().getThreeLetterIOCAcronym();
case RaceName:
return getRace().getName();
case RegattaName:
return getRegatta().getName();
case SailID:
return getCompetitor().getBoat().getSailID();
case WindStrength:
WindStrength windStrength = getWindStrength();
return windStrength != null ? windStrength.toString() : null;
case Year:
Integer year = getYear();
return year != null ? year.toString() : null;
}
return null;
}
}
@@ -0,0 +1,35 @@
package com.sap.sailing.datamining.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.sap.sailing.datamining.GPSFixWithContext;
import com.sap.sailing.datamining.Grouper;
import com.sap.sailing.datamining.shared.Dimension;
public class GroupByDimension implements Grouper {
private Dimension dimension;
private GroupByDimension(Dimension dimension) {
this.dimension = dimension;
}
@Override
public Map<String, Collection<GPSFixWithContext>> group(Collection<GPSFixWithContext> data) {
Map<String, Collection<GPSFixWithContext>> groupedData = new HashMap<String, Collection<GPSFixWithContext>>();
for (GPSFixWithContext gpsFix : data) {
String dimensionValueAsString = gpsFix.getStringRepresentation(dimension);
if (dimensionValueAsString != null) {
if (!groupedData.containsKey(dimensionValueAsString)) {
groupedData.put(dimensionValueAsString, new ArrayList<GPSFixWithContext>());
}
groupedData.get(dimensionValueAsString).add(gpsFix);
}
}
return groupedData;
}
}
@@ -3,7 +3,7 @@ package com.sap.sailing.datamining.shared;
public enum Dimension {
RegattaName, RaceName, LegNumber,
BoatClass,
BoatClassName,
Year,
LegType,
CompetitorName,
@@ -13,7 +13,17 @@ public enum WindStrength {
}
public boolean isInRange(double windStrengthInBeaufort) {
return lowerRange <= windStrengthInBeaufort && upperRange >= windStrengthInBeaufort;
return lowerRange <= windStrengthInBeaufort && upperRange > windStrengthInBeaufort;
}
public static WindStrength valueOf(double windStrengthInBeaufort) {
for (WindStrength windStrength : WindStrength.values()) {
if (windStrength.isInRange(windStrengthInBeaufort)) {
return windStrength;
}
}
//Since a speed less than 0 shouldn't be possible, the speed has to be >= 12 and is very strong.
return VeryStrong;
}
}