Skip to content

Instantly share code, notes, and snippets.

@sanity
Created September 23, 2009 15:39
Show Gist options
  • Save sanity/192080 to your computer and use it in GitHub Desktop.
Save sanity/192080 to your computer and use it in GitHub Desktop.
package com.uprizer.sensearray;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.gson.JsonParseException;
import com.uprizer.sensearray.io.api.resources.RecommendResource;
import com.uprizer.sensearray.tools.JsonVerifiable;
public class RecommendationPoolConfig implements JsonVerifiable {
static com.uprizer.sensearray.util.logging.Logger log = new com.uprizer.sensearray.util.logging.Logger(
RecommendationPoolConfig.class);
/**
* The name of the RecommendationPool, which can be used to indicate you
* want recommendations from this pool in an API call
*/
public String name;
/**
* The key associated with the items that should be selected from this pool.
* This would typically be something like "offer-id" or "ad-id".
*/
public String key;
/**
* The various options that SenseArray must evaluate before returning
* recommendations from this pool for a particular scenario.
*/
public Set<RecommendationOption> options;
public ABTest abTest = null;
@Override
public void verify() throws JsonParseException {
for (final RecommendationOption k : options) {
if (!((k.scalar != null) ^ (k.choices != null)))
throw new JsonParseException("One and only one of scalar and choices fields must be present in option "+k.name);
}
if (abTest != null) {
if (!(abTest.control != null) ^ (abTest.controlSet != null))
throw new JsonParseException("Either control or controlSet must be specified in abTest, but not both");
}
}
public static class ABTest {
public int every = 2;
public boolean log = true;
public String testKey = null;
public List<RecommendResource.Recommendation> control;
public Map<String, List<RecommendResource.Recommendation>> controlSet;
}
public static class LegacyTagPool {
public String name;
public Set<TagData> tags;
public RecommendationPoolConfig toRecommendationPoolConfig() throws CantConvertException {
final RecommendationPoolConfig ret = new RecommendationPoolConfig();
ret.name = name;
if (tags.isEmpty())
throw new CantConvertException();
ret.key = tags.iterator().next().tag.split(":")[0];
ret.options = new HashSet<RecommendationOption>();
for (final TagData td : tags) {
final String value = td.tag.substring(td.tag.indexOf(":") + 1);
ret.options.add(new RecommendationOption(value, td.bias));
}
return ret;
}
public static class CantConvertException extends Exception {
private static final long serialVersionUID = 5883323186887343462L;
}
public static class TagData {
public String tag;
public double bias;
}
}
/*
* Note: Either scalar or choices (which specifies several scalars must be
* specified)
*/
public static class RecommendationOption {
/**
* The name of this option. Typically this might be a number that
* uniquely identifies an offer or an ad to be recommended.
*/
public String name;
/**
* This indicates the value of this option - often a monetary value
* associated with an outcome of 1, allowing an expected revenue to be
* calculated by multiplying a probability with this scalar value.
*/
public Double scalar = null;
/**
* An alternative to scalar being specified is to specify a number of
* choices that the user might make if this option is presented, each
* having its own value.
*
* There must be a value for "scalar" or "option", but cannot be a value
* for both.
*/
public Map<String, Double> choices = null;
public RecommendationOption() {
}
public RecommendationOption(final String name, final Double scalar) {
this.name = name;
this.scalar = scalar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment