Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
QuantityLiteral.cs
1using System;
2using Waher.Content;
10
12{
17 {
18 private readonly PhysicalQuantity value;
19 private readonly string stringType;
20
25 : base(string.Empty, string.Empty)
26 {
27 }
28
34 : base(Value.Magnitude, CommonTypes.Encode(Value.Magnitude))
35 {
36 this.value = Value;
37 this.stringType = IoTSensorData.UnitNamespace + this.value?.Unit;
38 }
39
43 public override string StringType => this.stringType;
44
50 public override ISemanticLiteral Encapsulate(object Value)
51 {
52 if (Value is IPhysicalQuantity Typed)
53 return new QuantityLiteral(Typed.ToPhysicalQuantity());
54 else
55 return new StringLiteral(Value?.ToString() ?? string.Empty);
56 }
57
59 public override bool Equals(object obj)
60 {
61 return obj is QuantityLiteral Typed &&
62 (this.value?.Equals(Typed.value) ?? Typed.value is null);
63 }
64
66 public override int GetHashCode()
67 {
68 return this.value.GetHashCode();
69 }
70
74 public override object AssociatedObjectValue => this.value;
75
83 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
84 {
85 if (DataType.StartsWith(IoTSensorData.UnitNamespace, StringComparison.CurrentCultureIgnoreCase) &&
86 CommonTypes.TryParse(Value, out double Magnitude) &&
87 Unit.TryParse(DataType.Substring(IoTSensorData.UnitNamespace.Length), out Unit Unit2))
88 {
89 return new QuantityLiteral(new PhysicalQuantity(Magnitude, Unit2));
90 }
91 else
92 return new CustomLiteral(Value, DataType, Language);
93 }
94
100 public override Grade Supports(string DataType)
101 {
102 if (DataType.StartsWith(IoTSensorData.UnitNamespace, StringComparison.CurrentCultureIgnoreCase) &&
103 Unit.TryParse(DataType.Substring(IoTSensorData.UnitNamespace.Length), out _))
104 {
105 return Grade.Ok;
106 }
107 else
108 return Grade.NotAtAll;
109 }
110
116 public override Grade Supports(Type ValueType)
117 {
118 return ValueType == typeof(PhysicalQuantity) ? Grade.Ok : Grade.NotAtAll;
119 }
120
132 public override int CompareTo(object obj)
133 {
134 if (obj is QuantityLiteral Typed)
135 return this.value.CompareTo(Typed.value);
136
137 return base.CompareTo(obj);
138 }
139 }
140}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Abstract base class for semantic literal values.
Represents a unit.
Definition: Unit.cs:15
static bool TryParse(string UnitString, out Unit Unit)
Tries to parse a string into a unit.
Definition: Unit.cs:137
const string UnitNamespace
urn:nf:iot:sd:1.0:unit:
Semantic literal for physical quantities.
QuantityLiteral()
Semantic literal for physical quantities.
override Grade Supports(Type ValueType)
How well the type supports a given value type.
override ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
override string StringType
Type name (or null if literal value is a string)
override Grade Supports(string DataType)
How well the type supports a given data type.
override ISemanticLiteral Parse(string Value, string DataType, string Language)
Tries to parse a string value of the type supported by the class..
QuantityLiteral(PhysicalQuantity Value)
Semantic literal for physical quantities.
override bool Equals(object obj)
override int CompareTo(object obj)
Compares the current instance with another object of the same type and returns an integer that indica...
override object AssociatedObjectValue
Associated object value.
Interface for semantic literals.
Interface for objects that can be represented as a physical quantity.
Grade
Grade enumeration
Definition: Grade.cs:7