Skip to content

Commit

Permalink
Merge pull request #2608 from drdanz/robotinterface_param
Browse files Browse the repository at this point in the history
robotinterface: Allow replacing values from command line
  • Loading branch information
drdanz authored Jun 15, 2021
2 parents 1d72297 + a24fa00 commit 547e85c
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 50 deletions.
27 changes: 27 additions & 0 deletions doc/release/master/robotinterface_param.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
robotinterface_param {#master}
--------------------

## Libraries

### `robotinterface`

#### `XMLReader`

* The `getRobotFromFile` and `getRobotFromString` now accept a `Searchable` that
is used to replace params with the attribute `extern-name`.
The signatures are now:
```
XMLReaderResult getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& config = yarp::os::Property());
XMLReaderResult getRobotFromString(const std::string& filename,
const yarp::os::Searchable& config = yarp::os::Property());
```

* DTD version is now 3.1.

## Tools

### `yarprobotinterface`

* Arguments taken from the command line are now handled and used to replace
parameters marked with the attribute `extern-name`.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ void yarp::robotinterface::experimental::XMLReader::setEnableDeprecated(bool ena
mPriv->enable_deprecated = enab;
}

yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::experimental::XMLReader::getRobotFromFile(const std::string& fileName)
yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::experimental::XMLReader::getRobotFromFile(const std::string& fileName,
const yarp::os::Searchable& config)
{
std::string filename = fileName;
#if defined(_WIN32)
Expand Down Expand Up @@ -121,23 +122,26 @@ yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::experi
if (mPriv->enable_deprecated) {
yWarning() << "yarprobotinterface: using DEPRECATED xml parser for DTD v1.x";
mPriv->mReader = new yarp::robotinterface::impl::XMLReaderFileV1;
return mPriv->mReader->getRobotFromFile(filename, mPriv->verbose);
} else {
yError("Invalid DTD version, execution stopped.");
return yarp::robotinterface::experimental::XMLReaderResult::ParsingFailed();
return mPriv->mReader->getRobotFromFile(filename, config, mPriv->verbose);
}
} else if (dtd.majorVersion == 3) {

yError("Invalid DTD version, execution stopped.");
return yarp::robotinterface::experimental::XMLReaderResult::ParsingFailed();

}
if (dtd.majorVersion == 3) {
yDebug() << "yarprobotinterface: using xml parser for DTD v3.x";
mPriv->mReader = new yarp::robotinterface::impl::XMLReaderFileV3;
return mPriv->mReader->getRobotFromFile(filename, mPriv->verbose);
return mPriv->mReader->getRobotFromFile(filename, config, mPriv->verbose);
}

//ERROR HERE
yError("Invalid DTD version. Unable to choose parser for DTD.major: %d", dtd.majorVersion);
return yarp::robotinterface::experimental::XMLReaderResult::ParsingFailed();
}

yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::experimental::XMLReader::getRobotFromString(const std::string& xmlString)
yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::experimental::XMLReader::getRobotFromString(const std::string& xmlString,
const yarp::os::Searchable& config)
{
std::string curr_filename = " XML runtime string ";
std::unique_ptr<TiXmlDocument> doc = std::make_unique<TiXmlDocument>();
Expand Down Expand Up @@ -177,15 +181,15 @@ yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::experi
if (mPriv->enable_deprecated) {
yWarning() << "yarprobotinterface: using DEPRECATED xml parser for DTD v1.x";
mPriv->mReader = new yarp::robotinterface::impl::XMLReaderFileV1;
return mPriv->mReader->getRobotFromString(xmlString, mPriv->verbose);
return mPriv->mReader->getRobotFromString(xmlString, config, mPriv->verbose);
} else {
yError("Invalid DTD version, execution stopped.");
return yarp::robotinterface::experimental::XMLReaderResult::ParsingFailed();
}
} else if (dtd.majorVersion == 3) {
yDebug() << "yarprobotinterface: using xml parser for DTD v3.x";
mPriv->mReader = new yarp::robotinterface::impl::XMLReaderFileV3;
return mPriv->mReader->getRobotFromString(xmlString, mPriv->verbose);
return mPriv->mReader->getRobotFromString(xmlString, config, mPriv->verbose);
}

//ERROR HERE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <yarp/robotinterface/api.h>
#include <yarp/robotinterface/experimental/Robot.h>

#include <yarp/os/Searchable.h>
#include <yarp/os/Property.h>

#include <string>


Expand Down Expand Up @@ -55,15 +58,18 @@ class YARP_robotinterface_API XMLReader
* \param filename path to the XML file to load.
* \return result of parsing.
*/
XMLReaderResult getRobotFromFile(const std::string& filename);
XMLReaderResult getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& config = yarp::os::Property());

/**
* Parse the XML description of a robotinterface from a string.
*
* \param xmlString string containing the XML code to parse.
* \return result of parsing.
*/
XMLReaderResult getRobotFromString(const std::string& xmlString);
XMLReaderResult getRobotFromString(const std::string& filename,
const yarp::os::Searchable& config = yarp::os::Property());

void setVerbose(bool verbose);
void setEnableDeprecated(bool enab);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,17 @@ yarp::robotinterface::experimental::ActionList yarp::robotinterface::impl::XMLRe
return actions;
}

yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV1::getRobotFromFile(const std::string& filename, bool verb)
yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV1::getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& /*config*/,
bool verb)
{
mPriv->verbose_output = verb;
return mPriv->readRobotFromFile(filename);
}

yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV1::getRobotFromString(const std::string& xmlString, bool verb)
yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV1::getRobotFromString(const std::string& xmlString,
const yarp::os::Searchable& /*config*/,
bool verb)
{
mPriv->verbose_output = verb;
return mPriv->readRobotFromString(xmlString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ class XMLReaderFileV1 : public XMLReaderFileVx
XMLReaderFileV1();
~XMLReaderFileV1() override;

yarp::robotinterface::experimental::XMLReaderResult getRobotFromFile(const std::string& filename, bool verbose = false) override;
yarp::robotinterface::experimental::XMLReaderResult getRobotFromString(const std::string& xmlString, bool verbose = false) override;
yarp::robotinterface::experimental::XMLReaderResult getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& config,
bool verbose = false) override;
yarp::robotinterface::experimental::XMLReaderResult getRobotFromString(const std::string& xmlString,
const yarp::os::Searchable& config,
bool verbose = false) override;

private:
class Private;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class yarp::robotinterface::impl::XMLReaderFileV3::Private
#endif

bool verbose_output;
const yarp::os::Searchable* config;
std::string curr_filename;
unsigned int minorVersion;
unsigned int majorVersion;
Expand Down Expand Up @@ -415,7 +416,14 @@ yarp::robotinterface::experimental::Param yarp::robotinterface::impl::XMLReaderF
result.parsingIsSuccessful = false;
return yarp::robotinterface::experimental::Param();
}
param.value() = valueText;

std::string extern_name;
if (paramElem->QueryStringAttribute("extern-name", &extern_name) == TIXML_SUCCESS && config && config->check(extern_name)) {
// FIXME Check DTD >= 3.1
param.value() = config->find(extern_name).asString();
} else {
param.value() = valueText;
}

// yDebug() << param;
return param;
Expand Down Expand Up @@ -700,16 +708,26 @@ yarp::robotinterface::experimental::ActionList yarp::robotinterface::impl::XMLRe
}


yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV3::getRobotFromFile(const std::string& filename, bool verb)
yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV3::getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& config,
bool verb)
{
mPriv->config = &config;
mPriv->verbose_output = verb;
return mPriv->readRobotFromFile(filename);
auto ret = mPriv->readRobotFromFile(filename);
mPriv->config = nullptr;
return ret;
}

yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV3::getRobotFromString(const std::string& xmlString, bool verb)
yarp::robotinterface::experimental::XMLReaderResult yarp::robotinterface::impl::XMLReaderFileV3::getRobotFromString(const std::string& xmlString,
const yarp::os::Searchable& config,
bool verb)
{
mPriv->config = &config;
mPriv->verbose_output = verb;
return mPriv->readRobotFromString(xmlString);
auto ret = mPriv->readRobotFromString(xmlString);
mPriv->config = nullptr;
return ret;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ class XMLReaderFileV3 : public XMLReaderFileVx
XMLReaderFileV3();
~XMLReaderFileV3() override;

yarp::robotinterface::experimental::XMLReaderResult getRobotFromFile(const std::string& filename, bool verbose = false) override;
yarp::robotinterface::experimental::XMLReaderResult getRobotFromString(const std::string& xmlString, bool verbose = false) override;
yarp::robotinterface::experimental::XMLReaderResult getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& config,
bool verbose = false) override;
yarp::robotinterface::experimental::XMLReaderResult getRobotFromString(const std::string& xmlString,
const yarp::os::Searchable& config,
bool verbose = false) override;

private:
class Private;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <yarp/robotinterface/experimental/Robot.h>

#include <yarp/os/Searchable.h>

#include <string>

namespace yarp {
Expand All @@ -27,8 +29,12 @@ class XMLReaderFileVx
public:
bool verbose;
virtual ~XMLReaderFileVx(){};
virtual yarp::robotinterface::experimental::XMLReaderResult getRobotFromFile(const std::string& filename, bool verbose = false) = 0;
virtual yarp::robotinterface::experimental::XMLReaderResult getRobotFromString(const std::string& xmlString, bool verbose = false) = 0;
virtual yarp::robotinterface::experimental::XMLReaderResult getRobotFromFile(const std::string& filename,
const yarp::os::Searchable& config,
bool verbose = false) = 0;
virtual yarp::robotinterface::experimental::XMLReaderResult getRobotFromString(const std::string& xmlString,
const yarp::os::Searchable& config,
bool verbose = false) = 0;
};

} // namespace impl
Expand Down
9 changes: 8 additions & 1 deletion src/yarprobotinterface/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ bool yarprobotinterface::Module::configure(yarp::os::ResourceFinder& rf)
reader.setVerbose(verbosity);
reader.setEnableDeprecated(deprecated);

yarp::robotinterface::experimental::XMLReaderResult result = reader.getRobotFromFile(filename);
// Prepare configuration for sub-devices
yarp::os::Property config;
config.fromString(rf.toString());
// The --config option is consumed by yarprobotinterface, and never
// forwarded to the devices)
config.unput("config");

yarp::robotinterface::experimental::XMLReaderResult result = reader.getRobotFromFile(filename, config);

if (!result.parsingIsSuccessful) {
yFatal() << "Config file " << filename << " not parsed correctly.";
Expand Down
36 changes: 36 additions & 0 deletions src/yarprobotinterface/tests/fakeFrameGrabber/fakeFrameGrabber.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE devices PUBLIC "-//YARP//DTD yarprobotinterface 3.1//EN" "http://www.yarp.it/DTD/yarprobotinterfaceV3.1.dtd">

<devices robot="grabber_left" build="3">
<device name="grabber_left" type="fakeFrameGrabber">
<param name="fakeFrameGrabber_rpc_port"> /fakeFrameGrabber_left/rpc </param>
<param name="width">360</param>
<param name="height">240</param>
<param name="mode" extern-name="mode">grid</param>
</device>

<device name="grabber_right" type="fakeFrameGrabber">
<param name="fakeFrameGrabber_rpc_port"> /fakeFrameGrabber_right/rpc </param>
<param name="width">360</param>
<param name="height">240</param>
<param name="mode" extern-name="mode">grid</param>
</device>

<device name="wrapper_left" type="grabberDual"> <!-- frameGrabber_nws_yarp -->
<param name="name">/left</param>
<param name="period">33</param>
<action phase="startup" level="10" type="attach">
<param name="device"> grabber_left </param>
</action>
<action phase="shutdown" level="5" type="detach" />
</device>

<device name="wrapper_right" type="grabberDual">--> <!-- frameGrabber_nws_yarp -->
<param name="name">/right</param>
<param name="period">33</param>
<action phase="startup" level="10" type="attach">
<param name="device"> grabber_right </param>
</action>
<action phase="shutdown" level="5" type="detach" />
</device>
</devices>
6 changes: 3 additions & 3 deletions src/yarprobotinterface/tests/fakeFrameGrabber/grabber.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE robot PUBLIC "-//YARP//DTD yarprobotinterface 1.0//EN" "http://www.yarp.it/DTD/yarprobotinterfaceV1.0.dtd">
<!DOCTYPE robot PUBLIC "-//YARP//DTD yarprobotinterface 3.1//EN" "http://www.yarp.it/DTD/yarprobotinterfaceV3.1.dtd">

<robot name="grabber" build="2" portprefix="grabber">
<devices file="fakeFrameGrabber.xml" />
<robot name="grabber" build="3" portprefix="grabber" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="./fakeFrameGrabber.xml" />
</robot>
21 changes: 0 additions & 21 deletions src/yarprobotinterface/tests/fakeFrameGrabber/test_grabber.xml

This file was deleted.

Loading

0 comments on commit 547e85c

Please sign in to comment.