예제 #1
0
        public ActionResult SaveRule([FromBody] ClientRequest data)
        {
            Result result = new Result();

            // See the comments in the LoadSettings() method
            RuleEditor editor = this.GetRuleEditor();

            // Load the rule into the editor
            editor.LoadClientData(data.Data);

            if (editor.Rule.IsEmpty())
            {
                result.IsRuleEmpty = true;
            }
            else if (!editor.Rule.IsValid(new StorageService(this.environment).LoadRuleXml))
            {
                result.IsRuleValid = false;
                // Load the json string of invalid data into the Result object
                result.ClientInvalidData = editor.GetClientInvalidData();
            }
            else
            {
                // Save the rule
                new StorageService(this.environment)
                .SaveRule(editor.Rule.Id.ToString(), editor.Rule.GetRuleXml(), editor.Rule.IsLoadedRuleOfEvalType == null ? true : (bool)editor.Rule.IsLoadedRuleOfEvalType);
                // Send ID of this rule to the client
                result.Output = editor.Rule.Id.ToString();
            }

            return(Json(result));
        }
예제 #2
0
        public JsonResult EvaluateRule([FromBody] ClientRequest data)
        {
            Result result = new Result();

            // See the comments in the LoadSettings() method
            RuleEditor editor = this.GetRuleEditor();

            // Load the rule into the editor
            editor.LoadClientData(data.Data);

            // We are not saving the rule, just evaluating it. Tell the editor not to enforce the rule name validation
            editor.Rule.SkipNameValidation = true;

            StorageService storage = new StorageService(this.environment);

            if (editor.Rule.IsEmpty())
            {
                result.IsRuleEmpty = true;
            }
            else if (!editor.Rule.IsValid(storage.LoadRuleXml))
            {
                result.IsRuleValid = false;
                // Load the json string of invalid data into the Result object
                result.ClientInvalidData = editor.GetClientInvalidData();
            }
            else
            {
                // Create an instance of the Evaluator class. Because our rules might reference other rules of evaluation type
                // we use constructor that takes rule's XML and delegate of the method that can load referenced rules by their IDs.
                Evaluator <Patient> evaluator = new Evaluator <Patient>(editor.Rule.GetRuleXml(), storage.LoadRuleXml);

                Patient patient = GetSource();

                // Evaluate the patient against the rule
                bool success = evaluator.Evaluate(patient);

                // Return the evaluated patient back to the client
                result.Patient = patient;

                // Output the result of the evaluation to the client
                result.Output  = string.IsNullOrWhiteSpace(patient.Output) ? "The rule evaluated to " + success.ToString() : patient.Output;
                result.Output += Environment.NewLine;
                result.Output += "The rule calculation resulted in: " + result.Patient.CalculatedValue;
                result.Output += Environment.NewLine;

                var calculationShould =
                    patient.CountOfSku("TenDollarSku") * 10 +
                    patient.CountOfSku("TwentyDollarSku") * 20 +

                    // These are never included in the calculation for some reason.
                    patient.CountOfSku("FortyDollarSku") * 40 +
                    50;

                result.Output += "The rule SHOULD have calculated to: " + calculationShould;
            }

            return(Json(result));
        }