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 System.Web;
3using Waher.Content;
11
13{
18 {
19 private readonly PhysicalQuantity value;
20 private readonly string stringType;
21
26 : base(string.Empty, string.Empty)
27 {
28 }
29
35 : base(Value.Magnitude, CommonTypes.Encode(Value.Magnitude))
36 {
37 this.value = Value;
38 this.stringType = IoTSensorData.UnitNamespace + HttpUtility.UrlEncode(this.value?.Unit.ToString() ?? string.Empty);
39 }
40
44 public override string StringType => this.stringType;
45
51 public override ISemanticLiteral Encapsulate(object Value)
52 {
53 if (Value is IPhysicalQuantity Typed)
54 return new QuantityLiteral(Typed.ToPhysicalQuantity());
55 else
56 return new StringLiteral(Value?.ToString() ?? string.Empty);
57 }
58
60 public override bool Equals(object obj)
61 {
62 return obj is QuantityLiteral Typed &&
63 (this.value?.Equals(Typed.value) ?? Typed.value is null);
64 }
65
67 public override int GetHashCode()
68 {
69 return this.value.GetHashCode();
70 }
71
75 public override object AssociatedObjectValue => this.value;
76
84 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
85 {
86 if (DataType.StartsWith(IoTSensorData.UnitNamespace, StringComparison.CurrentCultureIgnoreCase) &&
87 CommonTypes.TryParse(Value, out double Magnitude) &&
88 Unit.TryParse(HttpUtility.UrlDecode(DataType[IoTSensorData.UnitNamespace.Length..]), out Unit Unit2))
89 {
90 return new QuantityLiteral(new PhysicalQuantity(Magnitude, Unit2));
91 }
92 else
93 return new CustomLiteral(Value, DataType, Language);
94 }
95
101 public override Grade Supports(string DataType)
102 {
103 if (DataType.StartsWith(IoTSensorData.UnitNamespace, StringComparison.CurrentCultureIgnoreCase) &&
104 Unit.TryParse(HttpUtility.UrlDecode(DataType[IoTSensorData.UnitNamespace.Length..]), out _))
105 {
106 return Grade.Ok;
107 }
108 else
109 return Grade.NotAtAll;
110 }
111
117 public override Grade Supports(Type ValueType)
118 {
119 return ValueType == typeof(PhysicalQuantity) ? Grade.Ok : Grade.NotAtAll;
120 }
121
133 public override int CompareTo(object obj)
134 {
135 if (obj is QuantityLiteral Typed)
136 return this.value.CompareTo(Typed.value);
137
138 return base.CompareTo(obj);
139 }
140 }
141}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Abstract base class for semantic literal values.
Represents a unit.
Definition: Unit.cs:16
override string ToString()
Definition: Unit.cs:535
static bool TryParse(string UnitString, out Unit Unit)
Tries to parse a string into a unit.
Definition: Unit.cs:120
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.
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7