Skip to content
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

Repeating life cycles #168

Closed
82 changes: 35 additions & 47 deletions source/LifecycleMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,62 @@

namespace marianatrench {

void LifeCycleMethodGraph::addNode(const LifecycleMethodCall& node) {
adj_list_[node];
const std::vector<LifecycleMethodCall>& LifecycleGraphNode::method_calls() const {
return method_calls_;
}

bool LifeCycleMethodGraph::operator==(const LifeCycleMethodGraph& other) const {
if (!(entry_point_ == other.entry_point_)) {
return false;
}

if (adj_list_.size() != other.adj_list_.size()) {
return false;
}

for (const auto& pair : adj_list_) {
const LifecycleMethodCall& node = pair.first;
const std::vector<LifecycleMethodCall>& neighbours = pair.second;
const std::vector<std::string>& LifecycleGraphNode::successors() const {
return successors_;
}

auto it = other.adj_list_.find(node);
if (it == other.adj_list_.end()) {
return false;
}
bool LifecycleGraphNode::operator==(const LifecycleGraphNode& other) const {
return method_calls_ == other.method_calls_ &&
successors_ == other.successors_;
}

const std::vector<LifecycleMethodCall>& other_neighbours = it->second;
if (neighbours.size() != other_neighbours.size()) {
return false;
}
void LifeCycleMethodGraph::addNode(
const std::string& node_name,
std::vector<LifecycleMethodCall> method_calls,
std::vector<std::string> successors) {
nodes_.emplace(
node_name, LifecycleGraphNode(std::move(method_calls), std::move(successors)));
}

for (size_t i = 0; i < neighbours.size(); ++i) {
if (!(neighbours[i] == other_neighbours[i])) {
return false;
}
}
bool LifeCycleMethodGraph::operator==(const LifeCycleMethodGraph& other) const {
if (nodes_.size() != other.nodes_.size()) {
return false;
}

return true;
return nodes_ == other.nodes_;
}

void LifeCycleMethodGraph::addEdge(
const LifecycleMethodCall& from,
const LifecycleMethodCall& to) {
adj_list_[from].push_back(to);
}

const std::vector<LifecycleMethodCall>& LifeCycleMethodGraph::getNeighbours(
const LifecycleMethodCall& node) const {
return adj_list_.at(node);
const LifecycleGraphNode* LifeCycleMethodGraph::getNode(const std::string& node_name) const {
auto it = nodes_.find(node_name);
if (it != nodes_.end()) {
return &it->second;
}
return nullptr;
}

LifeCycleMethodGraph LifeCycleMethodGraph::from_json(const Json::Value& value) {
LifeCycleMethodGraph graph;

for (const auto& node_name : value.getMemberNames()) {
const auto& node = value[node_name];
JsonValidation::validate_object(node, "node");
const auto& instructions = node["instructions"];

if(node_name == "entry") {
LifecycleMethodCall entry_point = LifecycleMethodCall::from_json(instructions[0]);
graph.entry_point_ = entry_point;
std::vector<LifecycleMethodCall> method_calls;
for (const auto& instruction : JsonValidation::null_or_array(node, "instructions")) {
method_calls.push_back(LifecycleMethodCall::from_json(instruction));
}

for (const auto& instruction :JsonValidation::null_or_array(node, "instructions")) {
LifecycleMethodCall call = LifecycleMethodCall::from_json(instruction);
graph.addNode(call);
for (const auto& successor : JsonValidation::null_or_array(node, "successors")) {
LifecycleMethodCall successor_call = LifecycleMethodCall::from_json(successor);
graph.addEdge(call, successor_call);
}
std::vector<std::string> successors;
for (const auto& successor : JsonValidation::null_or_array(node, "successors")) {
successors.push_back(JsonValidation::string(successor));
}

graph.addNode(node_name, std::move(method_calls), std::move(successors));
}
return graph;
}
Expand Down
40 changes: 25 additions & 15 deletions source/LifecycleMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,41 @@ class LifecycleMethodCall {
std::optional<std::string> defined_in_derived_class_;
};

class LifecycleGraphNode {
public:
LifecycleGraphNode(
std::vector<LifecycleMethodCall> method_calls,
std::vector<std::string> successors)
: method_calls_(std::move(method_calls)),
successors_(std::move(successors)) {}

const std::vector<LifecycleMethodCall>& method_calls() const;
const std::vector<std::string>& successors() const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: inline these since the implementation is really small

can we inline the implementation of these, since those should be small?

Suggested change
const std::vector<LifecycleMethodCall>& method_calls() const;
const std::vector<std::string>& successors() const;
const std::vector<LifecycleMethodCall>& method_calls() const { return method_calls_; }
const std::vector<std::string>& successors() const { return successors_; }

bool operator==(const LifecycleGraphNode& other) const;

INCLUDE_DEFAULT_COPY_CONSTRUCTORS_AND_ASSIGNMENTS(LifecycleGraphNode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consistency with the rest of our codebase, please move this right after the constructor


private:
std::vector<LifecycleMethodCall> method_calls_;
std::vector<std::string> successors_;
};


class LifeCycleMethodGraph {
public:
LifeCycleMethodGraph() : entry_point_("", "", {}, std::nullopt) {}
void addNode(const LifecycleMethodCall& node);
void addEdge(const LifecycleMethodCall& from, const LifecycleMethodCall& to);
const std::vector<LifecycleMethodCall>& getNeighbours(const LifecycleMethodCall& node) const;
LifeCycleMethodGraph() {}
void addNode(const std::string& node_name,std::vector<LifecycleMethodCall> method_calls,std::vector<std::string> successors);

const LifecycleGraphNode* getNode(const std::string& node_name) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: we use snake_case for method names, so it should be add_node and get_node.

bool operator==(const LifeCycleMethodGraph& other) const;

INCLUDE_DEFAULT_COPY_CONSTRUCTORS_AND_ASSIGNMENTS(LifeCycleMethodGraph)

static LifeCycleMethodGraph from_json(const Json::Value& value);

private:
struct NodeHasher {
std::size_t operator()(const LifecycleMethodCall& node) const {
return std::hash<std::string>{}(node.to_string());
}
};

std::unordered_map<LifecycleMethodCall, std::vector<LifecycleMethodCall>, NodeHasher> adj_list_;

// Define the entry point of the graph
LifecycleMethodCall entry_point_;


std::unordered_map<std::string, LifecycleGraphNode> nodes_;
};

/**
Expand Down
Loading