Constraint -> kt
This commit is contained in:
parent
2ea423c704
commit
b9e50885a8
8 changed files with 137 additions and 135 deletions
|
@ -42,6 +42,8 @@ import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
|
import org.mockito.Mockito.anyInt
|
||||||
|
import org.mockito.Mockito.anyString
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest
|
import org.powermock.core.classloader.annotations.PrepareForTest
|
||||||
import org.powermock.modules.junit4.PowerMockRunner
|
import org.powermock.modules.junit4.PowerMockRunner
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -119,6 +121,15 @@ class ConstraintsCheckerTest : TestBaseWithProfile() {
|
||||||
`when`(resourceHelper.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s")
|
`when`(resourceHelper.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s")
|
||||||
`when`(resourceHelper.gs(R.string.limitingbasalratio)).thenReturn("Limiting max basal rate to %1\$.2f U/h because of %2\$s")
|
`when`(resourceHelper.gs(R.string.limitingbasalratio)).thenReturn("Limiting max basal rate to %1\$.2f U/h because of %2\$s")
|
||||||
`when`(resourceHelper.gs(R.string.limitingpercentrate)).thenReturn("Limiting max percent rate to %1\$d%% because of %2\$s")
|
`when`(resourceHelper.gs(R.string.limitingpercentrate)).thenReturn("Limiting max percent rate to %1\$d%% because of %2\$s")
|
||||||
|
`when`(resourceHelper.gs(R.string.itmustbepositivevalue)).thenReturn("it must be positive value")
|
||||||
|
`when`(resourceHelper.gs(R.string.smbnotallowedinopenloopmode)).thenReturn("SMB not allowed in open loop mode")
|
||||||
|
`when`(resourceHelper.gs(R.string.pumplimit)).thenReturn("pump limit")
|
||||||
|
`when`(resourceHelper.gs(R.string.smbalwaysdisabled)).thenReturn("SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering")
|
||||||
|
`when`(resourceHelper.gs(R.string.limitingpercentrate, 0, "it must be positive value")).thenReturn("")
|
||||||
|
`when`(resourceHelper.gs(R.string.limitingbolus, 3.0, "pump limit")).thenReturn("")
|
||||||
|
`when`(resourceHelper.gs(R.string.limitingbolus, 6.0, "pump limit")).thenReturn("")
|
||||||
|
`when`(resourceHelper.gs(R.string.limitingbasalratio, 0.8, "pump limit")).thenReturn("")
|
||||||
|
`when`(resourceHelper.gs(R.string.limitingpercentrate, 200, "pump limit")).thenReturn("")
|
||||||
|
|
||||||
// RS constructor
|
// RS constructor
|
||||||
`when`(sp.getString(R.string.key_danars_address, "")).thenReturn("")
|
`when`(sp.getString(R.string.key_danars_address, "")).thenReturn("")
|
||||||
|
|
|
@ -49,7 +49,7 @@ class SafetyPluginTest : TestBaseWithProfile() {
|
||||||
private lateinit var safetyPlugin: SafetyPlugin
|
private lateinit var safetyPlugin: SafetyPlugin
|
||||||
|
|
||||||
val injector = HasAndroidInjector { AndroidInjector { } }
|
val injector = HasAndroidInjector { AndroidInjector { } }
|
||||||
val pumpDescription = PumpDescription()
|
private val pumpDescription = PumpDescription()
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun prepare() {
|
fun prepare() {
|
||||||
|
@ -198,7 +198,7 @@ class SafetyPluginTest : TestBaseWithProfile() {
|
||||||
`when`(sp.getString(R.string.key_age, "")).thenReturn("child")
|
`when`(sp.getString(R.string.key_age, "")).thenReturn("child")
|
||||||
var d = Constraint(Constants.REALLYHIGHBOLUS)
|
var d = Constraint(Constants.REALLYHIGHBOLUS)
|
||||||
d = safetyPlugin.applyBolusConstraints(d)
|
d = safetyPlugin.applyBolusConstraints(d)
|
||||||
Assert.assertEquals(3.0, d.value()!!, 0.01)
|
Assert.assertEquals(3.0, d.value(), 0.01)
|
||||||
Assert.assertEquals("""
|
Assert.assertEquals("""
|
||||||
Safety: Limiting bolus to 3.0 U because of max value in preferences
|
Safety: Limiting bolus to 3.0 U because of max value in preferences
|
||||||
Safety: Limiting bolus to 5.0 U because of hard limit
|
Safety: Limiting bolus to 5.0 U because of hard limit
|
||||||
|
@ -240,7 +240,7 @@ class SafetyPluginTest : TestBaseWithProfile() {
|
||||||
// Apply all limits
|
// Apply all limits
|
||||||
var d = Constraint(Constants.REALLYHIGHIOB)
|
var d = Constraint(Constants.REALLYHIGHIOB)
|
||||||
d = safetyPlugin.applyMaxIOBConstraints(d)
|
d = safetyPlugin.applyMaxIOBConstraints(d)
|
||||||
Assert.assertEquals(1.5, d.value()!!, 0.01)
|
Assert.assertEquals(1.5, d.value(), 0.01)
|
||||||
Assert.assertEquals("""
|
Assert.assertEquals("""
|
||||||
Safety: Limiting IOB to 1.5 U because of max value in preferences
|
Safety: Limiting IOB to 1.5 U because of max value in preferences
|
||||||
""".trimIndent(), d.getReasons(aapsLogger))
|
""".trimIndent(), d.getReasons(aapsLogger))
|
||||||
|
|
|
@ -12,6 +12,9 @@ import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
|
import org.mockito.Mockito.`when`
|
||||||
|
import org.mockito.Mockito.anyInt
|
||||||
|
import org.mockito.Mockito.anyLong
|
||||||
import org.powermock.modules.junit4.PowerMockRunner
|
import org.powermock.modules.junit4.PowerMockRunner
|
||||||
|
|
||||||
@RunWith(PowerMockRunner::class)
|
@RunWith(PowerMockRunner::class)
|
||||||
|
@ -24,6 +27,7 @@ class StorageConstraintPluginTest : TestBase() {
|
||||||
|
|
||||||
@Before fun prepareMock() {
|
@Before fun prepareMock() {
|
||||||
storageConstraintPlugin = StorageConstraintPlugin({ AndroidInjector { } }, aapsLogger, resourceHelper, rxBusWrapper)
|
storageConstraintPlugin = StorageConstraintPlugin({ AndroidInjector { } }, aapsLogger, resourceHelper, rxBusWrapper)
|
||||||
|
`when`(resourceHelper.gs(anyInt(), anyLong())).thenReturn("")
|
||||||
}
|
}
|
||||||
|
|
||||||
class MockedStorageConstraintPlugin constructor(
|
class MockedStorageConstraintPlugin constructor(
|
||||||
|
|
|
@ -1,129 +0,0 @@
|
||||||
package info.nightscout.androidaps.interfaces;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.logging.AAPSLogger;
|
|
||||||
import info.nightscout.androidaps.logging.LTag;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by mike on 19.03.2018.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class Constraint<T extends Comparable<T>> {
|
|
||||||
private T value;
|
|
||||||
private T originalValue;
|
|
||||||
|
|
||||||
private final List<String> reasons = new ArrayList<>();
|
|
||||||
private final List<String> mostLimiting = new ArrayList<>();
|
|
||||||
|
|
||||||
public Constraint(T value) {
|
|
||||||
this.value = value;
|
|
||||||
this.originalValue = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T value() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T originalValue() {
|
|
||||||
return originalValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Constraint<T> set(AAPSLogger aapsLogger, T value) {
|
|
||||||
this.value = value;
|
|
||||||
this.originalValue = value;
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + value);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Constraint<T> set(AAPSLogger aapsLogger, T value, String reason, Object from) {
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]");
|
|
||||||
this.value = value;
|
|
||||||
addReason(reason, from);
|
|
||||||
addMostLimingReason(reason, from);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Constraint<T> setIfDifferent(AAPSLogger aapsLogger, T value, String reason, Object from) {
|
|
||||||
if (!this.value.equals(value)) {
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of different value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]");
|
|
||||||
this.value = value;
|
|
||||||
addReason(reason, from);
|
|
||||||
addMostLimingReason(reason, from);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Constraint<T> setIfSmaller(AAPSLogger aapsLogger, T value, String reason, Object from) {
|
|
||||||
if (value.compareTo(this.value) < 0) {
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of smaller value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]");
|
|
||||||
this.value = value;
|
|
||||||
mostLimiting.clear();
|
|
||||||
addMostLimingReason(reason, from);
|
|
||||||
}
|
|
||||||
if (value.compareTo(this.originalValue) < 0) {
|
|
||||||
addReason(reason, from);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Constraint<T> setIfGreater(AAPSLogger aapsLogger, T value, String reason, Object from) {
|
|
||||||
if (value.compareTo(this.value) > 0) {
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of greater value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]");
|
|
||||||
this.value = value;
|
|
||||||
mostLimiting.clear();
|
|
||||||
addMostLimingReason(reason, from);
|
|
||||||
}
|
|
||||||
if (value.compareTo(this.originalValue) > 0) {
|
|
||||||
addReason(reason, from);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String translateFrom(Object from) {
|
|
||||||
return from.getClass().getSimpleName().replace("Plugin", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addReason(String reason, Object from) {
|
|
||||||
reasons.add(translateFrom(from) + ": " + reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addMostLimingReason(String reason, Object from) {
|
|
||||||
mostLimiting.add(translateFrom(from) + ": " + reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getReasons(AAPSLogger aapsLogger) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
int count = 0;
|
|
||||||
for (String r : reasons) {
|
|
||||||
if (count++ != 0) sb.append("\n");
|
|
||||||
sb.append(r);
|
|
||||||
}
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: " + originalValue + " to " + value + ". Reason: " + sb.toString());
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getReasonList() {
|
|
||||||
return reasons;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMostLimitedReasons(AAPSLogger aapsLogger) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
int count = 0;
|
|
||||||
for (String r : mostLimiting) {
|
|
||||||
if (count++ != 0) sb.append("\n");
|
|
||||||
sb.append(r);
|
|
||||||
}
|
|
||||||
aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: " + originalValue + " to " + value + ". Reason: " + sb.toString());
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getMostLimitedReasonList() {
|
|
||||||
return mostLimiting;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void copyReasons(Constraint<?> another) {
|
|
||||||
reasons.addAll(another.getReasonList());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
package info.nightscout.androidaps.interfaces
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.logging.AAPSLogger
|
||||||
|
import info.nightscout.androidaps.logging.LTag
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class Constraint<T : Comparable<T>>(private var value: T) {
|
||||||
|
|
||||||
|
private var originalValue: T
|
||||||
|
private val reasons: MutableList<String> = ArrayList()
|
||||||
|
private val mostLimiting: MutableList<String> = ArrayList()
|
||||||
|
fun value(): T {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
fun originalValue(): T {
|
||||||
|
return originalValue
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun set(aapsLogger: AAPSLogger, value: T): Constraint<T> {
|
||||||
|
this.value = value
|
||||||
|
originalValue = value
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Setting value $value")
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun set(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint<T> {
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]")
|
||||||
|
this.value = value
|
||||||
|
addReason(reason, from)
|
||||||
|
addMostLimingReason(reason, from)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setIfDifferent(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint<T> {
|
||||||
|
if (this.value != value) {
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of different value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]")
|
||||||
|
this.value = value
|
||||||
|
addReason(reason, from)
|
||||||
|
addMostLimingReason(reason, from)
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setIfSmaller(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint<T> {
|
||||||
|
if (value < this.value) {
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of smaller value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]")
|
||||||
|
this.value = value
|
||||||
|
mostLimiting.clear()
|
||||||
|
addMostLimingReason(reason, from)
|
||||||
|
}
|
||||||
|
if (value < originalValue) {
|
||||||
|
addReason(reason, from)
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setIfGreater(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint<T> {
|
||||||
|
if (value > this.value) {
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of greater value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]")
|
||||||
|
this.value = value
|
||||||
|
mostLimiting.clear()
|
||||||
|
addMostLimingReason(reason, from)
|
||||||
|
}
|
||||||
|
if (value > originalValue) {
|
||||||
|
addReason(reason, from)
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun translateFrom(from: Any): String {
|
||||||
|
return from.javaClass.simpleName.replace("Plugin", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addReason(reason: String, from: Any) {
|
||||||
|
reasons.add(translateFrom(from) + ": " + reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addMostLimingReason(reason: String, from: Any) {
|
||||||
|
mostLimiting.add(translateFrom(from) + ": " + reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getReasons(aapsLogger: AAPSLogger): String {
|
||||||
|
val sb = StringBuilder()
|
||||||
|
for ((count, r) in reasons.withIndex()) {
|
||||||
|
if (count != 0) sb.append("\n")
|
||||||
|
sb.append(r)
|
||||||
|
}
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: $originalValue to $value. Reason: $sb")
|
||||||
|
return sb.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
val reasonList: List<String>
|
||||||
|
get() = reasons
|
||||||
|
|
||||||
|
fun getMostLimitedReasons(aapsLogger: AAPSLogger): String {
|
||||||
|
val sb = StringBuilder()
|
||||||
|
for ((count, r) in mostLimiting.withIndex()) {
|
||||||
|
if (count != 0) sb.append("\n")
|
||||||
|
sb.append(r)
|
||||||
|
}
|
||||||
|
aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: $originalValue to $value. Reason: $sb")
|
||||||
|
return sb.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
val mostLimitedReasonList: List<String>
|
||||||
|
get() = mostLimiting
|
||||||
|
|
||||||
|
fun copyReasons(another: Constraint<*>) {
|
||||||
|
reasons.addAll(another.reasonList)
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
originalValue = value
|
||||||
|
}
|
||||||
|
}
|
|
@ -53,7 +53,7 @@ class OverlappingIntervalsTest {
|
||||||
list.reset()
|
list.reset()
|
||||||
list.add(someList)
|
list.add(someList)
|
||||||
Assert.assertEquals(startDate, list[0]?.data?.timestamp)
|
Assert.assertEquals(startDate, list[0]?.data?.timestamp)
|
||||||
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0)?.data?.timestamp)
|
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0).data.timestamp)
|
||||||
Assert.assertEquals(startDate + T.hours(1).msecs(), list.reversedList[0].data.timestamp)
|
Assert.assertEquals(startDate + T.hours(1).msecs(), list.reversedList[0].data.timestamp)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,7 +15,7 @@ import org.powermock.modules.junit4.PowerMockRunner
|
||||||
class MsgBolusStartTest : DanaRTestBase() {
|
class MsgBolusStartTest : DanaRTestBase() {
|
||||||
|
|
||||||
@Test fun runTest() {
|
@Test fun runTest() {
|
||||||
`when`(constraintChecker.applyBolusConstraints(Constraint(anyObject()))).thenReturn(Constraint(0.0))
|
`when`(constraintChecker.applyBolusConstraints(anyObject())).thenReturn(Constraint(0.0))
|
||||||
val packet = MsgBolusStart(injector, 1.0)
|
val packet = MsgBolusStart(injector, 1.0)
|
||||||
|
|
||||||
// test message decoding
|
// test message decoding
|
||||||
|
|
|
@ -15,7 +15,7 @@ import org.powermock.modules.junit4.PowerMockRunner
|
||||||
class MsgBolusStartWithSpeedTest : DanaRTestBase() {
|
class MsgBolusStartWithSpeedTest : DanaRTestBase() {
|
||||||
|
|
||||||
@Test fun runTest() {
|
@Test fun runTest() {
|
||||||
Mockito.`when`(constraintChecker.applyBolusConstraints(Constraint(anyObject()))).thenReturn(Constraint(0.0))
|
Mockito.`when`(constraintChecker.applyBolusConstraints(anyObject())).thenReturn(Constraint(0.0))
|
||||||
val packet = MsgBolusStartWithSpeed(injector, 0.0, 0)
|
val packet = MsgBolusStartWithSpeed(injector, 0.0, 0)
|
||||||
|
|
||||||
// test message decoding
|
// test message decoding
|
||||||
|
|
Loading…
Reference in a new issue