Newer
Older
Martin Christoph Hierholzer
committed
/*
* VariableNetworkNode.cc
*
* Created on: Jun 23, 2016
* Author: Martin Hierholzer
*/
#include "VariableNetworkNode.h"
#include "VariableNetwork.h"
#include "Application.h"
#include "EntityOwner.h"
#include "VariableNetworkNodeDumpingVisitor.h"
Martin Christoph Hierholzer
committed
namespace ChimeraTK {
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
VariableNetworkNode::VariableNetworkNode(const VariableNetworkNode &other)
: pdata(other.pdata)
Martin Christoph Hierholzer
committed
{}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
VariableNetworkNode& VariableNetworkNode::operator=(const VariableNetworkNode &rightHandSide) {
pdata = rightHandSide.pdata;
return *this;
}
/*********************************************************************************************************************/
VariableNetworkNode::VariableNetworkNode(EntityOwner *owner, ChimeraTK::TransferElementAbstractor *accessorBridge,
const std::string &name,
Martin Christoph Hierholzer
committed
VariableDirection direction, std::string unit, size_t nElements, UpdateMode mode,
Martin Christoph Hierholzer
committed
const std::string &description, const std::type_info* valueType,
const std::unordered_set<std::string> &tags)
: pdata(boost::make_shared<VariableNetworkNode_data>())
Martin Christoph Hierholzer
committed
{
pdata->owningModule = owner;
Martin Christoph Hierholzer
committed
pdata->type = NodeType::Application;
Martin Christoph Hierholzer
committed
pdata->appNode = accessorBridge;
pdata->name = name;
pdata->qualifiedName = owner->getQualifiedName() + "/" + name;
Martin Christoph Hierholzer
committed
pdata->mode = mode;
pdata->direction = direction;
pdata->valueType = valueType;
pdata->unit = unit;
pdata->nElements = nElements;
pdata->description = description;
Martin Christoph Hierholzer
committed
pdata->tags = tags;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
VariableNetworkNode::VariableNetworkNode(const std::string &name, const std::string &devAlias,
const std::string ®Name, UpdateMode mode, VariableDirection dir, const std::type_info &valTyp, size_t nElements)
: pdata(boost::make_shared<VariableNetworkNode_data>())
Martin Christoph Hierholzer
committed
{
Martin Christoph Hierholzer
committed
pdata->name = name;
Martin Christoph Hierholzer
committed
pdata->type = NodeType::Device;
pdata->mode = mode;
Martin Christoph Hierholzer
committed
pdata->direction = dir;
pdata->valueType = &valTyp;
pdata->deviceAlias = devAlias;
pdata->registerName = regName;
Martin Christoph Hierholzer
committed
pdata->nElements = nElements;
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
VariableNetworkNode::VariableNetworkNode(std::string pubName, VariableDirection dir, const std::type_info &valTyp,
size_t nElements)
: pdata(boost::make_shared<VariableNetworkNode_data>())
Martin Christoph Hierholzer
committed
{
Martin Christoph Hierholzer
committed
pdata->name = pubName;
Martin Christoph Hierholzer
committed
pdata->type = NodeType::ControlSystem;
pdata->mode = UpdateMode::push;
pdata->direction = dir;
pdata->valueType = &valTyp;
pdata->publicName = pubName;
Martin Christoph Hierholzer
committed
pdata->nElements = nElements;
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
VariableNetworkNode::VariableNetworkNode(VariableNetworkNode& nodeToTrigger, int)
: pdata(boost::make_shared<VariableNetworkNode_data>())
Martin Christoph Hierholzer
committed
{
pdata->type = NodeType::TriggerReceiver;
pdata->direction = VariableDirection::consuming;
Martin Christoph Hierholzer
committed
pdata->nodeToTrigger = nodeToTrigger;
Martin Christoph Hierholzer
committed
pdata->name = "trigger:"+nodeToTrigger.getName();
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
VariableNetworkNode::VariableNetworkNode(boost::shared_ptr<VariableNetworkNode_data> _pdata)
: pdata(_pdata)
{}
/*********************************************************************************************************************/
VariableNetworkNode::VariableNetworkNode()
: pdata(boost::make_shared<VariableNetworkNode_data>())
{}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetworkNode::setOwner(VariableNetwork *net) {
Martin Christoph Hierholzer
committed
assert(pdata->network == nullptr);
assert(pdata->type != NodeType::invalid);
pdata->network = net;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
void VariableNetworkNode::clearOwner() {
pdata->network = nullptr;
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
bool VariableNetworkNode::hasImplementation() const {
Martin Christoph Hierholzer
committed
return pdata->type == NodeType::Device || pdata->type == NodeType::ControlSystem || pdata->type == NodeType::Constant;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
void VariableNetworkNode::accept( Visitor<VariableNetworkNode>& visitor) const {
visitor.dispatch(*this);
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
bool VariableNetworkNode::operator==(const VariableNetworkNode& other) const {
Martin Christoph Hierholzer
committed
return (other.pdata == pdata) || (pdata->type == NodeType::invalid && other.pdata->type == NodeType::invalid);
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
bool VariableNetworkNode::operator!=(const VariableNetworkNode& other) const {
return !operator==(other);
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
bool VariableNetworkNode::operator<(const VariableNetworkNode& other) const {
if(pdata->type == NodeType::invalid && other.pdata->type == NodeType::invalid) return false;
return (other.pdata < pdata);
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
VariableNetworkNode VariableNetworkNode::operator>>(VariableNetworkNode other) {
Martin Christoph Hierholzer
committed
if(pdata->direction == VariableDirection::invalid) {
if(!other.hasOwner()) {
pdata->direction = VariableDirection::feeding;
}
else {
if(other.getOwner().hasFeedingNode()) {
pdata->direction = VariableDirection::consuming;
if(getType() == NodeType::Device) { // special treatment for Device-type variables: consumers are push-type
pdata->mode = UpdateMode::push;
}
Martin Christoph Hierholzer
committed
}
else {
pdata->direction = VariableDirection::feeding;
}
}
}
if(other.pdata->direction == VariableDirection::invalid) {
if(!hasOwner()) {
other.pdata->direction = VariableDirection::consuming;
if(other.getType() == NodeType::Device) { // special treatment for Device-type variables: consumers are push-type
other.pdata->mode = UpdateMode::push;
}
Martin Christoph Hierholzer
committed
}
else {
if(getOwner().hasFeedingNode()) {
other.pdata->direction = VariableDirection::consuming;
if(other.getType() == NodeType::Device) { // special treatment for Device-type variables: consumers are push-type
other.pdata->mode = UpdateMode::push;
}
Martin Christoph Hierholzer
committed
}
else {
other.pdata->direction = VariableDirection::feeding;
}
}
}
Martin Christoph Hierholzer
committed
Application::getInstance().connect(*this, other);
return *this;
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
VariableNetworkNode VariableNetworkNode::operator[](VariableNetworkNode trigger) {
// check if node already has a trigger
Martin Christoph Hierholzer
committed
if(pdata->externalTrigger.getType() != NodeType::invalid) {
Martin Christoph Hierholzer
committed
throw ChimeraTK::logic_error("Only one external trigger per variable network is allowed.");
Martin Christoph Hierholzer
committed
// force direction of the node we are operating on to be feeding
Martin Christoph Hierholzer
committed
if(pdata->direction == VariableDirection::invalid) pdata->direction = VariableDirection::feeding;
assert(pdata->direction == VariableDirection::feeding);
Martin Christoph Hierholzer
committed
// force direction of the triggering node to be feeding
if(trigger.pdata->direction == VariableDirection::invalid) trigger.pdata->direction = VariableDirection::feeding;
assert(trigger.pdata->direction == VariableDirection::feeding);
Martin Christoph Hierholzer
committed
// check if already existing in map
if(pdata->nodeWithTrigger.count(trigger) > 0) {
return pdata->nodeWithTrigger[trigger];
}
// create copy of the node
pdata->nodeWithTrigger[trigger].pdata = boost::make_shared<VariableNetworkNode_data>(*pdata);
// add ourselves as a trigger receiver to the other network
if(!trigger.hasOwner()) {
Application::getInstance().createNetwork().addNode(trigger);
Martin Christoph Hierholzer
committed
}
trigger.getOwner().addNodeToTrigger(pdata->nodeWithTrigger[trigger]);
// set flag and store pointer to other network
Martin Christoph Hierholzer
committed
pdata->nodeWithTrigger[trigger].pdata->externalTrigger = trigger;
// return the new node
Martin Christoph Hierholzer
committed
return pdata->nodeWithTrigger[trigger];
Martin Christoph Hierholzer
committed
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetworkNode::setValueType(const std::type_info& newType) const {
assert(*pdata->valueType == typeid(AnyType));
pdata->valueType = &newType;
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
void VariableNetworkNode::setDirection(VariableDirection newDirection) const {
assert(pdata->type == NodeType::ControlSystem);
assert(pdata->direction == VariableDirection::feeding);
pdata->direction = newDirection;
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
bool VariableNetworkNode::hasExternalTrigger() const {
return pdata->externalTrigger.getType() != NodeType::invalid;
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
VariableNetworkNode VariableNetworkNode::getExternalTrigger() {
assert(pdata->externalTrigger.getType() != NodeType::invalid);
return pdata->externalTrigger;
}
/*********************************************************************************************************************/
void VariableNetworkNode::dump(std::ostream& stream) const {
VariableNetworkNodeDumpingVisitor visitor(stream, " ");
visitor.dispatch(*this);
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
bool VariableNetworkNode::hasOwner() const {
return pdata->network != nullptr;
}
/*********************************************************************************************************************/
NodeType VariableNetworkNode::getType() const {
if(!pdata) return NodeType::invalid;
return pdata->type;
}
/*********************************************************************************************************************/
UpdateMode VariableNetworkNode::getMode() const {
return pdata->mode;
}
/*********************************************************************************************************************/
VariableDirection VariableNetworkNode::getDirection() const {
return pdata->direction;
}
/*********************************************************************************************************************/
const std::type_info& VariableNetworkNode::getValueType() const {
return *(pdata->valueType);
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
std::string VariableNetworkNode::getName() const {
Martin Christoph Hierholzer
committed
return pdata->name;
Martin Christoph Hierholzer
committed
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
std::string VariableNetworkNode::getQualifiedName() const {
return pdata->qualifiedName;
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
const std::string& VariableNetworkNode::getUnit() const {
return pdata->unit;
}
/*********************************************************************************************************************/
const std::string& VariableNetworkNode::getDescription() const {
return pdata->description;
}
/*********************************************************************************************************************/
VariableNetwork& VariableNetworkNode::getOwner() const {
assert(pdata->network != nullptr);
return *(pdata->network);
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
VariableNetworkNode VariableNetworkNode::getNodeToTrigger() {
assert(pdata->nodeToTrigger.getType() != NodeType::invalid);
return pdata->nodeToTrigger;
Martin Christoph Hierholzer
committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
}
/*********************************************************************************************************************/
const std::string& VariableNetworkNode::getPublicName() const {
assert(pdata->type == NodeType::ControlSystem);
return pdata->publicName;
}
/*********************************************************************************************************************/
const std::string& VariableNetworkNode::getDeviceAlias() const {
assert(pdata->type == NodeType::Device);
return pdata->deviceAlias;
}
/*********************************************************************************************************************/
const std::string& VariableNetworkNode::getRegisterName() const {
assert(pdata->type == NodeType::Device);
return pdata->registerName;
}
/*********************************************************************************************************************/
void VariableNetworkNode::setNumberOfElements(size_t nElements) {
pdata->nElements = nElements;
}
/*********************************************************************************************************************/
size_t VariableNetworkNode::getNumberOfElements() const {
return pdata->nElements;
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
ChimeraTK::TransferElementAbstractor& VariableNetworkNode::getAppAccessorNoType() {
Martin Christoph Hierholzer
committed
return *(pdata->appNode);
}
Martin Christoph Hierholzer
committed
/*********************************************************************************************************************/
void VariableNetworkNode::setMetaData(const std::string &name, const std::string &unit,
const std::string &description) {
Martin Christoph Hierholzer
committed
if(getType() != NodeType::Application) {
Martin Christoph Hierholzer
committed
throw ChimeraTK::logic_error("Calling VariableNetworkNode::updateMetaData() is not allowed for non-application type nodes.");
Martin Christoph Hierholzer
committed
}
pdata->name = name;
pdata->qualifiedName = pdata->owningModule->getQualifiedName()+"/"+name;
Martin Christoph Hierholzer
committed
pdata->unit = unit;
pdata->description = description;
}
/*********************************************************************************************************************/
void VariableNetworkNode::setMetaData(const std::string &name, const std::string &unit,
Martin Christoph Hierholzer
committed
const std::string &description, const std::unordered_set<std::string> &tags) {
setMetaData(name, unit, description);
Martin Christoph Hierholzer
committed
pdata->tags = tags;
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
void VariableNetworkNode::addTag(const std::string &tag) {
pdata->tags.insert(tag);
}
/*********************************************************************************************************************/
Martin Christoph Hierholzer
committed
const std::unordered_set<std::string>& VariableNetworkNode::getTags() const {
return pdata->tags;
}
/*********************************************************************************************************************/
void VariableNetworkNode::setAppAccessorPointer(ChimeraTK::TransferElementAbstractor *accessor) {
assert(getType() == NodeType::Application);
pdata->appNode = accessor;
}
/*********************************************************************************************************************/
EntityOwner* VariableNetworkNode::getOwningModule() const {
return pdata->owningModule;
}
/*********************************************************************************************************************/
void VariableNetworkNode::setOwningModule(EntityOwner *newOwner) const {
pdata->owningModule = newOwner;
}
/*********************************************************************************************************************/
void VariableNetworkNode::setPublicName(const std::string& name) const {
pdata->publicName = name;
}