Search in sources :

Example 6 with Group

use of com.optimizely.ab.config.Group in project java-sdk by optimizely.

the class GroupJacksonDeserializer method deserialize.

@Override
public Group deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = parser.getCodec().readTree(parser);
    String id = node.get("id").textValue();
    String policy = node.get("policy").textValue();
    List<TrafficAllocation> trafficAllocations = mapper.readValue(node.get("trafficAllocation").toString(), new TypeReference<List<TrafficAllocation>>() {
    });
    JsonNode groupExperimentsJson = node.get("experiments");
    List<Experiment> groupExperiments = new ArrayList<Experiment>();
    if (groupExperimentsJson.isArray()) {
        for (JsonNode groupExperimentJson : groupExperimentsJson) {
            groupExperiments.add(parseExperiment(groupExperimentJson, id));
        }
    }
    return new Group(id, policy, groupExperiments, trafficAllocations);
}
Also used : Group(com.optimizely.ab.config.Group) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with Group

use of com.optimizely.ab.config.Group in project java-sdk by optimizely.

the class Bucketer method bucket.

/**
 * Assign a {@link Variation} of an {@link Experiment} to a user based on hashed value from murmurhash3.
 * @param experiment The Experiment in which the user is to be bucketed.
 * @param bucketingId string A customer-assigned value used to create the key for the murmur hash.
 * @return Variation the user is bucketed into or null.
 */
@Nullable
public Variation bucket(@Nonnull Experiment experiment, @Nonnull String bucketingId) {
    // ---------- Bucket User ----------
    String groupId = experiment.getGroupId();
    // check whether the experiment belongs to a group
    if (!groupId.isEmpty()) {
        Group experimentGroup = projectConfig.getGroupIdMapping().get(groupId);
        // bucket to an experiment only if group entities are to be mutually exclusive
        if (experimentGroup.getPolicy().equals(Group.RANDOM_POLICY)) {
            Experiment bucketedExperiment = bucketToExperiment(experimentGroup, bucketingId);
            if (bucketedExperiment == null) {
                logger.info("User with bucketingId \"{}\" is not in any experiment of group {}.", bucketingId, experimentGroup.getId());
                return null;
            } else {
            }
            // don't perform further bucketing within the experiment
            if (!bucketedExperiment.getId().equals(experiment.getId())) {
                logger.info("User with bucketingId \"{}\" is not in experiment \"{}\" of group {}.", bucketingId, experiment.getKey(), experimentGroup.getId());
                return null;
            }
            logger.info("User with bucketingId \"{}\" is in experiment \"{}\" of group {}.", bucketingId, experiment.getKey(), experimentGroup.getId());
        }
    }
    return bucketToVariation(experiment, bucketingId);
}
Also used : Group(com.optimizely.ab.config.Group) Experiment(com.optimizely.ab.config.Experiment) Nullable(javax.annotation.Nullable)

Example 8 with Group

use of com.optimizely.ab.config.Group in project java-sdk by optimizely.

the class GroupGsonDeserializer method deserialize.

@Override
public Group deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String id = jsonObject.get("id").getAsString();
    String policy = jsonObject.get("policy").getAsString();
    List<Experiment> experiments = new ArrayList<Experiment>();
    JsonArray experimentsJson = jsonObject.getAsJsonArray("experiments");
    for (Object obj : experimentsJson) {
        JsonObject experimentObj = (JsonObject) obj;
        experiments.add(GsonHelpers.parseExperiment(experimentObj, id, context));
    }
    List<TrafficAllocation> trafficAllocations = GsonHelpers.parseTrafficAllocation(jsonObject.getAsJsonArray("trafficAllocation"));
    return new Group(id, policy, experiments, trafficAllocations);
}
Also used : JsonArray(com.google.gson.JsonArray) Group(com.optimizely.ab.config.Group) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 9 with Group

use of com.optimizely.ab.config.Group in project java-sdk by optimizely.

the class ProjectConfigGsonDeserializer method deserialize.

@Override
public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String accountId = jsonObject.get("accountId").getAsString();
    String projectId = jsonObject.get("projectId").getAsString();
    String revision = jsonObject.get("revision").getAsString();
    String version = jsonObject.get("version").getAsString();
    int datafileVersion = Integer.parseInt(version);
    // generic list type tokens
    Type groupsType = new TypeToken<List<Group>>() {
    }.getType();
    Type experimentsType = new TypeToken<List<Experiment>>() {
    }.getType();
    Type attributesType = new TypeToken<List<Attribute>>() {
    }.getType();
    Type eventsType = new TypeToken<List<EventType>>() {
    }.getType();
    Type audienceType = new TypeToken<List<Audience>>() {
    }.getType();
    List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType);
    List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType);
    List<Attribute> attributes;
    attributes = context.deserialize(jsonObject.get("attributes"), attributesType);
    List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType);
    List<Audience> audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType);
    boolean anonymizeIP = false;
    // live variables should be null if using V2
    List<LiveVariable> liveVariables = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
        Type liveVariablesType = new TypeToken<List<LiveVariable>>() {
        }.getType();
        liveVariables = context.deserialize(jsonObject.getAsJsonArray("variables"), liveVariablesType);
        anonymizeIP = jsonObject.get("anonymizeIP").getAsBoolean();
    }
    List<FeatureFlag> featureFlags = null;
    List<Rollout> rollouts = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        Type featureFlagsType = new TypeToken<List<FeatureFlag>>() {
        }.getType();
        featureFlags = context.deserialize(jsonObject.getAsJsonArray("featureFlags"), featureFlagsType);
        Type rolloutsType = new TypeToken<List<Rollout>>() {
        }.getType();
        rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType);
    }
    return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
}
Also used : Group(com.optimizely.ab.config.Group) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) JsonObject(com.google.gson.JsonObject) FeatureFlag(com.optimizely.ab.config.FeatureFlag) List(java.util.List) Rollout(com.optimizely.ab.config.Rollout) Audience(com.optimizely.ab.config.audience.Audience) Experiment(com.optimizely.ab.config.Experiment) LiveVariable(com.optimizely.ab.config.LiveVariable) ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventType(com.optimizely.ab.config.EventType) Type(java.lang.reflect.Type)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)9 Group (com.optimizely.ab.config.Group)9 Attribute (com.optimizely.ab.config.Attribute)4 EventType (com.optimizely.ab.config.EventType)4 FeatureFlag (com.optimizely.ab.config.FeatureFlag)4 LiveVariable (com.optimizely.ab.config.LiveVariable)4 ProjectConfig (com.optimizely.ab.config.ProjectConfig)4 Rollout (com.optimizely.ab.config.Rollout)4 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)4 Audience (com.optimizely.ab.config.audience.Audience)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonObject (com.google.gson.JsonObject)2 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)2 JSONObject (org.json.JSONObject)2 JSONObject (org.json.simple.JSONObject)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1