// public T Resolve<T>(string p_name, IAttributeDataCollection p_collection) // { // hasErrorInResolving = false; // // if (p_collection != null) // { // if (p_collection.HasAttribute(p_name)) // { // return p_collection.GetAttribute<T>(p_name); // } // } // // object result; // if (ResolveReservedVariable(p_name, out result)) // return (T)result; // // if (ResolveReference(p_name, p_collection, out result)) // return (T)result; // // if (_graph.variables.HasVariable(p_name)) // { // Variable<T> variable = _graph.variables.GetVariable<T>(p_name); // return variable.value; // } // // hasErrorInResolving = true; // errorMessage = "Variable/Attribute "+ p_name +" not found."; // return default(T); // } object ResolveNested(string[] p_properties, int p_index, object p_result, IAttributeDataCollection p_collection, bool p_referenced) { if (p_properties == null || p_result == null || p_index >= p_properties.Length - 1) { return(p_result); } p_index++; string property = p_properties[p_index]; FieldInfo fieldInfo = p_result.GetType().GetField(property); if (fieldInfo == null) { PropertyInfo propertyInfo = p_result.GetType().GetProperty(property); if (propertyInfo == null) { hasErrorInResolving = true; errorMessage = "Nested property lookup " + String.Join(".", p_properties) + " not found."; return(null); } p_result = propertyInfo.GetValue(p_result); } else { p_result = fieldInfo.GetValue(p_result); } if (typeof(Parameter).IsAssignableFrom(p_result.GetType())) { Parameter parameter = p_result as Parameter; if (!parameter.IsInReferenceChain(parameter)) { p_result = p_result.GetType().GetMethod("GetValue") .Invoke(p_result, new object[] { this, p_collection, p_referenced }); } else { hasErrorInResolving = true; errorMessage = "Reference chain encountered."; p_result = null; return(p_result); } } return(ResolveNested(p_properties, p_index, p_result, p_collection, p_referenced)); }
bool ResolveReference(string p_name, IAttributeDataCollection p_collection, out object p_result) { if (!p_name.StartsWith("$")) { p_result = null; return(false); } string name = p_name.Substring(1); object value = _graph.GetNodeById(name).GetModel(); if (value != null) { p_result = value; return(true); } hasErrorInResolving = true; errorMessage = "Invalid node reference " + name; p_result = null; return(false); }
public static T EvaluateExpression <T>(string p_expression, IParameterResolver p_resolver, IAttributeDataCollection p_collection, bool p_referenced) { hasErrorInEvaluation = false; if (_cachedExpressions == null) { _cachedExpressions = new Dictionary <string, Expression>(); } Expression cachedExpression; if (!_cachedExpressions.ContainsKey(p_expression)) { // We cache before macro replacement so runtime macro changes are not possible for performance reasons cachedExpression = new Expression(ReplaceMacros(p_expression)); _cachedExpressions.Add(p_expression, cachedExpression); } else { cachedExpression = _cachedExpressions[p_expression]; } EvaluateFunctionHandler evalFunction = (name, args) => EvaluateFunction <T>(name, args); cachedExpression.EvaluateFunction += evalFunction; EvaluateParameterHandler evalParam = (name, args) => EvaluateParameter(name, args, p_resolver, p_collection, p_referenced); cachedExpression.EvaluateParameter += evalParam; object obj = null; //try { obj = cachedExpression.Evaluate(); } // catch (Exception e) // { // // Only set if we didn't already encounter error in evaluation otherwise this may be unspecified exception as a result of the already logged error so we don't want to overwrite it // if (!hasErrorInEvaluation) // { // errorMessage = e.Message; // hasErrorInEvaluation = true;-+ // } // } cachedExpression.EvaluateFunction -= evalFunction; cachedExpression.EvaluateParameter -= evalParam; if (obj != null) { Type returnType = obj.GetType(); if (typeof(T).IsAssignableFrom(returnType)) { return((T)obj); } // Explicit numeric type casting at cost of precision/overflow if (typeof(T).IsNumericType() && returnType.IsNumericType()) { return((T)Convert.ChangeType(obj, typeof(T))); } if (typeof(T).IsImplicitlyAssignableFrom(returnType)) { return((T)Convert.ChangeType(obj, typeof(T))); } if (typeof(T) == typeof(string)) { return((T)(object)obj.ToString()); } Debug.LogWarning("Invalid expression casting " + obj.GetType() + " and " + typeof(T)); } return(default(T)); }
public static object EvaluateUntypedExpression(string p_expression, IParameterResolver p_resolver, IAttributeDataCollection p_collection, bool p_referenced) { hasErrorInEvaluation = false; if (_cachedExpressions == null) { _cachedExpressions = new Dictionary <string, Expression>(); } Expression cachedExpression; if (!_cachedExpressions.ContainsKey(p_expression)) { cachedExpression = new Expression(p_expression); _cachedExpressions.Add(p_expression, cachedExpression); } else { cachedExpression = _cachedExpressions[p_expression]; } EvaluateFunctionHandler evalFunction = (name, args) => EvaluateFunction(name, args); cachedExpression.EvaluateFunction += evalFunction; EvaluateParameterHandler evalParam = (name, args) => EvaluateParameter(name, args, p_resolver, p_collection, p_referenced); cachedExpression.EvaluateParameter += evalParam; object obj = null; try { obj = cachedExpression.Evaluate(); } catch (Exception e) { // Only set if we didn't already encounter error in evaluation otherwise this may be unspecified exception as a result of the already logged error so we don't want to overwrite it if (!hasErrorInEvaluation) { errorMessage = e.Message; hasErrorInEvaluation = true; } } cachedExpression.EvaluateFunction -= evalFunction; cachedExpression.EvaluateParameter -= evalParam; return(obj); }
public static object EvaluateTypedExpression(string p_expression, Type p_returnType, IParameterResolver p_resolver, IAttributeDataCollection p_collection = null) { MethodInfo method = typeof(ExpressionEvaluator).GetMethod("EvaluateExpression", BindingFlags.Public | BindingFlags.Static); MethodInfo generic = method.MakeGenericMethod(p_returnType); return(generic.Invoke(null, new object[] { p_expression, p_resolver, p_collection, false })); }
static void EvaluateParameter(string p_name, ParameterArgs p_args, IParameterResolver p_resolver, IAttributeDataCollection p_collection, bool p_referenced) { //Debug.Log("EvaluateParameter: "+p_name); p_args.Result = p_resolver.Resolve(p_name, p_collection, p_referenced); // Only log first error if (!hasErrorInEvaluation && p_resolver.hasErrorInResolving) { errorMessage = p_resolver.errorMessage; } hasErrorInEvaluation = hasErrorInEvaluation || p_resolver.hasErrorInResolving; }
public object Resolve(string p_name, IAttributeDataCollection p_collection, bool p_referenced) { hasErrorInResolving = false; string name = p_name; string[] nameSplit = null; if (p_name.IndexOf('.') > 0) { nameSplit = p_name.Split('.'); name = nameSplit[0]; } if (p_collection != null) { if (p_collection.HasAttribute(name)) { return(ResolveNested(nameSplit, 0, p_collection.GetAttribute(name), p_collection, p_referenced)); } } object result; if (ReservedParameters.Resolve(_graph, name, out result)) { return(ResolveNested(nameSplit, 0, result, p_collection, p_referenced)); } if (ResolveReference(name, p_collection, out result)) { return(ResolveNested(nameSplit, 0, result, p_collection, true)); } DashController controller = _graph.Controller; #if UNITY_EDITOR if (!Application.isPlaying) { controller = DashEditorCore.EditorConfig.editingController; } #endif if (controller != null && controller.Variables != null && controller.Variables.HasVariable(name)) { Variable variable = controller.Variables.GetVariable(name); return(ResolveNested(nameSplit, 0, variable.value, p_collection, p_referenced)); } if (_graph.variables.HasVariable(name)) { Variable variable = _graph.variables.GetVariable(name); return(ResolveNested(nameSplit, 0, variable.value, p_collection, p_referenced)); } if (DashCore.Instance.GlobalVariables != null && DashCore.Instance.GlobalVariables.HasVariable(name)) { Variable variable = DashCore.Instance.GlobalVariables.GetVariable(name); return(ResolveNested(nameSplit, 0, variable.value, p_collection, p_referenced)); } hasErrorInResolving = true; errorMessage = "Variable " + name + " not found."; return(null); }