2using System.Collections.Generic;
5using System.Threading.Tasks;
20 private readonly LinkedList<KeyValuePair<DateTime, double>> statistics =
new LinkedList<KeyValuePair<DateTime, double>>();
22 private string script;
23 private StringBuilder times =
null;
24 private StringBuilder values =
null;
26 private DateTime start;
27 private DateTime stop;
29 private Unit unit =
null;
30 private int count = 0;
31 private long last = 0;
51 public string For => this.@
for;
68 get => this.bucketTime;
69 set => this.bucketTime = value;
92 public override Task
FromXml(XmlElement Definition)
96 if (Definition.HasAttribute(
"timeVariable"))
98 this.times =
new StringBuilder();
99 this.times.Append(Definition.GetAttribute(
"timeVariable"));
100 this.times.Append(
":=[");
103 this.values =
new StringBuilder();
105 if (Definition.HasAttribute(
"valueVariable"))
106 this.values.Append(Definition.GetAttribute(
"valueVariable"));
108 this.values.Append(this.@
for);
110 this.values.Append(
":=[");
112 this.script = Values.Script.RemoveIndent(Definition.InnerText);
114 return Task.CompletedTask;
124 this.stop = this.start + this.bucketTime;
135 DateTime Timestamp = DateTime.Now;
143 return this.
Sample(Timestamp, v);
152 DateTime Timestamp = DateTime.Now;
160 return this.
Sample(Timestamp, v);
173 if (this.unit is
null)
175 this.unit = Value.
Unit;
179 throw new Exception(
"Incompatible units: " + Value.
Unit.
ToString() +
" and " +
this.unit.ToString());
181 return this.
Sample(Timestamp, v);
190 public DateTime
Sample(DateTime Timestamp,
double Value)
192 if (this.filter?.Filter(ref Timestamp, ref Value) ??
false)
197 while (Timestamp >= this.stop)
199 this.start = this.stop;
200 this.stop = this.start + this.bucketTime;
203 if (!(this.times is
null))
206 this.times.Append(
',');
211 if (!(this.values is
null))
214 this.values.Append(
',');
219 this.statistics.AddLast(
new KeyValuePair<DateTime, double>(Timestamp, Value));
240 return this.
Sample(Timestamp, v);
255 public void ExportXml(XmlWriter Output,
string RowElement)
261 Output.WriteStartElement(RowElement);
262 Output.WriteAttributeString(
"type", this.@
for);
263 Output.WriteAttributeString(
"count", this.count.ToString());
265 if (!(this.unit is
null))
266 Output.WriteAttributeString(
"unit", this.unit.
ToString());
268 foreach (KeyValuePair<DateTime, double> Rec
in this.statistics)
270 Output.WriteStartElement(
"Stat");
271 Output.WriteAttributeString(
"ts",
XML.
Encode(Rec.Key));
273 Output.WriteEndElement();
276 Output.WriteEndElement();
290 Output.WriteLine(
"{");
294 if (!(this.times is
null))
296 Output.Write(this.times.ToString());
297 Output.WriteLine(
"];");
300 if (!(this.values is
null))
302 Output.Write(this.values.ToString());
303 Output.WriteLine(
"];");
307 Output.WriteLine(
"GraphWidth:=1000;");
308 Output.WriteLine(
"GraphHeight:=400;");
310 Output.WriteLine(
";");
311 Output.WriteLine(
"}");
325 string s = this.script.Trim();
326 if (!
string.IsNullOrEmpty(s) &&
";|<>/\\]}".IndexOf(s[s.Length - 1]) >= 0)
327 s = s.Substring(0, s.Length - 1);
340 if (this.filter is
null)
341 this.filter = Filter;
343 this.filter.
Append(Filter);
Root node of a simulation model
DateTime StartTime
Start time
Duration BucketTime
Time to collect events, for statistical purposes.
Defines a custom graph (for a counter, variable, sample, etc.)
override Task Start()
Starts the node.
DateTime Dec()
Decrements counter.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
void ExportXml(XmlWriter Output, string RowElement)
Exports data to XML
string For
If the graph represents the visualization of a given entity. (Otherwise, null, or the empty string....
override bool ExportGraphScript(StreamWriter Output, string CustomColor, bool Span)
Exports the graph to a markdown output.
Duration BucketTime
Time to accumulate values.
long TotalCount
Total Counter
void Flush()
Terminates the ongoing collection of data.
override string LocalName
Local name of XML element defining contents of class.
override bool ParseChildren
If children are to be parsed by FromXml(XmlElement)
override void ExportGraph(StreamWriter Output)
Exports the graph to a markdown output.
DateTime Inc()
Increments counter.
void Add(IFilter Filter)
Adds a filter to the bucket.
CustomGraph(ISimulationNode Parent, Model Model)
Defines a custom graph (for a counter, variable, sample, etc.)
DateTime Sample(DateTime Timestamp, PhysicalQuantity Value)
Adds a sample
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
DateTime Sample(DateTime Timestamp, double Value)
Adds a sample
DateTime CountOccurrence(DateTime Timestamp)
Counts one occurrence
Abstract base class for graph nodes
ISimulationNode Parent
Parent node in the simulation model.
Helps with parsing of commong data types.
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
static string Encode(string s)
Encodes a string for use in XML.
double Magnitude
Magnitude
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.
override string ToString()
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
Interface for custom graph nodes
Interface for sample filters
void Append(IFilter Filter)
Appends a filter to the current filter.
Represents a duration value, as defined by the xsd:duration data type: http://www....