use of com.optimizely.ab.config.audience.AudienceIdCondition in project java-sdk by optimizely.
the class ExperimentUtils method evaluateAudience.
@Nonnull
public static DecisionResponse<Boolean> evaluateAudience(@Nonnull ProjectConfig projectConfig, @Nonnull Experiment experiment, @Nonnull Map<String, ?> attributes, @Nonnull String loggingEntityType, @Nonnull String loggingKey) {
DecisionReasons reasons = DefaultDecisionReasons.newInstance();
List<String> experimentAudienceIds = experiment.getAudienceIds();
// if there are no audiences, ALL users should be part of the experiment
if (experimentAudienceIds.isEmpty()) {
return new DecisionResponse(true, reasons);
}
List<Condition> conditions = new ArrayList<>();
for (String audienceId : experimentAudienceIds) {
AudienceIdCondition condition = new AudienceIdCondition(audienceId);
conditions.add(condition);
}
OrCondition implicitOr = new OrCondition(conditions);
logger.debug("Evaluating audiences for {} \"{}\": {}.", loggingEntityType, loggingKey, conditions);
Boolean result = implicitOr.evaluate(projectConfig, attributes);
String message = reasons.addInfo("Audiences for %s \"%s\" collectively evaluated to %s.", loggingEntityType, loggingKey, result);
logger.info(message);
return new DecisionResponse(result, reasons);
}
use of com.optimizely.ab.config.audience.AudienceIdCondition in project java-sdk by optimizely.
the class GsonHelpers method parseAudienceConditions.
static Condition parseAudienceConditions(JsonObject experimentJson) {
if (!experimentJson.has("audienceConditions"))
return null;
Gson gson = new Gson();
JsonElement conditionsElement = experimentJson.get("audienceConditions");
if (conditionsElement.isJsonArray()) {
List<Object> rawObjectList = gson.fromJson(conditionsElement, List.class);
return ConditionUtils.<AudienceIdCondition>parseConditions(AudienceIdCondition.class, rawObjectList);
} else {
Object jsonObject = gson.fromJson(conditionsElement, Object.class);
return ConditionUtils.<AudienceIdCondition>parseConditions(AudienceIdCondition.class, jsonObject);
}
}
use of com.optimizely.ab.config.audience.AudienceIdCondition in project java-sdk by optimizely.
the class JsonSimpleConfigParser method parseExperiments.
private List<Experiment> parseExperiments(JSONArray experimentJson, String groupId) {
List<Experiment> experiments = new ArrayList<Experiment>(experimentJson.size());
for (Object obj : experimentJson) {
JSONObject experimentObject = (JSONObject) obj;
String id = (String) experimentObject.get("id");
String key = (String) experimentObject.get("key");
Object statusJson = experimentObject.get("status");
String status = statusJson == null ? ExperimentStatus.NOT_STARTED.toString() : (String) experimentObject.get("status");
Object layerIdObject = experimentObject.get("layerId");
String layerId = layerIdObject == null ? null : (String) layerIdObject;
JSONArray audienceIdsJson = (JSONArray) experimentObject.get("audienceIds");
List<String> audienceIds = new ArrayList<String>(audienceIdsJson.size());
for (Object audienceIdObj : audienceIdsJson) {
audienceIds.add((String) audienceIdObj);
}
Condition conditions = null;
if (experimentObject.containsKey("audienceConditions")) {
Object jsonCondition = experimentObject.get("audienceConditions");
try {
conditions = ConditionUtils.<AudienceIdCondition>parseConditions(AudienceIdCondition.class, jsonCondition);
} catch (Exception e) {
// unable to parse conditions.
Logger.getAnonymousLogger().log(Level.ALL, "problem parsing audience conditions", e);
}
}
// parse the child objects
List<Variation> variations = parseVariations((JSONArray) experimentObject.get("variations"));
Map<String, String> userIdToVariationKeyMap = parseForcedVariations((JSONObject) experimentObject.get("forcedVariations"));
List<TrafficAllocation> trafficAllocations = parseTrafficAllocation((JSONArray) experimentObject.get("trafficAllocation"));
experiments.add(new Experiment(id, key, status, layerId, audienceIds, conditions, variations, userIdToVariationKeyMap, trafficAllocations, groupId));
}
return experiments;
}
use of com.optimizely.ab.config.audience.AudienceIdCondition in project java-sdk by optimizely.
the class JsonConfigParser method parseExperiments.
private List<Experiment> parseExperiments(JSONArray experimentJson, String groupId) {
List<Experiment> experiments = new ArrayList<Experiment>(experimentJson.length());
for (int i = 0; i < experimentJson.length(); i++) {
Object obj = experimentJson.get(i);
JSONObject experimentObject = (JSONObject) obj;
String id = experimentObject.getString("id");
String key = experimentObject.getString("key");
String status = experimentObject.isNull("status") ? ExperimentStatus.NOT_STARTED.toString() : experimentObject.getString("status");
String layerId = experimentObject.has("layerId") ? experimentObject.getString("layerId") : null;
JSONArray audienceIdsJson = experimentObject.getJSONArray("audienceIds");
List<String> audienceIds = new ArrayList<String>(audienceIdsJson.length());
for (int j = 0; j < audienceIdsJson.length(); j++) {
Object audienceIdObj = audienceIdsJson.get(j);
audienceIds.add((String) audienceIdObj);
}
Condition conditions = null;
if (experimentObject.has("audienceConditions")) {
Object jsonCondition = experimentObject.get("audienceConditions");
conditions = ConditionUtils.<AudienceIdCondition>parseConditions(AudienceIdCondition.class, jsonCondition);
}
// parse the child objects
List<Variation> variations = parseVariations(experimentObject.getJSONArray("variations"));
Map<String, String> userIdToVariationKeyMap = parseForcedVariations(experimentObject.getJSONObject("forcedVariations"));
List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(experimentObject.getJSONArray("trafficAllocation"));
experiments.add(new Experiment(id, key, status, layerId, audienceIds, conditions, variations, userIdToVariationKeyMap, trafficAllocations, groupId));
}
return experiments;
}
use of com.optimizely.ab.config.audience.AudienceIdCondition in project java-sdk by optimizely.
the class ConditionUtils method parseConditions.
public static <T> Condition parseConditions(Class<T> clazz, Object object) throws InvalidAudienceCondition {
if (object instanceof List) {
List<Object> objectList = (List<Object>) object;
return ConditionUtils.<T>parseConditions(clazz, objectList);
} else if (object instanceof String) {
// looking for audience conditions in experiment
AudienceIdCondition audienceIdCondition = new AudienceIdCondition<T>((String) object);
if (clazz.isInstance(audienceIdCondition)) {
return audienceIdCondition;
} else {
throw new InvalidAudienceCondition(String.format("Expected AudienceIdCondition got %s", clazz.getCanonicalName()));
}
} else {
try {
if (object instanceof LinkedTreeMap) {
// gson
if (clazz != UserAttribute.class) {
throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
}
LinkedTreeMap<String, ?> conditionMap = (LinkedTreeMap<String, ?>) object;
return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), (String) conditionMap.get("match"), conditionMap.get("value"));
}
} catch (NoClassDefFoundError ex) {
// no gson loaded... not sure we need to log this if they don't use gson.
logger.debug("parser: gson library not loaded");
}
try {
if (object instanceof JSONObject) {
// simple json
if (clazz != UserAttribute.class) {
throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
}
JSONObject conditionMap = (JSONObject) object;
return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), (String) conditionMap.get("match"), conditionMap.get("value"));
}
} catch (NoClassDefFoundError ex) {
logger.debug("parser: simple json not found");
}
try {
if (object instanceof org.json.JSONArray) {
// json
return ConditionUtils.<T>parseConditions(clazz, (org.json.JSONArray) object);
} else if (object instanceof org.json.JSONObject) {
// json
if (clazz != UserAttribute.class) {
throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
}
org.json.JSONObject conditionMap = (org.json.JSONObject) object;
String match = null;
Object value = null;
if (conditionMap.has("match")) {
match = (String) conditionMap.get("match");
}
if (conditionMap.has("value")) {
value = conditionMap.get("value");
}
return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), match, value);
}
} catch (NoClassDefFoundError ex) {
logger.debug("parser: json package not found.");
}
if (clazz != UserAttribute.class) {
throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
}
Map<String, ?> conditionMap = (Map<String, ?>) object;
return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), (String) conditionMap.get("match"), conditionMap.get("value"));
}
}
Aggregations