private void ReduceOne(IReducable[] arguments, ExpressionPath expression)
        {
            var varName = AddVariable(expression.Code);

            for (var index = 0; index < arguments.Length; index++)
            {
                var src = arguments[index];
                src.UpdateFromReduction(expression, varName);
            }
        }
        public void UpdateFromReduction(ExpressionPath expression, string varName)
        {
            if (_override != null)
            {
                return;
            }
            var full = FullMethods;

            if (expression.Equals(full))
            {
                _override = varName;
                return;
            }
            Path.UpdateFromReduction(expression, varName);
            if (Kind != Kind2.Method)
            {
                return;
            }
            foreach (var i in Arguments)
            {
                i.Reduce(expression, varName);
            }
            // Arguments                = Arguments.Select(dict.Reduce).ToArray();
        }
Exemple #3
0
        public void UpdateFromReduction(ExpressionPath expression, string varName)
        {
            if (!(_override is null))
            {
                return;
            }
            if (expression.Code == Code)
            {
                _override = varName;
                return;
            }

            foreach (var i in ConstructorArgs)
            {
                if (i is IReducable te)
                {
                    te.UpdateFromReduction(expression, varName);
                }
                else
                {
                    throw new System.NotImplementedException();
                }
            }
        }
        private void TryCreateInternal()
        {
            if (_args.Input.Is <PlanarDensity, Length, LinearDensity>("*"))
            {
                Debug.Write("");
            }

            var cc = _args.Input;
            var lu = cc.LeftMethodArgumentName + ".Unit";
            var ru = cc.RightMethodArgumentName + ".Unit";

            Scan(ValueOfSomeTypeExpressionRoot.Left, cc.Left.Unit, Kind2.Property, ExpressionPath.FromSplit(lu));
            Scan(ValueOfSomeTypeExpressionRoot.Right, cc.Right.Unit, Kind2.Property, ExpressionPath.FromSplit(ru));

            _conversionMethodScanner = ConversionMethodScanner.Scan(_resolver.Assembly);

            var reductor = new ExpressionsReductor(n =>
            {
                string varName = null;
                if (n == lu || n == ru)
                {
                    varName = n.Replace(".", "");
                }
                return(AddVariable(n, varName));
            });

            var convertType = Construct(cc.Right.Unit);

            if (convertType.Code == ru)
            {
                convertType = null;
            }
            else
            {
                reductor.AddAny(convertType);
            }
            // convertType.AddToDeleteMe(reductor);

            ICodeSource result = Construct(cc.Result.Unit);

            Func <string> addExtraValueMultiplication = null;

            ICodeSource G1()
            {
                if (!_resolver.TryGetValue(cc.Result.Unit.TypeName, out var type1))
                {
                    throw new NotImplementedException();
                }
                if (_conversionMethodScanner.Dictionary.TryGetValue(type1, out var list))
                {
                    var typesDict = TypeFinder.Make(_sink);
                    foreach (var i in list)
                    {
                        var aaa = typesDict.FindParameters(i, null, out var hl);
                        return(MethodCallCodeSource.Make(i, aaa, hl));
                    }
                }

                return(null);
            }

            if (result is null)
            {
                result = G1();
                if (result != null)
                {
                    addExtraValueMultiplication = () => { return(result.Code + "." + nameof(IUnitDefinition.Multiplication)); };
                }
            }

            if (result is null)
            {
            }
            reductor.AddAny(result);
            //result.AddToDeleteMe(reductor);
            reductor.ReduceExpressions();
            if (addExtraValueMultiplication != null)
            {
                reductor.ForceReduce(new ExpressionPath(result));
            }


            // =============================
            if (result?.Code == "specificHeatCapacity.Unit.DenominatorUnit")
            {
                Debug.WriteLine("");
            }
            switch (convertType)
            {
            case null:
                break;

            case MethodCallCodeSource _:
                _args.Result.ConvertRight(AddVariable(convertType.Code, "targetRightUnit"));
                break;

            default:
                _args.Result.ConvertRight(convertType.Code);
                break;
            }

            if (result is MethodCallCodeSource)
            {
                _args.Result.ResultUnit = AddVariable(result.Code, "resultUnit");
            }
            else
            {
                _args.Result.ResultUnit = result.Code;
            }
            if (addExtraValueMultiplication != null)
            {
                _args.Result.ResultMultiplication = addExtraValueMultiplication();
            }
        }
        private void Scan(ValueOfSomeTypeExpressionRoot root, XUnitTypeName x, Kind2 kind, ExpressionPath path = null)
        {
            if (!_resolver.TryGetValue(x.GetTypename(), out var type))
            {
                throw new NotImplementedException();
            }
            var info1 = new ValueOfSomeTypeExpression(root, type, new TreeExpression(path, null, kind), kind);

            _sink.Add(info1);
            if (kind != Kind2.Property)
            {
                return;
            }
            foreach (var propInfo in type.GetProperties())
            {
                var attribute = propInfo.GetCustomAttribute <RelatedUnitSourceAttribute>();
                if (attribute != null && attribute.Usage == RelatedUnitSourceUsage.DoNotUse)
                {
                    continue;
                }
                var pt = propInfo.PropertyType;
                if (!pt.Implements <IUnit>())
                {
                    continue;
                }
                Scan(root, new XUnitTypeName(propInfo.PropertyType.Name), kind, path + propInfo.Name);
            }

            foreach (var methodInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                var at = methodInfo.GetCustomAttribute <RelatedUnitSourceAttribute>();
                if (at is null || (at.Usage & RelatedUnitSourceUsage.ProvidesRelatedUnit) == 0)
                {
                    continue;
                }
                var returnType = methodInfo.ReturnType;
                if (!returnType.Implements <IUnit>())
                {
                    throw new Exception("Should return IUnit");
                }
                if (methodInfo.GetParameters().Length != 0)
                {
                    throw new Exception("Should be parameterles");
                }
                Scan(root, new XUnitTypeName(returnType.Name), Kind2.Method, path + $"{methodInfo.Name}");
            }
        }
 public void UpdateFromReduction(ExpressionPath expression, string varName)
 {
     TreeCode.UpdateFromReduction(expression, varName);
 }
        public void ForceReduce(ExpressionPath expression)
        {
            var reducedArray = _expressions.ToArray();

            ReduceOne(reducedArray, expression);
        }
 public TreeExpression(ExpressionPath path, IReadOnlyList <ExpressionPath> arguments, Kind2 kind)
 {
     Path      = path;
     Kind      = kind;
     Arguments = arguments ?? new ExpressionPath[0];
 }
Exemple #9
0
 public KeyWithCount(ExpressionPath key, int count)
 {
     Key   = key;
     Count = count;
 }