public GetTagsAutocompleteSuggestions(ITagsSource dataSource, IEnumerable <string> wordsToQuery) { Ensure.Argument.IsNotNull(dataSource, nameof(dataSource)); Ensure.Argument.IsNotNull(wordsToQuery, nameof(wordsToQuery)); this.dataSource = dataSource; this.wordsToQuery = wordsToQuery; }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <returns>The string.</returns> /// <param name="evalFunction">Eval function.</param> /// <param name="tags">Tags.</param> public string InterpretString(string evalFunction, ITagsSource tags){ if (string.IsNullOrWhiteSpace(evalFunction)) { throw new ArgumentOutOfRangeException("evalFunction cannot be null"); } // trim eval function. evalFunction = evalFunction.Trim(); // calculate the result. return this.Interpreter (evalFunction, tags); }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="evalFunction"></param> /// <param name="tags"></param> /// <returns></returns> public double InterpretDouble(string evalFunction, ITagsSource tags) { string result = this.InterpretString (evalFunction, tags); // parse to double. double resultDouble = 0; if (!double.TryParse (result, out resultDouble)) { OsmSharp.Logging.Log.TraceEvent("EvalInterpreter", OsmSharp.Logging.TraceEventType.Error, string.Format ("Cannot convert '{0}' to double.", result)); } return resultDouble; }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="evalFunction"></param> /// <param name="tags"></param> /// <returns></returns> public float InterpretFloat(string evalFunction, ITagsSource tags) { string result = this.InterpretString(evalFunction, tags); // parse to float. float resultFloat = 0; if (!float.TryParse(result, out resultFloat)) { System.Diagnostics.Debug.WriteLine(string.Format("EvalInterpreter: Cannot convert '{0}' to float.", result)); // OsmSharp.Logging.Log.TraceEvent("EvalInterpreter", OsmSharp.Logging.TraceEventType.Error, string.Format ("Cannot convert '{0}' to float.", result)); } return(resultFloat); }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="evalFunction"></param> /// <param name="tags"></param> /// <returns></returns> public double InterpretDouble(string evalFunction, ITagsSource tags) { string result = this.InterpretString(evalFunction, tags); // parse to double. double resultDouble = 0; if (!double.TryParse(result, out resultDouble)) { System.Diagnostics.Debug.WriteLine(string.Format("EvalInterpreter: Cannot convert '{0}' to double.", result)); // ("EvalInterpreter", OsmSharp.Logging.TraceEventType.Error, string.Format ("Cannot convert '{0}' to double.", result)); } return(resultDouble); }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <returns>The string.</returns> /// <param name="evalFunction">Eval function.</param> /// <param name="tags">Tags.</param> public string InterpretString(string evalFunction, ITagsSource tags) { if (string.IsNullOrWhiteSpace(evalFunction)) { throw new ArgumentOutOfRangeException("evalFunction cannot be null"); } // trim eval function. evalFunction = evalFunction.Trim(); // calculate the result. return(this.Interpreter(evalFunction, tags)); }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="evalFunction"></param> /// <param name="tags"></param> /// <returns></returns> public int InterpretInt(string evalFunction, ITagsSource tags) { string result = this.InterpretString(evalFunction, tags); // parse to double. int resultInt = 0; if (!int.TryParse(result, out resultInt)) { OsmSharp.Logging.Log.TraceEvent("EvalInterpreter", OsmSharp.Logging.TraceEventType.Error, string.Format("Cannot convert '{0}' to int.", result)); } return(resultInt); }
/// <summary> /// Interpreters the given expression. /// </summary> /// <param name="function"></param> /// <param name="tags"></param> /// <returns></returns> private string Interpreter(string expression, ITagsSource tags) { // test for one of the tokens. if (expression.StartsWith(TAG_TOKEN)) { // get the tag value. string[] args = this.ParseFunction(expression); if (args.Length != 1) { throw new EvalInterpreterException("Invalid argument count: {0}", expression); } string tagValue; if (!tags.TryGetValue(args[0], out tagValue)) { // the tag value. tagValue = null; } return(tagValue); } else if (expression.StartsWith(PROP_TOKEN)) { // not supported for now: mapcss interpretation code needs to change first to keep a list // of current properties. return(null); // return 'none'. } else if (expression.StartsWith(COND_TOKEN)) { // evaluate expression and decide on the next expression. string[] args = this.ParseFunction(expression); if (args.Length != 3) { throw new EvalInterpreterException("Invalid argument count: {0}", expression); } if (this.ParseBool(this.Interpreter(args[0], tags))) { // evaluate and return the true-part. return(this.Interpreter(args[1], tags)); } else { // evaluate and return the false-part. return(this.Interpreter(args[2], tags)); } } else if (expression.StartsWith(ANY_TOKEN)) { // return the first expression that does not evaluate to 'none'. string[] args = this.ParseFunction(expression); if (args.Length == 0) { throw new EvalInterpreterException("Invalid argument count: {0}", expression); } for (int idx = 0; idx < args.Length; idx++) { // evaluate each expression in the correct order. string result = this.Interpreter(args[idx], tags); if (!this.IsNone(result)) { // expression was found not returning 'none'. return(result); } } return(null); } else if (expression.StartsWith(MAX_TOKEN)) { // returns the maximum value of all arguments. } else if (expression.StartsWith(MIN_TOKEN)) { // returns the minimum value of all arguments. } throw new EvalInterpreterException("Failed to evaluate expression: {0}", expression); }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="evalFunction"></param> /// <param name="tags"></param> /// <returns></returns> public float InterpretFloat(string evalFunction, ITagsSource tags) { string result = this.InterpretString (evalFunction, tags); // parse to float. float resultFloat = 0; if (!float.TryParse (result, out resultFloat)) { OsmSharp.Logging.Log.TraceEvent("EvalInterpreter", OsmSharp.Logging.TraceEventType.Error, string.Format ("Cannot convert '{0}' to float.", result)); } return resultFloat; }
/// <summary> /// Interpreters the given expression. /// </summary> /// <param name="function"></param> /// <param name="tags"></param> /// <returns></returns> private string Interpreter(string expression, ITagsSource tags) { // test for one of the tokens. if (expression.StartsWith(TAG_TOKEN)) { // get the tag value. string[] args = this.ParseFunction(expression); if (args.Length != 1) { throw new EvalInterpreterException("Invalid argument count: {0}", expression); } string tagValue; if (!tags.TryGetValue(args[0], out tagValue)) { // the tag value. tagValue = null; } return tagValue; } else if (expression.StartsWith(PROP_TOKEN)) { // not supported for now: mapcss interpretation code needs to change first to keep a list // of current properties. return null; // return 'none'. } else if (expression.StartsWith(COND_TOKEN)) { // evaluate expression and decide on the next expression. string[] args = this.ParseFunction(expression); if (args.Length != 3) { throw new EvalInterpreterException("Invalid argument count: {0}", expression); } if (this.ParseBool(this.Interpreter(args[0], tags))) { // evaluate and return the true-part. return this.Interpreter(args[1], tags); } else { // evaluate and return the false-part. return this.Interpreter(args[2], tags); } } else if (expression.StartsWith(ANY_TOKEN)) { // return the first expression that does not evaluate to 'none'. string[] args = this.ParseFunction(expression); if (args.Length == 0) { throw new EvalInterpreterException("Invalid argument count: {0}", expression); } for (int idx = 0; idx < args.Length; idx++) { // evaluate each expression in the correct order. string result = this.Interpreter(args[idx], tags); if (!this.IsNone(result)) { // expression was found not returning 'none'. return result; } } return null; } else if (expression.StartsWith(MAX_TOKEN)) { // returns the maximum value of all arguments. } else if (expression.StartsWith(MIN_TOKEN)) { // returns the minimum value of all arguments. } throw new EvalInterpreterException("Failed to evaluate expression: {0}", expression); }
/// <summary> /// Interpreters the given eval function. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="evalFunction"></param> /// <param name="tags"></param> /// <returns></returns> public int InterpretInt(string evalFunction, ITagsSource tags) { string result = this.InterpretString (evalFunction, tags); // parse to double. int resultInt = 0; if (!int.TryParse (result, out resultInt)) { OsmSharp.Logging.Log.TraceEvent("EvalInterpreter", System.Diagnostics.TraceEventType.Error, string.Format ("Cannot convert '{0}' to int.", result)); } return resultInt; }
/// <summary> /// Returns the interpreted value of the boolstring according to the MapCSS eval boolean datatype rules. /// </summary> /// <param name="evalFunction"></param> /// <returns></returns> public bool InterpretBool(string evalFunction, ITagsSource tags) { string result = this.InterpretString(evalFunction, tags); return(!(result == null || result == "false" || result == string.Empty || result == "no")); }