Skip to content

Commit

Permalink
tests with BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
Panagiotis Liakos committed Jul 29, 2022
1 parent 9f4d7e4 commit 0033db0
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ private void next() {
SwingSegment swingSegment = swingSegments.get(currentElement);
if (swingSegment.getFinalTimestamp() >= (swingSegment.getInitialTimestamp() + currentTimestampOffset)) {
storedVal = (float) swingSegment.getLine().get(swingSegment.getInitialTimestamp() + currentTimestampOffset);
// System.out.println("LineDec: " + swingSegment.getLine() + "\t" + (swingSegment.getInitialTimestamp() + currentTimestampOffset) + "\t" + storedVal);
currentTimestampOffset++;
} else {
currentElement++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package gr.aueb.delorean.chimp;

import java.math.BigDecimal;
import java.util.List;

public class DecompressorSwingFilterBigDecimal {

private List<SwingSegmentBigDecimal> swingSegments;
private BigDecimal storedVal = new BigDecimal(0);
private boolean endOfStream = false;
private int currentElement = 0;
private int currentTimestampOffset = 0;

public DecompressorSwingFilterBigDecimal(List<SwingSegmentBigDecimal> constants) {
this.swingSegments = constants;
}

/**
* Returns the next pair in the time series, if available.
*
* @return Pair if there's next value, null if series is done.
*/
public BigDecimal readValue() {
next();
if(endOfStream) {
return null;
}
return storedVal;
}

private void next() {
SwingSegmentBigDecimal swingSegment = swingSegments.get(currentElement);
if (swingSegment.getFinalTimestamp() >= (swingSegment.getInitialTimestamp() + currentTimestampOffset)) {
storedVal = swingSegment.getLine().get(swingSegment.getInitialTimestamp() + currentTimestampOffset);
// System.out.println("LineDec: " + swingSegment.getLine() + "\t" + (swingSegment.getInitialTimestamp() + currentTimestampOffset) + "\t" + storedVal);
currentTimestampOffset++;
} else {
currentElement++;
if (currentElement < swingSegments.size()) {
swingSegment = swingSegments.get(currentElement);
storedVal = swingSegment.getLine().get(swingSegment.getInitialTimestamp());

currentTimestampOffset = 1;
} else {
endOfStream = true;
}
}
}

}
26 changes: 24 additions & 2 deletions src/main/java/gr/aueb/delorean/chimp/LinearFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package gr.aueb.delorean.chimp;

import java.util.Objects;

public class LinearFunction {

/** Constructors **/
Expand All @@ -23,8 +25,8 @@ public LinearFunction(long ts, double vs, long te, double ve) {
}

/** Public Methods **/
public float get(long ts) {
return (float) (this.a * ts + this.b);
public double get(long ts) {
return (this.a * ts + this.b);
}

/** Instance Variables **/
Expand All @@ -34,4 +36,24 @@ public float get(long ts) {
public String toString() {
return String.format("%.15fx+%f", a, b);
}

@Override
public int hashCode() {
return Objects.hash(a, b);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
LinearFunction other = (LinearFunction) obj;
return Double.doubleToLongBits(a) == Double.doubleToLongBits(other.a)
&& Double.doubleToLongBits(b) == Double.doubleToLongBits(other.b);
}


}
45 changes: 45 additions & 0 deletions src/main/java/gr/aueb/delorean/chimp/LinearFunctionBigDecimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright 2018 The ModelarDB Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gr.aueb.delorean.chimp;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class LinearFunctionBigDecimal {

MathContext mc = new MathContext(30, RoundingMode.HALF_UP) ;

/** Constructors **/
public LinearFunctionBigDecimal(long ts, BigDecimal vs, long te, BigDecimal ve) {
this.a = ve.subtract(vs).divide(new BigDecimal(te - ts), mc);
this.b = vs.subtract(a.multiply(new BigDecimal(ts)));
}

/** Public Methods **/
public BigDecimal get(long ts) {
return (this.a.multiply(new BigDecimal(ts)).add(b));
}

private BigDecimal a;
/** Instance Variables **/
private BigDecimal b;

@Override
public String toString() {
return String.format("%.15fx+%f", a, b);
}

}
27 changes: 27 additions & 0 deletions src/main/java/gr/aueb/delorean/chimp/PointBigDecimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gr.aueb.delorean.chimp;

import java.math.BigDecimal;

public class PointBigDecimal {

private final long timestamp;
private BigDecimal value;

public PointBigDecimal(long timestamp, BigDecimal value) {
this.timestamp = timestamp;
this.value = value;
}

public long getTimestamp() {
return timestamp;
}

public BigDecimal getValue() {
return value;
}

public void setValue(BigDecimal value) {
this.value = value;
}

}
11 changes: 3 additions & 8 deletions src/main/java/gr/aueb/delorean/chimp/SwingFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public List<SwingSegment> filter(Collection<Point> points, float epsilon) {
while (true) {
if (!iterator.hasNext()) {
if (uiOld != null && liOld != null) {
// System.out.println("need to start new line");
LinearFunction line = new LinearFunction(first.getTimestamp(), first.getValue(),
current.getTimestamp(),
(uiOld.get(current.getTimestamp()) + liOld.get(current.getTimestamp())) / 2);
Expand All @@ -39,24 +38,20 @@ public List<SwingSegment> filter(Collection<Point> points, float epsilon) {
}
previous = current;
current = iterator.next();
// System.out.println("Points: " + first.getValue() + "\t" + previous.getValue() + "\t" + current.getValue() + "\t" + uiOld + "\t" + liOld);
if (uiOld.get(current.getTimestamp()) < current.getValue() - epsilon
|| liOld.get(current.getTimestamp()) > current.getValue() + epsilon) {
// System.out.println("need to start new line: " + first.getValue() + "\t" + (uiOld.get(previous.getTimestamp()) + liOld.get(previous.getTimestamp())) / 2 + "\t" + line + "\t" + previous.getTimestamp());
swingSegments.add(new SwingSegment(first.getTimestamp(), previous.getTimestamp() - 1, first.getValue(), (uiOld.get(previous.getTimestamp()) + liOld.get(previous.getTimestamp())) / 2));
// swingSegments.add(new SwingSegment(first.getTimestamp(), previous.getTimestamp() - 1, first.getValue(), (uiOld.get(previous.getTimestamp()) + liOld.get(previous.getTimestamp())) / 2));
previous = first = new Point(previous.getTimestamp(), (uiOld.get(previous.getTimestamp()) + liOld.get(previous.getTimestamp())) / 2);
Point newPoint = new Point(previous.getTimestamp(), (float) (uiOld.get(previous.getTimestamp()) + liOld.get(previous.getTimestamp())) / 2);
swingSegments.add(new SwingSegment(first.getTimestamp(), previous.getTimestamp() - 1, first.getValue(), newPoint.getValue()));
previous = first = newPoint;
uiOld = new LinearFunction(previous.getTimestamp(), previous.getValue(), current.getTimestamp(),
current.getValue() + epsilon);
liOld = new LinearFunction(previous.getTimestamp(), previous.getValue(), current.getTimestamp(),
current.getValue() - epsilon);
// System.out.println("New range: " + current.getValue() + "\t" + current.getTimestamp() + "\t" + uiOld + "\t" + liOld);
} else {
LinearFunction uiNew = new LinearFunction(first.getTimestamp(), first.getValue(),
current.getTimestamp(), current.getValue() + epsilon);
LinearFunction liNew = new LinearFunction(first.getTimestamp(), first.getValue(),
current.getTimestamp(), current.getValue() - epsilon);
// System.out.println("Cand: " + current.getValue() + "\t" + current.getTimestamp() + "\t" + uiNew + "\t" + liNew);
if (uiOld == null || uiOld.get(current.getTimestamp()) > uiNew.get(current.getTimestamp())) {
uiOld = uiNew;
}
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/gr/aueb/delorean/chimp/SwingFilterBigDecimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package gr.aueb.delorean.chimp;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class SwingFilterBigDecimal {

MathContext mc = new MathContext(30, RoundingMode.HALF_UP) ;

public List<SwingSegmentBigDecimal> filter(Collection<PointBigDecimal> points, BigDecimal epsilon) {

List<SwingSegmentBigDecimal> swingSegments = new ArrayList<>();

PointBigDecimal first = null;
LinearFunctionBigDecimal uiOld = null;
LinearFunctionBigDecimal liOld = null;

Iterator<PointBigDecimal> iterator = points.iterator();

PointBigDecimal previous = first = iterator.next();
PointBigDecimal current = iterator.next();
uiOld = new LinearFunctionBigDecimal(previous.getTimestamp(), previous.getValue(), current.getTimestamp(),
current.getValue().add(epsilon));
liOld = new LinearFunctionBigDecimal(previous.getTimestamp(), previous.getValue(), current.getTimestamp(),
current.getValue().subtract(epsilon));

while (true) {
if (!iterator.hasNext()) {
if (uiOld != null && liOld != null) {
// System.out.println("need to start new line");
LinearFunctionBigDecimal line = new LinearFunctionBigDecimal(first.getTimestamp(), first.getValue(),
current.getTimestamp(),
(uiOld.get(current.getTimestamp()).add(liOld.get(current.getTimestamp()))).divide(new BigDecimal(2), mc));
swingSegments.add(new SwingSegmentBigDecimal(first.getTimestamp(), current.getTimestamp(), line));
} else {
swingSegments.add(new SwingSegmentBigDecimal(first.getTimestamp(), first.getTimestamp(), first.getValue(), first.getValue()));
}
return swingSegments;
}
previous = current;
current = iterator.next();
// System.out.println("Points: " + first.getValue() + "\t" + previous.getValue() + "\t" + current.getValue() + "\t" + uiOld + "\t" + liOld);
if (uiOld.get(current.getTimestamp()).compareTo(current.getValue().subtract(epsilon)) < 0
|| liOld.get(current.getTimestamp()).compareTo(current.getValue().add(epsilon)) > 0) {
PointBigDecimal newPoint = new PointBigDecimal(previous.getTimestamp(), (uiOld.get(previous.getTimestamp()).add(liOld.get(previous.getTimestamp()))).divide(new BigDecimal(2), mc));
System.out.println("need to start new line: " + first.getValue() + "\t" + newPoint.getValue() + "\t" + previous.getTimestamp());
swingSegments.add(new SwingSegmentBigDecimal(first.getTimestamp(), previous.getTimestamp() - 1, first.getValue(), newPoint.getValue()));
// swingSegments.add(new SwingSegment(first.getTimestamp(), previous.getTimestamp() - 1, first.getValue(), (uiOld.get(previous.getTimestamp()) + liOld.get(previous.getTimestamp())) / 2));
previous = first = newPoint;
uiOld = new LinearFunctionBigDecimal(previous.getTimestamp(), previous.getValue(), current.getTimestamp(),
current.getValue().add(epsilon));
liOld = new LinearFunctionBigDecimal(previous.getTimestamp(), previous.getValue(), current.getTimestamp(),
current.getValue().subtract(epsilon));
// System.out.println("New range: " + current.getValue() + "\t" + current.getTimestamp() + "\t" + uiOld + "\t" + liOld);
} else {
LinearFunctionBigDecimal uiNew = new LinearFunctionBigDecimal(first.getTimestamp(), first.getValue(),
current.getTimestamp(), current.getValue().add(epsilon));
LinearFunctionBigDecimal liNew = new LinearFunctionBigDecimal(first.getTimestamp(), first.getValue(),
current.getTimestamp(), current.getValue().subtract(epsilon));
// System.out.println("Cand: " + current.getValue() + "\t" + current.getTimestamp() + "\t" + uiNew + "\t" + liNew);
if (uiOld == null || uiOld.get(current.getTimestamp()).compareTo(uiNew.get(current.getTimestamp())) > 0) {
uiOld = uiNew;
}
if (liOld == null || liOld.get(current.getTimestamp()).compareTo(liNew.get(current.getTimestamp())) < 0) {
liOld = liNew;
}
}
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/gr/aueb/delorean/chimp/SwingSegmentBigDecimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gr.aueb.delorean.chimp;

import java.math.BigDecimal;

public class SwingSegmentBigDecimal {

private long initialTimestamp;
private long finalTimestamp;
private LinearFunctionBigDecimal line;

public SwingSegmentBigDecimal(long initialTimestamp, long finalTimestamp, LinearFunctionBigDecimal line) {
this.initialTimestamp = initialTimestamp;
this.finalTimestamp = finalTimestamp;
this.line = line;
}

public SwingSegmentBigDecimal(long initialTimestamp, long finalTimestamp, BigDecimal first, BigDecimal last) {
this.initialTimestamp = initialTimestamp;
this.finalTimestamp = finalTimestamp;
this.line = new LinearFunctionBigDecimal(initialTimestamp, first, finalTimestamp + 1, last);
}

public long getFinalTimestamp() {
return finalTimestamp;
}

public long getInitialTimestamp() {
return initialTimestamp;
}

public LinearFunctionBigDecimal getLine() {
return line;
}

@Override
public String toString() {
return String.format("%d-%d: %f", getInitialTimestamp(), getFinalTimestamp(), getLine());
}

}
Loading

0 comments on commit 0033db0

Please sign in to comment.