objectives cleanup & basal hard limits to constraints

This commit is contained in:
Milos Kozak 2018-03-22 23:05:00 +01:00
parent ba09538f46
commit 0f531a9954
8 changed files with 29 additions and 72 deletions

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.data;
import java.util.ArrayList;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
@ -40,6 +41,10 @@ public class ConstraintChecker implements ConstraintsInterface {
return isSMBModeEnabled(new Constraint<>(true));
}
public Constraint<Double> getMaxBasalAllowed(Profile profile) {
return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile);
}
@Override
public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {

View file

@ -137,6 +137,14 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
public void setStarted(Date started) {
this.started = started;
}
boolean isStarted() {
return started.getTime() > 0;
}
boolean isFinished() {
return accomplished.getTime() != 0;
}
}
// Objective 0
@ -302,42 +310,42 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
**/
@Override
public Constraint<Boolean> isLoopInvokationAllowed(Constraint<Boolean> value) {
if (objectives.get(0).started.getTime() == 0)
if (!objectives.get(0).isStarted())
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1), this);
return value;
}
@Override
public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
if (objectives.get(3).started.getTime() == 0)
if (!objectives.get(3).isStarted())
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4), this);
return value;
}
@Override
public Constraint<Boolean> isAutosensModeEnabled(Constraint<Boolean> value) {
if (objectives.get(5).started.getTime() == 0)
if (!objectives.get(5).isStarted())
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 6), this);
return value;
}
@Override
public Constraint<Boolean> isAMAModeEnabled(Constraint<Boolean> value) {
if (objectives.get(6).started.getTime() == 0)
if (!objectives.get(6).isStarted())
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 7), this);
return value;
}
@Override
public Constraint<Boolean> isSMBModeEnabled(Constraint<Boolean> value) {
if (objectives.get(7).started.getTime() == 0)
if (!objectives.get(7).isStarted())
value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 8), this);
return value;
}
@Override
public Constraint<Double> applyMaxIOBConstraints(Constraint<Double> maxIob) {
if (objectives.get(3).started.getTime() > 0&& objectives.get(3).accomplished.getTime() == 0)
if (objectives.get(3).isStarted() && !objectives.get(3).isFinished())
maxIob.set(0d, String.format(MainApp.gs(R.string.objectivenotfinished), 4), this);
return maxIob;
}

View file

@ -145,6 +145,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
Double maxBasalFromDaily = SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d);
double maxFromDaily = Math.floor(profile.getMaxDailyBasal() * maxBasalFromDaily * 100) / 100;
absoluteRate.setIfSmaller(maxFromDaily, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromDaily, MainApp.gs(R.string.maxdailybasalmultiplier)), this);
absoluteRate.setIfSmaller(HardLimits.maxBasal(), String.format(MainApp.gs(R.string.limitingbasalratio), HardLimits.maxBasal(), MainApp.gs(R.string.hardlimit)), this);
return absoluteRate;
}

View file

@ -175,7 +175,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface {
String units = profile.getUnits();
double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d);
double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value();
double minBg = Profile.toMgdl(profile.getTargetLow(), units);
double maxBg = Profile.toMgdl(profile.getTargetHigh(), units);
double targetBg = Profile.toMgdl(profile.getTarget(), units);
@ -208,8 +208,6 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface {
}
maxBasal = HardLimits.verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal());
if (!HardLimits.checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA))
return;
if (!HardLimits.checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC))

View file

@ -175,7 +175,8 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface {
String units = profile.getUnits();
double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d);
double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value();
double minBg = Profile.toMgdl(profile.getTargetLow(), units);
double maxBg = Profile.toMgdl(profile.getTargetHigh(), units);
double targetBg = Profile.toMgdl(profile.getTarget(), units);
@ -207,8 +208,6 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface {
targetBg = verifyHardLimits(tempTarget.target(), "targetBg", HardLimits.VERY_HARD_LIMIT_TEMP_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_TARGET_BG[1]);
}
maxBasal = verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal());
if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA))
return;
if (!checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC))

View file

@ -180,7 +180,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
String units = profile.getUnits();
double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d);
double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value();
double minBg = Profile.toMgdl(profile.getTargetLow(), units);
double maxBg = Profile.toMgdl(profile.getTargetHigh(), units);
double targetBg = Profile.toMgdl(profile.getTarget(), units);
@ -213,8 +213,6 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
}
maxBasal = verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal());
if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) return;
if (!checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC))
return;

View file

@ -200,6 +200,7 @@ public class ConstraintsCheckerTest {
when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d);
when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d);
when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d);
when(SP.getString(R.string.key_age, "")).thenReturn("child");
// Negative basal not allowed
Constraint<Double> d = new Constraint<>(-0.5d);
@ -214,6 +215,7 @@ public class ConstraintsCheckerTest {
Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" +
"SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" +
"SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" +
"SafetyPlugin: Limiting basal rate to 2.00 U/h because of hard limit\n" +
"DanaRPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" +
"DanaRSPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" +
"InsightPumpPlugin: Limiting basal rate to 1.10 U/h because of pump limit", d.getReasons());
@ -239,6 +241,7 @@ public class ConstraintsCheckerTest {
when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d);
when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d);
when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d);
when(SP.getString(R.string.key_age, "")).thenReturn("child");
// Negative basal not allowed
Constraint<Integer> i = new Constraint<>(-22);
@ -259,6 +262,7 @@ public class ConstraintsCheckerTest {
"SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" +
"SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" +
"SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" +
"SafetyPlugin: Limiting basal rate to 2.00 U/h because of hard limit\n" +
"SafetyPlugin: Limiting percent rate to 100% because of pump limit\n" +
"DanaRPlugin: Limiting percent rate to 200% because of pump limit\n" +
"DanaRSPlugin: Limiting percent rate to 200% because of pump limit\n" +

View file

@ -1,57 +0,0 @@
package info.nightscout.utils;
import android.content.Context;
import android.view.LayoutInflater;
import org.json.JSONArray;
import org.junit.Test;
import org.mockito.Mock;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
/**
* Created by mike on 30.12.2016.
*/
public class TimeListEditTest {
/*
JSONArray data = new JSONArray();
JSONArray data2 = new JSONArray();
TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00"));
TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00"));
@Test
public void doArrayTest() throws Exception {
tle.addItem(0, 0, 0.1, 0);
tle.addItem(1, 60 * 60, 0.2, 0);
assertEquals(2, tle.itemsCount());
tle.editItem(0, 2 * 60 * 60, 1, 0);
assertEquals(2, tle.itemsCount());
assertEquals(1d, tle.value1(0), 0.00001d);
assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0));
tle.removeItem(0);
assertEquals(0.2d, tle.value1(0), 0.00001d);
assertEquals(0, tle.value2(0), 0.00001d);
assertEquals(60 * 60, tle.secondFromMidnight(0));
//System.out.print(tle2.toString());
assertEquals(0, tle2.itemsCount());
tle2.addItem(0, 0, 1, 2);
assertEquals(1, tle2.itemsCount());
assertEquals(0, tle2.secondFromMidnight(0));
assertEquals(1d, tle2.value1(0), 0.00001d);
assertEquals(2d, tle2.value2(0), 0.00001d);
}
*/
@Test
public void fakeTest() throws Exception {
}
}