-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathExpressionOperationViewModel.cs
59 lines (52 loc) · 1.58 KB
/
ExpressionOperationViewModel.cs
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
using StringMath;
using System.Collections.Generic;
using System.Linq;
namespace Nodify.Calculator
{
public class ExpressionOperationViewModel : OperationViewModel
{
private MathExpr? _expr;
private string? _expression;
public string? Expression
{
get => _expression;
set => SetProperty(ref _expression, value)
.Then(GenerateInput);
}
private void GenerateInput()
{
try
{
_expr = Expression!.ToMathExpr();
ConnectorViewModel[]? toRemove = Input.Where(i => !_expr.LocalVariables.Contains(i.Title)).ToArray();
toRemove.ForEach(i => Input.Remove(i));
HashSet<string> existingVars = Input.Select(s => s.Title).Where(s => s != null).ToHashSet()!;
foreach (string variable in _expr.LocalVariables.Except(existingVars))
{
Input.Add(new ConnectorViewModel
{
Title = variable
});
}
OnInputValueChanged();
}
catch
{
}
}
protected override void OnInputValueChanged()
{
if (Output != null && _expr != null)
{
try
{
Input.ForEach(i => _expr.Substitute(i.Title!, i.Value));
Output.Value = _expr.Result;
}
catch
{
}
}
}
}
}