ELVE  1
ELVE Logic Visualization Explorer
/home/travis/build/stdgregwar/elve/Core/GraphData.cpp
00001 #include "GraphData.h"
00002 #include <QJsonObject>
00003 #include <QJsonArray>
00004 
00005 #include "Node.h"
00006 
00007 namespace Elve {
00008 
00009 GraphData::GraphData(const NodeDatas& nodesData,QString filename)
00010     : mDatas(nodesData), mFilename(filename)
00011 {
00012 
00013 }
00014 
00015 GraphData::GraphData(const QJsonObject& obj) {
00016     mFilename = obj.value("filename").toString("unnamed");
00017     size_t size = obj.value("node_count").toInt(0);
00018     mDatas.reserve(size);
00019     QJsonArray nodes = obj.value("nodes").toArray();
00020     for(const QJsonValue& j : nodes) {
00021         mDatas.emplace_back(
00022                     //(j.toObject().value("id").toInt(0)),
00023                     (j.toObject())
00024                     );
00025     }
00026 }
00027 
00028 GraphData::Builder::Builder() : mID(0)
00029 {
00030 
00031 }
00032 
00033 NodeID GraphData::Builder::id(const NodeName& name) {
00034     auto it = mIDs.find(name);
00035     if(it == mIDs.end()) {
00036         NodeID i = mID++;
00037         mNames.emplace(i,name);
00038         mIDs.emplace(name,i);
00039         return i;
00040     } else {
00041         return it->second;
00042     }
00043 }
00044 
00045 NodeType GraphData::Builder::type(const NodeID& id) {
00046     auto it = mTypes.find(id);
00047     if(it == mTypes.end()) {
00048         return NODE;
00049     }
00050     return it->second;
00051 }
00052 
00053 NodeProperties GraphData::Builder::props(const NodeID &id) {
00054     auto it = mProperties.find(id);
00055     if(it == mProperties.end()) {
00056         return {};
00057     }
00058     return it->second;
00059 }
00060 
00061 Index GraphData::Builder::index(const NodeID& id) {
00062     auto it = mIndice.find(id);
00063     if(it == mIndice.end()) {
00064         return 0;
00065     }
00066     return it->second;
00067 }
00068 
00069 Dependencies GraphData::Builder::dependencies(const NodeID& i) {
00070     auto it = mDependencies.find(i);
00071     if(it == mDependencies.end()) {
00072         return {};
00073     }
00074     return it->second;
00075 }
00076 
00077 void GraphData::Builder::setDependencies(const NodeName& name, const Dependencies &dependencies) {
00078     mDependencies[id(name)] = dependencies;
00079 }
00080 
00081 
00082 void GraphData::Builder::setDependencies(const NodeName& name, const NodeNames &dependencies) {
00083     Dependencies deps;
00084     Index from = 0;
00085     Index to = 0;
00086     for(const NodeName& n : dependencies) {
00087         deps.push_back(Dependency{id(n),from,to++});
00088     }
00089     setDependencies(name,deps);
00090 }
00091 
00092 void GraphData::Builder::setProperties(const NodeName& name, const NodeProperties& props) {
00093     mProperties[id(name)] = props;
00094 }
00095 
00096 void GraphData::Builder::addProperty(const NodeName& name,const QString& pname,const QJsonValue& val) {
00097     properties(name).insert(pname,val);
00098 }
00099 
00100 void GraphData::Builder::setNode(const NodeName& name, const Node& node) {
00101     setProperties(name,node.properties());
00102     setIoIndex(name,node.IOIndex());
00103     Dependencies deps;
00104     for(const Node::Connexion& c : node.fanIn()) {
00105         deps.push_back(Dependency{id(c.node->name()),c.from,c.to});
00106     }
00107     setDependencies(name,deps);
00108     //setType(name,node.type());
00109 }
00110 
00111 QJsonObject& GraphData::Builder::properties(const NodeName& name) {
00112     auto it = mProperties.find(id(name));
00113     if(it == mProperties.end()) {
00114         auto p = mProperties.emplace(id(name),QJsonObject{});
00115         return p.first->second;
00116     }
00117     return it->second;
00118 }
00119 
00120 void GraphData::Builder::setType(const NodeName& name, const NodeType& type) {
00121     NodeID i = id(name);
00122     mTypes[i] = type;
00123     if(type == OUTPUT_CLUSTER || type == OUTPUT) {
00124         mOutputs.push_back(i);
00125     } else if (type == INPUT_CLUSTER || type == INPUT) {
00126         mInputs.push_back(i);
00127     }
00128 }
00129 
00130 void GraphData::Builder::setIoIndex(const NodeName& name, const Index& index) {
00131     mIndice[id(name)] = index;
00132 }
00133 
00134 const SharedData GraphData::Builder::build(const QString& filename) {
00135     NodeDatas dats;
00136     for(NodeID i = 0; i < mID; i++) {
00137         dats.emplace_back(i,name(i),dependencies(i),type(i),props(i),index(i),
00138                           nodeInputCount(i),nodeOutputCount(i),
00139                           nodeInputNames(i),nodeOutputNames(i));
00140     }
00141     return std::make_shared<GraphData>(dats,filename);
00142 }
00143 
00144 const NodeIDs& GraphData::Builder::outputs() const {
00145     return mOutputs;
00146 }
00147 
00148 const NodeIDs& GraphData::Builder::inputs() const {
00149     return mInputs;
00150 }
00151 
00152 NodeName GraphData::Builder::name(const NodeID& id) const {
00153     auto it = mNames.find(id);
00154     if(it != mNames.end()) return it->second;
00155     return "no_name";
00156 }
00157 
00158 void GraphData::Builder::setNodeInputNames(const NodeName& name, const Names& names) {
00159     mNodeInputCount.at(id(name)) = names.size();
00160     mNodeInputsNames.at(id(name)) = names;
00161 }
00162 
00163 void GraphData::Builder::setNodeOutputNames(const NodeName& name, const Names& names) {
00164     mNodeOutputCount.at(id(name)) = names.size();
00165     mNodeOutputsNames.at(id(name)) = names;
00166 }
00167 
00168 void GraphData::Builder::setId(const NodeName &name, const NodeID &id) {
00169     mIDs[name] = id;
00170     mNames[id] = name;
00171     mID = id+1 > mID ? id+1 : mID;
00172 }
00173 
00174 void GraphData::Builder::addDependency(const NodeID &from, const NodeID &to) {
00175     mDependencies[to].push_back(from);
00176 }
00177 
00178 Names GraphData::Builder::nodeInputNames(const NodeID& id) {
00179     auto it = mNodeInputsNames.find(id);
00180     if(it == mNodeInputsNames.end()) {
00181         return {};
00182     }
00183     return it->second;
00184 }
00185 
00186 Names GraphData::Builder::nodeOutputNames(const NodeID& id) {
00187     auto it = mNodeOutputsNames.find(id);
00188     if(it == mNodeOutputsNames.end()) {
00189         return {};
00190     }
00191     return it->second;
00192 }
00193 
00194 int GraphData::Builder::nodeInputCount(const NodeID& id) {
00195     auto it = mNodeInputCount.find(id);
00196     if(it != mNodeInputCount.end()) {
00197         return it->second;
00198     } else {
00199         return dependencies(id).size(); //Defaults
00200     }
00201 }
00202 
00203 int GraphData::Builder::nodeOutputCount(const NodeID& id) {
00204     auto it = mNodeOutputCount.find(id);
00205     if(it != mNodeOutputCount.end()) {
00206         return it->second;
00207     } else {
00208         return type(id) == OUTPUT or type(id) == OUTPUT_CLUSTER ? 0 : 1; //Defaults
00209     }
00210 }
00211 
00212 const NodeDatas& GraphData::nodeDatas() const {
00213     return mDatas;
00214 }
00215 
00216 const QString& GraphData::filename() const {
00217     return mFilename;
00218 }
00219 
00220 QJsonObject GraphData::json() const {
00221     QJsonObject obj;
00222     obj.insert("filename",mFilename);
00223     obj.insert("node_count",(int)mDatas.size());
00224 
00225     using pair_type = NodeDatas::value_type;
00226     QJsonArray nodes;
00227     for(const NodeData& d : mDatas) {
00228         nodes.append(d.json());
00229     }
00230     obj.insert("nodes",nodes);
00231     return obj;
00232 }
00233 
00234 }
 All Classes Functions