-
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
Closed
Closed
Equality visitor #2335
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4a0607b
two nodes visitor
a-zakir dec35ae
fix
a-zakir f08d583
fix
a-zakir a6fbcf6
update
a-zakir f50b714
add tests
a-zakir f92c9a9
update
a-zakir fa23284
throw exception
a-zakir 2942ab4
use custom type for return
a-zakir 685ee9e
change return type
a-zakir 7da0198
change return type
a-zakir 31db247
add tests
a-zakir 233c2a1
print warning msg instead of error
a-zakir 057dce6
commutative nodes
a-zakir 593e334
partial revert
a-zakir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/solver/expressions/include/antares/solver/expressions/nodes/TwoNodesVisitor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
** 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 && (typeid(*x) == typeid(*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.warning() | ||
<< "Antares::Solver::Nodes TwoNodesVisitor: the inputs nodes must be of the same type!"; | ||
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 |
63 changes: 63 additions & 0 deletions
63
src/solver/expressions/include/antares/solver/expressions/visitors/EqualityVisitor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
** 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" | ||
#include "antares/solver/expressions/visitors/NodesComparisonResult.h" | ||
|
||
namespace Antares::Solver::Visitors | ||
{ | ||
class EqualityVisitor: public Nodes::TwoNodesVisitor<NodeComparisonResult> | ||
{ | ||
public: | ||
using Base = Nodes::TwoNodesVisitor<NodeComparisonResult>; | ||
|
||
private: | ||
NodeComparisonResult visit(const Nodes::AddNode& add1, const Nodes::AddNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::SubtractionNode& add1, | ||
const Nodes::SubtractionNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::MultiplicationNode& add1, | ||
const Nodes::MultiplicationNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::DivisionNode& add1, | ||
const Nodes::DivisionNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::EqualNode& add1, const Nodes::EqualNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::LessThanOrEqualNode& add1, | ||
const Nodes::LessThanOrEqualNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::GreaterThanOrEqualNode& add1, | ||
const Nodes::GreaterThanOrEqualNode& add2) override; | ||
NodeComparisonResult visit(const Nodes::NegationNode& neg1, | ||
const Nodes::NegationNode& neg2) override; | ||
NodeComparisonResult visit(const Nodes::VariableNode& param1, | ||
const Nodes::VariableNode& param2) override; | ||
NodeComparisonResult visit(const Nodes::ParameterNode& param1, | ||
const Nodes::ParameterNode& param2) override; | ||
NodeComparisonResult visit(const Nodes::LiteralNode& lit1, | ||
const Nodes::LiteralNode& lit2) override; | ||
NodeComparisonResult visit(const Nodes::PortFieldNode& port_field_node1, | ||
const Nodes::PortFieldNode& port_field_node2) override; | ||
NodeComparisonResult visit( | ||
const Nodes::ComponentVariableNode& component_variable_node1, | ||
const Nodes::ComponentVariableNode& component_variable_node2) override; | ||
NodeComparisonResult visit( | ||
const Nodes::ComponentParameterNode& component_parameter_node1, | ||
const Nodes::ComponentParameterNode& component_parameter_node2) override; | ||
}; | ||
} // namespace Antares::Solver::Visitors |
41 changes: 41 additions & 0 deletions
41
src/solver/expressions/include/antares/solver/expressions/visitors/NodesComparisonResult.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
** 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 | ||
|
||
namespace Antares::Solver::Visitors | ||
{ | ||
|
||
class NodeComparisonResult | ||
{ | ||
public: | ||
NodeComparisonResult() = default; | ||
NodeComparisonResult(bool); | ||
|
||
operator bool() const | ||
{ | ||
return nodeComparison_; | ||
} | ||
|
||
private: | ||
bool nodeComparison_ = false; | ||
}; | ||
|
||
} // namespace Antares::Solver::Visitors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include <antares/solver/expressions/nodes/ExpressionsNodes.h> | ||
#include <antares/solver/expressions/visitors/EqualityVisitor.h> | ||
|
||
namespace Antares::Solver::Visitors | ||
{ | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::AddNode& add1, const Nodes::AddNode& add2) | ||
{ | ||
return dispatch(*add1[0], *add2[0]) && dispatch(*add1[1], *add2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::SubtractionNode& sub1, | ||
const Nodes::SubtractionNode& sub2) | ||
{ | ||
return dispatch(*sub1[0], *sub2[0]) && dispatch(*sub1[1], *sub2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::MultiplicationNode& mult1, | ||
const Nodes::MultiplicationNode& mult2) | ||
{ | ||
return dispatch(*mult1[0], *mult2[0]) && dispatch(*mult1[1], *mult2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::DivisionNode& div1, | ||
const Nodes::DivisionNode& div2) | ||
{ | ||
return dispatch(*div1[0], *div2[0]) && dispatch(*div1[1], *div2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::EqualNode& equ1, | ||
const Nodes::EqualNode& equ2) | ||
{ | ||
return dispatch(*equ1[0], *equ2[0]) && dispatch(*equ1[1], *equ2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::LessThanOrEqualNode& lt1, | ||
const Nodes::LessThanOrEqualNode& lt2) | ||
{ | ||
return dispatch(*lt1[0], *lt2[0]) && dispatch(*lt1[1], *lt2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::GreaterThanOrEqualNode& gt1, | ||
const Nodes::GreaterThanOrEqualNode& gt2) | ||
{ | ||
return dispatch(*gt1[0], *gt2[0]) && dispatch(*gt1[1], *gt2[1]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::VariableNode& var1, | ||
const Nodes::VariableNode& var2) | ||
{ | ||
return var1.getValue() == var2.getValue(); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::ParameterNode& param1, | ||
const Nodes::ParameterNode& param2) | ||
{ | ||
return param1.getValue() == param2.getValue(); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::LiteralNode& lit1, | ||
const Nodes::LiteralNode& lit2) | ||
{ | ||
return lit1.getValue() == lit1.getValue(); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::NegationNode& neg1, | ||
const Nodes::NegationNode& neg2) | ||
{ | ||
return dispatch(*neg1[0], *neg2[0]); | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit(const Nodes::PortFieldNode& port_field_node1, | ||
const Nodes::PortFieldNode& port_field_node2) | ||
{ | ||
return port_field_node1 == port_field_node2; | ||
} | ||
|
||
NodeComparisonResult EqualityVisitor::visit( | ||
const Nodes::ComponentVariableNode& component_variable_node1, | ||
const Nodes::ComponentVariableNode& component_variable_node2) | ||
{ | ||
return component_variable_node1 == component_variable_node2; | ||
} | ||
|
||
NodeComparisonResult 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
** 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/>. | ||
*/ | ||
|
||
#include <antares/solver/expressions/visitors/NodesComparisonResult.h> | ||
|
||
namespace Antares::Solver::Visitors | ||
{ | ||
NodeComparisonResult::NodeComparisonResult(bool nodeComparison): | ||
nodeComparison_(nodeComparison) | ||
{ | ||
} | ||
|
||
} // namespace Antares::Solver::Visitors |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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