public IActionResult RunAutomationFunction([FromBody] JObject data) { FunctionParam functionParams = data.ToObject <FunctionParam>(); AuthenticatedModel loggedInUser = JsonConvert.DeserializeObject <AuthenticatedModel> (User.FindFirst(ClaimTypes.NameIdentifier).Value); AutomationFunctionsApplication.RunFunction(loggedInUser, functionParams.FileList, functionParams.FunctionId, functionParams.DnaId); return(new OkResult()); }
public bool TryProcess(string input, out double result) { result = double.NaN; var regex = new Regex( @"\s*(?<lhs>(?<id>\w[\w\d]*)\s*(?<args>\(\s*(?<arg>\w[\w\d]*)(\s*,\s*(?<arg>\w[\w\d]*))*\s*\))?\s*=)?\s*(?<def>.*)"); Match m = regex.Match(input); if (!m.Success) { result = double.NaN; return(false); } if (m.Groups["id"].Captures.Count == 1) { string id = m.Groups["id"].Captures[0].Value; if (m.Groups["def"].Captures.Count != 1) { throw new ArgumentException("No value to assign."); } Notation rhs = Notation.Parse(m.Groups["def"].Captures[0].Value); if (m.Groups["args"].Captures.Count == 1) { // 関数の場合 var param = new FunctionParam(rhs, from Capture c in m.Groups["arg"].Captures select c.Value); if (_functions.ContainsKey(id)) { _functions[id] = param; } else { _functions.Add(id, param); } result = double.NaN; } else { // 変数の場合 double r = Calculate(rhs, _variables, _functions); if (_variables.ContainsKey(id)) { _variables[id] = r; } else { _variables.Add(id, r); } result = r; } } else { try { result = Calculate(Notation.Parse(input), _variables, _functions); } catch (Exception) { return(false); } } return(true); }
private static double CallFunction(Function f, double[] args, Dictionary <string, double> variables, Dictionary <string, FunctionParam> functions) { switch (f.Token.ToLower()) { case "abs": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Abs(args[0])); case "acos": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Acos(args[0])); case "acosh": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Log(args[0] + Math.Sqrt(args[0] * args[0] - 1))); case "asin": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Asin(args[0])); case "asinh": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Log(args[0] + Math.Sqrt(args[0] * args[0] + 1))); case "atan": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Atan(args[0])); case "atan2": if (f.NumberOfArguments != 2) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Atan2(args[0], args[1])); case "atanh": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return((Math.Log(1 + args[0]) - Math.Log(1 - args[0])) / 2); case "ceiling": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Ceiling(args[0])); case "cos": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Cos(args[0])); case "cosh": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Cosh(args[0])); case "erf": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Erf(args[0])); case "erfc": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(1 - Erf(args[0])); case "exp": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Exp(args[0])); case "floor": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Floor(args[0])); case "log": switch (f.NumberOfArguments) { case 1: return(Math.Log(args[0])); case 2: return(Math.Log(args[0], args[1])); default: throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } case "log10": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Log10(args[0])); case "max": if (f.NumberOfArguments != 2) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Max(args[0], args[1])); case "min": if (f.NumberOfArguments != 2) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Min(args[0], args[1])); case "round": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Round(args[0])); case "sign": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Sign(args[0])); case "sin": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Sin(args[0])); case "sinh": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Sinh(args[0])); case "sqrt": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Sqrt(args[0])); case "tan": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Tan(args[0])); case "tanh": if (f.NumberOfArguments != 1) { throw new ArgumentException("No overload for function " + f.Token + " takes " + f.NumberOfArguments + " arguments."); } return(Math.Tanh(args[0])); default: if (!functions.ContainsKey(f.Token)) { throw new InvalidOperationException("No function " + f.Token); } FunctionParam fp = functions[f.Token]; if (f.NumberOfArguments != fp.ArgumentNames.Length) { throw new ArgumentException("User defined function " + f.Token + " takes " + fp.ArgumentNames.Length + " arguments."); } var arguments = new Dictionary <string, double>(); for (int i = 0; i < args.Length; i++) { arguments.Add(fp.ArgumentNames[i], args[i]); } return(Calculate(fp.Notation.Substitute(arguments), variables, functions)); } }