Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Possibility.cs
1using System.Collections;
2using System.Collections.Generic;
3using System.Text;
6
8{
13 {
19 public Possibility(string VariableName, ISemanticElement Value)
20 : this(VariableName, Value, null)
21 {
22 }
23
31 {
32 this.Name = VariableName;
33 this.Value = Value ?? new NullValue();
34 this.NextVariable = NextVariable;
35 }
36
40 public string Name { get; }
41
45 public ISemanticElement Value { get; set; }
46
50 public Possibility NextVariable { get; private set; }
51
57 public ISemanticElement this[string VariableName]
58 {
59 get => this.GetValue(VariableName);
60 set => this.SetValue(VariableName, value);
61 }
62
68 public ISemanticElement GetValue(string VariableName)
69 {
70 if (this.Name == VariableName)
71 return this.Value;
72
73 Possibility Loop = this.NextVariable;
74 while (!(Loop is null))
75 {
76 if (Loop.Name == VariableName)
77 return Loop.Value;
78
79 Loop = Loop.NextVariable;
80 }
81
82 return null;
83 }
84
90 public void SetValue(string VariableName, ISemanticElement Value)
91 {
92 if (this.Name == VariableName)
93 this.Value = Value;
94 else
95 {
96 Possibility Loop = this;
97
98 while (!(Loop.NextVariable is null) && Loop.NextVariable.Name != VariableName)
99 Loop = Loop.NextVariable;
100
101 if (Loop.NextVariable is null)
102 Loop.NextVariable = new Possibility(VariableName, Value);
103 else
104 Loop.NextVariable.Value = Value;
105 }
106 }
107
112 public override string ToString()
113 {
114 return GetSortedDescription(this);
115 }
116
123 public static string GetSortedDescription(Possibility P)
124 {
125 SortedDictionary<string, ISemanticElement> Sorted = new SortedDictionary<string, ISemanticElement>();
126
127 while (!(P is null))
128 {
129 Sorted[P.Name] = P.Value;
130 P = P.NextVariable;
131 }
132
133 StringBuilder sb = new StringBuilder();
134 bool First = true;
135
136 foreach (KeyValuePair<string, ISemanticElement> P2 in Sorted)
137 {
138 if (First)
139 First = false;
140 else
141 sb.Append(", ");
142
143 sb.Append(P2.Key);
144 sb.Append(':');
145 sb.Append(P2.Value.ToString());
146 }
147
148 return sb.ToString();
149 }
150
155 public IEnumerator<ISparqlResultItem> GetEnumerator()
156 {
157 return new PossibilityEnumerator(this);
158 }
159
164 IEnumerator IEnumerable.GetEnumerator()
165 {
166 return new PossibilityEnumerator(this);
167 }
168
169 private class PossibilityEnumerator : IEnumerator<ISparqlResultItem>
170 {
171 private readonly Possibility first;
172 private Possibility current;
173
174 public PossibilityEnumerator(Possibility First)
175 {
176 this.first = First;
177 this.current = null;
178 }
179
180 public ISparqlResultItem Current => this.current;
181 object IEnumerator.Current => this.current;
182
183 public void Dispose() { }
184
185 public bool MoveNext()
186 {
187 if (this.current is null)
188 this.current = this.first;
189 else
190 this.current = this.current.NextVariable;
191
192 return !(this.current is null);
193 }
194
195 public void Reset()
196 {
197 this.current = null;
198 }
199 }
200 }
201}
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
IEnumerator< ISparqlResultItem > GetEnumerator()
Gets an enumerator for all items in the possibility.
Definition: Possibility.cs:155
ISemanticElement GetValue(string VariableName)
Access to possible variable values, given a variable name.
Definition: Possibility.cs:68
static string GetSortedDescription(Possibility P)
Gets a text description of the sorted set of possibilities, starting with a given possibility P .
Definition: Possibility.cs:123
void SetValue(string VariableName, ISemanticElement Value)
Sets a variable value, given its variable name.
Definition: Possibility.cs:90
ISemanticElement Value
Variable value.
Definition: Possibility.cs:45
Possibility NextVariable
Previous possiblity
Definition: Possibility.cs:50
override string ToString()
Shows the collection of possible values as a string.
Definition: Possibility.cs:112
Possibility(string VariableName, ISemanticElement Value, Possibility NextVariable)
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:30
Possibility(string VariableName, ISemanticElement Value)
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:19
Interface for semantic nodes.
Interface for items in a record from the results of a SPARQL query.
Interface for result records of a SPARQL query.