Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GroupGraph.cs
1using System.Text;
2using System.Text.RegularExpressions;
3using System.Threading.Tasks;
4using SkiaSharp;
6using Waher.Script;
7
9{
16 public class GroupGraph : CommandRegEx
17 {
24 public GroupGraph()
25 : base(@"^groupgraph\s*.*$")
26 {
27 }
28
32 public override string Name => "GroupGraph";
33
42 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
43 ResponseCallbackHandler ResponseCallback)
44 {
45 string Script = OrgMessage.Substring(10).Trim();
46 object Result;
47 SKColor Color;
48
49 if (string.IsNullOrEmpty(Script))
50 {
51 Result = null;
52 Color = SKColors.Empty;
53 }
54 else
55 {
56 Expression Exp = new Expression(Script);
57 Result = await Exp.EvaluateAsync(State.Session);
58 Result = TopologyQuery.Simplify(Result, out Color);
59 }
60
61 StringBuilder sb = new StringBuilder();
62 string Title = IoTGateway.Gateway.XmppClient.BareJID;
63
64 sb.AppendLine("```dot");
65 sb.AppendLine("digraph G {");
66 sb.AppendLine("rankdir=LR");
67
68 TopologyQuery.EmbedResult(sb, Title, Result, Color);
69
70 foreach (RosterItem Item in IoTGateway.Gateway.XmppClient.Roster)
71 {
72 foreach (string Group in Item.Groups)
73 {
74 sb.Append('"');
75 sb.Append(Title);
76 sb.Append("\" -> \"");
77 sb.Append(Item.BareJid);
78 sb.Append("\" [label=\"");
79 sb.Append(Group);
80 sb.AppendLine("\"];");
81 }
82 }
83
84 sb.AppendLine("}");
85 sb.AppendLine("```");
86
87 await ResponseCallback(sb.ToString(), string.Empty);
88 }
89
93 public override HelpItem[] GetHelp()
94 {
95 return new HelpItem[]
96 {
97 new HelpItem("GroupGraph[ SCRIPT]", "Returns a GraphViz graph of the current broker and how it is connected to roster items with groups. Such a graph illustrates information flow authorized by group membership. Optional script can be provided, and its result embedded in the graph. Consolidating responses from multiple brokers generates a groups graph of all brokers.")
98 };
99 }
100 }
101}
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
string[] Groups
Any groups the roster item belongs to.
Definition: RosterItem.cs:186
string BareJid
Bare JID of the roster item.
Definition: RosterItem.cs:276
Class managing a script expression.
Definition: Expression.cs:39
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4275
readonly Variables Session
Session variables
Definition: ChatState.cs:41
An administrative command whose syntax is validated with a regular expression.
Definition: CommandRegEx.cs:12
Contains an item of information about a command.
Definition: HelpItem.cs:9
Returns a GraphViz graph of the current broker and how it is connected to roster items with groups....
Definition: GroupGraph.cs:17
override HelpItem[] GetHelp()
Markdown description of syntax.
Definition: GroupGraph.cs:93
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
Definition: GroupGraph.cs:42
GroupGraph()
Returns a GraphViz graph of the current broker and how it is connected to roster items with groups....
Definition: GroupGraph.cs:24
Executes script (if provided), and returns the result in the form of a GraphViz graph of the current ...
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.