1 #ifndef _NNDEPLOY_DAG_COMPOSITE_NODE_H_
2 #define _NNDEPLOY_DAG_COMPOSITE_NODE_H_
21 key_ =
"nndeploy::dag::CompositeNode";
22 is_composite_node_ =
true;
24 CompositeNode(
const std::string &name,
const std::vector<Edge *> &inputs,
25 const std::vector<Edge *> &outputs)
26 : dag::
Node(name, inputs, outputs) {
27 key_ =
"nndeploy::dag::CompositeNode";
28 for (
auto input : inputs) {
29 if (
nullptr == addEdge(input)) {
34 for (
auto output : outputs) {
35 if (
nullptr == addEdge(output)) {
41 is_composite_node_ =
true;
57 std::vector<std::shared_ptr<Edge>> inputs);
59 std::vector<std::shared_ptr<Edge>> outputs);
75 bool is_external =
true);
80 template <
typename T,
typename... Args,
81 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
84 template <
typename T,
typename... Args,
85 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
86 Node *
createNode(
const NodeDesc &desc, Args &...args);
88 template <
typename T,
typename... Args,
89 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
100 std::vector<Edge *> outputs);
114 std::shared_ptr<base::Param> param);
116 const std::string &node_name);
135 rapidjson::Document::AllocatorType &allocator);
155 template <
typename T,
typename... Args,
156 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
159 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
162 std::vector<Edge *> inputs;
163 std::vector<Edge *> outputs;
164 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
167 node_wrapper->
node_ = node;
168 node_wrapper->
name_ = name;
176 template <
typename T,
typename... Args,
177 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
179 const std::string &name = desc.
getName();
181 std::vector<std::string> input_names = desc.
getInputs();
182 std::vector<std::string> output_names = desc.
getOutputs();
185 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
188 std::vector<Edge *> inputs;
189 for (
auto input_name : input_names) {
191 if (input ==
nullptr) {
194 inputs.emplace_back(input);
196 std::vector<Edge *> outputs;
197 for (
auto output_name : output_names) {
199 if (output ==
nullptr) {
202 outputs.emplace_back(output);
204 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
205 if (node ==
nullptr) {
211 node_wrapper->
node_ = node;
212 node_wrapper->
name_ = name;
213 for (
auto input : inputs) {
215 if (input_wrapper ==
nullptr) {
216 input_wrapper = this->
addEdge(input);
218 input_wrapper->
consumers_.emplace_back(node_wrapper);
220 for (
auto output : outputs) {
222 if (output_wrapper ==
nullptr) {
223 output_wrapper = this->
addEdge(output);
225 output_wrapper->
producers_.emplace_back(node_wrapper);
236 template <
typename T,
typename... Args,
237 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
240 const std::string &name = desc.
getName();
242 std::vector<std::string> input_names = desc.
getInputs();
243 std::vector<std::string> output_names = desc.
getOutputs();
246 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
249 std::vector<Edge *> inputs;
250 for (
auto input_name : input_names) {
252 if (input ==
nullptr) {
255 inputs.emplace_back(input);
257 std::vector<Edge *> outputs;
258 for (
auto output_name : output_names) {
260 if (output ==
nullptr) {
263 outputs.emplace_back(output);
265 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
266 if (node ==
nullptr) {
272 node_wrapper->
node_ = node;
273 node_wrapper->
name_ = name;
274 for (
auto input : inputs) {
276 if (input_wrapper ==
nullptr) {
277 input_wrapper = this->
addEdge(input);
279 input_wrapper->
consumers_.emplace_back(node_wrapper);
281 for (
auto output : outputs) {
283 if (output_wrapper ==
nullptr) {
284 output_wrapper = this->
addEdge(output);
286 output_wrapper->
producers_.emplace_back(node_wrapper);
Composite node Composite node is a special type of node in nndeploy that enhances the capabilities of...
EdgeWrapper * addEdgeSharedPtr(std::shared_ptr< Edge > edge)
Node * createInfer(const NodeDesc &desc, base::InferenceType type)
base::Status setNodeDesc(Node *node, const NodeDesc &desc)
EdgeWrapper * getEdgeWrapper(Edge *edge)
std::shared_ptr< Node > getNodeSharedPtr(const std::string &name)
Edge * createEdge(const std::string &name)
base::Status markInputEdge(std::vector< Edge * > inputs)
virtual base::Status setInputSharedPtr(std::shared_ptr< Edge > input, int index=-1)
Set input edge (shared pointer)
std::set< std::string > used_edge_names_
std::shared_ptr< base::Param > getNodeParamSharedPtr(const std::string &node_name)
virtual base::Status deserialize(rapidjson::Value &json)
Deserialize from JSON.
Node * getNodeByKey(const std::string &key)
base::Status addNodeSharedPtr(std::shared_ptr< Node > node)
NodeWrapper * getNodeWrapper(const std::string &name)
virtual base::Status init()
Initialize node.
virtual base::Status setOutputSharedPtr(std::shared_ptr< Edge > output, int index=-1)
Set output edge (shared pointer)
std::vector< NodeWrapper * > sortDFS()
EdgeWrapper * getEdgeWrapper(const std::string &name)
std::set< std::string > used_node_names_
std::vector< std::shared_ptr< Node > > shared_node_repository_
NodeWrapper * getNodeWrapper(Node *node)
virtual base::Status construct()
base::Status setNodeParam(const std::string &node_name, base::Param *param)
Node * createNode(const NodeDesc &desc)
virtual base::Status setInputsSharedPtr(std::vector< std::shared_ptr< Edge >> inputs)
Set all input edges (shared pointer)
base::Status addNode(Node *node, bool is_external=true)
std::vector< Node * > getNodesByKey(const std::string &key)
virtual base::Status run()=0
Run node (pure virtual function)
virtual base::Status setOutputsSharedPtr(std::vector< std::shared_ptr< Edge >> outputs)
Set all output edges (shared pointer)
virtual base::Status deinit()
Deinitialize node.
base::Status updteEdge(EdgeWrapper *edge_wrapper, Edge *edge, bool is_external=true)
CompositeNode(const std::string &name, const std::vector< Edge * > &inputs, const std::vector< Edge * > &outputs)
std::shared_ptr< Edge > createEdgeSharedPtr(const std::string &name)
CompositeNode(const std::string &name)
Edge * getEdge(const std::string &name)
Node * createNode4Py(const NodeDesc &desc)
Node * getNode(const std::string &name)
virtual base::Status setInput(Edge *input, int index=-1)
Set input edge.
EdgeWrapper * addEdge(Edge *edge, bool is_external=true)
base::Status updateNodeIO(Node *node, std::vector< Edge * > inputs, std::vector< Edge * > outputs)
base::Param * getNodeParam(const std::string &node_name)
virtual base::Status defaultParam()
Configure default parameters.
std::vector< std::shared_ptr< Edge > > shared_edge_repository_
virtual base::Status deserialize(const std::string &json_str)
Deserialize from JSON string.
std::vector< EdgeWrapper * > edge_repository_
virtual std::string serialize()
Serialize to JSON string.
std::vector< NodeWrapper * > node_repository_
std::shared_ptr< Edge > getEdgeSharedPtr(const std::string &name)
virtual base::Status setOutput(Edge *output, int index=-1)
Set output edge.
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
Serialize to JSON.
virtual base::Status setInputs(std::vector< Edge * > inputs)
Set all input edges.
base::Status markOutputEdge(std::vector< Edge * > outputs)
virtual base::Status setOutputs(std::vector< Edge * > outputs)
Set all output edges.
base::Status setNodeParamSharedPtr(const std::string &node_name, std::shared_ptr< base::Param > param)
std::vector< NodeWrapper * > producers_
std::vector< NodeWrapper * > consumers_
Edge class in DAG graph for connecting nodes and transferring data.
std::vector< std::string > getInputs() const
Get input edge name list.
std::string getName() const
Get node name.
std::vector< std::string > getOutputs() const
Get output edge name list.
base::Status setCompositeNode(CompositeNode *composite_node)
Set parent composite node.
#define NNDEPLOY_LOGE(fmt,...)
#define NNDEPLOY_CC_API
api
EdgeWrapper * findEdgeWrapper(std::vector< EdgeWrapper * > &edge_repository, const std::string &edge_name)
Node * createNode(const std::string &node_key, const std::string &node_name)
Create node.