private static IEnumerable <Filter> TranslateFilterInternal(BinaryExpression binaryExpression) { switch (binaryExpression.NodeType) { case ExpressionType.AndAlso: case ExpressionType.And: var leftBinaryExpression = binaryExpression.Left as BinaryExpression; if (leftBinaryExpression == null) { throw new NotSupportedException(string.Format( "This expression is not a binary expression: {0}", binaryExpression.Left)); } IEnumerable <Filter> firstFilter = TranslateFilterInternal(leftBinaryExpression); var rightBinaryExpression = binaryExpression.Right as BinaryExpression; if (rightBinaryExpression == null) { throw new NotSupportedException(string.Format( "This expression is not a binary expression: {0}", binaryExpression.Right)); } IEnumerable <Filter> secondFilter = TranslateFilterInternal(rightBinaryExpression); return(firstFilter.Concat(secondFilter).ToArray()); case ExpressionType.Or: case ExpressionType.OrElse: throw new NotSupportedException(string.Format( "Oprerator {0} is not yet supported. There's no reason why it can't be; we just haven't done it yet. Feel free to send a pull request if you need this feature. It was used in expression: {1}", binaryExpression.NodeType, binaryExpression)); } ExpressionKey key = ParseKeyFromExpression(binaryExpression.Left); object constantValue = ParseValueFromExpression(binaryExpression.Right); Type underlyingPropertyType = key.PropertyType; underlyingPropertyType = Nullable.GetUnderlyingType(key.PropertyType) ?? underlyingPropertyType; object convertedValue = underlyingPropertyType.IsEnum ? Enum.Parse(underlyingPropertyType, underlyingPropertyType.GetEnumName(constantValue)) : constantValue; return(new[] { new Filter { PropertyName = key.Name, Value = convertedValue, ExpressionType = binaryExpression.NodeType } }); }
IEnumerator BlinkRoutine() { while (true) { var waitTime = Time.time + Random.value * Interval; while (waitTime > Time.time) { if (Request) { m_request = false; break; } yield return(null); } // close var value = 0.0f; var closeSpeed = 1.0f / CloseSeconds; while (true) { value += Time.deltaTime * closeSpeed; if (value >= 1.0f) { break; } m_controller.Expression.SetWeight(ExpressionKey.CreateFromPreset(VrmLib.ExpressionPreset.Blink), value); yield return(null); } m_controller.Expression.SetWeight(ExpressionKey.CreateFromPreset(VrmLib.ExpressionPreset.Blink), 1.0f); // wait... yield return(new WaitForSeconds(ClosingTime)); // open value = 1.0f; var openSpeed = 1.0f / OpeningSeconds; while (true) { value -= Time.deltaTime * openSpeed; if (value < 0) { break; } m_controller.Expression.SetWeight(ExpressionKey.CreateFromPreset(VrmLib.ExpressionPreset.Blink), value); yield return(null); } m_controller.Expression.SetWeight(ExpressionKey.CreateFromPreset(VrmLib.ExpressionPreset.Blink), 0); } }
public ComplexSequentialExpression(string expression) { if (expression == null) throw new ArgumentNullException("expression"); if (!ComplexSequentialExpression.PredicateRegex.IsMatch(expression)) throw new FormatException(); var match = ComplexSequentialExpression._pattern.Match(expression); if (!match.Success) throw new ArgumentException("${Resources.UnknownExpressionKey}", "expression"); var keyChar = match.Groups[1].Value[0]; this.key = (ExpressionKey)keyChar; this.content = match.Groups[2].Value; }
public Lambda Parse(string expressionText, Parameter[] parameters) { var expressionKey = new ExpressionKey(expressionText, parameters); var cachedLambda = FindInCache(expressionKey); if (cachedLambda != null) { return(cachedLambda); } var lambda = _innerParser.Parse(expressionText, parameters); SaveInCache(expressionKey, lambda); return(lambda); }
IEnumerator RoutineNest(VrmLib.ExpressionPreset preset, float velocity, float wait) { for (var value = 0.0f; value <= 1.0f; value += velocity) { VRM.Expression.SetWeight(ExpressionKey.CreateFromPreset(preset), value); yield return(null); } VRM.Expression.SetWeight(ExpressionKey.CreateFromPreset(preset), 1.0f); yield return(new WaitForSeconds(wait)); for (var value = 1.0f; value >= 0; value -= velocity) { VRM.Expression.SetWeight(ExpressionKey.CreateFromPreset(preset), value); yield return(null); } VRM.Expression.SetWeight(ExpressionKey.CreateFromPreset(preset), 0); yield return(new WaitForSeconds(wait * 2)); }
public bool Equals(ExpressionKey <TKey, TValue> x, ExpressionKey <TKey, TValue> y) { if (x.IsKey && y.IsKey) { return(KeyKeyMatcher(x.KeyItem, y.KeyItem)); } if (!x.IsKey && !y.IsKey) { throw new ArgumentException("Cannot compare value without key"); } if (x.IsKey) { return(KeyValueMatcher(x.KeyItem, y.ValueItem)); } return(KeyValueMatcher(y.KeyItem, x.ValueItem)); }
private Lambda FindInCache(ExpressionKey expressionKey) { if (_cachedExpressions.ContainsKey(expressionKey)) { var expressionRef = _cachedExpressions[expressionKey]; Lambda lambda = expressionRef.Target as Lambda; if (lambda != null) { return(lambda); } else { _cachedExpressions.Remove(expressionKey); RemoveDeadExpressions(); } } return(null); }
public CachedExpression Get(Expression expression) { if (expression == null) { throw new System.ArgumentNullException("expression"); } CachedExpression result; var key = new ExpressionKey(_comparer, expression); if (_cache.TryGetValue(key, out result)) { return(result); } else { result = new CachedExpression(expression); _cache.Add(key, result); return(result); } }
public void AddCall(IProxyCall call, SetupKind kind) { var expr = call.SetupExpression.PartialMatcherAwareEval(); var keyText = call.Method.DeclaringType.FullName + "::" + expr.ToStringFixed(true); if (kind == SetupKind.PropertySet) { keyText = "set::" + keyText; } var constants = new ConstantsVisitor(expr).Values; var key = new ExpressionKey(keyText, constants); if (!call.IsConditional) { // if it's not a conditional call, we do // all the override setups. // TODO maybe add the conditionals to other // record like calls to be user friendly and display // somethig like: non of this calls were performed. if (calls.ContainsKey(key)) { // Remove previous from ordered calls InterceptionContext.RemoveOrderedCall(calls[key]); } calls[key] = call; } InterceptionContext.AddOrderedCall(call); }
private void SaveInCache(ExpressionKey key, Lambda lambda) { _cachedExpressions[key] = new WeakReference(lambda); }
/// <summary> /// Validates, creates and compiles the property expression; whilst also determinig the property friendly <see cref="Text"/>. /// </summary> /// <param name="propertyExpression">The <see cref="Expression"/> to reference the entity property.</param> /// <param name="probeForJsonRefDataSidProperties">Indicates whether to probe for the <see cref="T:JsonPropertyAttribute"/> via alternate <c>Sid</c> or <c>Sids</c> properties as implemented for reference data.</param> /// <returns>A <see cref="PropertyExpression{TEntity, TProperty}"/> which contains (in order) the compiled <see cref="System.Func{TEntity, TProperty}"/>, member name and resulting property text.</returns> public static PropertyExpression <TEntity, TProperty> Create(Expression <Func <TEntity, TProperty> > propertyExpression, bool probeForJsonRefDataSidProperties = false) { if (propertyExpression == null) { throw new ArgumentNullException("propertyExpression"); } if (propertyExpression.Body.NodeType != ExpressionType.MemberAccess) { throw new InvalidOperationException("Only Member access expressions are supported."); } var me = (MemberExpression)propertyExpression.Body; // Check cache and reuse as this is an expensive operation. var key = new ExpressionKey { Type = me.Member.DeclaringType, Name = me.Member.Name, ProbeForJsonRefDataSidProperties = probeForJsonRefDataSidProperties }; if (_expressions.ContainsKey(key)) { return(_expressions[key]); } if (me.Member.MemberType != MemberTypes.Property) { throw new InvalidOperationException("Expression results in a Member that is not a Property."); } if (!me.Member.DeclaringType.GetTypeInfo().IsAssignableFrom(typeof(TEntity).GetTypeInfo())) { throw new InvalidOperationException("Expression results in a Member for a different Entity class."); } var pe = new PropertyExpression <TEntity, TProperty>() { Name = me.Member.Name }; // Either get the friendly text from a corresponding DisplayTextAttribute or split the PascalCase member name into friendlier sentence case text. DisplayAttribute ca = me.Member.GetCustomAttribute <DisplayAttribute>(true); pe.Text = ca == null?Beef.CodeGen.CodeGenerator.ToSentenceCase(pe.Name) : ca.Name; // Get the JSON property name. JsonPropertyAttribute jpa = me.Member.GetCustomAttribute <JsonPropertyAttribute>(true); if (jpa == null && probeForJsonRefDataSidProperties) { // Probe corresponding Sid or Sids properties for value (using the standardised naming convention). var pi = me.Member.DeclaringType.GetProperty($"{pe.Name}Sid"); if (pi == null) { pi = me.Member.DeclaringType.GetProperty($"{pe.Name}Sids"); } if (pi != null) { jpa = pi.GetCustomAttribute <JsonPropertyAttribute>(true); } } pe.JsonPropertyAttribute = jpa; pe.JsonName = jpa == null ? pe.Name : jpa.PropertyName; // Compile the expression. pe._func = propertyExpression.Compile(); // Recheck cache and use/update accordingly. lock (_lock) { if (_expressions.ContainsKey(key)) { return(_expressions[key]); } _expressions.Add(key, pe); } return(pe); }