Exemple #1
0
        private static LiveModel CreateLiveModelOrExit(string clientJsonPath)
        {
            if (!File.Exists(clientJsonPath))
            {
                WriteErrorAndExit($"Could not find file with path '{clientJsonPath}'.");
            }

            string json = File.ReadAllText(clientJsonPath);

            ApiStatus apiStatus = new ApiStatus();

            Configuration config;

            if (!Configuration.TryLoadConfigurationFromJson(json, out config, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            LiveModel liveModel = new LiveModel(config);

            if (!liveModel.TryInit(apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            liveModel.BackgroundError += LiveModel_BackgroundError;

            return(liveModel);
        }
Exemple #2
0
        // TODO: Pull this out to a separate sample once we implement the simulator in this.
        public static void BasicUsageExample(string [] args)
        {
            const float  outcome     = 1.0f;
            const string eventId     = "event_id";
            const string contextJson = "{'GUser':{'id':'a','major':'eng','hobby':'hiking'},'_multi':[ { 'TAction':{'a1':'f1'} },{'TAction':{'a2':'f2'}}]}";

            if (args.Length != 1)
            {
                WriteErrorAndExit("Missing path to client configuration json");
            }

            if (!File.Exists(args[0]))
            {
                WriteErrorAndExit($"Could not find file with path '{args[0]}'.");
            }

            string json = File.ReadAllText(args[0]);

            ApiStatus apiStatus = new ApiStatus();

            Configuration config;

            if (!Configuration.TryLoadConfigurationFromJson(json, out config, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            LiveModel liveModel = new LiveModel(config);

            if (!liveModel.TryInit(apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            RankingResponse rankingResponse = new RankingResponse();

            if (!liveModel.TryChooseRank(eventId, contextJson, rankingResponse, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            long actionId;

            if (!rankingResponse.TryGetChosenAction(out actionId, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            Console.WriteLine($"Chosen action id: {actionId}");

            if (!liveModel.TryReportOutcome(eventId, outcome, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }
        }
        public void Test_CustomSender_FailsWhenNotRegistered()
        {
            const int TypeNotRegisteredError = 10; // see errors_data.h

            ApiStatus apiStatus = new ApiStatus();
            LiveModel liveModel = CreateLiveModel();

            Assert.IsFalse(liveModel.TryInit(apiStatus), "Should not be able to configure a model with BINDING_SENDER if custom factory is not set.");

            Assert.AreEqual(TypeNotRegisteredError, apiStatus.ErrorCode);
        }
        private void Run_TestCustomSender_InitFailure(Action <ApiStatus> senderInit, string expectedString, bool expectPrefix = false)
        {
            FactoryContext factoryContext = CreateFactoryContext(initAction: senderInit);

            ApiStatus apiStatus = new ApiStatus();
            LiveModel liveModel = CreateLiveModel(factoryContext);

            Assert.IsFalse(liveModel.TryInit(apiStatus), "MockSender.Init should be called and fail, which means LiveModel.Init should fail.");

            Assert.AreEqual(NativeMethods.OpaqueBindingError, apiStatus.ErrorCode);

            if (!expectPrefix)
            {
                Assert.AreEqual(expectedString, apiStatus.ErrorMessage);
            }
            else
            {
                Assert.IsTrue(apiStatus.ErrorMessage.StartsWith(expectedString));
            }
        }