|
1 | | -using System; |
2 | | -using System.Linq; |
3 | | -using System.ComponentModel; |
4 | | -using System.Reactive.Linq; |
5 | | - |
6 | | -namespace Bonsai.Scripting |
7 | | -{ |
8 | | - [Obsolete] |
9 | | - [DefaultProperty("Script")] |
10 | | - [WorkflowElementCategory(ElementCategory.Condition)] |
11 | | - [Description("A Python script used to determine which elements of the input sequence are accepted.")] |
12 | | - public class PythonCondition : Combinator |
13 | | - { |
14 | | - public PythonCondition() |
15 | | - { |
16 | | - Script = "def process(value):\n return True"; |
17 | | - } |
18 | | - |
19 | | - [Editor("Bonsai.Scripting.PythonScriptEditor, Bonsai.Scripting", DesignTypes.UITypeEditor)] |
20 | | - [Description("The script that determines the criteria for the condition.")] |
21 | | - public string Script { get; set; } |
22 | | - |
23 | | - public override IObservable<TSource> Process<TSource>(IObservable<TSource> source) |
24 | | - { |
25 | | - return Observable.Defer(() => |
26 | | - { |
27 | | - var engine = PythonEngine.Create(); |
28 | | - var scope = engine.CreateScope(); |
29 | | - engine.Execute(Script, scope); |
30 | | - |
31 | | - object condition; |
32 | | - PythonProcessor<TSource, bool> processor; |
33 | | - if (PythonHelper.TryGetClass(scope, "Condition", out condition)) |
34 | | - { |
35 | | - processor = new PythonProcessor<TSource, bool>(engine.Operations, condition); |
36 | | - } |
37 | | - else processor = new PythonProcessor<TSource, bool>(scope); |
38 | | - |
39 | | - if (processor.Load != null) |
40 | | - { |
41 | | - processor.Load(); |
42 | | - } |
43 | | - |
44 | | - var result = source.Where(processor.Process); |
45 | | - if (processor.Unload != null) |
46 | | - { |
47 | | - result = result.Finally(processor.Unload); |
48 | | - } |
49 | | - |
50 | | - return result; |
51 | | - }); |
52 | | - } |
53 | | - } |
54 | | -} |
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Reactive.Linq; |
| 5 | + |
| 6 | +namespace Bonsai.Scripting |
| 7 | +{ |
| 8 | + [Obsolete] |
| 9 | + [DefaultProperty("Script")] |
| 10 | + [WorkflowElementCategory(ElementCategory.Condition)] |
| 11 | + [Description("A Python script used to determine which elements of the input sequence are accepted.")] |
| 12 | + public class PythonCondition : Combinator |
| 13 | + { |
| 14 | + public PythonCondition() |
| 15 | + { |
| 16 | + Script = "def process(value):\n return True"; |
| 17 | + } |
| 18 | + |
| 19 | + [Editor("Bonsai.Scripting.PythonScriptEditor, Bonsai.Scripting", DesignTypes.UITypeEditor)] |
| 20 | + [Description("The script that determines the criteria for the condition.")] |
| 21 | + public string Script { get; set; } |
| 22 | + |
| 23 | + public override IObservable<TSource> Process<TSource>(IObservable<TSource> source) |
| 24 | + { |
| 25 | + return Observable.Defer(() => |
| 26 | + { |
| 27 | + var engine = PythonEngine.Create(); |
| 28 | + var scope = engine.CreateScope(); |
| 29 | + engine.Execute(Script, scope); |
| 30 | + |
| 31 | + PythonProcessor<TSource, bool> processor; |
| 32 | + if (PythonHelper.TryGetClass(scope, "Condition", out object condition)) |
| 33 | + { |
| 34 | + processor = new PythonProcessor<TSource, bool>(engine.Operations, condition); |
| 35 | + } |
| 36 | + else processor = new PythonProcessor<TSource, bool>(scope); |
| 37 | + |
| 38 | + if (processor.Load != null) |
| 39 | + { |
| 40 | + processor.Load(); |
| 41 | + } |
| 42 | + |
| 43 | + var result = source.Where(processor.Process); |
| 44 | + if (processor.Unload != null) |
| 45 | + { |
| 46 | + result = result.Finally(processor.Unload); |
| 47 | + } |
| 48 | + |
| 49 | + return result; |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments