Cerata
A library to generate structural hardware designs
graph.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <memory>
18 #include <string>
19 #include <optional>
20 #include <utility>
21 #include <vector>
22 #include <unordered_map>
23 
24 #include "cerata/node.h"
25 #include "cerata/array.h"
26 #include "cerata/pool.h"
27 
28 namespace cerata {
29 
30 // Forward Decl.
31 class Component;
32 class Instance;
33 
37 class Graph : public Named {
38  public:
40  enum ID {
42  INSTANCE
43  };
44 
46  Graph(std::string name, ID id) : Named(std::move(name)), id_(id) {}
47 
49  ID id() const { return id_; }
51  bool IsComponent() const { return id_ == COMPONENT; }
53  bool IsInstance() const { return id_ == INSTANCE; }
55  virtual Graph &Add(const std::shared_ptr<Object> &object);
57  virtual Graph &Add(const std::vector<std::shared_ptr<Object>> &objects);
59  virtual Graph &Remove(Object *obj);
60 
62  template<typename T>
63  std::vector<T *> GetAll() const {
64  std::vector<T *> result;
65  for (const auto &o : objects_) {
66  auto co = std::dynamic_pointer_cast<T>(o);
67  if (co != nullptr) {
68  result.push_back(co.get());
69  }
70  }
71  return result;
72  }
73 
75  template<typename T>
76  T *Get(const std::string &name) const {
77  for (const auto &o : objects_) {
78  if (o->name() == name) {
79  auto result = dynamic_cast<T *>(o.get());
80  if (result == nullptr) {
81  CERATA_LOG(FATAL, "Object with name " + name + " is not of type " + ::cerata::ToString<T>());
82  }
83  return result;
84  }
85  }
86  CERATA_LOG(FATAL, "Object with name " + name + " does not exist on graph " + this->name()
87  + "\n Should be one of the following: " + ToStringAllOjects());
88  }
89 
91  std::optional<Node *> FindNode(const std::string &node_name) const;
93  Node *GetNode(const std::string &node_name) const;
95  inline Node *operator()(const std::string &node_name) const { return GetNode(node_name); }
96 
98  std::vector<Node *> GetNodesOfTypes(std::initializer_list<Node::NodeID> ids) const;
100  size_t CountNodes(Node::NodeID id) const;
102  size_t CountArrays(Node::NodeID id) const;
104  std::vector<Node *> GetNodes() const { return GetAll<Node>(); }
106  std::vector<Node *> GetNodesOfType(Node::NodeID id) const;
108  std::vector<NodeArray *> GetArraysOfType(Node::NodeID id) const;
110  std::vector<Node *> GetImplicitNodes() const;
111 
113  PortArray *prt_arr(const std::string &name) const;
115  SignalArray *sig_arr(const std::string &name) const;
117  Port *prt(const std::string &name) const;
119  Signal *sig(const std::string &name) const;
121  Parameter *par(const std::string &name) const;
122 
124  Parameter *par(const Parameter &param) const;
126  Parameter *par(const std::shared_ptr<Parameter> &param) const;
127 
129  std::unordered_map<std::string, std::string> meta() const { return meta_; }
131  std::vector<Object *> objects() const { return ToRawPointers(objects_); }
133  bool Has(const std::string &name);
134 
136  Graph &SetMeta(const std::string &key, std::string value);
137 
139  std::string ToString() const { return name(); }
140 
142  std::string ToStringAllOjects() const;
143 
144  protected:
148  std::vector<std::shared_ptr<Object>> objects_;
150  std::unordered_map<std::string, std::string> meta_;
151 };
152 
158 class Component : public Graph {
159  public:
161  explicit Component(std::string name) : Graph(std::move(name), COMPONENT) {}
162 
164  Graph &Add(const std::shared_ptr<Object> &object) override;
166  Graph &Add(const std::vector<std::shared_ptr<Object>> &objects) override;
168  Graph &Remove(Object *object) override;
169 
176  Instance *Instantiate(Component *comp, const std::string &name = "");
177 
184  Instance *Instantiate(const std::shared_ptr<Component> &comp, const std::string &name = "");
185 
187  std::vector<Instance *> children() const { return ToRawPointers(children_); }
188 
190  virtual std::vector<const Component *> GetAllInstanceComponents() const;
191 
193  bool HasChild(const std::string &name) const;
195  bool HasChild(const Instance &inst) const;
196 
199 
200  protected:
206  Component &AddChild(std::unique_ptr<Instance> child);
207 
209  std::vector<std::unique_ptr<Instance>> children_;
210 
212  bool was_instantiated = false;
213 
216 };
217 
219 std::shared_ptr<Component> component(std::string name,
220  const std::vector<std::shared_ptr<Object>> &nodes,
221  ComponentPool *component_pool = default_component_pool());
223 std::shared_ptr<Component> component(std::string name,
224  ComponentPool *component_pool = default_component_pool());
225 
231 class Instance : public Graph {
232  public:
234  Graph &Add(const std::shared_ptr<Object> &obj) override;
236  Component *component() const { return component_; }
238  Graph *parent() const { return parent_; }
243 
244  protected:
246  friend Component;
248  explicit Instance(Component *comp, std::string name, Component* parent);
250  static std::unique_ptr<Instance> Make(Component *component, const std::string &name, Component* parent);
257 };
258 
259 } // namespace cerata
cerata::Component::was_instantiated
bool was_instantiated
Whether this component was instantiated.
Definition: graph.h:212
cerata::Component::GetAllInstanceComponents
virtual std::vector< const Component * > GetAllInstanceComponents() const
Returns all unique Components that are referred to by child Instances of this graph.
Definition: graph.cc:268
cerata::Instance::parent_
Graph * parent_
The parent of this instance.
Definition: graph.h:254
cerata::Component
A Component graph.
Definition: graph.h:158
cerata::Graph::Add
virtual Graph & Add(const std::shared_ptr< Object > &object)
Add an object to the component.
Definition: graph.cc:32
cerata::Graph::sig_arr
SignalArray * sig_arr(const std::string &name) const
Shorthand to Get<SignalArray>(...)
Definition: graph.cc:183
cerata::Graph::ToString
std::string ToString() const
Return a human-readable representation.
Definition: graph.h:139
cerata::Graph::GetAll
std::vector< T * > GetAll() const
Get all objects of a specific type.
Definition: graph.h:63
cerata::SignalArray
An array of signal nodes.
Definition: array.h:100
cerata::Component::Component
Component(std::string name)
Construct an empty Component.
Definition: graph.h:161
cerata::Component::Add
Graph & Add(const std::shared_ptr< Object > &object) override
Add an object to the component.
Definition: graph.cc:361
cerata::Graph::IsInstance
bool IsInstance() const
Return true if this graph is an instance, false otherwise.
Definition: graph.h:53
cerata::Instance::parent
Graph * parent() const
Return the parent graph.
Definition: graph.h:238
cerata::Component::Instantiate
Instance * Instantiate(Component *comp, const std::string &name="")
Add an Instance of another Component to this component.
Definition: graph.cc:295
cerata::Instance::component
Component * component() const
Return the component this is an instance of.
Definition: graph.h:236
cerata::Graph
A graph representing a hardware structure.
Definition: graph.h:37
cerata::Graph::meta_
std::unordered_map< std::string, std::string > meta_
KV storage for metadata of tools or specific backend implementations.
Definition: graph.h:150
cerata::ToRawPointers
std::vector< T * > ToRawPointers(const std::vector< std::shared_ptr< T >> &list)
Convert a list of shared pointers to raw pointers.
Definition: utils.h:125
cerata::Instance::Add
Graph & Add(const std::shared_ptr< Object > &obj) override
Add a node to the component, throwing an exception if the node is a signal.
Definition: graph.cc:396
cerata::ComponentPool
A pool of Components.
Definition: pool.h:86
cerata::Signal
A Signal Node.
Definition: signal.h:30
cerata::Graph::Graph
Graph(std::string name, ID id)
Construct a new graph.
Definition: graph.h:46
cerata::Graph::meta
std::unordered_map< std::string, std::string > meta() const
Return a copy of the metadata.
Definition: graph.h:129
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::Graph::CountArrays
size_t CountArrays(Node::NodeID id) const
Count nodes of a specific array type.
Definition: graph.cc:128
cerata::Graph::objects_
std::vector< std::shared_ptr< Object > > objects_
Graph objects.
Definition: graph.h:148
cerata::Instance::Make
static std::unique_ptr< Instance > Make(Component *component, const std::string &name, Component *parent)
Create an instance.
Definition: graph.cc:373
cerata::Graph::IsComponent
bool IsComponent() const
Return true if this graph is a component, false otherwise.
Definition: graph.h:51
cerata::Instance::comp_to_inst_map
NodeMap * comp_to_inst_map()
Return the component node to instance node mapping.
Definition: graph.h:242
cerata::Graph::GetNodesOfTypes
std::vector< Node * > GetNodesOfTypes(std::initializer_list< Node::NodeID > ids) const
Obtain all nodes which ids are in a list of Node::IDs.
Definition: graph.cc:202
cerata::Graph::GetImplicitNodes
std::vector< Node * > GetImplicitNodes() const
Return all graph nodes that do not explicitly belong to the graph.
Definition: graph.cc:187
cerata::Instance::comp_to_inst
NodeMap comp_to_inst
Mapping from component port and parameter nodes to instantiated nodes.
Definition: graph.h:256
cerata::Node
A node.
Definition: node.h:42
cerata::NodeMap
std::unordered_map< const Node *, Node * > NodeMap
A mapping from one object to another object, used in e.g. type generic rebinding.
Definition: node.h:135
cerata::Component::children
std::vector< Instance * > children() const
Returns all Instance graphs from this Component.
Definition: graph.h:187
cerata::Instance::component_
Component * component_
The component that this instance instantiates.
Definition: graph.h:252
cerata::Graph::Get
T * Get(const std::string &name) const
Get one object of a specific type.
Definition: graph.h:76
cerata::Graph::id
ID id() const
Return the graph type ID.
Definition: graph.h:49
cerata::Named::name
std::string name() const
Return the name of the object.
Definition: utils.h:45
cerata::Node::NodeID
NodeID
Node type IDs with different properties.
Definition: node.h:45
cerata::Graph::prt_arr
PortArray * prt_arr(const std::string &name) const
Shorthand to Get<PortArray>(...)
Definition: graph.cc:179
cerata::Component::Remove
Graph & Remove(Object *object) override
Remove an object from the component.
Definition: graph.cc:366
cerata::component
std::shared_ptr< Component > component(std::string name, const std::vector< std::shared_ptr< Object >> &objects, ComponentPool *component_pool)
Construct a Component with initial nodes.
Definition: graph.cc:248
cerata::Graph::prt
Port * prt(const std::string &name) const
Shorthand to Get<Port>(...)
Definition: graph.cc:159
cerata::Graph::par
Parameter * par(const std::string &name) const
Shorthand to Get<Parameter>(...)
Definition: graph.cc:167
cerata::Instance::SetParent
Graph & SetParent(Graph *parent)
Set the parent.
Definition: graph.cc:408
cerata::Graph::SetMeta
Graph & SetMeta(const std::string &key, std::string value)
Set metadata.
Definition: graph.cc:215
cerata::Graph::GetNodes
std::vector< Node * > GetNodes() const
Get all nodes.
Definition: graph.h:104
cerata::default_component_pool
ComponentPool * default_component_pool()
Return a global default component pool.
Definition: pool.h:95
cerata::Object
A Cerata Object on a graph.
Definition: object.h:34
cerata::Component::children_
std::vector< std::unique_ptr< Instance > > children_
Instances.
Definition: graph.h:209
cerata::Named
Convenience structure for anything that is named. Names are case-sensitive.
Definition: utils.h:40
cerata::Graph::sig
Signal * sig(const std::string &name) const
Shorthand to Get<Signal>(...)
Definition: graph.cc:163
cerata::Graph::GetArraysOfType
std::vector< NodeArray * > GetArraysOfType(Node::NodeID id) const
Get all arrays of a specific type.
Definition: graph.cc:148
cerata::Graph::FindNode
std::optional< Node * > FindNode(const std::string &node_name) const
Find a node with a specific name.
Definition: graph.cc:100
cerata::Parameter
A Parameter node.
Definition: parameter.h:29
cerata::Graph::ToStringAllOjects
std::string ToStringAllOjects() const
Return a comma separated list of object names.
Definition: graph.cc:229
cerata::Instance
An instance.
Definition: graph.h:231
cerata::Graph::COMPONENT
@ COMPONENT
A component graph.
Definition: graph.h:41
cerata::Component::HasChild
bool HasChild(const std::string &name) const
Return true if child graph exists on instance.
Definition: graph.cc:322
cerata::Graph::GetNodesOfType
std::vector< Node * > GetNodesOfType(Node::NodeID id) const
Get all nodes of a specific type.
Definition: graph.cc:138
cerata::Component::AddChild
Component & AddChild(std::unique_ptr< Instance > child)
Add and take ownership of an Instance graph.
Definition: graph.cc:240
cerata::Graph::Has
bool Has(const std::string &name)
Return true if object with name already exists on graph.
Definition: graph.cc:220
cerata::Graph::objects
std::vector< Object * > objects() const
Get all objects.
Definition: graph.h:131
cerata::Graph::INSTANCE
@ INSTANCE
An instance graph.
Definition: graph.h:42
cerata::PortArray
An array of port nodes.
Definition: array.h:123
cerata::Graph::GetNode
Node * GetNode(const std::string &node_name) const
Get a Node of a specific type with a specific name.
Definition: graph.cc:109
cerata::Graph::CountNodes
size_t CountNodes(Node::NodeID id) const
Count nodes of a specific node type.
Definition: graph.cc:118
cerata::Graph::Remove
virtual Graph & Remove(Object *obj)
Remove an object from the component.
Definition: graph.cc:91
cerata::Graph::ID
ID
Graph type ID for convenient run-time type checking.
Definition: graph.h:40
cerata::Instance::Instance
Instance(Component *comp, std::string name, Component *parent)
Construct an Instance of a Component, copying over all its ports and parameters.
Definition: graph.cc:378
cerata::Graph::id_
ID id_
Graph type id for convenience.
Definition: graph.h:146
cerata::Component::inst_to_comp_map
NodeMap * inst_to_comp_map()
Return the component node to instance node mapping.
Definition: graph.h:198
cerata::Port
A port is a terminator node on a graph.
Definition: port.h:57
cerata::Component::inst_to_comp
NodeMap inst_to_comp
Mapping for instance nodes that have been connected.
Definition: graph.h:215
cerata::Instance::Component
friend Component
Only a Component should be able to make instances.
Definition: graph.h:246
cerata::Graph::operator()
Node * operator()(const std::string &node_name) const
Obtain a node by name.
Definition: graph.h:95