Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
ControlElementContainer.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
19
20#include<map>
21
22#include "ControlElement.h"
23
24#include "../../editors/ControlEditor.h"
25
26ControlElementContainer::ControlElementContainer()
27{
28 ClearContainer();
29}
30ControlElementContainer::~ControlElementContainer()
31{
32 //for (auto* cElement : m_cLineList) delete cElement;
33 //for (auto* cElement : m_constantList) delete cElement;
34 //for (auto* cElement : m_exponentialList) delete cElement;
35 //for (auto* cElement : m_gainList) delete cElement;
36 //for (auto* cElement : m_ioControlList) delete cElement;
37 //for (auto* cElement : m_limiterList) delete cElement;
38 //for (auto* cElement : m_multiplierList) delete cElement;
39 //for (auto* cElement : m_rateLimiterList) delete cElement;
40 //for (auto* cElement : m_sumList) delete cElement;
41 //for (auto* cElement : m_tfList) delete cElement;
42 //for (auto* cElement : m_dividerList) delete cElement;
43 //for (auto* cElement : m_mathExprList) delete cElement;
44 //ClearContainer();
45}
46void ControlElementContainer::FillContainer(ControlEditor* editor)
47{
48 ClearContainer();
49 m_ctrlElementsList = editor->GetControlElementList();
50 m_cLineList = editor->GetConnectionLineList();
51
52 for (auto& cElement : m_ctrlElementsList) {
53 if (Constant* constant = dynamic_cast<Constant*>(cElement.get())) {
54 m_constantList.push_back(constant);
55 }
56 else if (Exponential* exponential = dynamic_cast<Exponential*>(cElement.get())) {
57 m_exponentialList.push_back(exponential);
58 }
59 else if (Gain* gain = dynamic_cast<Gain*>(cElement.get())) {
60 m_gainList.push_back(gain);
61 }
62 else if (IOControl* ioControl = dynamic_cast<IOControl*>(cElement.get())) {
63 m_ioControlList.push_back(ioControl);
64 }
65 else if (Limiter* limiter = dynamic_cast<Limiter*>(cElement.get())) {
66 m_limiterList.push_back(limiter);
67 }
68 else if (Multiplier* multiplier = dynamic_cast<Multiplier*>(cElement.get())) {
69 m_multiplierList.push_back(multiplier);
70 }
71 else if (RateLimiter* rateLimiter = dynamic_cast<RateLimiter*>(cElement.get())) {
72 m_rateLimiterList.push_back(rateLimiter);
73 }
74 else if (Sum* sum = dynamic_cast<Sum*>(cElement.get())) {
75 m_sumList.push_back(sum);
76 }
77 else if (TransferFunction* tf = dynamic_cast<TransferFunction*>(cElement.get())) {
78 m_tfList.push_back(tf);
79 }
80 else if (Divider* divider = dynamic_cast<Divider*>(cElement.get())) {
81 m_dividerList.push_back(divider);
82 }
83 else if (MathExpression* mathExpr = dynamic_cast<MathExpression*>(cElement.get())) {
84 m_mathExprList.push_back(mathExpr);
85 }
86 }
87}
88
89void ControlElementContainer::ClearContainer()
90{
91 m_cLineList.clear();
92 m_constantList.clear();
93 m_exponentialList.clear();
94 m_gainList.clear();
95 m_ioControlList.clear();
96 m_limiterList.clear();
97 m_multiplierList.clear();
98 m_rateLimiterList.clear();
99 m_sumList.clear();
100 m_tfList.clear();
101 m_dividerList.clear();
102 m_mathExprList.clear();
103}
104
105void ControlElementContainer::FillContainer(std::vector< std::shared_ptr<ControlElement> > controlElementList,
106 std::vector< std::shared_ptr<ConnectionLine> > connectionLineList)
107{
108 ClearContainer();
109 m_ctrlElementsList = controlElementList;
110 m_cLineList = connectionLineList;
111
112 for (auto it = controlElementList.begin(), itEnd = controlElementList.end(); it != itEnd; ++it) {
113 if (Constant* constant = dynamic_cast<Constant*>(it->get())) {
114 m_constantList.push_back(constant);
115 }
116 else if (Exponential* exponential = dynamic_cast<Exponential*>(it->get())) {
117 m_exponentialList.push_back(exponential);
118 }
119 else if (Gain* gain = dynamic_cast<Gain*>(it->get())) {
120 m_gainList.push_back(gain);
121 }
122 else if (IOControl* ioControl = dynamic_cast<IOControl*>(it->get())) {
123 m_ioControlList.push_back(ioControl);
124 }
125 else if (Limiter* limiter = dynamic_cast<Limiter*>(it->get())) {
126 m_limiterList.push_back(limiter);
127 }
128 else if (Multiplier* multiplier = dynamic_cast<Multiplier*>(it->get())) {
129 m_multiplierList.push_back(multiplier);
130 }
131 else if (RateLimiter* rateLimiter = dynamic_cast<RateLimiter*>(it->get())) {
132 m_rateLimiterList.push_back(rateLimiter);
133 }
134 else if (Sum* sum = dynamic_cast<Sum*>(it->get())) {
135 m_sumList.push_back(sum);
136 }
137 else if (TransferFunction* tf = dynamic_cast<TransferFunction*>(it->get())) {
138 m_tfList.push_back(tf);
139 }
140 else if (Divider* divider = dynamic_cast<Divider*>(it->get())) {
141 m_dividerList.push_back(divider);
142 }
143 else if (MathExpression* mathExpr = dynamic_cast<MathExpression*>(it->get())) {
144 m_mathExprList.push_back(mathExpr);
145 }
146 }
147}
148
149void ControlElementContainer::GetContainerCopy(std::vector< std::shared_ptr<ControlElement> >& controlElementList,
150 std::vector< std::shared_ptr<ConnectionLine> >& connectionLineList)
151{
152 controlElementList.clear();
153 connectionLineList.clear();
154
155 std::map<Element*, Element*> cLineMap;
156 std::map<Element*, Element*> cElementMap;
157
158 // Copy connection lines
159 for (auto& cLine : m_cLineList) {
160 ConnectionLine* copyLine = static_cast<ConnectionLine*>(cLine->GetCopy());
161 connectionLineList.emplace_back(copyLine);
162 cLineMap[cLine.get()] = copyLine;
163 }
164
165 // Copy elements (exept connection line).
166 std::map<Node*, Node*> nodeMap;
167 for (auto& cElement : m_ctrlElementsList) {
168 ControlElement* copyElement = static_cast<ControlElement*>(cElement->GetCopy());
169 cElementMap[cElement.get()] = copyElement;
170 controlElementList.emplace_back(copyElement);
171 // Copy nodes.
172 std::vector<Node*> nodeListCopy;
173 for (Node* node : copyElement->GetNodeList()) {
174 Node* copyNode = node->GetCopy();
175 nodeMap[node] = copyNode;
176 nodeListCopy.push_back(copyNode);
177 }
178 copyElement->SetNodeList(nodeListCopy);
179 }
180
181 // Correct the parent and child pointers
182 for (auto& copyElement : controlElementList) {
183 // Child
184 for (Element* child : copyElement->GetChildList()) {
185 auto it = cLineMap.find(child);
186 if (it != cLineMap.end())
187 copyElement->ReplaceChild(child, it->second);
188 }
189 }
190 for (auto& copyLine : connectionLineList) {
191 // Nodes
192 for (Node* node : copyLine->GetNodeList()) {
193 auto it = nodeMap.find(node);
194 if (it != nodeMap.end())
195 copyLine->ReplaceNode(node, it->second);
196 }
197
198 // Parent element
199 for (Element* parent : copyLine->GetParentList()) {
200 auto it = cElementMap.find(parent);
201 if (it != cElementMap.end())
202 copyLine->ReplaceParent(parent, it->second);
203 }
204 // Parent cLine
205 Element* parentLine = static_cast<Element*>(copyLine->GetParentLine());
206 auto it = cLineMap.find(parentLine);
207 if (it != cLineMap.end())
208 copyLine->SetParentLine(static_cast<ConnectionLine*>(it->second));
209 else
210 copyLine->SetParentLine(nullptr);
211
212 // Childs
213 for (Element* child : copyLine->GetChildList()) {
214 auto it = cLineMap.find(child);
215
216 if (it != cLineMap.end())
217 copyLine->ReplaceChild(child, it->second);
218 }
219 }
220
221 //int nodeID = 0;
222 //for (auto it = m_ctrlElementsList.begin(), itEnd = m_ctrlElementsList.end(); it != itEnd; ++it) {
223 // ControlElement* oldElement = *it;
224 // ControlElement* copy = static_cast<ControlElement*>(oldElement->GetCopy());
225 // controlElementList.push_back(copy);
226 // // Copy nodes.
227 // std::vector<Node*> nodeList = copy->GetNodeList();
228 // std::vector<Node*> nodeListCopy;
229 // for (auto itN = nodeList.begin(), itEndN = nodeList.end(); itN != itEndN; ++itN) {
230 // Node* node = *itN;
231 // node->SetID(nodeID);
232 // Node* copyNode = new Node();
233 // *copyNode = *node;
234 // nodeListCopy.push_back(copyNode);
235 // nodeID++;
236 // }
237 // copy->SetNodeList(nodeListCopy);
238 //
239 // // Replace children to copies.
240 // auto childList = copy->GetChildList();
241 // for (auto itC = childList.begin(), itEndC = childList.end(); itC != itEndC; ++itC) {
242 // ConnectionLine* child = static_cast<ConnectionLine*>(*itC);
243 // // Replace child's parent to copy.
244 // for (auto itCL = connectionLineList.begin(), itEndCL = connectionLineList.end(); itCL != itEndCL; ++itCL) {
245 // ConnectionLine* copyLine = *itCL;
246 // if (copyLine->GetID() == child->GetID()) {
247 // // Replace node.
248 // nodeList = child->GetNodeList();
249 // for (auto itN = nodeList.begin(), itEndN = nodeList.end(); itN != itEndN; ++itN) {
250 // Node* node = *itN;
251 // for (auto itCN = nodeListCopy.begin(), itEndCN = nodeListCopy.end(); itCN != itEndCN; ++itCN) {
252 // Node* nodeCopy = *itCN;
253 // if (node->GetID() == nodeCopy->GetID()) {
254 // copyLine->ReplaceNode(node, nodeCopy);
255 // break;
256 // }
257 // }
258 // }
259 // copyLine->ReplaceParent(oldElement, copy);
260 // copy->ReplaceChild(child, copyLine);
261 // }
262 // }
263 // }
264 //}
265}
Base class of a control element. Provide general methods to other control classes.
Connection between two control elements or other connection line and an element.
A control element that provides a constant value.
Definition Constant.h:37
Control element that divides two inputs.
Definition Divider.h:33
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:112
virtual void ReplaceParent(Element *oldParent, Element *newParent)
Replace a parent.
Definition Element.cpp:559
virtual void ReplaceChild(Element *oldChild, Element *newChild)
Replace a child from the list.
Definition Element.cpp:575
Generates an output following an exponential function.
Definition Exponential.h:33
Provide an output multiplying the input by a constant.
Definition Gain.h:37
Provides the communication with the power element.
Definition IOControl.h:43
Limits the input value by superior and inferior values.
Definition Limiter.h:33
A generic math expression block that can perform math and conditional operations with the inputs.
Multiplies two inputs.
Definition Multiplier.h:33
Node of a control element. This class manages the user interaction with the connection and control el...
Limits the rising and/or falling rate.
Definition RateLimiter.h:33
Sum the all inputs (can choose the input signal).
Definition Sum.h:34
Calculates the time response by a frequency domain transfer function.