Skip to content

ThresholdGaps

roosit edited this page Nov 15, 2026 · 1 revision

ThresholdGaps

Up | Previous | Next | Русский

Full source with short explanations

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 ThresholdGaps.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 static com.velitask.plugin.official.processing.FormulaParameters.signed;

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;

/* Base of the pause formulas by stop and movement thresholds; the sensor value is defined by the subclass. */
public abstract class ThresholdGaps<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 STOP_BELOW = "stopBelow";
    public static final String RESUME_ABOVE = "resumeAbove";
    public static final String DELAY_SEC = "delaySec";

    /* Prefix of the localization keys of the pause formula parameters: Plugin.ImportStep.gaps. */
    private static final String KEY = _KEY + ".gaps";

    private DoubleSpinnerProperty mStopBelow;
    private DoubleSpinnerProperty mResumeAbove;
    private DoubleSpinnerProperty mDelaySec;

    /* The subclass sets the step name, the logic version and the atom class. */
    protected ThresholdGaps(String name, int version, Class<A> atomClass) {
        super(name, version, atomClass);
    }

    /* The atom value in the units of the thresholds; the thresholds in the settings are entered in the same units. */
    protected abstract double value(A atom);

    @Override
    /* The thresholds may be negative: range -1e9...1e9; by default all three parameters are 0. */
    protected IProperty[] defineFormulaParameters() {
        mStopBelow = signed(STOP_BELOW, localized(KEY + "." + STOP_BELOW + ".title"),
                localizedOrEmpty(KEY + "." + STOP_BELOW + ".description"), 0);
        mResumeAbove = signed(RESUME_ABOVE, localized(KEY + "." + RESUME_ABOVE + ".title"),
                localizedOrEmpty(KEY + "." + RESUME_ABOVE + ".description"), 0);
        mDelaySec = decimal(DELAY_SEC, localized(KEY + "." + DELAY_SEC + ".title"),
                localizedOrEmpty(KEY + "." + DELAY_SEC + ".description"), 0);
        return new IProperty[]{mStopBelow, mResumeAbove, mDelaySec};
    }

    @Override
    protected boolean[] detect(GapInput<A> input, IStepControl control) {
        A[] atoms = input.atoms();
        long[] times = input.times();
        boolean[] pauseAfter = new boolean[atoms.length];
        double stopBelow = mStopBelow.get();
        double resumeAbove = mResumeAbove.get();
        long delayMs = (long) (mDelaySec.get() * 1000);
        /* stopFrom - the atom where the stop began; -1 while there is no stop. */
        int stopFrom = -1;
        for (int i = 0; i < atoms.length; i++) {
            double value = value(atoms[i]);
            if (stopFrom < 0) {
                /* A stop begins when the value became lower than the stop threshold. */
                if (value < stopBelow) {
                    stopFrom = i;
                }
            /* A stop ends when the value became higher than the moving threshold. */
            } else if (value > resumeAbove) {
                mark(pauseAfter, times, stopFrom, i, delayMs);
                stopFrom = -1;
            }
        }
        /* A stop that did not end before the end of the recording is closed by the last atom. */
        if (stopFrom >= 0) {
            mark(pauseAfter, times, stopFrom, atoms.length - 1, delayMs);
        }
        return pauseAfter;
    }

    /* A stop not longer than the minimal one is not a pause; otherwise a pause after every atom of the stop. */
    private void mark(boolean[] pauseAfter, long[] times, int first, int last, long delayMs) {
        if (times[last] - times[first] <= delayMs) {
            return;
        }
        for (int i = first; i < last; i++) {
            pauseAfter[i] = true;
        }
    }
}

Purpose

ThresholdGaps<A> is the abstract base of the formulas that look for pauses by the sensor value with two thresholds: below the stop threshold the sensor has "stopped", above the moving threshold it has "started again". What the value measures is decided by the subclass through value(A atom). At the moment there is one subclass, SpeedThresholdGaps ("Pauses by stop: speed").

How it works

Data. value(atom) is called for every atom; the times are taken from GapInput.times().

Parameters. Three formula parameters, all with the default 0:

Parameter Key Meaning
"Stop threshold" stopBelow A stop begins when the value drops below the threshold.
"Moving threshold" resumeAbove A stop ends when the value rises above the threshold.
"Minimal stop, s" delaySec A shorter stop is not a pause.

The thresholds are signed numbers (range from -1·10⁹ to 1·10⁹), the time is fractional seconds. The units of the thresholds are the units of the subclass value. 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 and marks a found pause as a stop.

Algorithm is a state machine with two states: "moving" and "stop from atom stopFrom".

  1. While there is no stop, the first atom with a value strictly below stopBelow begins it.
  2. During a stop, the first atom with a value strictly above resumeAbove ends it.
  3. If the time from the start of the stop to this atom is strictly greater than delaySec, a pause is set after every atom of the stop, including the last one before the resuming atom.
  4. A stop that did not end before the end of the recording is closed by the last atom (with the same time check).

Features and limitations

  • Values between stopBelow and resumeAbove do not change the state. If resumeAbove is less than stopBelow, a stop can end right after it began. The parameter description advises setting the moving threshold not lower than the stop threshold, so that fluctuations around the threshold do not split the pause.
  • With the default 0 for both thresholds the formula finds no stops for non-negative values (speed): the value is never below zero.
  • The end point of the recording also takes part in the iteration as an atom.
  • The class is abstract and is not registered in BuiltinScenarios itself.

Clone this wiki locally