00001 #include "Node.h"
00002 #include <algorithm>
00003 #include <QJsonArray>
00004 #include <QHash>
00005 #include <Graph.h>
00006
00007 #include "utils.h"
00008
00009 namespace Elve {
00010 Node::Node(const NodeData &data) : mData(data),mLevel(-1)
00011 {
00012
00013 }
00014
00015 const NodeName& Node::name() const {
00016 return mData.name();
00017 }
00018
00019 void Node::addChild(Node* child, Index from, Index to)
00020 {
00021 child->_addAncestor(this,from,to);
00022 _addChild(child,from,to);
00023 }
00024
00025
00026 void Node::addAncestor(Node* anc, Index from, Index to)
00027 {
00028 anc->_addChild(this,from,to);
00029 _addAncestor(anc,from,to);
00030 }
00031
00032 size_t Node::ancestorCount() const {
00033 return mAncestors.size();
00034 }
00035
00036 size_t Node::childCount() const
00037 {
00038 return mChildren.size();
00039 }
00040
00041 const Index& Node::IOIndex() const
00042 {
00043 return mData.ioIndex();
00044 }
00045
00046 void Node::_addChild(Node* child, Index from, Index to)
00047 {
00048 lazy_add(mChildren,child);
00049 mFanOut.push_back({child,from,to});
00050 }
00051
00052 void Node::_addAncestor(Node* anc, Index from, Index to)
00053 {
00054 lazy_add(mAncestors,anc);
00055 mFanIn.push_back({anc,from,to});
00056 }
00057
00058 const Node::Ancestors& Node::ancestors() const
00059 {
00060 return mAncestors;
00061 }
00062
00063 bool Node::isInput() const
00064 {
00065 return mData.type() == INPUT or mData.type() == INPUT_CLUSTER;
00066 }
00067
00068 bool Node::isOutput() const
00069 {
00070 return mData.type() == OUTPUT or mData.type() == OUTPUT_CLUSTER;
00071 }
00072
00073 const NodeType &Node::type() const
00074 {
00075 return mData.type();
00076 }
00077
00078 bool Node::isCluster() const {
00079 return type() == CLUSTER || type() == INPUT_CLUSTER || type() == OUTPUT_CLUSTER;
00080 }
00081
00082 const NodeLevel& Node::level() const {
00083 if(type() == INPUT) {
00084 return mLevel = 0;
00085 }
00086 if(mLevel == -1) {
00087 if(properties().contains("level")) {
00088 mLevel = properties().value("level").toInt();
00089 } else {
00090 for(const Node* an : ancestors()) {
00091 if(an->level() +1 > mLevel) {
00092 mLevel = an->level() + 1;
00093 }
00094 }
00095 }
00096 }
00097 return mLevel;
00098 }
00099
00100 const SharedGraph Node::getClusteredGraph() const
00101 {
00102 return mGraph;
00103 }
00104
00105 void Node::setClusteredGraph(SharedGraph graph)
00106 {
00107 mGraph = graph;
00108 }
00109
00110 int Node::inputCount() const {
00111 return mData.inputCount();
00112 }
00113
00114 int Node::outputCount() const {
00115 return mData.outputCount();
00116 }
00117
00118 Name Node::inputName(Index i) const {
00119 return mData.inputName(i);
00120 }
00121
00122 Name Node::outputName(Index i) const {
00123 return mData.outputName(i);
00124 }
00125
00126 const Node::Fan& Node::fanIn() const {
00127 return mFanIn;
00128 }
00129
00130 const Node::Fan& Node::fanOut() const {
00131 return mFanOut;
00132 }
00133
00134 const QJsonObject& Node::properties() const {
00135 return mData.properties();
00136 }
00137
00138 const NodeData& Node::data() const {
00139 return mData;
00140 }
00141
00142 const NodeID& Node::id() const {
00143 return mData.id();
00144 }
00145
00146 const Node::Children& Node::children() const
00147 {
00148 return mChildren;
00149 }
00150 }