-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Equality visitor #2335
Equality visitor #2335
Changes from 6 commits
4a0607b
dec35ae
f08d583
a6fbcf6
f50b714
f92c9a9
fa23284
2942ab4
685ee9e
7da0198
31db247
233c2a1
057dce6
593e334
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
** Copyright 2007-2024, RTE (https://www.rte-france.com) | ||
** See AUTHORS.txt | ||
** SPDX-License-Identifier: MPL-2.0 | ||
** This file is part of Antares-Simulator, | ||
** Adequacy and Performance assessment for interconnected energy networks. | ||
** | ||
** Antares_Simulator is free software: you can redistribute it and/or modify | ||
** it under the terms of the Mozilla Public Licence 2.0 as published by | ||
** the Mozilla Foundation, either version 2 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** Antares_Simulator is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** Mozilla Public Licence 2.0 for more details. | ||
** | ||
** You should have received a copy of the Mozilla Public Licence 2.0 | ||
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>. | ||
*/ | ||
#pragma once | ||
#include <optional> | ||
#include <vector> | ||
|
||
#include <antares/logs/logs.h> | ||
#include <antares/solver/expressions/nodes/NodesForwardDeclaration.h> | ||
|
||
namespace Antares::Solver::Nodes | ||
{ | ||
namespace | ||
{ | ||
template<class RetT, class VisitorT, class NodeT> | ||
std::optional<RetT> tryType(const Node& node1, const Node& node2, VisitorT& visitor) | ||
{ | ||
auto x = dynamic_cast<const NodeT*>(&node1); | ||
auto y = dynamic_cast<const NodeT*>(&node2); | ||
if (x && y) | ||
{ | ||
return visitor.visit(*x, *y); | ||
} | ||
else | ||
{ | ||
return {}; | ||
} | ||
} | ||
} // namespace | ||
|
||
template<class R> | ||
class TwoNodesVisitor | ||
{ | ||
public: | ||
virtual ~TwoNodesVisitor() = default; | ||
|
||
R dispatch(const Node& node1, const Node& node2) | ||
{ | ||
using FunctionT = std::optional<R> (*)(const Node&, const Node&, TwoNodesVisitor<R>&); | ||
a-zakir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::vector<FunctionT> functions{&tryType<R, TwoNodesVisitor<R>, AddNode>, | ||
&tryType<R, TwoNodesVisitor<R>, SubtractionNode>, | ||
&tryType<R, TwoNodesVisitor<R>, MultiplicationNode>, | ||
&tryType<R, TwoNodesVisitor<R>, DivisionNode>, | ||
&tryType<R, TwoNodesVisitor<R>, EqualNode>, | ||
&tryType<R, TwoNodesVisitor<R>, LessThanOrEqualNode>, | ||
&tryType<R, TwoNodesVisitor<R>, GreaterThanOrEqualNode>, | ||
&tryType<R, TwoNodesVisitor<R>, NegationNode>, | ||
&tryType<R, TwoNodesVisitor<R>, ParameterNode>, | ||
&tryType<R, TwoNodesVisitor<R>, VariableNode>, | ||
&tryType<R, TwoNodesVisitor<R>, LiteralNode>, | ||
&tryType<R, TwoNodesVisitor<R>, PortFieldNode>, | ||
&tryType<R, TwoNodesVisitor<R>, ComponentVariableNode>, | ||
&tryType<R, TwoNodesVisitor<R>, ComponentParameterNode>}; | ||
for (auto f: functions) | ||
{ | ||
if (auto ret = f(node1, node2, *this); ret.has_value()) | ||
{ | ||
return ret.value(); | ||
} | ||
} | ||
logs.error() << "Antares::Solver::Nodes Visitor: unsupported Node!"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If Literal lit1(2), lit2(2);
AddNode node1(&lit1, &lit2);
MultiplicationNode node2(&lit1, &lit2);
EqualityVisitor equal;
// Here, we go through l. 78 above, but the caller only gets a log error
// and an `R()`, not so easy to catch
equal.dispatch(&node1, &node2); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update: the code print warning message and returns false |
||
return R(); | ||
} | ||
|
||
virtual R visit(const AddNode&, const AddNode&) = 0; | ||
virtual R visit(const SubtractionNode&, const SubtractionNode&) = 0; | ||
virtual R visit(const MultiplicationNode&, const MultiplicationNode&) = 0; | ||
virtual R visit(const DivisionNode&, const DivisionNode&) = 0; | ||
virtual R visit(const EqualNode&, const EqualNode&) = 0; | ||
virtual R visit(const LessThanOrEqualNode&, const LessThanOrEqualNode&) = 0; | ||
virtual R visit(const GreaterThanOrEqualNode&, const GreaterThanOrEqualNode&) = 0; | ||
virtual R visit(const NegationNode&, const NegationNode&) = 0; | ||
virtual R visit(const LiteralNode&, const LiteralNode&) = 0; | ||
virtual R visit(const VariableNode&, const VariableNode&) = 0; | ||
virtual R visit(const ParameterNode&, const ParameterNode&) = 0; | ||
virtual R visit(const PortFieldNode&, const PortFieldNode&) = 0; | ||
virtual R visit(const ComponentVariableNode&, const ComponentVariableNode&) = 0; | ||
virtual R visit(const ComponentParameterNode&, const ComponentParameterNode&) = 0; | ||
}; | ||
|
||
} // namespace Antares::Solver::Nodes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
** Copyright 2007-2024, RTE (https://www.rte-france.com) | ||
** See AUTHORS.txt | ||
** SPDX-License-Identifier: MPL-2.0 | ||
** This file is part of Antares-Simulator, | ||
** Adequacy and Performance assessment for interconnected energy networks. | ||
** | ||
** Antares_Simulator is free software: you can redistribute it and/or modify | ||
** it under the terms of the Mozilla Public Licence 2.0 as published by | ||
** the Mozilla Foundation, either version 2 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** Antares_Simulator is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** Mozilla Public Licence 2.0 for more details. | ||
** | ||
** You should have received a copy of the Mozilla Public Licence 2.0 | ||
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>. | ||
*/ | ||
#pragma once | ||
|
||
#include "antares/solver/expressions/nodes/TwoNodesVisitor.h" | ||
|
||
namespace Antares::Solver::Visitors | ||
{ | ||
|
||
class EqualityVisitor: public Nodes::TwoNodesVisitor<bool> | ||
{ | ||
public: | ||
using Base = Nodes::TwoNodesVisitor<bool>; | ||
|
||
private: | ||
bool visit(const Nodes::AddNode& add1, const Nodes::AddNode& add2) override; | ||
bool visit(const Nodes::SubtractionNode& add1, const Nodes::SubtractionNode& add2) override; | ||
bool visit(const Nodes::MultiplicationNode& add1, | ||
const Nodes::MultiplicationNode& add2) override; | ||
bool visit(const Nodes::DivisionNode& add1, const Nodes::DivisionNode& add2) override; | ||
bool visit(const Nodes::EqualNode& add1, const Nodes::EqualNode& add2) override; | ||
bool visit(const Nodes::LessThanOrEqualNode& add1, | ||
const Nodes::LessThanOrEqualNode& add2) override; | ||
bool visit(const Nodes::GreaterThanOrEqualNode& add1, | ||
const Nodes::GreaterThanOrEqualNode& add2) override; | ||
bool visit(const Nodes::NegationNode& neg1, const Nodes::NegationNode& neg2) override; | ||
bool visit(const Nodes::VariableNode& param1, const Nodes::VariableNode& param2) override; | ||
bool visit(const Nodes::ParameterNode& param1, const Nodes::ParameterNode& param2) override; | ||
bool visit(const Nodes::LiteralNode& lit1, const Nodes::LiteralNode& lit2) override; | ||
bool visit(const Nodes::PortFieldNode& port_field_node1, | ||
const Nodes::PortFieldNode& port_field_node2) override; | ||
bool visit(const Nodes::ComponentVariableNode& component_variable_node1, | ||
const Nodes::ComponentVariableNode& component_variable_node2) override; | ||
bool visit(const Nodes::ComponentParameterNode& component_parameter_node1, | ||
const Nodes::ComponentParameterNode& component_parameter_node2) override; | ||
}; | ||
} // namespace Antares::Solver::Visitors |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <antares/solver/expressions/nodes/ExpressionsNodes.h> | ||
#include <antares/solver/expressions/visitors/EqualityVisitor.h> | ||
|
||
namespace Antares::Solver::Visitors | ||
{ | ||
|
||
bool EqualityVisitor::visit(const Nodes::AddNode& add1, const Nodes::AddNode& add2) | ||
{ | ||
return dispatch(*add1[0], *add2[0]) && dispatch(*add1[1], *add2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::SubtractionNode& sub1, const Nodes::SubtractionNode& sub2) | ||
{ | ||
return dispatch(*sub1[0], *sub2[0]) && dispatch(*sub1[1], *sub2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::MultiplicationNode& mult1, | ||
const Nodes::MultiplicationNode& mult2) | ||
{ | ||
return dispatch(*mult1[0], *mult2[0]) && dispatch(*mult1[1], *mult2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::DivisionNode& div1, const Nodes::DivisionNode& div2) | ||
{ | ||
return dispatch(*div1[0], *div2[0]) && dispatch(*div1[1], *div2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::EqualNode& equ1, const Nodes::EqualNode& equ2) | ||
{ | ||
return dispatch(*equ1[0], *equ2[0]) && dispatch(*equ1[1], *equ2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::LessThanOrEqualNode& lt1, | ||
const Nodes::LessThanOrEqualNode& lt2) | ||
{ | ||
return dispatch(*lt1[0], *lt2[0]) && dispatch(*lt1[1], *lt2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::GreaterThanOrEqualNode& gt1, | ||
const Nodes::GreaterThanOrEqualNode& gt2) | ||
{ | ||
return dispatch(*gt1[0], *gt2[0]) && dispatch(*gt1[1], *gt2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::VariableNode& var1, const Nodes::VariableNode& var2) | ||
{ | ||
return var1.getValue() == var2.getValue(); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::ParameterNode& param1, const Nodes::ParameterNode& param2) | ||
{ | ||
return param1.getValue() == param2.getValue(); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::LiteralNode& lit1, const Nodes::LiteralNode& lit2) | ||
{ | ||
return lit1.getValue() == lit1.getValue(); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::NegationNode& neg1, const Nodes::NegationNode& neg2) | ||
{ | ||
return dispatch(*neg1[0], *neg2[0]) && dispatch(*neg1[1], *neg2[1]); | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::PortFieldNode& port_field_node1, | ||
const Nodes::PortFieldNode& port_field_node2) | ||
{ | ||
return port_field_node1 == port_field_node2; | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::ComponentVariableNode& component_variable_node1, | ||
const Nodes::ComponentVariableNode& component_variable_node2) | ||
{ | ||
return component_variable_node1 == component_variable_node2; | ||
} | ||
|
||
bool EqualityVisitor::visit(const Nodes::ComponentParameterNode& component_parameter_node1, | ||
const Nodes::ComponentParameterNode& component_parameter_node2) | ||
{ | ||
return component_parameter_node1 == component_parameter_node2; | ||
} | ||
|
||
} // namespace Antares::Solver::Visitors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Different node types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the return type is handled in the dispatch method