Search in sources :

Example 1 with OptimizelyDecisionContext

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

the class DecisionService method getVariationFromDeliveryRule.

/**
 * @param projectConfig     The Project config
 * @param flagKey           The flag key for the feature flag
 * @param rules             The experiments belonging to a rollout
 * @param ruleIndex         The index of the rule
 * @param user              The OptimizelyUserContext
 * @return                  Returns a DecisionResponse Object containing a AbstractMap.SimpleEntry<Variation, Boolean>
 *                          where the Variation is the result and the Boolean is the skipToEveryoneElse.
 */
DecisionResponse<AbstractMap.SimpleEntry> getVariationFromDeliveryRule(@Nonnull ProjectConfig projectConfig, @Nonnull String flagKey, @Nonnull List<Experiment> rules, @Nonnull int ruleIndex, @Nonnull OptimizelyUserContext user) {
    DecisionReasons reasons = DefaultDecisionReasons.newInstance();
    Boolean skipToEveryoneElse = false;
    AbstractMap.SimpleEntry<Variation, Boolean> variationToSkipToEveryoneElsePair;
    // Check forced-decisions first
    Experiment rule = rules.get(ruleIndex);
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, rule.getKey());
    DecisionResponse<Variation> forcedDecisionResponse = validatedForcedDecision(optimizelyDecisionContext, projectConfig, user);
    reasons.merge(forcedDecisionResponse.getReasons());
    Variation variation = forcedDecisionResponse.getResult();
    if (variation != null) {
        variationToSkipToEveryoneElsePair = new AbstractMap.SimpleEntry<>(variation, false);
        return new DecisionResponse(variationToSkipToEveryoneElsePair, reasons);
    }
    // Handle a regular decision
    String bucketingId = getBucketingId(user.getUserId(), user.getAttributes());
    Boolean everyoneElse = (ruleIndex == rules.size() - 1);
    String loggingKey = everyoneElse ? "Everyone Else" : String.valueOf(ruleIndex + 1);
    Variation bucketedVariation = null;
    DecisionResponse<Boolean> audienceDecisionResponse = ExperimentUtils.doesUserMeetAudienceConditions(projectConfig, rule, user.getAttributes(), RULE, String.valueOf(ruleIndex + 1));
    reasons.merge(audienceDecisionResponse.getReasons());
    String message;
    if (audienceDecisionResponse.getResult()) {
        message = reasons.addInfo("User \"%s\" meets conditions for targeting rule \"%s\".", user.getUserId(), loggingKey);
        reasons.addInfo(message);
        logger.debug(message);
        DecisionResponse<Variation> decisionResponse = bucketer.bucket(rule, bucketingId, projectConfig);
        reasons.merge(decisionResponse.getReasons());
        bucketedVariation = decisionResponse.getResult();
        if (bucketedVariation != null) {
            message = reasons.addInfo("User \"%s\" bucketed for targeting rule \"%s\".", user.getUserId(), loggingKey);
            logger.debug(message);
            reasons.addInfo(message);
        } else if (!everyoneElse) {
            message = reasons.addInfo("User \"%s\" is not bucketed for targeting rule \"%s\".", user.getUserId(), loggingKey);
            logger.debug(message);
            reasons.addInfo(message);
            // Skip the rest of rollout rules to the everyone-else rule if audience matches but not bucketed.
            skipToEveryoneElse = true;
        }
    } else {
        message = reasons.addInfo("User \"%s\" does not meet conditions for targeting rule \"%d\".", user.getUserId(), ruleIndex + 1);
        reasons.addInfo(message);
        logger.debug(message);
    }
    variationToSkipToEveryoneElsePair = new AbstractMap.SimpleEntry<>(bucketedVariation, skipToEveryoneElse);
    return new DecisionResponse(variationToSkipToEveryoneElsePair, reasons);
}
Also used : DecisionResponse(com.optimizely.ab.optimizelydecision.DecisionResponse) OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) DefaultDecisionReasons(com.optimizely.ab.optimizelydecision.DefaultDecisionReasons) DecisionReasons(com.optimizely.ab.optimizelydecision.DecisionReasons)

Example 2 with OptimizelyDecisionContext

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

the class DecisionService method getVariationFromExperimentRule.

public DecisionResponse<Variation> getVariationFromExperimentRule(@Nonnull ProjectConfig projectConfig, @Nonnull String flagKey, @Nonnull Experiment rule, @Nonnull OptimizelyUserContext user, @Nonnull List<OptimizelyDecideOption> options) {
    DecisionReasons reasons = DefaultDecisionReasons.newInstance();
    String ruleKey = rule != null ? rule.getKey() : null;
    // Check Forced-Decision
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
    DecisionResponse<Variation> forcedDecisionResponse = validatedForcedDecision(optimizelyDecisionContext, projectConfig, user);
    reasons.merge(forcedDecisionResponse.getReasons());
    Variation variation = forcedDecisionResponse.getResult();
    if (variation != null) {
        return new DecisionResponse(variation, reasons);
    }
    // regular decision
    DecisionResponse<Variation> decisionResponse = getVariation(rule, user, projectConfig, options);
    reasons.merge(decisionResponse.getReasons());
    variation = decisionResponse.getResult();
    return new DecisionResponse(variation, reasons);
}
Also used : DecisionResponse(com.optimizely.ab.optimizelydecision.DecisionResponse) OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) DefaultDecisionReasons(com.optimizely.ab.optimizelydecision.DefaultDecisionReasons) DecisionReasons(com.optimizely.ab.optimizelydecision.DecisionReasons)

Example 3 with OptimizelyDecisionContext

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

the class DecisionServiceTest method validatedForcedDecisionWithoutRuleKey.

@Test
public void validatedForcedDecisionWithoutRuleKey() {
    String userId = "testUser1";
    String flagKey = "multi_variate_feature";
    String variationKey = "521740985";
    OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
    OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
    DecisionResponse<Variation> response = decisionService.validatedForcedDecision(optimizelyDecisionContext, v4ProjectConfig, optimizelyUserContext);
    Variation variation = response.getResult();
    assertEquals(variationKey, variation.getKey());
}
Also used : OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyForcedDecision(com.optimizely.ab.OptimizelyForcedDecision) Test(org.junit.Test)

Example 4 with OptimizelyDecisionContext

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

the class DecisionServiceTest method validatedForcedDecisionWithRuleKey.

@Test
public void validatedForcedDecisionWithRuleKey() {
    String userId = "testUser1";
    String ruleKey = "2637642575";
    String flagKey = "multi_variate_feature";
    String variationKey = "2346257680";
    OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
    OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
    DecisionResponse<Variation> response = decisionService.validatedForcedDecision(optimizelyDecisionContext, v4ProjectConfig, optimizelyUserContext);
    Variation variation = response.getResult();
    assertEquals(variationKey, variation.getKey());
}
Also used : OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyForcedDecision(com.optimizely.ab.OptimizelyForcedDecision) Test(org.junit.Test)

Aggregations

OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)4 OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)2 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)2 DecisionReasons (com.optimizely.ab.optimizelydecision.DecisionReasons)2 DecisionResponse (com.optimizely.ab.optimizelydecision.DecisionResponse)2 DefaultDecisionReasons (com.optimizely.ab.optimizelydecision.DefaultDecisionReasons)2 Test (org.junit.Test)2