コード例 #1
0
ファイル: PythonCondition.cs プロジェクト: aalmada/bonsai
        public override IObservable <TSource> Process <TSource>(IObservable <TSource> source)
        {
            return(Observable.Defer(() =>
            {
                var engine = PythonEngine.Create();
                var scope = engine.CreateScope();
                engine.Execute(Script, scope);

                object condition;
                PythonProcessor <TSource, bool> processor;
                if (PythonHelper.TryGetClass(scope, "Condition", out condition))
                {
                    processor = new PythonProcessor <TSource, bool>(engine.Operations, condition);
                }
                else
                {
                    processor = new PythonProcessor <TSource, bool>(scope);
                }

                if (processor.Load != null)
                {
                    processor.Load();
                }

                var result = source.Where(processor.Process);
                if (processor.Unload != null)
                {
                    result = result.Finally(processor.Unload);
                }

                return result;
            }));
        }
コード例 #2
0
ファイル: PythonSink.cs プロジェクト: spacelabswc/bonsai
        public override IObservable <TSource> Process <TSource>(IObservable <TSource> source)
        {
            return(Observable.Defer(() =>
            {
                var scriptTask = new Task(() => { });
                scriptTask.Start();

                var engine = CreateEngine();
                var scope = engine.CreateScope();
                engine.Execute(Script, scope);

                object sink;
                PythonProcessor <TSource, object> processor;
                if (PythonHelper.TryGetClass(scope, "Sink", out sink))
                {
                    processor = new PythonProcessor <TSource, object>(engine.Operations, sink);
                }
                else
                {
                    processor = new PythonProcessor <TSource, object>(scope);
                }

                if (processor.Load != null)
                {
                    processor.Load();
                }

                return source.Do(input =>
                {
                    scriptTask = scriptTask.ContinueWith(task =>
                    {
                        processor.Process(input);
                    });
                }).Finally(() =>
                {
                    var unloadAction = processor.Unload;
                    if (unloadAction != null)
                    {
                        scriptTask = scriptTask.ContinueWith(task => unloadAction());
                    }

                    engine.Runtime.IO.OutputWriter.Close();
                    scriptTask.Wait();
                });
            }));
        }
コード例 #3
0
        public override Expression Build(IEnumerable <Expression> arguments)
        {
            var engine       = PythonEngine.Create();
            var scope        = engine.CreateScope();
            var script       = PythonHelper.ReturnsDecorator + Script;
            var scriptSource = engine.CreateScriptSourceFromString(script);

            scriptSource.Execute(scope);

            var scopeExpression    = Expression.Constant(scope);
            var outputType         = PythonHelper.GetOutputType(scope, PythonHelper.GenerateFunction);
            var generatorType      = Expression.GetFuncType(typeof(PythonGenerator));
            var generateExpression = Expression.Call(
                scopeExpression,
                "GetVariable",
                new[] { generatorType },
                Expression.Constant(PythonHelper.GenerateFunction));

            var combinatorExpression = Expression.Constant(this);

            return(Expression.Call(combinatorExpression, "Generate", new[] { outputType }, generateExpression));
        }
コード例 #4
0
        public override Expression Build(IEnumerable <Expression> arguments)
        {
            var engine       = PythonEngine.Create();
            var scope        = engine.CreateScope();
            var script       = PythonHelper.ReturnsDecorator + Script;
            var scriptSource = engine.CreateScriptSourceFromString(script);

            scriptSource.Execute(scope);

            object selectMany;
            var    source         = arguments.Single();
            var    observableType = source.Type.GetGenericArguments()[0];

            if (PythonHelper.TryGetClass(scope, "SelectMany", out selectMany))
            {
                var classExpression = Expression.Constant(selectMany);
                var opExpression    = Expression.Constant(engine.Operations);
                var outputType      = PythonHelper.GetOutputType(engine.Operations, selectMany, PythonHelper.ProcessFunction);
                return(Expression.Call(
                           typeof(PythonTransform),
                           "Process",
                           new[] { observableType, outputType },
                           source,
                           opExpression,
                           classExpression));
            }
            else
            {
                var outputType      = PythonHelper.GetOutputType(scope, PythonHelper.ProcessFunction);
                var scopeExpression = Expression.Constant(scope);
                return(Expression.Call(
                           typeof(PythonSelectMany),
                           "Process",
                           new[] { observableType, outputType },
                           source,
                           scopeExpression));
            }
        }