forked from bonsai-rx/expression-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionSource.cs
More file actions
78 lines (70 loc) · 3.19 KB
/
ExpressionSource.cs
File metadata and controls
78 lines (70 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Bonsai.Expressions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Linq;
namespace Bonsai.Scripting.Expressions
{
/// <summary>
/// Represents an operator that uses an expression script to generate an
/// observable sequence with a single element.
/// </summary>
[DefaultProperty(nameof(Expression))]
[WorkflowElementCategory(ElementCategory.Source)]
[TypeDescriptionProvider(typeof(ExpressionSourceTypeDescriptionProvider))]
[Description("An expression script used to generate a sequence with a single element.")]
public class ExpressionSource : ZeroArgumentExpressionBuilder, IScriptingElement
{
/// <summary>
/// Gets or sets the name of the expression source.
/// </summary>
[Externalizable(false)]
[Category(nameof(CategoryAttribute.Design))]
[Description("The name of the expression source.")]
public string Name { get; set; }
/// <summary>
/// Gets or sets a description for the expression source.
/// </summary>
[Externalizable(false)]
[Category(nameof(CategoryAttribute.Design))]
[Description("A description for the expression source.")]
[Editor(DesignTypes.MultilineStringEditor, DesignTypes.UITypeEditor)]
public string Description { get; set; }
/// <summary>
/// Gets or sets the expression that generates the singleton element.
/// </summary>
/// <remarks>
/// The <c>it</c> parameter stores the singleton <see cref="Unit"/> object
/// representing no data.
/// </remarks>
[Editor("Bonsai.Scripting.Expressions.Design.ExpressionScriptEditor, Bonsai.Scripting.Expressions.Design", DesignTypes.UITypeEditor)]
[Description("The expression that generates the singleton element.")]
public string Expression { get; set; } = "it";
/// <inheritdoc/>
public override Expression Build(IEnumerable<Expression> arguments)
{
var config = ParsingConfigHelper.CreateParsingConfig(typeof(Unit));
var generator = DynamicExpressionHelper.ParseLambda(config, typeof(Unit), null, Expression);
return System.Linq.Expressions.Expression.Call(typeof(ExpressionSource), nameof(Process), new[] { generator.ReturnType }, generator);
}
static IObservable<TResult> Process<TResult>(Func<Unit, TResult> generator)
{
return Observable.Return(Unit.Default).Select(generator);
}
class ExpressionSourceTypeDescriptionProvider : TypeDescriptionProvider
{
static readonly TypeDescriptionProvider parentProvider = TypeDescriptor.GetProvider(typeof(ExpressionSource));
public ExpressionSourceTypeDescriptionProvider()
: base(parentProvider)
{
}
public override ICustomTypeDescriptor GetExtendedTypeDescriptor(object instance)
{
return new ScriptingElementTypeDescriptor(instance,
"An expression script used to generate a sequence with a single element.");
}
}
}
}