public ResponseModel Post([FromBody] RawTextFileModel file) { this._logger.LogInformation(LoggerEvents.RequestPassed, "Processing request"); string directoryPath = @""; Random random = new Random(); string fileFullPath = $"{directoryPath}{random.Next(10000)}tmp.{file.Type}"; System.IO.File.WriteAllText(fileFullPath, file.Content); string output = Toulbar2Operations.RunToulbar2(fileFullPath, true, _logger); System.IO.File.Delete(fileFullPath); // Creating response: var response = new ResponseModel(); response.RawOutput = output; int maxWeight = Toulbar2Operations.CalcualteMaxWeightFromFile(file.Content, file.Type); var rgx = new Regex(@"New solution: .*\n (.*)"); var rgx2 = new Regex(@"Optimum: (\d+) in (\d+) .* and (\d+\.?\d*)"); var match = rgx.Match(output); if (match.Success) { string[] variables = match.Groups[1].Value.Split(" "); int counter = 1; foreach (string variable in variables) { int v = int.Parse(variable); response.Variables.Add(new Variable() { Name = counter.ToString(), Value = v }); counter++; } match = rgx2.Match(output); if (match.Success) { int weight = 0; int.TryParse(match.Groups[1].Value, out weight); response.AccomplishementPercentage = (maxWeight - weight) / (double)maxWeight * 100; int memory = 0; int.TryParse(match.Groups[2].Value, out memory); response.Memory = memory; double time = 0; double.TryParse(match.Groups[3].Value, out time); response.Time = time; } } else { response.RawOutput += "\nSolution not found!!!"; } this._logger.LogInformation(LoggerEvents.ResponseCreated, "Succesfully created response"); return(response); }
public ResponseModel Post([FromBody] WCNFModel value) { this._logger.LogInformation(LoggerEvents.RequestPassed, "Processing request ..."); string directoryPath = @""; string fileFullPath; Dictionary <int, string> dict; (fileFullPath, dict) = CreateWCNFFile(value, directoryPath); this._logger.LogInformation(LoggerEvents.FileCreated, "File created, file path: {PATH}", fileFullPath); string output = Toulbar2Operations.RunToulbar2(fileFullPath, true, _logger); // Creating response: var response = new ResponseModel(); response.RawOutput = output; int maxWeight = value.Functions.Select(x => x.Weight).Sum(); var rgx = new Regex(@"New solution: .*\n (.*)"); var rgx2 = new Regex(@"Optimum: (\d+) in (\d+) .* and (\d+\.?\d*)"); var match = rgx.Match(output); if (match.Success) { string[] variables = match.Groups[1].Value.Split(" "); int counter = 1; foreach (string variable in variables) { int v = int.Parse(variable); response.Variables.Add(new Variable() { Name = dict[counter], Value = v }); counter++; } match = rgx2.Match(output); if (match.Success) { int weight = 0; int.TryParse(match.Groups[1].Value, out weight); response.AccomplishementPercentage = (maxWeight - weight) / (double)maxWeight * 100; int memory = 0; int.TryParse(match.Groups[2].Value, out memory); response.Memory = memory; double time = 0; double.TryParse(match.Groups[3].Value, out time); response.Time = time; } } else { response.RawOutput += "\nSolution not found!!!"; } this._logger.LogInformation(LoggerEvents.ResponseCreated, "Succesfully created response"); return(response); }
public ResponseModel Get(int id) { this._logger.LogInformation(LoggerEvents.RequestPassed, "Processing request ..."); string directoryPath = @"app"; string trailer = ".wcsp"; string fileFullPath = $"/{directoryPath}/examples/{id}{trailer}"; string output = Toulbar2Operations.RunToulbar2(fileFullPath, false, _logger); // Creating response: var response = new ResponseModel(); response.RawOutput = output; var rgx = new Regex(@"(New solution:) (\d+) (.*\n) (.*)"); var match = rgx.Match(output); //int maxWeight = value.Functions.Select(x => x.Weight).Sum(); int weight = int.Parse(match.Groups[2].Value); response.AccomplishementPercentage = 100;// (maxWeight - weight) / (double)maxWeight * 100; string[] variables = match.Groups[4].Value.Split(" "); int counter = 1; foreach (string variable in variables) { int v = int.Parse(variable); response.Variables.Add(new Variable() { Name = counter.ToString(), Value = v }); counter++; } var rgx2 = new Regex(@"Optimum: \d+ in (\d+) .*and ([0-9]*.?[0-9]*)"); match = rgx2.Match(output); response.Memory = int.Parse(match.Groups[1].Value); response.Time = double.Parse(match.Groups[2].Value); this._logger.LogInformation(LoggerEvents.ResponseCreated, "Succesfully created response"); return(response); }