Skip to content

Commit

Permalink
Enable DOT representation for STRL.
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritkalra committed Oct 31, 2023
1 parent 146bf51 commit a3d4a49
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
14 changes: 14 additions & 0 deletions schedulers/tetrisched/include/tetrisched/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ class Expression : public std::enable_shared_from_this<Expression> {
protected:
/// The name of the Expression.
std::string name;
/// A unique ID for the Expression.
std::string id;
/// The parsed result from the Expression.
/// Used for retrieving the solution from the solver.
ParseResultPtr parsedResult;
Expand Down Expand Up @@ -275,6 +277,9 @@ class Expression : public std::enable_shared_from_this<Expression> {
/// Returns the name of this Expression.
std::string getName() const;

/// Returns the ID of this Expression.
std::string getId() const;

/// Returns the number of children of this Expression.
size_t getNumChildren() const;

Expand All @@ -301,6 +306,12 @@ class Expression : public std::enable_shared_from_this<Expression> {
/// The Solution is only available if `populateResults` has been called on
/// this Expression.
std::optional<SolutionResultPtr> getSolution() const;

/// Exports the STRL rooted at this Expression into a DOT file.
void exportToDot(std::string filename) const;

/// Retrieves the descriptive name for the Expression.
virtual std::string getDescriptiveName() const;
};

/// A `ChooseExpression` represents a choice of a required number of machines
Expand Down Expand Up @@ -332,6 +343,7 @@ class ChooseExpression : public Expression {
CapacityConstraintMap& capacityConstraints,
Time currentTime) override;
SolutionResultPtr populateResults(SolverModelPtr solverModel) override;
std::string getDescriptiveName() const override;
};

class MalleableChooseExpression : public Expression {
Expand Down Expand Up @@ -365,6 +377,7 @@ class MalleableChooseExpression : public Expression {
CapacityConstraintMap& capacityConstraints,
Time currentTime) override;
SolutionResultPtr populateResults(SolverModelPtr solverModel) override;
std::string getDescriptiveName() const override;
};

/// An `AllocationExpression` represents the allocation of the given number of
Expand Down Expand Up @@ -392,6 +405,7 @@ class AllocationExpression : public Expression {
CapacityConstraintMap& capacityConstraints,
Time currentTime) override;
SolutionResultPtr populateResults(SolverModelPtr solverModel) override;
std::string getDescriptiveName() const override;
};

/// An `ObjectiveExpression` collates the objectives from its children and
Expand Down
5 changes: 4 additions & 1 deletion schedulers/tetrisched/python/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ void defineSTRLExpressions(py::module_& tetrisched_m) {
"Adds a child to this Expression.")
.def("getSolution", &tetrisched::Expression::getSolution,
"Returns the solution for this Expression.")
.def("exportToDot", &tetrisched::Expression::exportToDot,
"Exports the Expression to a dot file.")
.def("__str__",
[](const tetrisched::Expression& expr) {
return "Expression<name=" + expr.getName() +
", type=" + expr.getTypeString() + ">";
})
.def_property_readonly("name", &tetrisched::Expression::getName);
.def_property_readonly("name", &tetrisched::Expression::getName)
.def_property_readonly("id", &tetrisched::Expression::getId);

// Define the ChooseExpression.
py::class_<tetrisched::ChooseExpression, tetrisched::Expression,
Expand Down
73 changes: 72 additions & 1 deletion schedulers/tetrisched/src/Expression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#include "tetrisched/Expression.hpp"

#include <algorithm>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <stack>

/// A method to generate a UUID for different classes.
std::string generateUUID() {
boost::uuids::uuid uuid = boost::uuids::random_generator()();
return boost::uuids::to_string(uuid);
}

namespace tetrisched {

Expand Down Expand Up @@ -125,10 +135,12 @@ size_t CapacityConstraintMap::size() const {
/* Method definitions for Expression */

Expression::Expression(std::string name, ExpressionType type)
: name(name), type(type) {}
: name(name), id(generateUUID()), type(type) {}

std::string Expression::getName() const { return name; }

std::string Expression::getId() const { return id; }

size_t Expression::getNumChildren() const { return children.size(); }

std::vector<ExpressionPtr> Expression::getChildren() const { return children; }
Expand Down Expand Up @@ -266,6 +278,50 @@ std::optional<SolutionResultPtr> Expression::getSolution() const {
return solution;
}

void Expression::exportToDot(std::string fileName) const {
// Open the file for writing.
std::ofstream dotFile;
dotFile.open(fileName);

// Output the Graph.
dotFile << "digraph " << name << " {" << std::endl;

// Go through all the nodes in the graph and output a mapping from
std::stack<const Expression*> nodesToVisit;
nodesToVisit.push(this);
std::unordered_set<std::string> visitedNodes;

while (!nodesToVisit.empty()) {
const Expression* node = nodesToVisit.top();
nodesToVisit.pop();

// Check if we have already visited this node.
if (visitedNodes.find(node->getId()) != visitedNodes.end()) {
// We have already visited this node. Skip it.
continue;
}
visitedNodes.insert(node->getId());

// Output the node.
dotFile << "\"" << node->getId() << "\" [label=\""
<< node->getDescriptiveName() << "\"]" << std::endl;

// Output the edges.
for (auto& child : node->getChildren()) {
dotFile << "\"" << node->getId() << "\" -> \"" << child->getId() << "\""
<< std::endl;
nodesToVisit.push(child.get());
}
}

// their ID to their name.
dotFile << "}";
}

std::string Expression::getDescriptiveName() const {
return this->getTypeString();
}

/* Method definitions for ChooseExpression */

ChooseExpression::ChooseExpression(std::string taskName,
Expand Down Expand Up @@ -412,6 +468,11 @@ SolutionResultPtr ChooseExpression::populateResults(
return solution;
}

std::string ChooseExpression::getDescriptiveName() const {
return "Choose(" + name + ", S=" + std::to_string(startTime) +
", F=" + std::to_string(endTime) + ")";
}

/* Method definitions for GeneralizedChoose */
MalleableChooseExpression::MalleableChooseExpression(
std::string taskName, Partitions resourcePartitions,
Expand Down Expand Up @@ -756,6 +817,11 @@ SolutionResultPtr MalleableChooseExpression::populateResults(
return solution;
}

std::string MalleableChooseExpression::getDescriptiveName() const {
return "MalleableChoose(" + name + ", S=" + std::to_string(startTime) +
", F=" + std::to_string(endTime) + ")";
}

/* Method definitions for AllocationExpression */

AllocationExpression::AllocationExpression(
Expand Down Expand Up @@ -808,6 +874,11 @@ SolutionResultPtr AllocationExpression::populateResults(
return solution;
}

std::string AllocationExpression::getDescriptiveName() const {
return "Allocation(" + name + ", S=" + std::to_string(startTime) +
", F=" + std::to_string(endTime) + ")";
}

/* Method definitions for ObjectiveExpression */

ObjectiveExpression::ObjectiveExpression(std::string name)
Expand Down

0 comments on commit a3d4a49

Please sign in to comment.