Antares Simulator
Power System Simulator
AstDOTStyleVisitor.h
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #pragma once
22 
23 #include <map>
24 #include <ostream>
25 
26 #include "antares/expressions/visitors/NodeVisitor.h"
27 
28 namespace Antares::Expressions::Visitors
29 {
37 struct BoxStyle
38 {
45  std::string_view color;
46 
53  std::string_view shape;
54 
61  std::string_view style;
62 };
63 
76 class AstDOTStyleVisitor: public NodeVisitor<void, std::ostream&>
77 {
78 public:
82  AstDOTStyleVisitor() = default;
83 
92  static void NewTreeGraph(std::ostream& os, const std::string& tree_name = "ExpressionTree");
93 
101  void EndTreeGraph(std::ostream& os);
102 
108  std::string name() const override;
109 
119  void operator()(std::ostream& os, const Nodes::Node* root);
120 
121 private:
122  void visit(const Nodes::SumNode* node, std::ostream& os) override;
123  void visit(const Nodes::SubtractionNode* node, std::ostream& os) override;
124  void visit(const Nodes::MultiplicationNode* node, std::ostream& os) override;
125  void visit(const Nodes::DivisionNode* node, std::ostream& os) override;
126  void visit(const Nodes::EqualNode* node, std::ostream& os) override;
127  void visit(const Nodes::LessThanOrEqualNode* node, std::ostream& os) override;
128  void visit(const Nodes::GreaterThanOrEqualNode* node, std::ostream& os) override;
129  void visit(const Nodes::NegationNode* node, std::ostream& os) override;
130  void visit(const Nodes::VariableNode* node, std::ostream& os) override;
131  void visit(const Nodes::ParameterNode* node, std::ostream& os) override;
132  void visit(const Nodes::LiteralNode* node, std::ostream& os) override;
133  void visit(const Nodes::PortFieldNode* node, std::ostream& os) override;
134  void visit(const Nodes::PortFieldSumNode* node, std::ostream& os) override;
135  void visit(const Nodes::TimeShiftNode* node, std::ostream& os) override;
136  void visit(const Nodes::TimeIndexNode* node, std::ostream& os) override;
137  void visit(const Nodes::TimeSumNode* node, std::ostream& os) override;
138  void visit(const Nodes::AllTimeSumNode* node, std::ostream& os) override;
139  void visit(const Nodes::FunctionNode* node, std::ostream& os) override;
140 
141  void computeNumberNodesPerType();
142  void makeLegend(std::ostream& os);
143 
152  unsigned int getNodeID(const Nodes::Node* node);
153 
165  static void emitNode(unsigned int id,
166  const std::string& label,
167  const BoxStyle& box_style,
168  std::ostream& os);
169 
181  void processParentNode(const Nodes::ParentNode* node,
182  const std::string& label,
183  const BoxStyle& box_style,
184  std::ostream& os);
185 
191  std::map<const Nodes::Node*, unsigned int> nodeIds_;
192 
198  std::map<std::string, unsigned int> nbNodesPerType_;
199 
205  unsigned int nodeCount_ = 0;
206 };
207 } // namespace Antares::Expressions::Visitors
Represents a AllTimeSumNode node in a syntax tree.
Definition: AllTimeSumNode.h:31
Represents a division node in a syntax tree.
Definition: DivisionNode.h:31
Represents an equality comparison node in a syntax tree.
Definition: EqualNode.h:31
AST node representing a function expression (max, min, pow, ...).
Definition: FunctionNode.h:63
Represents a greater than or equal comparison node in a syntax tree.
Definition: GreaterThanOrEqualNode.h:31
Represents a less than or equal comparison node in a syntax tree.
Definition: LessThanOrEqualNode.h:31
Represents a literal node in a syntax tree, storing a double value.
Definition: LiteralNode.h:11
Represents a multiplication node in a syntax tree.
Definition: MultiplicationNode.h:31
Represents a negation node in a syntax tree.
Definition: NegationNode.h:31
Base class for nodes in a syntax tree.
Definition: Node.h:30
Represents a parameter node in a syntax tree, storing a string value.
Definition: ParameterNode.h:14
Definition: ParentNode.h:41
Represents a port field node in a syntax tree.
Definition: PortFieldNode.h:32
Represents a port field node where the expression is a sum.
Definition: PortFieldSumNode.h:32
Represents a subtraction node in a syntax tree.
Definition: SubtractionNode.h:31
Definition: SumNode.h:29
Definition: TimeIndexNode.h:28
Definition: TimeShiftNode.h:29
Definition: TimeSumNode.h:28
Represents a variable node in a syntax tree, storing a string value.
Definition: VariableNode.h:20
A visitor class for generating DOT style output for ASTs (Abstract Syntax Trees).
Definition: AstDOTStyleVisitor.h:77
AstDOTStyleVisitor()=default
Default constructor.
void EndTreeGraph(std::ostream &os)
Ends the current tree graph.
Definition: AstDOTStyleVisitor.cpp:244
static void NewTreeGraph(std::ostream &os, const std::string &tree_name="ExpressionTree")
Begins a new tree graph.
Definition: AstDOTStyleVisitor.cpp:238
std::string name() const override
Returns the name of this visitor.
Definition: AstDOTStyleVisitor.cpp:189
void operator()(std::ostream &os, const Nodes::Node *root)
Outputs the DOT representation of a node to a stream.
Definition: AstDOTStyleVisitor.cpp:261
Represents the style attributes for a box in a graph.
Definition: AstDOTStyleVisitor.h:38
std::string_view style
The style of the box.
Definition: AstDOTStyleVisitor.h:61
std::string_view shape
The shape of the box.
Definition: AstDOTStyleVisitor.h:53
std::string_view color
The color of the box.
Definition: AstDOTStyleVisitor.h:45