Beispiel #1
0
 public FuncDef(AsyncFn func, int minArity, int maxArity, string name)
 {
     this.func         = func;
     kind              = FuncKind.AsyncFn;
     this.minArity     = minArity;
     this.maxArity     = maxArity;
     this.flags        = FuncFlags.Defaults;
     flagsArgCanBeLazy = flagsArgCanBeVector = 0xFFFFFFFF;
     this.name         = name;
 }
Beispiel #2
0
 public IEnumerable <FuncDef> GetFunc(string name, int arity)
 {
     if (name == null)
     {
         var ctx = this;
         while (ctx != null)
         {
             foreach (var v in ctx.values)
             {
                 var fd = v as FuncDef;
                 if (fd != null)
                 {
                     yield return(fd);
                 }
             }
             ctx = ctx.parent;
         }
     }
     else
     {
         int i = IndexOf(name);
         if (i >= 0)
         {
             var v = this[i];
             if (v is FuncDef)
             {
                 var def = (FuncDef)v;
                 if (def.minArity <= arity && arity <= def.maxArity)
                 {
                     yield return(def);
                 }
             }
             else if (v is LazyAsync)
             {
                 AsyncFn f =
                     (ae, args) =>
                 {
                     return(OPs._call_func(ae, i, args));
                 };
                 yield return(new FuncDef(f, 0, int.MaxValue, name));
             }
         }
     }
     for (int i = externalFuncs.Count - 1; i >= 0; i--)
     {
         foreach (var fd in externalFuncs[i](name, arity))
         {
             yield return(fd);
         }
     }
 }
Beispiel #3
0
 public CallExpr(AsyncFn func, params Expr[] args) : this((Delegate)func, args)
 {
 }
Beispiel #4
0
 public CallExpr(AsyncFn func, IList <Expr> args) : this((Delegate)func, args)
 {
 }
Beispiel #5
0
        public static async Task <object> CalcAsync(AsyncExprCtx ae, object arg, int nIns, int nOuts, AsyncFn calc, params int[] indexesOfNotNullableArgs)
        {
            var dict = (W.Common.IIndexedDict)arg;

            try
            {
                var data = dict.ValuesList;
                if (data.NotNullsAt(indexesOfNotNullableArgs) == false)
                {
                    return(null);
                }
                var maxTime = DateTime.MinValue;
                foreach (ITimedObject to in data)
                {
                    if (to != null && !to.IsEmpty && maxTime < to.Time)
                    {
                        maxTime = to.Time;
                    }
                }
                var res = await calc(ae, data);

                if (res == null)
                {
                    return(null);
                }
                int n = data.Length;
                if (maxTime > DateTime.MinValue)
                {
                    var lst = res as IList;
                    if (lst != null)
                    {
                        for (int i = lst.Count - 1; i >= 0; i--)
                        {
                            lst[i] = TimedObject.TryAsTimed(lst[i], maxTime, DateTime.MaxValue);
                        }
                    }
                    else
                    {
                        res = TimedObject.TryAsTimed(res, maxTime, DateTime.MaxValue);
                    }
                }
                if (n == nIns)
                {
                    return(res);
                }
                else
                {
                    var outs = new object[nOuts + n - nIns];
                    var lst  = res as IList;
                    if (lst != null)
                    {
                        lst.CopyTo(outs, 0);
                    }
                    else
                    {
                        outs[0] = res;
                    }
                    for (int i = n - 1; i >= nIns; i--)
                    {
                        outs[nOuts + i - nIns] = data[i];
                    }
                    return(outs);
                }
            }
            catch (Exception ex)
            {
                Utils.ToLog(ex.Message, dict);
                return(ex.Wrap());
            }
        }
Beispiel #6
0
        public static async Task Load(IDictionary <string, object> dataExprsAndCookies, DateTime StartDate, DateTime EndDate,
                                      long[] ids, string idParamName, string contextInitializationScript,
                                      OnDataLoading onDataLoading,
                                      Action <float> onProgress,
                                      int maxPartSize,
                                      System.Threading.CancellationToken ct)
        {
            bool timeRange         = StartDate < EndDate;
            bool withAggr          = ids.Length > 1;
            int  nTotalPartsToLoad = 0;
            int  nPartsLoaded      = 0;

            AsyncFn fProgress = async(aec, args) =>
            {
                if (!ct.IsCancellationRequested)
                {
                    var info = (LoadingInfo)await OPs.ConstValueOf(aec, args[0]);

                    var value = await OPs.ConstValueOf(aec, args[1]);

                    var data = value as IList;
                    if (data != null)
                    {
                        var aggData = await info.Aggregate(aec, data);

                        onDataLoading(info.dataExpr, info.cookie, (IList)aggData);
                        Interlocked.Increment(ref nPartsLoaded);
                    }
                    else // calc n
                    {
                        int n = Convert.ToInt32(value);
                        Interlocked.Add(ref nTotalPartsToLoad, n);
                    }
                    if (onProgress != null)
                    {
                        onProgress(100f * nPartsLoaded / nTotalPartsToLoad);
                    }
                }
                return(string.Empty);
            };

            // initialize code generator context
            var defs = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            defs.Add(idParamName, ids);
            defs.Add("PROGRESS", new FuncDef(fProgress, 2, "PROGRESS"));
            if (timeRange)
            {
                defs.Add(nameof(ValueInfo.A_TIME__XT), StartDate.ToOADate());
                defs.Add(nameof(ValueInfo.B_TIME__XT), EndDate.ToOADate());
            }
            else
            {
                defs.Add(nameof(ValueInfo.At_TIME__XT), StartDate.ToOADate());
            }
            var rootCtx = new Generator.Ctx(defs, CoreFuncDefs().GetFuncs);

            Generator.Generate(Parser.ParseToExpr(contextInitializationScript), rootCtx);
            rootCtx.UseFuncs(new FuncDefs().AddFrom(withAggr ? typeof(FuncDefs_Aggr) : typeof(FuncDefs_NoAggr)).GetFuncs);

            var infos = new List <LoadingInfo>(dataExprsAndCookies.Count);
            var tasks = new List <Task>();

            foreach (var pair in dataExprsAndCookies)
            {
                infos.Add(new LoadingInfo(pair.Key, pair.Value, rootCtx));
            }

            var loadingExpr = Parser.ParseToExpr(@"(
	PartsOfLimitedSize("     + idParamName + " ," + maxPartSize.ToString() + @")
	..let(parts)
	,let(n,COLUMNS(parts)) .. PROGRESS("     + sLoadingInfo + @"),
	,_ParFor(
		let(i,0),
		i<n,
		(
			let("             + idParamName + @", parts[i])
			,FindSolutionExpr({}, "             + sUsedParamsDict + @".Keys()) . ExprToExecutable() . AtNdx(0) .. PROGRESS(" + sLoadingInfo + @")
			,let(i, i+1)
		)
	)
)");
            var rootAec     = new AsyncExprRootCtx(rootCtx.name2ndx, rootCtx.values, OPs.GlobalMaxParallelismSemaphore);

            foreach (var info in infos)
            {
                tasks.Add(Task.Factory.StartNew(() => info.LoadData(rootAec, loadingExpr, ct)).Unwrap());
            }
            await TaskEx.WhenAll(tasks);
        }
Beispiel #7
0
 public FuncDef(AsyncFn func, int arity, string name) : this(func, arity, arity, name)
 {
 }