예제 #1
0
        private static AlgebraExpression SimplifySpecificKind(AlgebraExpression expression, CancellationToken cancellationToken)
        {
            AlgebraExpression simplified = expression;
            AlgebraExpression lastResult;

            // find applicable simplifiers
            IReadOnlyCollection <object> simplifiers = GetSimplifiers(expression);

            do
            {
                lastResult = simplified;

                // query each simplifier
                foreach (var simplifier in simplifiers)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var original = simplified;

                    MethodInfo method = simplifier.GetType().GetRuntimeMethod(nameof(ISimplifier <AlgebraExpression> .Simplify), new[] { expression.GetType(), typeof(CancellationToken) });
                    simplified = method.Invoke(simplifier, new object[] { simplified, cancellationToken }) as AlgebraExpression;

                    // trace
                    if (!original.Equals(simplified))
                    {
                        Debug.WriteLine($"Simplified '{original}' to '{simplified}' using '{simplifier}'");
                    }

                    // stop if not the same kind of expression
                    if (lastResult.GetType() != simplified.GetType())
                    {
                        return(simplified);
                    }
                }
            } while (!lastResult.Equals(simplified));

            return(simplified);
        }
예제 #2
0
 private static IReadOnlyCollection <object> GetSimplifiers(AlgebraExpression expression)
 {
     return(FindSimplifiers(expression.GetType())
            .Select(Activator.CreateInstance)
            .ToList());
 }