Welcome to Chris Johnsons' Portfolio

G'day. My name is Christopher Johnson and you've reached my humble portfolio which contains bits and pieces that I've created over the years. I've decided to post as much as I can in order to show where I've come from and in what ways I've improved. Thanks for stopping by. I hope you enjoy what you see. If you have any questions then feel free to contact me. Chris.

Twitter Update

Follow me on Twitter

Navigation

My Websites

Most Recent Additions

Browse by Month

Content Syndication

Meta

GUI Generator Code Snippets

GraphUtil.cpp – Graph Util Class

#include "GraphUtil.h"
 
using namespace std;
 
JobGraph& operator >>(YAML::Parser& in, JobGraph& graph) {
    processDocuments(in, graph);
    addDependsOnGraphItems(graph);
    updateConditionTrees(graph);
    return graph;
}
 
void processDocuments(YAML::Parser& in, JobGraph &graph) {
    YAML::Node documentChild;
 
    while (in.GetNextDocument(documentChild)) {
        processNodes(documentChild, graph);
    }
}
 
void processNodes(const YAML::Node &documentChild, JobGraph &graph) {
    if (documentChild.GetType() != YAML::CT_SEQUENCE) {
        return;
    }
 
    map<string, vector<string> > dependencyMap;
 
    for (YAML::Iterator seqIt = documentChild.begin(); seqIt != documentChild.end(); ++seqIt) {
        const YAML::Node &mapChild = *seqIt;
 
        if (mapChild.GetType() != YAML::CT_MAP) {
            continue;
        }
 
        for (YAML::Iterator mapIt = mapChild.begin(); mapIt != mapChild.end(); ++mapIt) {
            string nodeId;
            mapIt.first() >> nodeId;
 
            if (isValidGraphName(nodeId) == false) {
                qWarning() << nodeId.c_str() << " is not a valid node name. Node ignored";
                continue;
            }
 
            if (graph.getNode(&nodeId) != NULL) {
                qWarning() << nodeId.c_str() << " has been repeated, second instance of"
                                   << " the node not included";
                continue;
            }
 
            NodeItem node(&nodeId);
            const YAML::Node& mapValue = mapIt.second();
            processNode(mapValue, node, dependencyMap);
 
            if (node.getAttributes().size() != 0) {
                graph.addNode(&node);
            } else {
                qWarning() << "Node: " << node.getId()->c_str() << " was not added as"
                                  << " it did not contain any descriptors";
            }
        }
    }
 
    resolveDependsOn(graph, dependencyMap);
}
 
void resolveDependsOn(JobGraph &graph, map<string, vector<string> > dependencyMap) {
    for (map<string, vector<string> >::iterator mapIter = dependencyMap.begin(); 
           mapIter != dependencyMap.end(); mapIter++) {
        for (vector<string>::iterator vecIter = mapIter->second.begin(); 
               vecIter != mapIter->second.end(); vecIter++) {
            NodeItem *fromNode = graph.getNode(&(mapIter->first));
 
            if (fromNode == NULL) {
                qWarning() << "Dependency from " << mapIter->first.c_str() 
                                  << " to " << vecIter->c_str() << " cannot be created as node "
                                  << mapIter->first.c_str() << "does not exist.";
            }
 
            NodeItem *toNode = graph.getNode(&(*vecIter));
 
            if (toNode == NULL) {
                qWarning() << "Dependency from " << mapIter->first.c_str() 
                                  << " to " << vecIter->c_str() << " cannot be created as node " 
                                 << vecIter->c_str() << "does not exist.";
            }
 
            graph.addEdge(fromNode, toNode);
        }
    }
}
 
void addDependsOnGraphItems(JobGraph& graph) {
    const vector<NodeItem*> &nodes = graph.getNodes();
    foreach(NodeItem *node, nodes) {
        const vector<ComponentDescriptor* > &descriptors = node->getAttributes();
        string currNodeName = *(node->getId());
        addDependsOnNodeEnable(graph, node);
        foreach(ComponentDescriptor *descriptor, descriptors) {
            addDependsOnDescriptorValue(graph, descriptor, currNodeName);
            addDependsOnDescriptorWidget(graph, descriptor, currNodeName);
        }
    }
}
 
void addDependsOnNodeEnable(JobGraph &graph, NodeItem *node) {
    ConditionTree *tree = node->getConditionTree();
 
    if (tree != NULL) {
        set<string> dependencies = tree->getRequiredDescriptorNames();
        foreach(string nodeDescriptorStr, dependencies) {
            string nodeName = getNodeFromEmbedStr(nodeDescriptorStr);
            NodeItem *node2 = graph.getNode(&nodeName);
 
            if (node2 == NULL) {
                qWarning() << "Couldn't find node " << nodeName.c_str() << " therefore the "
                                  << "value dependency between it and " << node2->getId()->c_str() 
                                  << "could not be created\n";
                continue;
            }
 
            string descriptorName = getDescriptorFromEmbedStr(nodeDescriptorStr);
            ComponentDescriptor *dependDescriptor = 
                        node2->getComponentDescriptorById(&descriptorName);
 
            if (dependDescriptor == NULL) {
                qWarning() << "Couldn't find descriptor " << descriptorName.c_str() 
                                  << " therefore the value dependency between it and "
                                  << node2->getId()->c_str() << "could not be created\n";
                continue;
            }
 
            dependDescriptor->addNodeDependsOn(node);
        }
    }
}