Search in sources :

Example 1 with AudienceIdCondition

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);
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) DecisionResponse(com.optimizely.ab.optimizelydecision.DecisionResponse) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) ArrayList(java.util.ArrayList) OrCondition(com.optimizely.ab.config.audience.OrCondition) DefaultDecisionReasons(com.optimizely.ab.optimizelydecision.DefaultDecisionReasons) DecisionReasons(com.optimizely.ab.optimizelydecision.DecisionReasons) Nonnull(javax.annotation.Nonnull)

Example 2 with AudienceIdCondition

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);
    }
}
Also used : AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject)

Example 3 with AudienceIdCondition

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;
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Example 4 with AudienceIdCondition

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;
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 5 with AudienceIdCondition

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"));
    }
}
Also used : AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap)

Aggregations

AudienceIdCondition (com.optimizely.ab.config.audience.AudienceIdCondition)6 Condition (com.optimizely.ab.config.audience.Condition)4 OrCondition (com.optimizely.ab.config.audience.OrCondition)2 ArrayList (java.util.ArrayList)2 JSONObject (org.json.simple.JSONObject)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1 AndCondition (com.optimizely.ab.config.audience.AndCondition)1 EmptyCondition (com.optimizely.ab.config.audience.EmptyCondition)1 NotCondition (com.optimizely.ab.config.audience.NotCondition)1 NullCondition (com.optimizely.ab.config.audience.NullCondition)1 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)1 InvalidAudienceCondition (com.optimizely.ab.internal.InvalidAudienceCondition)1 DecisionReasons (com.optimizely.ab.optimizelydecision.DecisionReasons)1 DecisionResponse (com.optimizely.ab.optimizelydecision.DecisionResponse)1 DefaultDecisionReasons (com.optimizely.ab.optimizelydecision.DefaultDecisionReasons)1 List (java.util.List)1