Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SemanticElements.cs
1using System.Collections.Generic;
2using System;
7
9{
14 {
15 private static readonly Dictionary<Type, ISemanticLiteral> literalPerType = new Dictionary<Type, ISemanticLiteral>();
16 private static readonly Dictionary<string, ISemanticLiteral> dataTypes = new Dictionary<string, ISemanticLiteral>();
17
18 static SemanticElements()
19 {
20 Types.OnInvalidated += (Sender, e) =>
21 {
22 lock (literalPerType)
23 {
24 literalPerType.Clear();
25 }
26
27 lock (dataTypes)
28 {
29 dataTypes.Clear();
30 }
31 };
32 }
33
37 public static readonly SemanticElements Instance = new SemanticElements();
38
43 {
44 }
45
51 public override bool Contains(IElement Element)
52 {
53 return Element is ISemanticElement;
54 }
55
59 public override int? Size => null;
60
68 public static ISemanticElement Parse(string Value, string DataType, string Language)
69 {
70 ISemanticLiteral LiteralType;
71
72 lock (dataTypes)
73 {
74 if (!dataTypes.TryGetValue(DataType, out LiteralType))
75 {
76 LiteralType = Types.FindBest<ISemanticLiteral, string>(DataType)
77 ?? new CustomLiteral(string.Empty, DataType);
78
79 dataTypes[DataType] = LiteralType;
80 }
81 }
82
83 return LiteralType.Parse(Value, DataType, Language);
84 }
85
91 public static ISemanticElement Encapsulate(object Value)
92 {
93 if (Value is ISemanticElement Element)
94 return Element;
95
96 if (Value is Uri Uri)
97 return new UriNode(Uri, Uri.OriginalString);
98
99 return EncapsulateLiteral(Value);
100 }
101
107 public static ISemanticLiteral EncapsulateLiteral(object Value)
108 {
109 if (Value is null)
110 return new UndefinedLiteral();
111
112 Type T = Value?.GetType() ?? typeof(object);
113 ISemanticLiteral Literal;
114
115 lock (literalPerType)
116 {
117 if (!literalPerType.TryGetValue(T, out Literal))
118 {
119 Literal = Types.FindBest<ISemanticLiteral, Type>(T)
120 ?? new CustomLiteral(string.Empty, string.Empty);
121
122 literalPerType[T] = Literal;
123 }
124 }
125
126 return Literal.Encapsulate(Value);
127 }
128
130 public override bool Equals(object obj)
131 {
132 return obj.GetType() == typeof(SemanticElements);
133 }
134
136 public override int GetHashCode()
137 {
138 return typeof(SemanticElements).GetHashCode();
139 }
140
147 public int Compare(IElement x, IElement y)
148 {
149 if (x is IComparable Left)
150 return Left.CompareTo(y);
151 else
152 return 1;
153 }
154 }
155}
override bool Contains(IElement Element)
Checks if the set contains an element.
static ISemanticLiteral EncapsulateLiteral(object Value)
Encapsulates an object as a semantic literal.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
int Compare(IElement x, IElement y)
Compares two elements.
static readonly SemanticElements Instance
Instance reference to the set of semantic elements.
override bool Equals(object obj)
Compares the element to another. If elements are equal.
override? int Size
Size of set, if finite and known, otherwise null is returned.
static ISemanticElement Encapsulate(object Value)
Encapsulates an object as a semantic element.
static ISemanticElement Parse(string Value, string DataType, string Language)
Parses a string literal value.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
Base class for all types of elements.
Definition: Element.cs:13
Element()
Base class for all types of elements.
Definition: Element.cs:17
Base class for all types of sets.
Definition: Set.cs:14
Interface for semantic nodes.
Interface for semantic literals.
ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
ISemanticLiteral Parse(string Value, string DataType, string Language)
Tries to parse a string value of the type supported by the class..
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11