Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SetUnit.cs
6
8{
13 {
14 private readonly Unit unit;
15
26 {
27 this.unit = Unit;
28 }
29
33 public Unit Unit => this.unit;
34
42 {
43 if (Operand.AssociatedObjectValue is double D)
44 return new PhysicalQuantity(D, this.unit);
45
46 if (Operand is Measurement M)
47 {
48 if (Unit.TryConvert(M.Magnitude, M.Unit, this.unit, out double Magnitude) &&
49 Unit.TryConvert(M.Error, M.Unit, this.unit, out double Error))
50 {
51 return new Measurement(Magnitude, this.unit, Error);
52 }
53 else
54 throw new ScriptRuntimeException("Unable to convert from " + M.Unit.ToString() + " to " + this.unit.ToString() + ".", this);
55 }
56
57 if (Operand.AssociatedObjectValue is IPhysicalQuantity PQ)
58 {
60 if (Unit.TryConvert(Q.Magnitude, Q.Unit, this.unit, out double Magnitude))
61 return new PhysicalQuantity(Magnitude, this.unit);
62 else
63 throw new ScriptRuntimeException("Unable to convert from " + Q.Unit.ToString() + " to " + this.unit.ToString() + ".", this);
64 }
65
66 throw new ScriptRuntimeException("Unable to set physical unit.", this);
67 }
68 }
69}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Base class for unary scalar operators.
PhysicalQuantity ToPhysicalQuantity()
Converts underlying object to a physical quantity.
Sets a physical unit
Definition: SetUnit.cs:13
Unit Unit
Unit to set.
Definition: SetUnit.cs:33
SetUnit(ScriptNode Operand, Unit Unit, int Start, int Length, Expression Expression)
Sets a physical unit
Definition: SetUnit.cs:24
override IElement EvaluateScalar(IElement Operand, Variables Variables)
Evaluates the operator on scalar operands.
Definition: SetUnit.cs:41
Represents a unit.
Definition: Unit.cs:15
static bool TryConvert(double From, Unit FromUnit, Unit ToUnit, out double To)
Tries to convert a magnitude in one unit to a magnitude in another.
Definition: Unit.cs:1201
override string ToString()
Definition: Unit.cs:517
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Interface for objects that can be represented as a physical quantity.