Search in sources :

Example 1 with Simplify

use of com.goebl.simplify.Simplify in project ma-modules-public by infiniteautomation.

the class MultiPointLatestDatabaseStream method simplify.

/**
 * Simplify according to our requirements
 *
 * TODO This currently only works for Numeric Points
 *
 * @param list
 * @return
 */
protected List<DataPointVOPointValueTimeBookend> simplify(List<DataPointVOPointValueTimeBookend> list) {
    LogStopWatch logStopWatch = new LogStopWatch();
    if (info.simplifyTolerance != null) {
        // TODO improve Simplify code to return a list
        Simplify<DataPointVOPointValueTimeBookend> simplify = new Simplify<DataPointVOPointValueTimeBookend>(new DataPointVOPointValueTimeBookend[0], SimplifyPointValueExtractor.extractor);
        DataPointVOPointValueTimeBookend[] simplified = simplify.simplify(list.toArray(new DataPointVOPointValueTimeBookend[list.size()]), info.simplifyTolerance, info.simplifyHighQuality);
        logStopWatch.stop("Finished Simplify, tolerance: " + info.simplifyTolerance);
        return Arrays.asList(simplified);
    } else {
        if (list.size() < info.simplifyTarget)
            return list;
        // Compute target bounds as 10% of target
        int lowerTarget = info.simplifyTarget - (int) (info.simplifyTarget * 0.1);
        int upperTarget = info.simplifyTarget + (int) (info.simplifyTarget * 0.1);
        // Compute tolerance bounds and initial tolerance
        Double max = Double.MIN_VALUE;
        Double min = Double.MAX_VALUE;
        for (DataPointVOPointValueTimeBookend value : list) {
            if (value.getPvt().getDoubleValue() > max)
                max = value.getPvt().getDoubleValue();
            if (value.getPvt().getDoubleValue() < min)
                min = value.getPvt().getDoubleValue();
        }
        double difference = max - min;
        double tolerance = difference / 20d;
        double topBound = difference;
        double bottomBound = 0;
        // Determine max iterations we can allow
        int maxIterations = 100;
        int iteration = 1;
        Simplify<DataPointVOPointValueTimeBookend> simplify = new Simplify<DataPointVOPointValueTimeBookend>(new DataPointVOPointValueTimeBookend[0], SimplifyPointValueExtractor.extractor);
        DataPointVOPointValueTimeBookend[] simplified = simplify.simplify(list.toArray(new DataPointVOPointValueTimeBookend[list.size()]), tolerance, info.simplifyHighQuality);
        DataPointVOPointValueTimeBookend[] best = simplified;
        while (simplified.length < lowerTarget || simplified.length > upperTarget) {
            if (simplified.length > info.simplifyTarget) {
                bottomBound = tolerance;
            } else {
                topBound = tolerance;
            }
            // Adjust tolerance
            tolerance = bottomBound + (topBound - bottomBound) / 2.0d;
            simplify = new Simplify<DataPointVOPointValueTimeBookend>(new DataPointVOPointValueTimeBookend[0], SimplifyPointValueExtractor.extractor);
            simplified = simplify.simplify(list.toArray(new DataPointVOPointValueTimeBookend[list.size()]), tolerance, info.simplifyHighQuality);
            // Keep our best effort
            if (Math.abs(info.simplifyTarget - simplified.length) < Math.abs(info.simplifyTarget - best.length))
                best = simplified;
            if (iteration > maxIterations) {
                simplified = best;
                break;
            }
            iteration++;
        }
        logStopWatch.stop("Finished Simplify, target: " + info.simplifyTarget + " actual " + simplified.length);
        return Arrays.asList(simplified);
    }
}
Also used : Simplify(com.goebl.simplify.Simplify) DataPointVOPointValueTimeBookend(com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend) LogStopWatch(com.serotonin.log.LogStopWatch)

Aggregations

Simplify (com.goebl.simplify.Simplify)1 DataPointVOPointValueTimeBookend (com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)1 LogStopWatch (com.serotonin.log.LogStopWatch)1