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);
}
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);
}
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);
}
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);
}
Aggregations