Exemplo n.º 1
0
        public static void Create(uint numActions, string modelOutputDir, int policyAction)
        {
            if (Explorer == null)
            {
                Explorer = new EpsilonGreedyExplorer <TContext>(new MartPolicy <TContext>(policyAction), LoadSettings().Epsilon, numActions);
            }

            if (Configuration == null)
            {
                Configuration = new DecisionServiceConfiguration <TContext>(appToken, Explorer)
                {
                    BlobOutputDir = modelOutputDir,
                    BatchConfig   = new BatchingConfiguration
                    {
                        MaxDuration            = TimeSpan.FromSeconds(2),
                        MaxBufferSizeInBytes   = 1024,
                        MaxEventCount          = 100,
                        MaxUploadQueueCapacity = 4,
                        UploadRetryPolicy      = BatchUploadRetryPolicy.Retry
                    }
                };
            }

            if (Service == null)
            {
                Service = new DecisionService <TContext>(Configuration);
            }

            if (!File.Exists(settingsFile))
            {
                File.WriteAllText(settingsFile, JsonConvert.SerializeObject(new DecisionServiceSettings()));
            }
        }
Exemplo n.º 2
0
        public void TestGetVariationSavesBucketedVariationIntoUserProfile()
        {
            Experiment experiment = ProjectConfig.Experiments[6];
            Variation  variation  = experiment.Variations[0];

            Decision decision = new Decision(variation.Id);

            UserProfile originalUserProfile = new UserProfile(UserProfileId,
                                                              new Dictionary <string, Decision>());

            UserProfileServiceMock.Setup(ups => ups.Lookup(UserProfileId)).Returns(originalUserProfile.ToMap());

            UserProfile expectedUserProfile = new UserProfile(UserProfileId,
                                                              new Dictionary <string, Decision>
            {
                { experiment.Id, decision }
            });

            var mockBucketer = new Mock <Bucketer>(LoggerMock.Object);

            mockBucketer.Setup(m => m.Bucket(ProjectConfig, experiment, UserProfileId, UserProfileId)).Returns(variation);

            DecisionService decisionService = new DecisionService(mockBucketer.Object, ErrorHandlerMock.Object, ProjectConfig, UserProfileServiceMock.Object, LoggerMock.Object);

            Assert.IsTrue(TestData.CompareObjects(variation, decisionService.GetVariation(experiment, UserProfileId, new UserAttributes())));

            LoggerMock.Verify(l => l.Log(LogLevel.INFO, string.Format("Saved variation \"{0}\" of experiment \"{1}\" for user \"{2}\".", variation.Id,
                                                                      experiment.Id, UserProfileId)), Times.Once);
            UserProfileServiceMock.Verify(_ => _.Save(It.IsAny <Dictionary <string, object> >()), Times.Once);
        }
Exemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void setUp()
        public virtual void setUp()
        {
            decisionService   = engineRule.DecisionService;
            repositoryService = engineRule.RepositoryService;
            historyService    = engineRule.HistoryService;
            testRule.deploy(DISH_DRG_DMN);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Optimizely constructor for managing Full Stack .NET projects.
        /// </summary>
        /// <param name="datafile">string JSON string representing the project</param>
        /// <param name="eventDispatcher">EventDispatcherInterface</param>
        /// <param name="logger">LoggerInterface</param>
        /// <param name="errorHandler">ErrorHandlerInterface</param>
        /// <param name="skipJsonValidation">boolean representing whether JSON schema validation needs to be performed</param>
        public Optimizely(string datafile,
                          IEventDispatcher eventDispatcher = null,
                          ILogger logger                        = null,
                          IErrorHandler errorHandler            = null,
                          UserProfileService userProfileService = null,
                          bool skipJsonValidation               = false)
        {
            IsValid            = false; // invalid until proven valid
            Logger             = logger ?? new NoOpLogger();
            EventDispatcher    = eventDispatcher ?? new DefaultEventDispatcher(Logger);
            ErrorHandler       = errorHandler ?? new NoOpErrorHandler();
            Bucketer           = new Bucketer(Logger);
            EventBuilder       = new EventBuilder(Bucketer, Logger);
            UserProfileService = userProfileService;
            NotificationCenter = new NotificationCenter(Logger);

            try
            {
                if (!ValidateInputs(datafile, skipJsonValidation))
                {
                    Logger.Log(LogLevel.ERROR, "Provided 'datafile' has invalid schema.");
                    return;
                }

                Config          = ProjectConfig.Create(datafile, Logger, ErrorHandler);
                IsValid         = true;
                DecisionService = new DecisionService(Bucketer, ErrorHandler, Config, userProfileService, Logger);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.ERROR, "Provided 'datafile' is in an invalid format. " + ex.Message);
            }
        }
Exemplo n.º 5
0
        public async Task TestSingleActionDSUploadMultipleEvents()
        {
            joinServer.Reset();

            string uniqueKey = "test interaction";

            var dsConfig = new DecisionServiceConfiguration(MockCommandCenter.SettingsBlobUri);

            dsConfig.JoinServerType        = JoinServerType.CustomSolution;
            dsConfig.LoggingServiceAddress = MockJoinServer.MockJoinServerAddress;

            using (var ds = DecisionService
                            .Create <TestContext>(dsConfig)
                            .ExploitUntilModelReady(new ConstantPolicy <TestContext>()))
            {
                int chosenAction1 = await ds.ChooseActionAsync(uniqueKey, new TestContext());

                int chosenAction2 = await ds.ChooseActionAsync(uniqueKey, new TestContext());

                ds.ReportReward(1.0f, uniqueKey);
                ds.ReportOutcome(JsonConvert.SerializeObject(new { value = "test outcome" }), uniqueKey);
            }

            Assert.AreEqual(4, joinServer.EventBatchList.Sum(batch => batch.ExperimentalUnitFragments.Count));
        }
Exemplo n.º 6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void setup()
        public virtual void setup()
        {
            ClockUtil.CurrentTime = TEST_DATE;
            historyService        = rule.HistoryService;
            decisionService       = rule.DecisionService;
            decisionInstanceIds   = new List <string>();
        }
Exemplo n.º 7
0
        public void TestGetVariationSavesANewUserProfile()
        {
            Experiment experiment = ProjectConfig.Experiments[6];
            Variation  variation  = experiment.Variations[0];
            Decision   decision   = new Decision(variation.Id);

            UserProfile expectedUserProfile = new UserProfile(UserProfileId, new Dictionary <string, Decision>
            {
                { experiment.Id, decision }
            });

            var mockBucketer = new Mock <Bucketer>(LoggerMock.Object);

            mockBucketer.Setup(m => m.Bucket(ProjectConfig, experiment, UserProfileId, UserProfileId)).Returns(variation);

            Dictionary <string, object> userProfile = null;

            UserProfileServiceMock.Setup(up => up.Lookup(UserProfileId)).Returns(userProfile);

            DecisionService decisionService = new DecisionService(mockBucketer.Object, ErrorHandlerMock.Object, ProjectConfig,
                                                                  UserProfileServiceMock.Object, LoggerMock.Object);

            Assert.IsTrue(TestData.CompareObjects(variation, decisionService.GetVariation(experiment, UserProfileId, new UserAttributes())));
            UserProfileServiceMock.Verify(_ => _.Save(It.IsAny <Dictionary <string, object> >()), Times.Once);
        }
Exemplo n.º 8
0
        public void TestBucketLogsCorrectlyWhenUserProfileFailsToSave()
        {
            Experiment experiment = ProjectConfig.Experiments[6];
            Variation  variation  = experiment.Variations[0];
            Decision   decision   = new Decision(variation.Id);
            Bucketer   bucketer   = new Bucketer(LoggerMock.Object);

            UserProfileServiceMock.Setup(up => up.Save(It.IsAny <Dictionary <string, object> >())).Throws(new System.Exception());

            var experimentBucketMap = new Dictionary <string, Decision>();

            experimentBucketMap[experiment.Id] = decision;

            UserProfile expectedUserProfile = new UserProfile(UserProfileId, experimentBucketMap);
            UserProfile saveUserProfile     = new UserProfile(UserProfileId, new Dictionary <string, Decision>());

            DecisionService decisionService = new DecisionService(bucketer,
                                                                  ErrorHandlerMock.Object, ProjectConfig, UserProfileServiceMock.Object, LoggerMock.Object);

            decisionService.SaveVariation(experiment, variation, saveUserProfile);

            LoggerMock.Verify(l => l.Log(LogLevel.ERROR, string.Format
                                             ("Failed to save variation \"{0}\" of experiment \"{1}\" for user \"{2}\".", variation.Id, experiment.Id, UserProfileId))
                              , Times.Once);
            ErrorHandlerMock.Verify(er => er.HandleError(It.IsAny <OptimizelySDK.Exceptions.OptimizelyRuntimeException>()), Times.Once);
        }
Exemplo n.º 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void init()
        public virtual void init()
        {
            historyService  = engineRule.HistoryService;
            decisionService = engineRule.DecisionService;

            authRule.createUserAndGroup("demo", "groupId");
        }
Exemplo n.º 10
0
        public void TestSettingsBlobPolling()
        {
            joinServer.Reset();

            commandCenter.CreateBlobs(createSettingsBlob: true, createModelBlob: false, vwArgs: "--cb_explore 2");

            var dsConfig = new DecisionServiceConfiguration(MockCommandCenter.SettingsBlobUri);

            dsConfig.JoinServerType           = JoinServerType.CustomSolution;
            dsConfig.LoggingServiceAddress    = MockJoinServer.MockJoinServerAddress;
            dsConfig.PollingForSettingsPeriod = TimeSpan.FromMilliseconds(500);

            using (var ds = DecisionService
                            .Create <TestContext>(dsConfig)
                            // TODO .WithTopSlotEpsilonGreedy(.2f)
                            .ExploitUntilModelReady(new ConstantPolicy <TestContext>()))
            {
                byte[] settingsBytes = null;

                dsConfig.SettingsPollSuccessCallback = data => settingsBytes = data;

                for (int i = 0; i < 50 && settingsBytes == null; i++)
                {
                    Thread.Sleep(100);
                    if (i % 2 == 0)
                    {
                        // change the settings blob's etag frequently to make sure polling detects it
                        commandCenter.CreateBlobs(createSettingsBlob: true, createModelBlob: false, vwArgs: "abc", initialExplorationEpsilon: 0.4f);
                    }
                }

                Assert.IsNotNull(settingsBytes);
                Assert.IsTrue(Enumerable.SequenceEqual(settingsBytes, commandCenter.GetSettingsBlobContent("abc", 0.4f)));
            }
        }
Exemplo n.º 11
0
        public void TestGetForcedVariationReturnsNullWhenUserIsNotWhitelisted()
        {
            Bucketer        bucketer        = new Bucketer(LoggerMock.Object);
            DecisionService decisionService = new DecisionService(bucketer, ErrorHandlerMock.Object, ProjectConfig, null, LoggerMock.Object);

            Assert.IsNull(decisionService.GetWhitelistedVariation(WhitelistedExperiment, GenericUserId));
        }
Exemplo n.º 12
0
        public void TestMultiActionDSUploadSingleEvent()
        {
            joinServer.Reset();

            string uniqueKey = "test interaction";

            var dsConfig = new DecisionServiceConfiguration(MockCommandCenter.SettingsBlobUri);

            dsConfig.JoinServerType        = JoinServerType.CustomSolution;
            dsConfig.LoggingServiceAddress = MockJoinServer.MockJoinServerAddress;

            int[] chosenActions;
            using (var ds = DecisionService.Create <TestContext>(dsConfig)
                            //.WithTopSlotEpsilonGreedy(.2f)
                            .ExploitUntilModelReady(new ConstantPolicy <TestContext>()))
            {
                chosenActions = ds.ChooseRanking(uniqueKey, new TestContext());
            }

            Assert.AreEqual(1, joinServer.RequestCount);
            Assert.AreEqual(1, joinServer.EventBatchList.Count);
            Assert.AreEqual(1, joinServer.EventBatchList[0].ExperimentalUnitFragments.Count);
            Assert.AreEqual(uniqueKey, joinServer.EventBatchList[0].ExperimentalUnitFragments[0].Id);
            Assert.IsTrue(joinServer.EventBatchList[0].ExperimentalUnitFragments[0].Value.ToLower().Contains("\"a\":[" + string.Join(",", chosenActions) + "],"));
        }
Exemplo n.º 13
0
        public void TestSingleActionDSUploadSingleEvent()
        {
            joinServer.Reset();

            string uniqueKey = "test interaction";

            commandCenter.CreateBlobs(createSettingsBlob: true, createModelBlob: false, vwArgs: "--cb_explore_adf --epsilon 0.5");

            var dsConfig = new DecisionServiceConfiguration(MockCommandCenter.SettingsBlobUri);

            dsConfig.JoinServerType        = JoinServerType.CustomSolution;
            dsConfig.LoggingServiceAddress = MockJoinServer.MockJoinServerAddress;

            int chosenAction;

            using (var ds = DecisionService
                            .Create <TestContext>(dsConfig)
                            .ExploitUntilModelReady(new ConstantPolicy <TestContext>()))
            {
                chosenAction = ds.ChooseAction(uniqueKey, new TestContext());
            }

            Assert.AreEqual(1, joinServer.RequestCount);
            Assert.AreEqual(1, joinServer.EventBatchList.Count);
            Assert.AreEqual(1, joinServer.EventBatchList[0].ExperimentalUnitFragments.Count);
            Assert.AreEqual(uniqueKey, joinServer.EventBatchList[0].ExperimentalUnitFragments[0].Id);
            Assert.IsTrue(joinServer.EventBatchList[0].ExperimentalUnitFragments[0].Value.ToLower().Contains("\"a\":[" + chosenAction + ","));
        }
Exemplo n.º 14
0
        public void TestGetVariationForFeatureRolloutWhenUserIsNotBucketedInTheTargetingRuleButBucketedToEveryoneElseRule()
        {
            var featureFlag      = ProjectConfig.GetFeatureFlagFromKey("boolean_single_variable_feature");
            var rolloutId        = featureFlag.RolloutId;
            var rollout          = ProjectConfig.GetRolloutFromId(rolloutId);
            var experiment       = rollout.Experiments[0];
            var everyoneElseRule = rollout.Experiments[rollout.Experiments.Count - 1];
            var variation        = everyoneElseRule.Variations[0];
            var expectedDecision = new FeatureDecision(everyoneElseRule, variation, FeatureDecision.DECISION_SOURCE_ROLLOUT);

            var userAttributes = new UserAttributes {
                { "browser_type", "chrome" }
            };

            BucketerMock.Setup(bm => bm.Bucket(It.IsAny <ProjectConfig>(), experiment, It.IsAny <string>(), It.IsAny <string>())).Returns <Variation>(null);
            BucketerMock.Setup(bm => bm.Bucket(It.IsAny <ProjectConfig>(), everyoneElseRule, It.IsAny <string>(), It.IsAny <string>())).Returns(variation);
            var decisionService = new DecisionService(BucketerMock.Object, ErrorHandlerMock.Object, ProjectConfig, null, LoggerMock.Object);

            var actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, "user_1", userAttributes);

            Assert.IsTrue(TestData.CompareObjects(expectedDecision, actualDecision));

            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, $"Attempting to bucket user \"user_1\" into rollout rule \"{experiment.Key}\"."));
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "User \"user_1\" is excluded due to traffic allocation. Checking \"Eveyrone Else\" rule now."));
        }
Exemplo n.º 15
0
        public void TestBatchingByTime()
        {
            var serviceConfig = new DecisionServiceConfiguration<TestContext>("",
                new EpsilonGreedyExplorer<TestContext>(new TestPolicy(), epsilon: 0.2f, numActions: 10))
            {
                BatchConfig = new BatchingConfiguration()
                {
                    MaxDuration = TimeSpan.FromMilliseconds(100),
                    MaxEventCount = 2,
                    MaxBufferSizeInBytes = 10 * 1024 * 1024
                }
            };

            var service = new DecisionService<TestContext>(serviceConfig);

            string uniqueKey = "eventid";

            service.ChooseAction(uniqueKey, new TestContext());
            
            System.Threading.Thread.Sleep(200);

            service.ChooseAction(uniqueKey, new TestContext());

            var batch = JsonConvert.DeserializeObject<EventBatch>(File.ReadAllText(batchOutputFile), new EventJsonConverter());
            Assert.AreEqual(1, batch.Events.Count);
        }
Exemplo n.º 16
0
        public void TestDevModeSettingsAndExampleLog()
        {
            joinServer.Reset();

            var testTraceListener = new TestTraceListener();

            Trace.Listeners.Add(testTraceListener);

            string vwArgs = "--cb_explore_adf --epsilon 0.5";

            commandCenter.CreateBlobs(createSettingsBlob: true, createModelBlob: false, vwArgs: vwArgs);

            var dsConfig = new DecisionServiceConfiguration(MockCommandCenter.SettingsBlobUri)
            {
                JoinServerType           = JoinServerType.CustomSolution,
                LoggingServiceAddress    = MockJoinServer.MockJoinServerAddress,
                PollingForModelPeriod    = TimeSpan.MinValue,
                PollingForSettingsPeriod = TimeSpan.MinValue,
                DevelopmentMode          = true
            };

            int numInteractionEvents = 25;
            var eventIdList          = new List <string>();

            using (var ds = DecisionService
                            .Create <TestADFContextWithFeatures>(dsConfig)
                            // .With<TestADFContextWithFeatures, TestADFFeatures>(context => context.ActionDependentFeatures)
                            // TODO .WithTopSlotEpsilonGreedy(.5f)
                            .ExploitUntilModelReady(new ConstantPolicy <TestADFContextWithFeatures>(ctx => ctx.ActionDependentFeatures.Count)))
            {
                byte[] modelContent = commandCenter.GetCBADFModelBlobContent(numExamples: 5, numFeatureVectors: 10, vwDefaultArgs: vwArgs);
                using (var modelStream = new MemoryStream(modelContent))
                {
                    ds.UpdateModel(modelStream);
                }
                for (int i = 1; i <= numInteractionEvents; i++)
                {
                    var    interId    = "inter" + i;
                    var    obserId    = "obser" + i;
                    Random rg         = new Random(i);
                    int    numActions = rg.Next(5, 20);
                    var    context    = TestADFContextWithFeatures.CreateRandom(numActions, rg);
                    int[]  action     = ds.ChooseRanking(interId, context);
                    ds.ReportReward(i / 100f, obserId);

                    eventIdList.Add(interId);
                    eventIdList.Add(obserId);
                }
            }
            // Number of batches must be exactly equal to number of events uploaded in development mode
            // and each batch must contain exactly one event
            Assert.AreEqual(numInteractionEvents * 2, joinServer.EventBatchList.Count);
            Assert.AreEqual(numInteractionEvents * 2, joinServer.EventBatchList.Sum(ebl => ebl.ExperimentalUnitFragments.Count));
            var eblList = joinServer.EventBatchList.Select(ebl => ebl.ExperimentalUnitFragments[0].Id).OrderBy(id => id);

            Assert.IsTrue(eblList.SequenceEqual(eventIdList.OrderBy(id => id)));

            // Trace messages must contain context information
            Assert.AreEqual(numInteractionEvents, testTraceListener.Messages.Count(m => m.Contains("Example Context")));
        }
Exemplo n.º 17
0
        public static void SampleCodeUsingASAJoinServer()
        {
            // Create configuration for the decision service
            var serviceConfig = new DecisionServiceConfiguration<ExpandedContext, ExpandedActionDependentFeatures>(
                authorizationToken: "sample-code",
                explorer: new EpsilonGreedyExplorer<ExpandedContext>(new ExpandedPolicy(), epsilon: 0.8f))
            {
                EventHubConnectionString = "",
                EventHubInputName = "",
                GetContextFeaturesFunc = ExpandedContext.GetFeaturesFromContext
            };

            var service = new DecisionService<ExpandedContext, ExpandedActionDependentFeatures>(serviceConfig);

            //string uniqueKey = "sample-asa-client-";
            string uniqueKey = "scratch-key-";

            var rg = new Random(uniqueKey.GetHashCode());

            for (int i = 1; i < 20; i++)
            {
                int numActions = rg.Next(5, 10);

                DateTime timeStamp = DateTime.UtcNow;
                string key = uniqueKey + Guid.NewGuid().ToString();

                uint[] action = service.ChooseAction(new UniqueEventID { Key = key, TimeStamp = timeStamp }, ExpandedContext.CreateRandom(numActions, rg), (uint)numActions);
                service.ReportReward(i / 100f, new UniqueEventID { Key = key, TimeStamp = timeStamp });

                System.Threading.Thread.Sleep(1);
            }

            service.Flush();
        }
Exemplo n.º 18
0
        public void SetUp()
        {
            _decisionBlobStorage = new Mock <IDecisionBlobStorageRepository>();
            _repository          = new Mock <IRepositoryWrapper>();
            var mapper = new Mock <IMapper>();

            _decisionVmCreator = new Mock <IDecisionVmInitializer>();
            _uniqueId          = new Mock <IUniqueIdService>();
            _repository.Setup(rep => rep.Decesion.Attach(new Decesion()));
            _repository.Setup(rep => rep.Decesion.Create(new Decesion()));
            _repository.Setup(rep => rep.Decesion.Update(new Decesion()));
            _repository.Setup(rep => rep.SaveAsync());
            _repository.Setup(rep => rep.DecesionTarget.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <DecesionTarget, bool> > >(),
                                                                               It.IsAny <Func <IQueryable <DecesionTarget>, IIncludableQueryable <DecesionTarget, object> > >())).ReturnsAsync(new DecesionTarget());
            mapper.Setup(m => m.Map <IEnumerable <DecisionTargetDTO> >(It.IsAny <IEnumerable <DecesionTarget> >()))
            .Returns(GetTestDecisionTargetsDtoList);
            mapper.Setup(m => m.Map <DecisionDTO>(It.IsAny <Decesion>()))
            .Returns(() => GetTestDecisionsDtoListElement());
            mapper.Setup(m => m.Map <IEnumerable <DecisionDTO> >(It.IsAny <IEnumerable <Decesion> >())).Returns(GetTestDecisionsDtoList);
            mapper.Setup(m => m.Map <OrganizationDTO>(It.IsAny <Organization>()))
            .Returns(GetTestOrganizationDtoList()[0]);
            mapper.Setup(m => m.Map <IEnumerable <OrganizationDTO> >(It.IsAny <IEnumerable <Organization> >()))
            .Returns(GetTestOrganizationDtoList());
            _decisionService = new DecisionService(_repository.Object, mapper.Object, _decisionVmCreator.Object, _decisionBlobStorage.Object, _uniqueId.Object);
        }
Exemplo n.º 19
0
        public void IndexAsync()
        {
            AsyncManager.OutstandingOperations.Increment();

            var explorer = new EpsilonGreedyExplorer <string>(new MartPolicy(), epsilon: .2f, numActions: 10);

            var serviceConfig = new DecisionServiceConfiguration <string>(
                authorizationToken: "c01ff675-5710-4814-a961-d03d2d6bce65",
                explorer: explorer);

            var service = new DecisionService <string>(serviceConfig);

            var rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                int    context   = rand.Next(100);
                string uniqueKey = i.ToString();
                service.ChooseAction(uniqueKey, context.ToString());
                service.ReportReward((float)(context % 2), uniqueKey);
            }
            service.Flush();

            AsyncManager.OutstandingOperations.Decrement();
        }
Exemplo n.º 20
0
        public void TestGetVariationForFeatureRolloutCheckAudienceInEveryoneElseRule()
        {
            var featureFlag      = ProjectConfig.GetFeatureFlagFromKey("boolean_single_variable_feature");
            var rolloutId        = featureFlag.RolloutId;
            var rollout          = ProjectConfig.GetRolloutFromId(rolloutId);
            var everyoneElseRule = rollout.Experiments[2];
            var variation        = everyoneElseRule.Variations[0];
            var expectedDecision = new FeatureDecision(everyoneElseRule, variation, FeatureDecision.DECISION_SOURCE_ROLLOUT);

            BucketerMock.Setup(bm => bm.Bucket(It.IsAny <ProjectConfig>(), everyoneElseRule, It.IsAny <string>(), WhitelistedUserId)).Returns(variation);
            BucketerMock.Setup(bm => bm.Bucket(It.IsAny <ProjectConfig>(), everyoneElseRule, It.IsAny <string>(), GenericUserId)).Returns <Variation>(null);
            var decisionService = new DecisionService(BucketerMock.Object, ErrorHandlerMock.Object, ProjectConfig, null, LoggerMock.Object);

            // Returned variation id should be of everyone else rule as it passes audience Id checking.
            var actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, WhitelistedUserId, null);

            Assert.True(TestData.CompareObjects(expectedDecision, actualDecision));

            // Returned variation id should be null.
            actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, GenericUserId, null);
            Assert.Null(actualDecision);

            // Returned variation id should be null as it fails audience Id checking.
            everyoneElseRule.AudienceIds = new string[] { ProjectConfig.Audiences[0].Id };
            actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, GenericUserId, null);
            Assert.Null(actualDecision);

            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "User \"testUser1\" does not meet the conditions to be in rollout rule for audience \"Chrome users\"."), Times.Once);
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "User \"testUser1\" does not meet the conditions to be in rollout rule for audience \"iPhone users in San Francisco\"."), Times.Once);
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "User \"genericUserId\" does not meet the conditions to be in rollout rule for audience \"Chrome users\"."), Times.Exactly(2));
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "User \"genericUserId\" does not meet the conditions to be in rollout rule for audience \"iPhone users in San Francisco\"."), Times.Exactly(3));
        }
Exemplo n.º 21
0
        public ListResponeMessage <DecisionInfo> GetListWithCondition(
            string decisionCode = "",
            string quoteCode    = "", int customerID    = 0,
            DateTime?fromDate   = null, DateTime?toDate = null, int pageSize = 10, int pageIndex = 0, string _userID = "")
        {
            ListResponeMessage <DecisionInfo> ret = new ListResponeMessage <DecisionInfo>();

            try
            {
                DecisionSeachCriteria _criteria = new DecisionSeachCriteria();
                _criteria.DecisionCode = decisionCode;
                _criteria.QuoteCode    = quoteCode;
                _criteria.CustomerID   = customerID;
                _criteria.FromDate     = fromDate;
                _criteria.ToDate       = toDate;
                _criteria.pageSize     = pageSize;
                _criteria.pageIndex    = pageIndex;
                ret.isSuccess          = true;
                ret.data         = DecisionService.GetInstance().getAllDecision(pageSize, pageIndex, _criteria, _userID);
                ret.totalRecords = DecisionService.GetInstance().getTotalRecords(_criteria, _userID);
            }
            catch (Exception ex)
            {
                ret.isSuccess     = false;
                ret.err.msgCode   = "005";
                ret.err.msgString = ex.ToString();
            }
            return(ret);
        }
Exemplo n.º 22
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void setup()
        public virtual void setup()
        {
            historyService      = engineRule.HistoryService;
            decisionService     = engineRule.DecisionService;
            managementService   = engineRule.ManagementService;
            decisionInstanceIds = new List <string>();
        }
Exemplo n.º 23
0
        public SingleResponeMessage <DecisionInfo> Get(int id, string _userID)
        {
            SingleResponeMessage <DecisionInfo> ret = new SingleResponeMessage <DecisionInfo>();

            try
            {
                DecisionInfo item = DecisionService.GetInstance().GetDecision(id, _userID);
                if (item == null)
                {
                    ret.isSuccess     = false;
                    ret.err.msgCode   = "001";
                    ret.err.msgString = "no Decision found";
                    return(ret);
                }
                ret.item      = item;
                ret.isSuccess = true;
            }
            catch (Exception ex)
            {
                ret.isSuccess     = false;
                ret.err.msgCode   = "Internal Error !!!";
                ret.err.msgString = ex.ToString();
            }
            return(ret);
        }
Exemplo n.º 24
0
        public async Task <ActionMessage> Put(int id, [FromForm] DecisionInfo _Decision, [FromForm] List <IFormFile> files, string _userID)
        {
            ActionMessage ret = new ActionMessage();

            try
            {
                ret = DecisionService.GetInstance().editDecision(id, _Decision, GetUserId(), _userID);
                //update list file
                DocumentService.GetInstance().DeleteDocumentsNotExitsInList(_Decision.ListDocument, TableFile.Decision.ToString(), id);
                foreach (var item in files)
                {
                    DocumentInfo documentInfo = new DocumentInfo();
                    documentInfo.TableName = TableFile.Decision.ToString();
                    documentInfo.PreferId  = id.ToString();
                    documentInfo.FileName  = item.FileName;
                    documentInfo.Link      = DateTime.Now.ToString("yyMMddHHmmssfff") + "-" + Utils.ChuyenTVKhongDau(item.FileName);
                    documentInfo.Length    = item.Length.ToString();
                    documentInfo.Type      = item.ContentType;
                    ret = await FilesHelpers.UploadFile(TableFile.Decision.ToString(), _Decision.DecisionID.ToString(), item, documentInfo.Link);

                    DocumentService.GetInstance().InsertDocument(documentInfo, GetUserId());
                }
            }
            catch (Exception ex)
            {
                ret.isSuccess     = false;
                ret.err.msgCode   = "Internal Error !!!";
                ret.err.msgString = ex.ToString();
            }
            return(ret);
        }
Exemplo n.º 25
0
        public async Task <ActionMessage> PostwithAttFile([FromForm] DecisionInfo DecisionObj, [FromForm] List <IFormFile> files)
        {
            ActionMessage ret     = new ActionMessage();
            int           insetId = -1;

            try
            {
                insetId       = DecisionService.GetInstance().createDecision2(DecisionObj, GetUserId());
                ret.isSuccess = true;
                if (insetId > -1)
                {
                    ret.id = insetId;
                    foreach (var item in files)
                    {
                        DocumentInfo documentInfo = new DocumentInfo();
                        documentInfo.TableName = TableFile.Decision.ToString();
                        documentInfo.PreferId  = insetId.ToString();
                        documentInfo.Link      = DateTime.Now.ToString("yyMMddHHmmssfff") + "-" + Utils.ChuyenTVKhongDau(item.FileName);
                        documentInfo.FileName  = item.FileName;
                        documentInfo.Length    = item.Length.ToString();
                        documentInfo.Type      = item.ContentType;
                        ret = await FilesHelpers.UploadFile(TableFile.Decision.ToString(), insetId.ToString(), item, documentInfo.Link);

                        DocumentService.GetInstance().InsertDocument(documentInfo, GetUserId());
                    }
                }
            }
            catch (Exception ex)
            {
                ret.isSuccess     = false;
                ret.err.msgCode   = "Internal Error !!!";
                ret.err.msgString = ex.ToString();
            }
            return(ret);
        }
Exemplo n.º 26
0
        public void TestGetVariationEvaluatesUserProfileBeforeAudienceTargeting()
        {
            Experiment experiment = ProjectConfig.Experiments[8];
            Variation  variation  = experiment.Variations[0];

            Decision    decision    = new Decision(variation.Id);
            UserProfile userProfile = new UserProfile(UserProfileId, new Dictionary <string, Decision>
            {
                { experiment.Id, decision }
            });

            UserProfileServiceMock.Setup(up => up.Lookup(WhitelistedUserId)).Returns(userProfile.ToMap());

            DecisionService decisionService = new DecisionService(BucketerMock.Object, ErrorHandlerMock.Object, ProjectConfig, UserProfileServiceMock.Object, LoggerMock.Object);

            decisionService.GetVariation(experiment, GenericUserId, new UserAttributes());

            LoggerMock.Verify(l => l.Log(LogLevel.INFO, string.Format("User \"{0}\" does not meet conditions to be in experiment \"{1}\".",
                                                                      GenericUserId, experiment.Key)), Times.Once);

            // ensure that a user with a saved user profile, sees the same variation regardless of audience evaluation
            decisionService.GetVariation(experiment, UserProfileId, new UserAttributes());

            BucketerMock.Verify(_ => _.Bucket(It.IsAny <ProjectConfig>(), It.IsAny <Experiment>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
Exemplo n.º 27
0
        public void TestInvalidAnswer()
        {
            IDataSource      dataSource = new DataSource.DataSource(BuildStaticDoughnutTree());
            IDecisionService service    = new DecisionService(dataSource);

            Assert.Throws <NodeNotFoundException>(() => service.GetCurrentQuestion(new List <bool> {
                false, false
            }));
            Assert.Throws <NodeNotFoundException>(() => service.GetCurrentQuestion(new List <bool> {
                true, true, true, true
            }));
            Assert.Throws <NodeNotFoundException>(() => service.GetCurrentQuestion(new List <bool> {
                true, true, false, true
            }));
            Assert.Throws <NodeNotFoundException>(() => service.GetCurrentQuestion(new List <bool> {
                true, true, false, false
            }));
            Assert.Throws <NodeNotFoundException>(() => service.GetCurrentQuestion(new List <bool> {
                true, false, true, false
            }));
            Assert.Throws <NodeNotFoundException>(() => service.GetCurrentQuestion(new List <bool> {
                true, false, false, false
            }));

            //Assert.(3, _service.GetTreeMaxpath());
        }
Exemplo n.º 28
0
        public void TestGetVariationForFeatureRolloutAudienceAndTrafficeAllocationCheck()
        {
            var featureFlag = ProjectConfig.GetFeatureFlagFromKey("boolean_single_variable_feature");
            var rolloutId   = featureFlag.RolloutId;
            var rollout     = ProjectConfig.GetRolloutFromId(rolloutId);
            var expWithAudienceiPhoneUsers = rollout.Experiments[1];
            var expWithAudienceChromeUsers = rollout.Experiments[0];
            var expWithNoAudience          = rollout.Experiments[2];
            var varWithAudienceiPhoneUsers = expWithAudienceiPhoneUsers.Variations[0];
            var varWithAudienceChromeUsers = expWithAudienceChromeUsers.Variations[0];
            var varWithNoAudience          = expWithNoAudience.Variations[0];

            var mockBucketer = new Mock <Bucketer>(LoggerMock.Object)
            {
                CallBase = true
            };

            mockBucketer.Setup(bm => bm.GenerateBucketValue(It.IsAny <string>())).Returns(980);
            var decisionService = new DecisionService(mockBucketer.Object, ErrorHandlerMock.Object, ProjectConfig, null, LoggerMock.Object);

            // Calling with audience iPhone users in San Francisco.
            var actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, GenericUserId, new UserAttributes
            {
                { "device_type", "iPhone" },
                { "location", "San Francisco" }
            });

            // Returned variation id should be '177773' because of audience 'iPhone users in San Francisco'.
            var expectedDecision = new FeatureDecision(expWithAudienceiPhoneUsers, varWithAudienceiPhoneUsers, FeatureDecision.DECISION_SOURCE_ROLLOUT);

            Assert.IsTrue(TestData.CompareObjects(expectedDecision, actualDecision));

            // Calling with audience Chrome users.
            actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, GenericUserId, new UserAttributes
            {
                { "browser_type", "chrome" }
            });

            // Returned variation id should be '177771' because of audience 'Chrome users'.
            expectedDecision = new FeatureDecision(expWithAudienceChromeUsers, varWithAudienceChromeUsers, FeatureDecision.DECISION_SOURCE_ROLLOUT);
            Assert.IsTrue(TestData.CompareObjects(expectedDecision, actualDecision));

            // Calling with no audience.
            mockBucketer.Setup(bm => bm.GenerateBucketValue(It.IsAny <string>())).Returns(8000);
            actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, GenericUserId, new UserAttributes());

            // Returned variation id should be of everyone else rule because of no audience.
            expectedDecision = new FeatureDecision(expWithNoAudience, varWithNoAudience, FeatureDecision.DECISION_SOURCE_ROLLOUT);
            Assert.IsTrue(TestData.CompareObjects(expectedDecision, actualDecision));

            // Calling with audience 'Chrome users' and traffice allocation '9500'.
            mockBucketer.Setup(bm => bm.GenerateBucketValue(It.IsAny <string>())).Returns(9500);
            actualDecision = decisionService.GetVariationForFeatureRollout(featureFlag, GenericUserId, new UserAttributes
            {
                { "browser_type", "chrome" }
            });

            // Returned decision entity should be null because bucket value exceeds traffice allocation of everyone else rule.
            Assert.Null(actualDecision);
        }
Exemplo n.º 29
0
        public async Task GetDecisionTest(int decisionId)
        {
            _decisionService = CreateDecisionService(decisionId);

            var decision = await _decisionService.GetDecisionAsync(decisionId);

            Assert.Equal(decisionId, decision.ID);
        }
Exemplo n.º 30
0
        public SingleResponeMessage <DecisionInfo> GetByCode(string code, string _userID)
        {
            SingleResponeMessage <DecisionInfo> ret = new SingleResponeMessage <DecisionInfo>();

            ret.isSuccess = true;
            ret.item      = DecisionService.GetInstance().GetDecisionByCode(code, _userID);
            return(ret);
        }
Exemplo n.º 31
0
        public virtual bool evaluate(DelegateCaseExecution caseExecution)
        {
            DecisionService        decisionService = caseExecution.ProcessEngineServices.DecisionService;
            DmnDecisionTableResult result          = evaluateDecision(decisionService, caseExecution);
            DmnDecisionRuleResult  singleResult    = result.SingleResult;

            return((bool?)singleResult.SingleEntry.Value);
        }
Exemplo n.º 32
0
        public void TestFirstQuestion()
        {
            IDataSource      dataSource = new DataSource.DataSource(BuildStaticDoughnutTree());
            IDecisionService _service   = new DecisionService(dataSource);

            Assert.Equal(dataSource.DecisionTree.Statement, _service.GetFirstQuestion());
            //Assert.(3, _service.GetTreeMaxpath());
        }
        public void TestADFExplorationResult()
        {
            joinServer.Reset();

            var dsConfig = new DecisionServiceConfiguration<TestADFContext, DummyADFType>(
                authorizationToken: MockCommandCenter.AuthorizationToken,
                explorer: new EpsilonGreedyExplorer<TestADFContext>(new TestADFPolicy(), epsilon: 0.5f))
            {
                PollingForModelPeriod = TimeSpan.MinValue,
                PollingForSettingsPeriod = TimeSpan.MinValue,
                JoinServerType = JoinServerType.CustomSolution,
                LoggingServiceAddress = MockJoinServer.MockJoinServerAddress
            };

            var ds = new DecisionService<TestADFContext, DummyADFType>(dsConfig);

            string uniqueKey = "eventid";

            for (int i = 1; i <= 100; i++)
            {
                var adfContext = new TestADFContext(i);
                uint[] action = ds.ChooseAction(new UniqueEventID { Key = uniqueKey }, adfContext, (uint)adfContext.ActionDependentFeatures.Count);

                Assert.AreEqual(i, action.Length);

                // verify all unique actions in the list
                Assert.AreEqual(action.Length, action.Distinct().Count());

                // verify the actions are in the expected range
                Assert.AreEqual((i * (i + 1)) / 2, action.Sum(a => a));

                ds.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey });
            }

            ds.Flush();

            Assert.AreEqual(200, joinServer.EventBatchList.Sum(b => b.ExperimentalUnitFragments.Count));
        }
Exemplo n.º 34
0
        public void TestBatchingByCount()
        {
            var serviceConfig = new DecisionServiceConfiguration<TestContext>("",
                new EpsilonGreedyExplorer<TestContext>(new TestPolicy(), epsilon: 0.2f, numActions: 10))
            {
                BatchConfig = new BatchingConfiguration()
                {
                    MaxDuration = TimeSpan.FromDays(30),
                    MaxEventCount = 2,
                    MaxBufferSizeInBytes = 10 * 1024 * 1024
                }
            };

            var service = new DecisionService<TestContext>(serviceConfig);

            string uniqueKey = "eventid";

            service.ChooseAction(uniqueKey, new TestContext());
            service.ReportOutcome("my json outcome", uniqueKey);
            service.ReportReward(0.5f, uniqueKey);

            var batch = JsonConvert.DeserializeObject<EventBatch>(File.ReadAllText(batchOutputFile), new EventJsonConverter());
            Assert.AreEqual(2, batch.Events.Count);
        }
Exemplo n.º 35
0
        public void TestRcv1ModelUpdateFromStream()
        {
            joinServer.Reset();

            int numActions = 10;
            int numFeatures = 1024;

            var dsConfig = new DecisionServiceConfiguration<TestRcv1Context>(
                authorizationToken: MockCommandCenter.AuthorizationToken,
                explorer: new EpsilonGreedyExplorer<TestRcv1Context>(new TestRcv1ContextPolicy(), epsilon: 0.5f, numActions: (uint)numActions))
            {
                JoinServerType = JoinServerType.CustomSolution,
                LoggingServiceAddress = MockJoinServer.MockJoinServerAddress,
                PollingForModelPeriod = TimeSpan.MinValue,
                PollingForSettingsPeriod = TimeSpan.MinValue
            };

            var ds = new DecisionService<TestRcv1Context>(dsConfig);

            string uniqueKey = "eventid";

            string modelFile = "test_vw_adf{0}.model";

            for (int i = 1; i <= 100; i++)
            {
                Random rg = new Random(i);

                if (i % 50 == 1)
                {
                    int modelIndex = i / 50;
                    string currentModelFile = string.Format(modelFile, modelIndex);

                    byte[] modelContent = commandCenter.GetCBModelBlobContent(numExamples: 3 + modelIndex, numFeatures: numFeatures, numActions: numActions);

                    var modelStream = new MemoryStream(modelContent);

                    ds.UpdatePolicy(new VWPolicy<TestRcv1Context>(modelStream));
                }

                var context = TestRcv1Context.CreateRandom(numActions, numFeatures, rg);

                DateTime timeStamp = DateTime.UtcNow;

                uint action = ds.ChooseAction(new UniqueEventID { Key = uniqueKey }, context);

                // verify the actions are in the expected range
                Assert.IsTrue(action >= 1 && action <= numActions);

                ds.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey });
            }

            ds.Flush();

            Assert.AreEqual(200, joinServer.EventBatchList.Sum(b => b.ExperimentalUnitFragments.Count));
        }
        public void TestADFModelUpdateFromFile()
        {
            joinServer.Reset();

            var dsConfig = new DecisionServiceConfiguration<TestADFContextWithFeatures, TestADFFeatures>(
                authorizationToken: MockCommandCenter.AuthorizationToken,
                explorer: new EpsilonGreedyExplorer<TestADFContextWithFeatures>(new TestADFWithFeaturesPolicy(), epsilon: 0.5f))
            {
                JoinServerType = JoinServerType.CustomSolution,
                LoggingServiceAddress = MockJoinServer.MockJoinServerAddress,
                PollingForModelPeriod = TimeSpan.MinValue,
                PollingForSettingsPeriod = TimeSpan.MinValue
            };

            var ds = new DecisionService<TestADFContextWithFeatures, TestADFFeatures>(dsConfig);

            string uniqueKey = "eventid";

            string modelFile = "test_vw_adf{0}.model";
            var actualModelFiles = new List<string>();

            for (int i = 1; i <= 100; i++)
            {
                Random rg = new Random(i);

                if (i % 50 == 1)
                {
                    int modelIndex = i / 50;
                    string currentModelFile = string.Format(modelFile, modelIndex);

                    byte[] modelContent = commandCenter.GetCBADFModelBlobContent(numExamples: 3 + modelIndex, numFeatureVectors: 4 + modelIndex);
                    System.IO.File.WriteAllBytes(currentModelFile, modelContent);

                    ds.UpdatePolicy(new VWPolicy<TestADFContextWithFeatures, TestADFFeatures>(GetFeaturesFromContext, currentModelFile));

                    actualModelFiles.Add(currentModelFile);
                }

                int numActions = rg.Next(5, 20);
                var context = TestADFContextWithFeatures.CreateRandom(numActions, rg);

                uint[] action = ds.ChooseAction(new UniqueEventID { Key = uniqueKey }, context, (uint)context.ActionDependentFeatures.Count);

                Assert.AreEqual(numActions, action.Length);

                // verify all unique actions in the list
                Assert.AreEqual(action.Length, action.Distinct().Count());

                // verify the actions are in the expected range
                Assert.AreEqual((numActions * (numActions + 1)) / 2, action.Sum(a => a));

                ds.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey });
            }

            ds.Flush();

            Assert.AreEqual(200, joinServer.EventBatchList.Sum(b => b.ExperimentalUnitFragments.Count));

            foreach (string actualModelFile in actualModelFiles)
            {
                System.IO.File.Delete(actualModelFile);
            }
        }
Exemplo n.º 37
0
        public static void SampleCodeUsingActionDependentFeatures()
        {
            // Create configuration for the decision service
            var serviceConfig = new DecisionServiceConfiguration<ADFContext, ADFFeatures>(
                authorizationToken: "",
                explorer: new EpsilonGreedyExplorer<ADFContext>(new ADFPolicy(), epsilon: 0.8f))
            {
                PollingForModelPeriod = TimeSpan.MinValue,
                PollingForSettingsPeriod = TimeSpan.MinValue
            };

            var service = new DecisionService<ADFContext, ADFFeatures>(serviceConfig);

            string uniqueKey = "eventid";

            var rg = new Random(uniqueKey.GetHashCode());

            var vwPolicy = new VWPolicy<ADFContext, ADFFeatures>(GetFeaturesFromContext);

            for (int i = 1; i < 100; i++)
            {
                if (i == 30)
                {
                    string vwModelFile = TrainNewVWModelWithRandomData(numExamples: 5, numActions: 10);

                    vwPolicy = new VWPolicy<ADFContext, ADFFeatures>(GetFeaturesFromContext, vwModelFile);

                    // Alternatively, VWPolicy can also be loaded from an IO stream:
                    // var vwModelStream = new MemoryStream(File.ReadAllBytes(vwModelFile));
                    // vwPolicy = new VWPolicy<ADFContext, ADFFeatures>(GetFeaturesFromContext, vwModelStream);

                    // Manually updates decision service with a new policy for consuming VW models.
                    service.UpdatePolicy(vwPolicy);
                }
                if (i == 60)
                {
                    string vwModelFile = TrainNewVWModelWithRandomData(numExamples: 6, numActions: 8);

                    // Evolves the existing VWPolicy with a new model
                    vwPolicy.ModelUpdate(vwModelFile);
                }

                int numActions = rg.Next(5, 10);
                uint[] action = service.ChooseAction(new UniqueEventID { Key = uniqueKey }, ADFContext.CreateRandom(numActions, rg), (uint)numActions);
                service.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey });
            }

            service.Flush();
        }