public override bool SaveItem(bool copyCurrent = false) { var json = JsonView?.Text.ToString(); if (!string.IsNullOrEmpty(json)) { try { var rule = JsonConvert.DeserializeObject <Rule>(json); RuleRequest ruleRequest = new RuleRequest(rule.Name, rule.Actions, null, rule.TimeZoneId); string locationId = GetRuleLocation(rule); if (copyCurrent) { ruleRequest.Name += $"-copy"; STClient.CreateRule(locationId, ruleRequest); } else { var response = STClient.UpdateRule(rule.Id, locationId, ruleRequest); } RefreshScreen(); } catch (SmartThingsNet.Client.ApiException exp) { ShowErrorMessage($"Error {exp.ErrorCode}{Environment.NewLine}{exp.Message}"); } catch (Exception exp) { ShowErrorMessage($"Error {exp.Message}"); } } return(true); }
public RuleResult Post([FromBody] RuleRequest req) { var cMap = new ConditionDataProvider().GetConditionMap(req.Condition.CommonName); var result = new ConditionEvaluator().Evaluate(req, cMap); return(result); }
public object Put(RuleRequest request) { if (request.ZoneRequired && request.ExcludeCircularZone) { throw new HttpError(HttpStatusCode.BadRequest, ErrorCode.Rule_TwoTypeZoneVerificationSelected.ToString()); } //Check if rate with same name already exists if ((request.Type != RuleType.Default) && (_dao.GetAll().Any(x => x.Id != request.Id && x.Name == request.Name))) { throw new HttpError(HttpStatusCode.Conflict, ErrorCode.Rule_DuplicateName.ToString()); } //if (_dao.GetAll().Any(x => x.Priority == request.Priority && x.Category == (int)request.Category)) if ( _dao.GetAll() .Where(x => x.Id != request.Id) .Any(x => x.Priority == request.Priority && x.Category == (int)request.Category)) { throw new HttpError(HttpStatusCode.Conflict, ErrorCode.Rule_InvalidPriority.ToString()); } var command = Mapper.Map <UpdateRule>(request); _commandBus.Send(command); return(new HttpResult(HttpStatusCode.OK, "OK")); }
public async Task <IActionResult> Post([FromBody] RuleRequest request) { var runner = new RuleRunner(new RuleDefinitionRepository(_context)); var result = await runner.ApplyAsync(request); return(Ok(result)); }
/// <summary> /// Delete Rule /// </summary> /// <param name="request">rule request.</param> /// <returns>Rule message.</returns> public RuleMessage DeleteRule(RuleRequest request) { RuleMessage message = new RuleMessage(); try { request.FindRuleDto = FindRuleDto.RuleId; RuleResponsePivot response = _serviceRule.FindRules(request.ToPivot()); if (response?.RulePivot != null) { _serviceRule.DeleteRule(request.ToPivot()); message.OperationSuccess = true; } else { message.ErrorMessage = RuleMessageResource.NotFound; message.ErrorType = ErrorType.FunctionalError; message.OperationSuccess = false; } } catch (Exception e) { message.ErrorType = ErrorType.TechnicalError; message.ErrorMessage = e.Message; } return(message); }
private void ImportRule(string filePath) { try { string json = File.ReadAllText(filePath); var rule = JsonConvert.DeserializeObject <Rule>(json); RuleRequest ruleRequest = new RuleRequest(rule.Name, rule.Actions, null, rule.TimeZoneId); // TODO: prompt for which location string locationId = STClient.GetAllLocations().Items.FirstOrDefault().LocationId.ToString(); var response = STClient.CreateRule(locationId, ruleRequest); ShowMessage($"Rule added!"); } catch (SmartThingsNet.Client.ApiException exp) { ShowErrorMessage($"Error {exp.ErrorCode}{Environment.NewLine}{exp.Message}"); } catch (Exception exp) { ShowErrorMessage($"Error {exp.Message}"); } finally { ImportItem(); } }
public void TestRefactor() { var req = new RuleRequest(); req.Condition = new Condition(); req.Condition.CommonName = "uti"; req.Tests = new List <Test>() { new Test() { Name = "dysuria" }, new Test() { Name = "frequency" }, new Test() { Name = "hematuria" }, new Test() { Name = "back pain" } }; var cMap = new ConditionDataProvider().GetConditionMap("uti"); var result = new ConditionEvaluator().Evaluate(req, cMap); TestContext.WriteLine("Highest symptom matches" + result.DiagnosticCertainty.ToString()); Assert.AreNotEqual(1, result.DiagnosticCertainty); }
public IRuleEngineResult Post(RuleRequest request) { var rng = new Random(); var handler = new BusinessRuleRequestHandler(); return(handler.Handle(request).Result); }
/// <summary> /// From Rule Request to Rule Request pivot. /// </summary> /// <param name="ruleRequest">the request to assemble.</param> /// <returns>Rule Request pivot result.</returns> public static RuleRequestPivot ToPivot(this RuleRequest ruleRequest) { return(new RuleRequestPivot { FindRulePivot = Utility.EnumToEnum <FindRuleDto, FindRulePivot>(ruleRequest.FindRuleDto), RulePivot = ruleRequest.RuleDto.ToPivot() }); }
/// <summary> /// Validate find rule. /// </summary> /// <param name="request">the request to validate.</param> /// <returns>validation errors.</returns> private List <string> ValidateFindRule(RuleRequest request) { List <string> errors = new List <string>(); if (request?.RuleDto == null) { errors.Add(RuleMessageResource.NullRequest); } return(errors); }
public object Delete(RuleRequest request) { var command = new DeleteRule { CompanyId = AppConstants.CompanyId, RuleId = request.Id }; _commandBus.Send(command); return(new HttpResult(HttpStatusCode.OK, "OK")); }
public async Task <RuleResponse> ApplyAsync(RuleRequest request) { var definition = await _repository.GetDefinitionByNameAsync(request.Rule); var rules = definition.Rules?.OrderBy(x => x.Priority); if (rules == null || !rules.Any()) { return(new RuleResponse() { Rule = request.Rule, RuleId = "no_rule_found", Values = null }); } var properites = definition.Properties; var results = new List <RuleTracker>(); var tasks = new List <Task>(); foreach (var rule in rules) { tasks.Add(Task.Run(() => { var executor = new RuleExecutor(properites, request.Parameters); var result = executor.Run(rule); results.Add(result); })); } Task.WaitAll(tasks.ToArray()); var passedRule = results .Where(x => x.Passed) .OrderBy(x => x.Priority) .FirstOrDefault(); if (passedRule != null) { return(new RuleResponse() { Rule = request.Rule, RuleId = passedRule.Name, Values = passedRule.Results }); } else { return(new RuleResponse() { Rule = request.Rule, RuleId = "no_successful_rule", Values = null }); } }
/// <summary> /// Validate create rule. /// </summary> /// <param name="request">the request to validate.</param> /// <returns>validation errors.</returns> private List <string> ValidateDeleteRule(RuleRequest request) { List <string> errors = new List <string>(); if (request?.RuleDto == null) { errors.Add(RuleMessageResource.NullRequest); } else { errors.AddRange(GenericValidationAttribute <RuleDto> .ValidateAttributes("RuleId", request.RuleDto.RuleId.ToString())); } return(errors); }
/// <summary> /// Validate create rule. /// </summary> /// <param name="request">the request to validate.</param> /// <returns>validation errors.</returns> private List <string> ValidateCreateRule(RuleRequest request) { List <string> errors = new List <string>(); if (request?.RuleDto == null) { errors.Add(RuleMessageResource.NullRequest); } else { errors.AddRange(GenericValidationAttribute <RuleDto> .ValidateAttributes("RulePrefix", request.RuleDto.RulePrefix)); errors.AddRange(GenericValidationAttribute <RuleDto> .ValidateAttributes("RuleName", request.RuleDto.RuleName)); } return(errors); }
/// <summary> /// Search Rule /// </summary> /// <param name="request">rule request.</param> /// <returns>Rule message.</returns> public RuleMessage FindRules(RuleRequest request) { RuleMessage message = new RuleMessage(); try { message = _serviceRule.FindRules(request.ToPivot()).ToMessage(); message.OperationSuccess = true; } catch (Exception e) { message.ErrorType = ErrorType.TechnicalError; message.ErrorMessage = e.Message; } return(message); }
/// <summary> /// Create new Rule. /// </summary> /// <param name="request">rule request.</param> /// <returns>Rule message.</returns> public RuleMessage CreateRule(RuleRequest request) { RuleMessage message = new RuleMessage(); try { message = _serviceRule.CreateRule(request.ToPivot()).ToMessage(); if (message?.RuleDto != null) { //create user rule. UserMessage userMessage = _serviceUser.GetAllUsers().ToMessage(); if (userMessage?.UserDtoList != null) { foreach (var user in userMessage.UserDtoList) { _serviceUserRule.CreateUserRule(new UserRuleRequestPivot() { UserRulePivot = new UserRulePivot() { RuleId = message.RuleDto.RuleId, UserRuleStatus = false, UserId = user.UserId } }); } } message.OperationSuccess = true; } else { if (message != null) { message.ErrorMessage = RuleMessageResource.ServiceResponseNull; message.ErrorType = ErrorType.TechnicalError; message.OperationSuccess = false; } } } catch (Exception e) { message.ErrorType = ErrorType.TechnicalError; message.ErrorMessage = e.Message; } return(message); }
public IHttpActionResult UpdateRule(RuleRequest request) { List <string> errors = ValidateUpdateRule(request); RuleMessage message = new RuleMessage(); if (errors != null && errors.Any()) { message.ErrorMessage = RuleMessageResource.ErrorsValidation; message.ErrorType = ErrorType.ValidationError; message.Errors = new List <string>(); message.OperationSuccess = false; message.Errors.AddRange(errors); } else { message = _serviceRuleClient.UpdateRule(request); } return(Json(message)); }
public RuleResponse Apply(RuleRequest request) { return(ApplyAsync(request).Result); }
public Rule UpdateRule(string ruleId, string locationId, RuleRequest ruleRequest) { return(_rulesApi.UpdateRule(_accessToken, ruleId, locationId, ruleRequest)); }
public object CreateRule(string locationId, RuleRequest ruleRequest) { return(_rulesApi.CreateRule(_accessToken, locationId, ruleRequest)); }
public object Get(RuleRequest request) { return(_dao.GetAll()); }