Skip to content

Commit 1a114c5

Browse files
committed
Fix: chooseMultivariate(variantMap) ignore empty array members
1 parent 905b36d commit 1a114c5

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

improveai/src/main/java/ai/improve/DecisionContext.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public <T> T random(List<T> variants) {
107107
}
108108

109109
/**
110-
* @see ai.improve.DecisionModel#optimize(Map)
110+
* @see ai.improve.DecisionModel#chooseMultivariate(Map)
111111
*/
112112
public Decision<Map<String, ?>> chooseMultivariate(Map<String, ?> variants) {
113113
if(variants == null || variants.size() <= 0) {
@@ -119,11 +119,17 @@ public <T> T random(List<T> variants) {
119119
List<List> categories = new ArrayList();
120120
for(Map.Entry<String, ?> entry : variants.entrySet()) {
121121
if(entry.getValue() instanceof List) {
122-
categories.add((List)entry.getValue());
122+
if(((List)entry.getValue()).size() > 0) {
123+
categories.add((List) entry.getValue());
124+
allKeys.add(entry.getKey());
125+
}
123126
} else {
124127
categories.add(Arrays.asList(entry.getValue()));
128+
allKeys.add(entry.getKey());
125129
}
126-
allKeys.add(entry.getKey());
130+
}
131+
if(categories.size() <= 0) {
132+
throw new IllegalArgumentException("valueMap values are all empty list!");
127133
}
128134

129135
List<Map<String, ?>> combinations = new ArrayList();

improveai/src/main/java/ai/improve/DecisionModel.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,19 @@ public <T> Decision<T> chooseFrom(List<T> variants, List<Double> scores) {
337337
* {"style":"italic", "size":5},
338338
* ])
339339
* @param variants Variants can be any JSON encodeable data structure of arbitrary complexity
340-
* like chooseFrom(). The value of the dictionary is expected to be a List.
341-
* If not, it would be treated as an one-element List anyway. So
342-
* optimize({"style":["bold", "italic"], "size":3}) is equivalent to
343-
* optimize({"style":["bold", "italic"], "size":[3]})
340+
* like chooseFrom(). The value of the dictionary is expected to be a list.
341+
* If not, it would be automatically wrapped as a list containing a single item.
342+
* So chooseMultivariate({"style":["bold", "italic"], "size":3}) is equivalent to
343+
* chooseMultivariate({"style":["bold", "italic"], "size":[3]})
344344
* @return An IMPDecision object.
345-
* */
345+
*/
346346
public Decision<Map<String, ?>> chooseMultivariate(Map<String, ?> variants) {
347347
return given(null).chooseMultivariate(variants);
348348
}
349349

350+
/**
351+
* A shorthand of chooseMultivariate(variantMap).get().
352+
*/
350353
public Map<String, ?> optimize(Map<String, ?> variants) {
351354
return given(null).optimize(variants);
352355
}

improveai/src/test/java/ai/improve/DecisionModelTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,32 @@ public void testOptimize() {
716716
assertNotNull(theme.get("size"));
717717
}
718718

719+
@Test
720+
public void testOptimize_empty_member() {
721+
Map variants = new HashMap();
722+
variants.put("font", Arrays.asList("Italic", "Bold"));
723+
variants.put("size", Arrays.asList(12, 13));
724+
variants.put("color", new ArrayList<String>());
725+
DecisionModel decisionModel = new DecisionModel("theme");
726+
Map<String, String> theme = decisionModel.optimize(variants);
727+
assertEquals(2, theme.size());
728+
assertNotNull(theme.get("font"));
729+
assertNotNull(theme.get("size"));
730+
}
731+
732+
@Test
733+
public void testOptimize_all_empty() {
734+
Map variants = new HashMap();
735+
variants.put("color", new ArrayList<String>());
736+
DecisionModel decisionModel = new DecisionModel("theme");
737+
try {
738+
decisionModel.optimize(variants);
739+
fail(DefaultFailMessage);
740+
} catch (IllegalArgumentException e) {
741+
e.printStackTrace();
742+
}
743+
}
744+
719745
@Test
720746
public void testWhichVariadic() {
721747
DecisionModel decisionModel = new DecisionModel("theme");

0 commit comments

Comments
 (0)