Skip to content

Commit 3b14693

Browse files
committed
WIP: Support VersionInformation in ScriptDreader DataVariant
1 parent a00ede6 commit 3b14693

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

include/ur_client_library/control/script_reader.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,7 @@ namespace control
4444
class ScriptReader
4545
{
4646
public:
47-
struct RobotInfo
48-
{
49-
VersionInformation version_info;
50-
RobotType robot_type;
51-
};
52-
53-
using DataVariant = std::variant<std::string, double, int, bool>;
47+
using DataVariant = std::variant<std::string, double, int, bool, VersionInformation>;
5448
using DataDict = std::unordered_map<std::string, DataVariant>;
5549

5650
ScriptReader() = default;

include/ur_client_library/ur/version_information.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class VersionInformation
5454
*/
5555
static VersionInformation fromString(const std::string& str);
5656

57+
/*!
58+
* \brief Generates a string representation of the version information such as "5.12.0.1101319"
59+
*/
60+
std::string toString() const;
61+
5762
bool isESeries() const;
5863

5964
friend bool operator==(const VersionInformation& v1, const VersionInformation& v2);
@@ -77,4 +82,4 @@ class VersionInformation
7782
std::vector<std::string> splitString(std::string input, const std::string& delimiter = ".");
7883
} // namespace urcl
7984

80-
#endif // ifndef UR_CLIENT_LIBRARY_UR_VERSION_INFORMATION_H_INCLUDED
85+
#endif // ifndef UR_CLIENT_LIBRARY_UR_VERSION_INFORMATION_H_INCLUDED

src/control/script_reader.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ bool operator<(const ScriptReader::DataVariant& lhs, const ScriptReader::DataVar
8686
return std::get<int>(lhs) < std::get<int>(rhs);
8787
}
8888
}
89+
if (std::holds_alternative<VersionInformation>(lhs))
90+
{
91+
return std::get<VersionInformation>(lhs) < std::get<VersionInformation>(rhs);
92+
}
8993
throw std::invalid_argument("The comparison operator is only allowed for numeric values.");
9094
}
9195

@@ -141,6 +145,10 @@ bool operator==(const ScriptReader::DataVariant& lhs, const ScriptReader::DataVa
141145
{
142146
return std::get<bool>(lhs) == std::get<bool>(rhs);
143147
}
148+
if (std::holds_alternative<VersionInformation>(lhs))
149+
{
150+
return std::get<VersionInformation>(lhs) == std::get<VersionInformation>(rhs);
151+
}
144152
throw std::runtime_error("Unknown variant type passed to equality check. Please contact the developers.");
145153
}
146154

@@ -224,6 +232,10 @@ void ScriptReader::replaceVariables(std::string& script_code, const DataDict& da
224232
{
225233
std::get<bool>(data.at(key)) ? replaced_value = "True" : replaced_value = "False";
226234
}
235+
else if (std::holds_alternative<VersionInformation>(data.at(key)))
236+
{
237+
replaced_value = std::get<VersionInformation>(data.at(key)).toString();
238+
}
227239
else
228240
{
229241
// This is more of a reminder if we add types to the variant and forget to add it here.
@@ -341,6 +353,7 @@ bool ScriptReader::checkCondition(const std::string& condition, const DataDict&
341353
std::regex string_pattern(R"(^['"]([^'"]+)?['"]$)");
342354
std::regex number_pattern(R"(^-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$)");
343355
std::regex boolean_pattern(R"(^(true|false|yes|no|on|off)$)", std::regex::icase);
356+
std::regex version_pattern(R"(^v(\d+\.\d+(\.\d+)?(-\d+)?)$)");
344357
if (std::regex_search(value_str, match, string_pattern))
345358
{
346359
value = match[1]; // Extract the string content without quotes
@@ -353,6 +366,10 @@ bool ScriptReader::checkCondition(const std::string& condition, const DataDict&
353366
{
354367
value = parseBoolean(value_str);
355368
}
369+
else if (std::regex_search(value_str, match, version_pattern))
370+
{
371+
value = VersionInformation::fromString(match[1]);
372+
}
356373
else if (data.count(value_str))
357374
{
358375
value = data.at(value_str);

src/ur/version_information.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ VersionInformation VersionInformation::fromString(const std::string& str)
8484

8585
return info;
8686
}
87+
std::string VersionInformation::toString() const
88+
{
89+
return std::to_string(this->major) + "." + std::to_string(this->minor) + "." + std::to_string(this->bugfix) + "-" +
90+
std::to_string(this->build);
91+
}
8792

8893
bool VersionInformation::isESeries() const
8994
{
@@ -144,4 +149,4 @@ bool operator>=(const VersionInformation& v1, const VersionInformation& v2)
144149
{
145150
return !(v1 < v2);
146151
}
147-
} // namespace urcl
152+
} // namespace urcl

tests/test_script_reader.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ TEST_F(ScriptReaderTest, DataVariantOperators)
353353
data["str2"] = "bar";
354354
data["bool1"] = true;
355355
data["bool2"] = false;
356+
data["version1"] = urcl::VersionInformation::fromString("10.7.0");
357+
data["version2"] = urcl::VersionInformation::fromString("5.22.1");
356358

357359
// Equality
358360
EXPECT_TRUE(data["int1"] == 5);
@@ -368,12 +370,16 @@ TEST_F(ScriptReaderTest, DataVariantOperators)
368370
EXPECT_FALSE(data["bool1"] == 0.0);
369371
EXPECT_FALSE(data["bool1"] == 3.14);
370372
EXPECT_FALSE(data["bool1"] == 42);
373+
EXPECT_TRUE(data["version1"] == urcl::VersionInformation::fromString("10.7.0"));
374+
EXPECT_FALSE(data["version2"] == urcl::VersionInformation::fromString("10.7.0"));
375+
EXPECT_FALSE(data["version2"] == data["version1"]);
371376

372377
// Inequality
373378
EXPECT_TRUE(data["int1"] != 6);
374379
EXPECT_FALSE(data["int1"] != 5);
375380
EXPECT_TRUE(data["str1"] != std::string("bar"));
376381
EXPECT_FALSE(data["str1"] != std::string("foo"));
382+
EXPECT_TRUE(data["version1"] != urcl::VersionInformation::fromString("1.2.3"));
377383

378384
// Less, Greater, etc. (numeric only)
379385
EXPECT_TRUE(data["int1"] < data["int2"]);
@@ -383,6 +389,10 @@ TEST_F(ScriptReaderTest, DataVariantOperators)
383389
EXPECT_TRUE(data["double1"] <= data["double2"]);
384390
EXPECT_TRUE(data["double1"] >= data["double2"]);
385391
EXPECT_FALSE(data["int1"] < data["double1"]);
392+
EXPECT_TRUE(data["version1"] > data["version2"]);
393+
EXPECT_TRUE(data["version1"] >= data["version1"]);
394+
EXPECT_TRUE(data["version1"] <= data["version1"]);
395+
EXPECT_TRUE(data["version2"] < data["version1"]);
386396

387397
// Invalid comparisons (should throw)
388398
EXPECT_THROW((void)(data["str1"] < data["str2"]), std::invalid_argument);

0 commit comments

Comments
 (0)