Example #1
0
        private void AddPieceFunction(int pieceKey, CalculateFunctionType funcType,
                                      Dictionary <string, string> parameters)
        {
            ICalculateWay newCalculateWay = null;

            switch (funcType)
            {
            case CalculateFunctionType.Constrant:
                newCalculateWay = new ConstCalculateWay();
                break;

            case CalculateFunctionType.Liner:
                newCalculateWay = new LinerCalculateWay();
                break;

            case CalculateFunctionType.Power:
                newCalculateWay = new PowerCalculateWay();
                break;

            case CalculateFunctionType.Ln:
                newCalculateWay = new LnCalculateWay();
                break;
            }

            if (newCalculateWay == null || !newCalculateWay.InitParameter(parameters))
            {
                return;
            }
            PieceWise[pieceKey] = newCalculateWay;
            Prepare();
        }
        private async Task <Dictionary <int, ICalculateWay> > GetDefaultPieceWiseFunctionAsync()
        {
            var tempCache = _cacheCacheProvider.GetPieceWiseFunctionFromNormalCache();

            if (tempCache != null)
            {
                return(tempCache);
            }

            var chain = await _blockchainService.GetChainAsync();

            var tokenStub = _tokenStTokenContractReaderFactory.Create(new ChainContext
            {
                BlockHash   = chain.LastIrreversibleBlockHash,
                BlockHeight = chain.LastIrreversibleBlockHeight
            });

            CalculateFeeCoefficientsOfType parameters;

            if (CalculateAlgorithmContext.CalculateFeeTypeEnum == (int)FeeTypeEnum.Tx)
            {
                parameters = await tokenStub.GetCalculateFeeCoefficientOfSender.CallAsync(new Empty());
            }
            else
            {
                parameters = await tokenStub.GetCalculateFeeCoefficientOfContract.CallAsync(new SInt32Value
                                                                                            { Value = CalculateAlgorithmContext.CalculateFeeTypeEnum });
            }
            var calWayDic = new Dictionary <int, ICalculateWay>();

            foreach (var func in parameters.Coefficients)
            {
                ICalculateWay newCalculateWay = null;
                switch (func.FunctionType)
                {
                case CalculateFunctionTypeEnum.Liner:
                    newCalculateWay = new LinerCalculateWay();
                    break;

                case CalculateFunctionTypeEnum.Power:
                    newCalculateWay = new PowerCalculateWay();
                    break;
                }

                if (newCalculateWay == null)
                {
                    Logger.LogWarning($"could not find mapped function type {func.FunctionType}");
                    continue;
                }

                var funDicToLowerKey = func.CoefficientDic.ToDictionary(x => x.Key.ToLower(), x => x.Value);
                calWayDic[func.PieceKey] = newCalculateWay;
                calWayDic[func.PieceKey].InitParameter(funDicToLowerKey);
            }
            _cacheCacheProvider.SetPieceWiseFunctionToNormalCache(calWayDic);
            return(calWayDic);
        }
Example #3
0
 public ICalculateAlgorithm Add(int limit, ICalculateWay func)
 {
     // to do
     PieceWise[limit] = func;
     return(this);
 }