Newer
Older
Martin Christoph Hierholzer
committed
/*
* VariableNetwork.cc
*
* Created on: Jun 14, 2016
* Author: Martin Hierholzer
*/
#include "VariableNetwork.h"
#include "Accessor.h"
namespace ChimeraTK {
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
bool VariableNetwork::hasAppNode(AccessorBase *a, AccessorBase *b) const {
if(feeder.type == NodeType::Application) {
if(a == feeder.appNode || (b != nullptr && b == feeder.appNode) ) return true;
}
Martin Christoph Hierholzer
committed
// search for a and b in the inputAccessorList
Martin Christoph Hierholzer
committed
size_t c = count_if( consumerList.begin(), consumerList.end(),
[a,b](const Node n) {
Martin Christoph Hierholzer
committed
if(n.type != NodeType::Application) return false;
return a == n.appNode || ( b != nullptr && b == n.appNode );
Martin Christoph Hierholzer
committed
} );
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
if(c > 0) return true;
Martin Christoph Hierholzer
committed
return false;
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
bool VariableNetwork::hasFeedingNode() const {
Martin Christoph Hierholzer
committed
if(feeder.type == NodeType::invalid) return false;
Martin Christoph Hierholzer
committed
return true;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
size_t VariableNetwork::countConsumingNodes() const {
return consumerList.size();
}
/*********************************************************************************************************************/
size_t VariableNetwork::countFixedImplementations() const {
Martin Christoph Hierholzer
committed
size_t count = 0;
Martin Christoph Hierholzer
committed
if(feeder.hasImplementation()) count++;
count += count_if( consumerList.begin(), consumerList.end(),
[](const Node n) {
return n.hasImplementation();
} );
Martin Christoph Hierholzer
committed
return count;
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetwork::addAppNode(AccessorBase &a) {
if(hasAppNode(&a)) return; // already in the network
Node node;
node.type = NodeType::Application;
node.mode = a.getUpdateMode();
node.appNode = &a;
if(a.isFeeding()) {
if(hasFeedingNode()) {
Martin Christoph Hierholzer
committed
throw ApplicationExceptionWithID<ApplicationExceptionID::illegalVariableNetwork>(
"Trying to add a feeding accessor to a network already having a feeding accessor.");
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
feeder = node;
Martin Christoph Hierholzer
committed
valueType = &(a.getValueType());
}
else {
Martin Christoph Hierholzer
committed
consumerList.push_back(node);
Martin Christoph Hierholzer
committed
}
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetwork::addConsumingPublication(const std::string& name) {
Node node;
node.type = NodeType::ControlSystem;
node.mode = UpdateMode::push;
node.publicName = name;
consumerList.push_back(node);
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetwork::addFeedingPublication(AccessorBase &a, const std::string& name) {
Martin Christoph Hierholzer
committed
addFeedingPublication(a.getValueType(), name);
}
/*********************************************************************************************************************/
void VariableNetwork::addFeedingPublication(const std::type_info &typeInfo, const std::string& name) {
Martin Christoph Hierholzer
committed
if(hasFeedingNode()) {
Martin Christoph Hierholzer
committed
throw ApplicationExceptionWithID<ApplicationExceptionID::illegalVariableNetwork>(
"Trying to add control-system-to-device publication to a network already having a feeding accessor.");
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
feeder.type = NodeType::ControlSystem;
feeder.mode = UpdateMode::push;
feeder.publicName = name;
Martin Christoph Hierholzer
committed
valueType = &typeInfo;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetwork::addConsumingDeviceRegister(const std::string &deviceAlias, const std::string ®isterName,
Martin Christoph Hierholzer
committed
UpdateMode mode) {
Martin Christoph Hierholzer
committed
Node node;
node.type = NodeType::Device;
node.mode = mode;
node.deviceAlias = deviceAlias;
node.registerName = registerName;
consumerList.push_back(node);
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetwork::addFeedingDeviceRegister(AccessorBase &a, const std::string &deviceAlias,
Martin Christoph Hierholzer
committed
const std::string ®isterName, UpdateMode mode) {
addFeedingDeviceRegister(a.getValueType(), deviceAlias, registerName, mode);
}
/*********************************************************************************************************************/
void VariableNetwork::addFeedingDeviceRegister(const std::type_info &typeInfo, const std::string &deviceAlias,
const std::string ®isterName, UpdateMode mode) {
Martin Christoph Hierholzer
committed
if(hasFeedingNode()) {
Martin Christoph Hierholzer
committed
throw ApplicationExceptionWithID<ApplicationExceptionID::illegalVariableNetwork>(
"Trying to add a feeding device register to a network already having a feeding accessor.");
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
feeder.type = NodeType::Device;
feeder.mode = mode;
feeder.deviceAlias = deviceAlias;
feeder.registerName = registerName;
Martin Christoph Hierholzer
committed
valueType = &typeInfo;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
void VariableNetwork::dump() const {
std::cout << "VariableNetwork {" << std::endl;
Martin Christoph Hierholzer
committed
std::cout << " feeder";
feeder.dump();
std::cout << " number of consumers: " << consumerList.size() << std::endl;
size_t count = 0;
for(auto &consumer : consumerList) {
std::cout << " consumer " << ++count << ":";
consumer.dump();
}
Martin Christoph Hierholzer
committed
std::cout << "}" << std::endl;
}
}