Repository navigation
StayGaps
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file StayGaps.java: the production code stays clean and the explanations live in the wiki.
package com.velitask.plugin.official.processing.gaps;
import static com.velitask.plugin.official.processing.FormulaParameters.decimal;
import com.velitask.sdk.data.SensorAtom;
import com.velitask.sdk.properties.DoubleSpinnerProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.scenario.GapInput;
import com.velitask.sdk.scenario.GapsStep;
import com.velitask.sdk.scenario.IStepControl;
import com.velitask.sdk.scenario.StepException;
/* Base of the pause formulas by stay: points stay within the given radius for longer than the given time. */
public abstract class StayGaps<A extends SensorAtom> extends GapsStep<A> {
/* Names of the formula parameters: they are also the keys of the values in scenarios. */
public static final String RADIUS_METERS = "radiusMeters";
public static final String MIN_STAY_SEC = "minStaySec";
/* Prefix of the localization keys of the pause formula parameters: Plugin.ImportStep.gaps. */
private static final String KEY = _KEY + ".gaps";
/* Cancellation is checked when the index of the anchor point is a multiple of 4096: the search on a long recording is slow. */
private static final int CANCEL_CHECK_MASK = 0xFFF;
private DoubleSpinnerProperty mRadiusMeters;
private DoubleSpinnerProperty mMinStaySec;
/* The subclass sets the step name, the logic version and the atom class. */
protected StayGaps(String name, int version, Class<A> atomClass) {
super(name, version, atomClass);
}
/* The distance between the positions of two atoms, in metres; computed by the subclass. */
protected abstract double distanceMeters(A from, A to);
@Override
/* The radius and the minimal stay time; both are 0 by default. */
protected IProperty[] defineFormulaParameters() {
mRadiusMeters = decimal(RADIUS_METERS, localized(KEY + "." + RADIUS_METERS + ".title"),
localizedOrEmpty(KEY + "." + RADIUS_METERS + ".description"), 0);
mMinStaySec = decimal(MIN_STAY_SEC, localized(KEY + "." + MIN_STAY_SEC + ".title"),
localizedOrEmpty(KEY + "." + MIN_STAY_SEC + ".description"), 0);
return new IProperty[]{mRadiusMeters, mMinStaySec};
}
@Override
/* Throws StepException if the run is cancelled. */
protected boolean[] detect(GapInput<A> input, IStepControl control) throws StepException {
A[] atoms = input.atoms();
long[] times = input.times();
boolean[] pauseAfter = new boolean[atoms.length];
double radius = mRadiusMeters.get();
long minStayMs = (long) (mMinStaySec.get() * 1000);
/* anchor - the first point of the stay being checked: distances are counted from it, not from the previous point. */
int anchor = 0;
while (anchor < atoms.length) {
if ((anchor & CANCEL_CHECK_MASK) == 0) {
/* Cancellation is checked once per 4096 anchor points. */
control.throwIfCancelled();
}
/* end goes forward while the points stay within the radius of the anchor. */
int end = anchor + 1;
while (end < atoms.length && distanceMeters(atoms[anchor], atoms[end]) <= radius) {
end++;
}
/* A stay is two or more points, longer than the minimal time (strictly). */
if (end - 1 > anchor && times[end - 1] - times[anchor] > minStayMs) {
for (int i = anchor; i < end - 1; i++) {
/* A pause after every point of the stay except the last; the search continues from the first point beyond the radius. */
pauseAfter[i] = true;
}
anchor = end;
} else {
/* There is no stay: move the anchor to the next point. */
anchor++;
}
}
return pauseAfter;
}
}StayGaps<A> is the abstract base of the formulas that look for pauses by stay: a set of consecutive points stays within the given radius for longer than the given time. How to measure the distance between two atoms is defined by the subclass through distanceMeters(from, to). At the moment there is one subclass, GeoStayGaps ("Pauses at a stop: GPS").
Data. The formula takes the atoms and their times (GapInput.atoms(), times()) and calls distanceMeters(...) for pairs of atoms.
Parameters. Two formula parameters, both with the default 0:
| Parameter | Key | Meaning |
|---|---|---|
| "Stay radius, m" | radiusMeters |
Points within this distance from the first point of the stay count as standing still. 0 - only points with identical coordinates count as a stop. |
| "Minimal stay time, s" | minStaySec |
A shorter stay is not a pause. 0 - any stop becomes a pause. |
They are complemented by the common parameter of the base class "Stood if moved less than, m" (stoodMeters, default 30): it does not affect the search for pauses and marks an already found pause as a stop by the displacement of the track.
Algorithm.
- The anchor point
anchoris the first point of the stay being checked. - The pointer
endgoes forward while the distance from the anchor point to the next point is not greater than the radius (non-strict). - If at least one more point was found in the radius and the time from the anchor point to the last point in the radius is strictly greater than
minStaySec, a pause is set after every point of the stay except the last, and the search continues from the first point beyond the radius. - Otherwise the anchor point moves one forward.
- The distance is counted from the anchor (first) point of the stay, not from the previous one: a slow drift gradually takes points out of the radius, and the stay breaks off when the accumulated drift exceeds the radius.
- For every anchor point without a stay, the tail of points within the radius is scanned, so on very long recordings the search is noticeably more expensive than for the single-pass formulas; the cancellation of the run is checked when the index of the anchor point is a multiple of 4096.
- The stay time is the difference of the start times of the first and the last points of the stay; the duration of the last atom is not included.
- A point that went beyond the radius and became the new anchor does not belong to the previous stay.
- The class is abstract and is not registered in
BuiltinScenariositself.