Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Warn when input/output of mapping is deduced automatically #5238

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions Sofa/framework/Core/src/sofa/core/Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,40 +232,6 @@ class Mapping : public BaseMapping
return BaseMapping::canCreate(obj, context, arg);
}

/// Construction method called by ObjectFactory.
///
/// This implementation read the input and output attributes to
/// find the input and output models of this mapping.
template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
typename T::SPtr obj = sofa::core::objectmodel::New<T>();

if (context)
context->addObject(obj);

if (arg)
{
std::string inPath, outPath;
if (arg->getAttribute("input"))
inPath = arg->getAttribute("input");
else
inPath = "@../";

if (arg->getAttribute("output"))
outPath = arg->getAttribute("output");
else
outPath = "@./";

obj->fromModel.setPath( inPath );
obj->toModel.setPath( outPath );

obj->parse(arg);
}

return obj;
}

template<class T>
static std::string shortName(const T* ptr = nullptr, objectmodel::BaseObjectDescription* arg = nullptr)
{
Expand Down
34 changes: 34 additions & 0 deletions Sofa/framework/Core/src/sofa/core/Mapping.inl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,40 @@ void Mapping<In,Out>::init()
{
Inherit1::init();

const auto setLink = [this]<typename LinkType>(LinkType& link, const char* attributeName, const std::string& defaultPath)
{
if (!link)
{
typename LinkType::DestType* state = nullptr;
this->getContext()->findLinkDest(state, defaultPath, nullptr);

if (state)
{
const std::string linkPath = "@" + state->getPathName();
msg_warning()
<< "The attribute '" << attributeName
<< "' of this component has not been defined. It is automatically set to '"
<< linkPath << "'. It is recommended to define it to avoid any error.";
link.setPath(linkPath);
}
else
{
msg_error()
<< "The attribute '" << attributeName
<< "' of this component has not been defined, and could not be found automatically.";
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
}
}
};

setLink(this->fromModel, "input", "@../");
setLink(this->toModel, "output", "@./");

if (this->d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid)
{
return;
}

if(toModel && !testMechanicalState(toModel.get()))
{
setNonMechanical();
Expand Down
1 change: 0 additions & 1 deletion Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ bool BaseLink::updateLinks()
Base* ptr;
std::string path = getLinkedPath(i);
/// Search for path and if any returns the pointer to the proper object.
/// Search for path and if any returns the pointer to the proper object.
if(!getLinkedBase() && !path.empty())
{
ptr = PathResolver::FindBaseFromClassAndPath(getOwner(), getDestClass(), path);
Expand Down
4 changes: 3 additions & 1 deletion Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ std::string BaseNode::internalGetPathName() const {
// no smarter choice without breaking the "Node" heritage
str = parents[0]->internalGetPathName();
str += '/';
str += getName();
const auto& myName = getName();
assert(!myName.empty());
str += myName;
}
return str;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ using namespace sofa::defaulttype;
NodeElement::NodeElement(const std::string& name, const std::string& type, BaseElement* parent)
: Element<core::objectmodel::BaseNode>(name, type, parent)
{
if (name.empty())
{
const std::string nodeName = "node";
msg_warning("XML") << "Node name cannot be empty. An arbitrary name '" << nodeName << "' has been provided";
attributes["name"] = nodeName;
}
}

NodeElement::~NodeElement()
Expand Down
14 changes: 10 additions & 4 deletions Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace sofa::simulation
using core::objectmodel::BaseNode;
using core::objectmodel::BaseObject;

Node::Node(const std::string& name)
Node::Node()
: core::objectmodel::BaseNode()
, sofa::core::objectmodel::Context()
, child(initLink("child", "Child nodes"))
Expand Down Expand Up @@ -113,15 +113,21 @@ Node::Node(const std::string& name)
, initialized(false)
{
_context = this;
setName(name);
f_printLog.setValue(DEBUG_LINK);
}


Node::~Node()
Node::Node(std::string nodeName) : Node()
{
if (nodeName.empty())
{
nodeName = "node";
msg_warning(GetClass()->className) << "Node name cannot be empty. An arbitrary name '" << nodeName << "' has been provided";
}
setName(nodeName);
}

Node::~Node() = default;

void Node::parse( sofa::core::objectmodel::BaseObjectDescription* arg )
{
Inherit1::parse( arg );
Expand Down
3 changes: 2 additions & 1 deletion Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class SOFA_SIMULATION_CORE_API Node : public sofa::core::objectmodel::BaseNode,

typedef sofa::core::visual::DisplayFlags DisplayFlags;
protected:
Node(const std::string& name="");
Node();
explicit Node(std::string nodeName);

virtual ~Node() override;
public:
Expand Down