-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathAndOr.cs
115 lines (103 loc) · 3.98 KB
/
AndOr.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections.Generic;
using System.Linq;
using Autodesk.DesignScript.Runtime;
using CoreNodeModels.Properties;
using Dynamo.Graph.Nodes;
using Newtonsoft.Json;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM;
namespace CoreNodeModels.Logic
{
/// <summary>
/// Abstract base class for short-circuiting binary logic operators.
/// </summary>
[SupressImportIntoVM]
public abstract class BinaryLogic : VariableInputNode
{
private readonly Operator _op;
/// <summary>
/// Private constructor used for serialization.
/// </summary>
/// <param name="inPorts">A collection of <see cref="PortModel"/> objects.</param>
/// <param name="outPorts">A collection of <see cref="PortModel"/> objects.</param>
protected BinaryLogic(Operator op, IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts)
{
_op = op;
}
protected BinaryLogic(Operator op)
{
_op = op;
InPorts.Add(new PortModel(PortType.Input, this, new PortData("bool0", Resources.PortDataOperandToolTip)));
InPorts.Add(new PortModel(PortType.Input, this, new PortData("bool1", Resources.PortDataOperandToolTip)));
OutPorts.Add(new PortModel(PortType.Output, this, new PortData("", Resources.PortDataResultToolTip)));
RegisterAllPorts();
}
public override IEnumerable<AssociativeNode> BuildOutputAst(
List<AssociativeNode> inputAstNodes)
{
var inputs = inputAstNodes as IEnumerable<AssociativeNode>;
return new[]
{
AstFactory.BuildAssignment(
GetAstIdentifierForOutputIndex(0),
inputs.Reverse()
.Aggregate(
(current, node) =>
AstFactory.BuildBinaryExpression(node, current, _op)))
};
}
protected override void RemoveInput()
{
if (InPorts.Count > 2)
base.RemoveInput();
}
protected override string GetInputName(int index)
{
return "bool" + index;
}
protected override string GetInputTooltip(int index)
{
return "Boolean #" + index;
}
}
/// <summary>
/// Short-circuiting Logical AND
/// </summary>
[NodeName("And")]
[NodeCategory("Core.Math")]
[NodeDescription("AndDescription", typeof(Resources))]
[IsDesignScriptCompatible]
[OutPortTypes("bool")]
[AlsoKnownAs("DSCore.Logic.And", "DSCoreNodesUI.Logic.And")]
public class And : BinaryLogic
{
/// <summary>
/// Private constructor used for serialization.
/// </summary>
/// <param name="inPorts">A collection of <see cref="PortModel"/> objects.</param>
/// <param name="outPorts">A collection of <see cref="PortModel"/> objects.</param>
[JsonConstructor]
private And(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base( Operator.and, inPorts, outPorts) { }
public And() : base(Operator.and) { }
}
/// <summary>
/// Short-circuiting Logical OR
/// </summary>
[NodeName("Or")]
[NodeCategory("Core.Math")]
[NodeDescription("OrDescription", typeof(Resources))]
[IsDesignScriptCompatible]
[OutPortTypes("bool")]
[AlsoKnownAs("DSCore.Logic.Or", "DSCoreNodesUI.Logic.Or")]
public class Or : BinaryLogic
{
/// <summary>
/// Private constructor used for serialization.
/// </summary>
/// <param name="inPorts">A collection of <see cref="PortModel"/> objects.</param>
/// <param name="outPorts">A collection of <see cref="PortModel"/> objects.</param>
[JsonConstructor]
private Or(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base( Operator.or, inPorts, outPorts) { }
public Or() : base(Operator.or) { }
}
}