Esempio n. 1
0
        public ObjectDecorationValueEvaluatorTest()
        {
            FunctionBinder binder = new FunctionBinder(new Type[] { typeof(BuiltInFunctions) });
            ExpressionEvaluationContext <object> context = new ExpressionEvaluationContext <object>(
                VariableMock.GetGlobalVariable, VariableMock.GetLocalVariable, binder, null);

            _evaluator = new ObjectDecorationInterpreter <object>(context);
        }
Esempio n. 2
0
 public ExpressionEvaluationContext(Func <string, string> globalEvaluator,
                                    Func <string, T, object> localEvaluator,
                                    FunctionBinder functionBinder,
                                    ILogger logger)
 {
     _globalEvaluator = globalEvaluator;
     _localEvaluator  = localEvaluator;
     _functionBinder  = functionBinder;
     _logger          = logger;
 }
Esempio n. 3
0
        private static Task <FunctionExecutor> GetAsync(string functionDir, string functionJsonFileName, TraceWriter log)
        {
            TaskCompletionSource <FunctionExecutor> tcs;

            lock (_d) {
                if (_d.TryGetValue(functionJsonFileName, out tcs))
                {
                    return(tcs.Task);
                }
                _d.Add(functionJsonFileName, tcs = new TaskCompletionSource <FunctionExecutor>());
            }

            try {
                var functionJsonLastWriteTime = File.GetLastWriteTimeUtc(functionJsonFileName);
                var functionJson = JObject.Parse(File.ReadAllText(functionJsonFileName));

                var metadata = functionJson["cloudPad"];

                var applicationBase  = (string)metadata["applicationBase"];
                var scriptFile       = (string)metadata["scriptFile"];
                var typeName         = (string)metadata["typeName"];
                var methodName       = (string)metadata["methodName"];
                var providerName     = (string)metadata["providerName"];
                var connectionString = (string)metadata["connectionString"];

                log.Info($"InitializingFunction ApplicationBase='{applicationBase}', ScriptFile='{scriptFile}'", nameof(FunctionExecutor));

                var root     = Path.GetFullPath(Path.Combine(functionDir, applicationBase));
                var fullPath = Path.Combine(root, scriptFile);
                var dir      = Path.GetDirectoryName(fullPath);

                // binder...
                UpdateAssemblyResolveHandler(functionDir, root);

                log.Info($"ScriptFileLoad '{fullPath}'", nameof(FunctionExecutor));

                var assembly = Assembly.LoadFrom(fullPath);
                var type     = assembly.GetType(typeName);
                var method   = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
                var func     = FunctionBinder.Bind(method);

                // launch cleanup task

                var f = new FunctionExecutor(func, functionJsonLastWriteTime, dir, providerName: providerName, connectionString: connectionString);

                tcs.SetResult(f);
            } catch (Exception ex) {
                tcs.SetException(ex);
            }

            return(tcs.Task);
        }
        public TextDecorationExEvaluator(string objectDecoration,
                                         Func <string, string> evaluateVariable,
                                         Func <string, IEnvelope, object> evaluateRecordVariable,
                                         ILogger logger
                                         )
        {
            _tree = TextDecorationParserFacade.ParseTextDecoration(objectDecoration);
            FunctionBinder binder = new FunctionBinder(new Type[] { typeof(BuiltInFunctions) });
            ExpressionEvaluationContext <IEnvelope> evalContext = new ExpressionEvaluationContext <IEnvelope>(
                evaluateVariable,
                evaluateRecordVariable,
                binder,
                logger
                );
            var validator = new TextDecorationValidator <IEnvelope>(evalContext);

            validator.Visit(_tree, null); //Should throw if cannot resolve function
            _interpreter = new TextDecorationInterpreter <IEnvelope>(evalContext);
        }
Esempio n. 5
0
 public static Func <T1, T2> Function <T1, T2>(IVar <T1> input, IExpr <T2> output,
                                               OrderedDictionary /*<TensorExpr.Shared, TensorExpr>*/ updates = null, IDictionary givens = null, string name = null)
 {
     return(FunctionBinder.Function(input, output, updates, givens, name: name));
 }
Esempio n. 6
0
 public static Func <R> Function <R>(IExpr <R> output,
                                     OrderedDictionary updates = null, IDictionary givens = null, string name = null)
 {
     return(FunctionBinder.Function(output, updates, givens, name: name));
 }
        public void Build(DeclarationSyntax declaration, ContainerBinder containingScope)
        {
            declaration.Match()
                .With<NamespaceSyntax>(@namespace =>
                {
                    var imports = @namespace.UsingDirectives.SelectMany(u => GatherImportedSymbols(u, containingScope));
                    var namesCount = @namespace.Names.Count;
                    for(var i = 0; i < namesCount; i++)
                    {
                        var name = @namespace.Names[i];
                        var last = i == namesCount - 1;
                        var reference = containingScope.GetMembers(name.ValueText).OfType<NamespaceReference>().Single();
                        containingScope = new NamespaceBinder(containingScope, reference, last ? imports : Enumerable.Empty<ImportedSymbol>());
                        // The innermost binder is the one that has the imports and should be associated with the syntax node
                        if(last)
                            binders.Add(@namespace, containingScope);
                    }

                    foreach(var member in @namespace.Members)
                        Build(member, containingScope);
                })
                .With<ClassSyntax>(@class =>
                {
                    var scope = new ClassBinder(containingScope, @class);
                    binders.Add(@class, scope);
                    foreach(var member in @class.Members)
                        Build(member, scope);
                })
                .With<FunctionSyntax>(function =>
                {
                    foreach(var parameter in function.Parameters)
                        Build(parameter.Type.Type, containingScope);
                    // TODO deal with return type
                    Binder scope = new FunctionBinder(containingScope);
                    binders.Add(function, scope);
                    foreach(var statement in function.Body)
                        scope = Build(statement, scope);
                })
                //		.With<VariableDeclaration>(global =>
                //		{
                //			global.Type.BindNames(scope);
                //			global.InitExpression?.BindNames(scope);
                //		})
                .Exhaustive();
        }
 private void Build(ClassMemberSyntax member, ClassBinder containingScope)
 {
     member.Match()
         .With<FieldSyntax>(field =>
         {
             Build(field.Type.Type, containingScope);
             if(field.InitExpression != null)
                 Build(field.InitExpression, containingScope);
         })
         .With<ConstructorSyntax>(constructor =>
         {
             foreach(var parameter in constructor.Parameters)
                 Build(parameter.Type.Type, containingScope);
             Binder scope = new FunctionBinder(containingScope);
             binders.Add(constructor, scope);
             foreach(var statement in constructor.Body)
                 scope = Build(statement, scope);
         })
         .With<DestructorSyntax>(destructor =>
         {
             foreach(var parameter in destructor.Parameters)
                 if(parameter.Type != null)
                     Build(parameter.Type.Type, containingScope);
             Binder scope = new FunctionBinder(containingScope);
             binders.Add(destructor, scope);
             foreach(var statement in destructor.Body)
                 scope = Build(statement, scope);
         })
         .With<IndexerMethodSyntax>(indexer =>
         {
             foreach(var parameter in indexer.Parameters)
                 if(parameter.Type != null)
                     Build(parameter.Type.Type, containingScope);
             // TODO deal with return type
             Binder scope = new FunctionBinder(containingScope);
             binders.Add(indexer, scope);
             foreach(var statement in indexer.Body)
                 scope = Build(statement, scope);
         })
         .With<MethodSyntax>(method =>
         {
             foreach(var parameter in method.Parameters)
                 if(parameter.Type != null)
                     Build(parameter.Type.Type, containingScope);
             // TODO deal with return type
             Binder scope = new FunctionBinder(containingScope);
             binders.Add(method, scope);
             foreach(var statement in method.Body)
                 scope = Build(statement, scope);
         })
         .Exhaustive();
 }
Esempio n. 9
0
        internal Expression GetExecutionPlan(Expression expression)
        {
            LambdaExpression lambda = expression as LambdaExpression;

            if (lambda != null)
            {
                expression = lambda.Body;
            }

            var translation = expression;

            translation = PartialEvaluator.Eval(expression, ExpressionHelper.CanBeEvaluatedLocally);
            translation = FunctionBinder.Bind(this, translation);
            //translation = PartialEvaluator.Eval(translation, ExpressionHelper.CanBeEvaluatedLocally);
            translation = QueryBinder.Bind(ExpressionBuilder, this, translation);


            translation = AggregateRewriter.Rewrite(Dialect, translation);
            translation = UnusedColumnRemover.Remove(translation);
            translation = RedundantColumnRemover.Remove(translation);
            translation = RedundantSubqueryRemover.Remove(translation);
            translation = RedundantJoinRemover.Remove(translation);

            var bound = RelationshipBinder.Bind(ExpressionBuilder, translation);

            if (bound != translation)
            {
                translation = bound;
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }
            translation = ComparisonRewriter.Rewrite(ExpressionBuilder, translation);

            var rewritten = RelationshipIncluder.Include(ExpressionBuilder, this, translation);

            if (rewritten != translation)
            {
                translation = rewritten;
                translation = UnusedColumnRemover.Remove(translation);
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantSubqueryRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }

            rewritten = SingletonProjectionRewriter.Rewrite(this.ExpressionBuilder, translation);
            if (rewritten != translation)
            {
                translation = rewritten;
                translation = UnusedColumnRemover.Remove(translation);
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantSubqueryRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }

            rewritten = ClientJoinedProjectionRewriter.Rewrite(this, translation);
            if (rewritten != translation)
            {
                translation = rewritten;
                translation = UnusedColumnRemover.Remove(translation);
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantSubqueryRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }

            //
            translation = this.ExpressionBuilder.Translate(translation);

            var        parameters = lambda != null ? lambda.Parameters : null;
            Expression provider   = Find(expression, parameters, typeof(InternalDbContext));

            if (provider == null)
            {
                Expression rootQueryable = Find(expression, parameters, typeof(IQueryable));
                provider = Expression.Property(rootQueryable, typeof(IQueryable).GetProperty("Provider"));
            }

            return(ExecutionBuilder.Build(this.Dialect, this, translation, provider));
        }
Esempio n. 10
0
 public void Bind(BindingContext context, FunctionBinder binder)
 {
     Body.Bind(context, binder);
     NumLocals = binder.Scope.NumVariables;
 }