Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Oct 15, 2024
1 parent ca54927 commit 3e63bce
Show file tree
Hide file tree
Showing 8 changed files with 585 additions and 297 deletions.
41 changes: 31 additions & 10 deletions src/ext/yuni/src/yuni/io/file/stream.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
** gitlab: https://gitlab.com/libyuni/libyuni/ (mirror)
*/
#pragma once
#include "stream.h"

#include "../../core/string.h"
#include "stream.h"

namespace Yuni
{
namespace IO
{
namespace File
{
inline Stream::Stream() : pFd(nullptr)
inline Stream::Stream():
pFd(nullptr)
{
// Do nothing
}
Expand All @@ -27,7 +29,9 @@ inline Stream::~Stream()
{
// The check is mandatory to avoid SegV on some platform (Darwin for example)
if (pFd)
{
(void)::fclose(pFd);
}
}

inline bool Stream::openRW(const AnyString& filename)
Expand Down Expand Up @@ -104,7 +108,9 @@ bool Stream::readline(StringT& buffer, bool trim)
// to perform maintenance (about the internal size and the final zero)
buffer.resize(static_cast<uint>(::strlen(buffer.c_str())));
if (trim)
{
buffer.trimRight("\r\n");
}
return true;
}
buffer.clear();
Expand All @@ -113,7 +119,7 @@ bool Stream::readline(StringT& buffer, bool trim)

inline bool Stream::put(char c)
{
return (EOF != ::fputc((int)c, pFd));
return (-1 != ::fputc((int)c, pFd));
}

inline uint Stream::write(bool value)
Expand All @@ -130,7 +136,7 @@ inline uint Stream::write(bool value, uint64_t maxsize)

inline uint Stream::write(char buffer)
{
return (EOF != ::fputc((int)buffer, pFd)) ? 1 : 0;
return (-1 != ::fputc((int)buffer, pFd)) ? 1 : 0;
}

inline uint Stream::write(float value)
Expand Down Expand Up @@ -264,17 +270,18 @@ inline uint64_t Stream::write(const U& buffer)

inline uint Stream::write(char buffer, uint64_t maxsize)
{
return (maxsize != 0) ? ((EOF != ::fputc(static_cast<int>(buffer), pFd)) ? 1 : 0) : 0;
return (maxsize != 0) ? ((-1 != ::fputc(static_cast<int>(buffer), pFd)) ? 1 : 0) : 0;
}

template<class U>
inline uint64_t Stream::write(const U& buffer, uint64_t maxsize)
{
String string(buffer);
return (uint64_t)::fwrite(string.c_str(),
1,
string.size() > maxsize ? static_cast<size_t>(maxsize) : string.size(),
pFd);
1,
string.size() > maxsize ? static_cast<size_t>(maxsize)
: string.size(),
pFd);
}

inline bool Stream::operator!() const
Expand Down Expand Up @@ -314,11 +321,15 @@ inline uint64_t Stream::read(CString<CSizeT, ExpT>& buffer, uint64_t size)
assert(pFd and "File not opened");
assert(size <= static_cast<uint64_t>(2 * 1024) * 1024u * 1024u);
if (0 == size)
{
return 0;
}

// special case for static strings
if (not buffer.expandable and size > buffer.chunkSize)
{
size = buffer.chunkSize;
}

// Resizing the buffer
buffer.resize(static_cast<uint>(size));
Expand All @@ -330,14 +341,20 @@ inline uint64_t Stream::read(CString<CSizeT, ExpT>& buffer, uint64_t size)
typedef CString<CSizeT, ExpT> StringType;
typedef typename StringType::Char C;
// Reading the file
size_t result
= ::fread(const_cast<char*>(buffer.data()), 1, static_cast<size_t>(sizeof(C) * size), pFd);
size_t result = ::fread(const_cast<char*>(buffer.data()),
1,
static_cast<size_t>(sizeof(C) * size),
pFd);
// Setting the good size, because we may have read less than asked
if (result < static_cast<size_t>(buffer.size()))
{
buffer.truncate(static_cast<uint>(result));
}
// Making sure that the buffer is zero-terminated if required
if (buffer.zeroTerminated)
{
*(reinterpret_cast<C*>(buffer.data() + buffer.size())) = C();
}
return result;
}

Expand All @@ -356,10 +373,14 @@ inline uint64_t Stream::chunkRead(CString<ChunkSizeT, ExpandableT>& buffer)
const uint64_t result = ::fread(buffer.data(), 1, sizeof(C) * buffer.chunkSize, pFd);
// Setting the good size, because we may have read less than asked
if (result < buffer.size())
{
buffer.truncate(static_cast<typename StringType::Size>(result));
}
// Making sure that the buffer is zero-terminated if required
if (buffer.zeroTerminated)
{
*((C*)(buffer.data() + buffer.size())) = C();
}
return result;
}

Expand Down
4 changes: 1 addition & 3 deletions src/libs/antares/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ add_subdirectory(study-loader)
add_subdirectory(sys)
add_subdirectory(utils)
add_subdirectory(writer)
if(WITH_ANTLR4)
add_subdirectory(antlr-interface)
endif()
add_subdirectory(antlr-interface)

add_subdirectory(optimization-options)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class AstDOTStyleVisitor: public NodeVisitor<void, std::ostream&>
*/
void operator()(std::ostream& os, Nodes::Node* root);

void setGraphName(const std::string& name);
void setExpression(const std::string& expr);

private:
void visit(const Nodes::SumNode* node, std::ostream& os) override;
void visit(const Nodes::SubtractionNode* node, std::ostream& os) override;
Expand Down Expand Up @@ -200,5 +203,7 @@ class AstDOTStyleVisitor: public NodeVisitor<void, std::ostream&>
* This counter is incremented each time a new node ID is needed.
*/
unsigned int nodeCount_ = 0;
std::string graphName_;
std::string expression_;
};
} // namespace Antares::Solver::Visitors
43 changes: 32 additions & 11 deletions src/solver/expressions/visitors/AstDOTStyleVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "antares/solver/expressions/visitors/AstDOTStyleVisitor.h"

#include <algorithm>
#include <regex>

#include "antares/solver/expressions/nodes/ExpressionsNodes.h"

Expand Down Expand Up @@ -84,7 +85,8 @@ void AstDOTStyleVisitor::visit(const Nodes::SumNode* node, std::ostream& os)
for (auto* child: node->getOperands())
{
auto childId = getNodeID(child);
os << " " << id << " -> " << childId << ";\n";
os << " " << graphName_ + std::to_string(id) << " -> "
<< graphName_ + std::to_string(childId) << ";\n";
dispatch(child, os);
}
}
Expand Down Expand Up @@ -142,7 +144,8 @@ void AstDOTStyleVisitor::visit(const Nodes::NegationNode* node, std::ostream& os
auto id = getNodeID(node);
emitNode(id, "-", NodeStyle::NegationStyle, os);
auto childId = getNodeID(node->child());
os << " " << id << " -> " << childId << ";\n";
os << " " << graphName_ + std::to_string(id) << " -> " << graphName_ + std::to_string(childId)
<< ";\n";
dispatch(node->child(), os);
}

Expand Down Expand Up @@ -209,8 +212,9 @@ void AstDOTStyleVisitor::emitNode(unsigned int id,
const BoxStyle& box_style,
std::ostream& os)
{
os << " " << id << " [label=\"" << label << "\", shape=\"" << box_style.shape << "\", style=\""
<< box_style.style << "\", color=\"" << box_style.color << "\"];\n";
os << " " << graphName_ + std::to_string(id) << " [label=\"" << label << "\", shape=\""
<< box_style.shape << "\", style=\"" << box_style.style << "\", color=\"" << box_style.color
<< "\"];\n";
}

// Process binary operation nodes like Add, Subtract, etc.
Expand All @@ -228,29 +232,35 @@ void AstDOTStyleVisitor::processBinaryOperation(const Nodes::BinaryNode* node,
auto leftId = getNodeID(left);
auto rightId = getNodeID(right);

os << " " << id << " -> " << leftId << ";\n";
os << " " << id << " -> " << rightId << ";\n";
os << " " << graphName_ + std::to_string(id) << " -> " << graphName_ + std::to_string(leftId)
<< ";\n";
os << " " << graphName_ + std::to_string(id) << " -> " << graphName_ + std::to_string(rightId)
<< ";\n";

dispatch(left, os);
dispatch(right, os);
}

void AstDOTStyleVisitor::NewTreeGraph(std::ostream& os, const std::string& tree_name)
{
os << "digraph " + tree_name + " {\n";
os << "subgraph cluster_" + tree_name + " {\n";
os << "node[style = filled]\n";
os << "color = blue\n";
}

void AstDOTStyleVisitor::EndTreeGraph(std::ostream& os)
{
computeNumberNodesPerType();

// Graph title showing the total number of nodes
os << "label=\"AST Diagram(Total nodes : " << nodeCount_ << ")\"\n";
// std::regex_replace(expression_, std::regex("\\("), "\\(");
// std::regex_replace(expression_, std::regex("\\)"), "\\)");
os << "label=\"" << expression_ << "\"\n";

os << "labelloc = \"t\"\n";

makeLegendTitle(os);
makeLegend(os);
// makeLegendTitle(os);
// makeLegend(os);
os << "}\n";

nodeCount_ = 0;
Expand All @@ -260,8 +270,19 @@ void AstDOTStyleVisitor::EndTreeGraph(std::ostream& os)

void AstDOTStyleVisitor::operator()(std::ostream& os, Nodes::Node* root)
{
NewTreeGraph(os);
NewTreeGraph(os, graphName_);
dispatch(root, os);
EndTreeGraph(os);
}

void AstDOTStyleVisitor::setGraphName(const std::string& name)
{
graphName_ = name;
}

void AstDOTStyleVisitor::setExpression(const std::string& expr)
{
expression_ = expr;
}

} // namespace Antares::Solver::Visitors
6 changes: 5 additions & 1 deletion src/tests/src/solver/modelParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ set(SOURCE_FILES
testModelTranslator.cpp
test_full.cpp
enum_operators.h
ConvertorVisitor.h
)

# Add executable
add_executable(TestModelParser ${SOURCE_FILES})

find_package(antlr4-runtime CONFIG REQUIRED)
# Link libraries
target_link_libraries(TestModelParser
PRIVATE
Boost::unit_test_framework
Antares::modelConverter
Antares::modelParser
Antares::antares-solver-libObjectModel
Antares::solver-expressions
Antares::antlr-interface
Antares::modeler-ortools-impl
)

# Storing test-toybox under the folder Unit-tests in the IDE
Expand Down
Loading

0 comments on commit 3e63bce

Please sign in to comment.