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

Adds RangeValueRule bindings. #113

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions maliput-sys/src/api/rules/aliases.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <maliput/api/rules/discrete_value_rule.h>
#include <maliput/api/rules/range_value_rule.h>

#include <memory>
#include <vector>
Expand All @@ -39,6 +40,7 @@ namespace rules {

// Workaround for supporting nested types: https://github.com/dtolnay/cxx/issues/1198
using DiscreteValueRuleDiscreteValue = DiscreteValueRule::DiscreteValue;
using RangeValueRuleRange = RangeValueRule::Range;

} // namespace rules
} // namespace api
Expand Down
13 changes: 13 additions & 0 deletions maliput-sys/src/api/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub mod ffi {
// RoadRulebook bindings definitions.
type RoadRulebook;
fn RoadRulebook_GetDiscreteValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<DiscreteValueRule>;
fn RoadRulebook_GetRangeValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<RangeValueRule>;

// DiscreteValueRule::DiscreteValue bindings definitions.
type DiscreteValueRuleDiscreteValue;
Expand All @@ -180,5 +181,17 @@ pub mod ffi {
fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;

// RangeValueRule::Range bindings definitions.
type RangeValueRuleRange;
fn RangeValueRuleRange_description(range: &RangeValueRuleRange) -> String;
fn RangeValueRuleRange_min(range: &RangeValueRuleRange) -> f64;
fn RangeValueRuleRange_max(range: &RangeValueRuleRange) -> f64;

// RangeValueRule::Range bindings definitions.
type RangeValueRule;
fn RangeValueRule_id(rule: &RangeValueRule) -> String;
fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
}
}
83 changes: 65 additions & 18 deletions maliput-sys/src/api/rules/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@
namespace maliput {
namespace api {
namespace rules {
namespace {

std::unique_ptr<std::vector<RelatedRule>> related_rules_from_state(const Rule::State& rule_state) {
std::vector<RelatedRule> related_rules;
for (const auto& related_rule : rule_state.related_rules) {
rust::Vec<rust::String> rule_ids;
for (const auto& rule_id : related_rule.second) {
rule_ids.push_back({rule_id.string()});
}
related_rules.push_back({related_rule.first, rule_ids});
}
return std::make_unique<std::vector<RelatedRule>>(std::move(related_rules));
}

std::unique_ptr<std::vector<RelatedUniqueId>> related_unique_ids_from_state(const Rule::State& rule_state) {
std::vector<RelatedUniqueId> related_unique_ids;
for (const auto& related_unique_id : rule_state.related_unique_ids) {
rust::Vec<rust::String> unique_ids;
for (const auto& rule_id : related_unique_id.second) {
unique_ids.push_back({rule_id.string()});
}
related_unique_ids.push_back({related_unique_id.first, unique_ids});
}
return std::make_unique<std::vector<RelatedUniqueId>>(std::move(related_unique_ids));
}

} // namespace

std::unique_ptr<std::vector<ConstTrafficLightPtr>> TrafficLightBook_TrafficLights(const TrafficLightBook& traffic_light_book) {
const auto traffic_lights_cpp = traffic_light_book.TrafficLights();
Expand Down Expand Up @@ -199,27 +226,11 @@ std::unique_ptr<DiscreteValueRule> RoadRulebook_GetDiscreteValueRule(const RoadR
}

std::unique_ptr<std::vector<RelatedRule>> DiscreteValueRuleDiscreteValue_related_rules(const DiscreteValueRuleDiscreteValue& discrete_value) {
std::vector<RelatedRule> related_rules;
for (const auto& related_rule : discrete_value.related_rules) {
rust::Vec<rust::String> rule_ids;
for (const auto& rule_id : related_rule.second) {
rule_ids.push_back({rule_id.string()});
}
related_rules.push_back({related_rule.first, rule_ids});
}
return std::make_unique<std::vector<RelatedRule>>(std::move(related_rules));
return related_rules_from_state(discrete_value);
}

std::unique_ptr<std::vector<RelatedUniqueId>> DiscreteValueRuleDiscreteValue_related_unique_ids(const DiscreteValueRuleDiscreteValue& discrete_value) {
std::vector<RelatedUniqueId> related_unique_ids;
for (const auto& related_unique_id : discrete_value.related_unique_ids) {
rust::Vec<rust::String> unique_ids;
for (const auto& rule_id : related_unique_id.second) {
unique_ids.push_back({rule_id.string()});
}
related_unique_ids.push_back({related_unique_id.first, unique_ids});
}
return std::make_unique<std::vector<RelatedUniqueId>>(std::move(related_unique_ids));
return related_unique_ids_from_state(discrete_value);
}

rust::String DiscreteValueRule_id(const DiscreteValueRule& rule) {
Expand All @@ -230,6 +241,42 @@ rust::String DiscreteValueRule_type_id(const DiscreteValueRule& rule) {
return rule.type_id().string();
}

rust::String RangeValueRuleRange_description(const RangeValueRuleRange& range) {
return rust::String(range.description);
}

rust::f64 RangeValueRuleRange_min(const RangeValueRuleRange& range) {
return range.min;
}

rust::f64 RangeValueRuleRange_max(const RangeValueRuleRange& range) {
return range.max;
}

rust::i32 RangeValueRuleRange_severity(const RangeValueRuleRange& range) {
return range.severity;
}

std::unique_ptr<std::vector<RelatedRule>> RangeValueRuleRange_related_rules(const RangeValueRuleRange& range) {
return related_rules_from_state(range);
}

std::unique_ptr<std::vector<RelatedUniqueId>> RangeValueRuleRange_related_unique_ids(const RangeValueRuleRange& range) {
return related_unique_ids_from_state(range);
}

rust::String RangeValueRule_id(const RangeValueRule& rule) {
return rule.id().string();
}

rust::String RangeValueRule_type_id(const RangeValueRule& rule) {
return rule.type_id().string();
}

std::unique_ptr<RangeValueRule> RoadRulebook_GetRangeValueRule(const RoadRulebook& road_rulebook, const rust::String& id) {
return std::make_unique<RangeValueRule>(road_rulebook.GetRangeValueRule(Rule::Id{std::string(id)}));
}

} // namespace rules
} // namespace api
} // namespace maliput
47 changes: 47 additions & 0 deletions maliput/src/api/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ pub struct RoadRulebook<'a> {

impl<'a> RoadRulebook<'a> {
/// Returns the DiscreteValueRule with the specified `id`.
/// ## Arguments
/// * `rule_id` - The id of the rule.
/// ## Return
/// The DiscreteValueRule with the given id.
pub fn get_discrete_value_rule(&self, rule_id: &String) -> DiscreteValueRule {
DiscreteValueRule {
discrete_value_rule: maliput_sys::api::rules::ffi::RoadRulebook_GetDiscreteValueRule(
Expand All @@ -500,6 +504,16 @@ impl<'a> RoadRulebook<'a> {
),
}
}
/// Returns the RangeValueRule with the specified `id`.
/// ## Arguments
/// * `rule_id` - The id of the rule.
/// ## Return
/// The RangeValueRule with the given id.
pub fn get_range_value_rule(&self, rule_id: &String) -> RangeValueRule {
RangeValueRule {
range_value_rule: maliput_sys::api::rules::ffi::RoadRulebook_GetRangeValueRule(self.road_rulebook, rule_id),
}
}
}

/// ## Rule
Expand Down Expand Up @@ -534,3 +548,36 @@ impl DiscreteValueRule {
maliput_sys::api::rules::ffi::DiscreteValueRule_type_id(&self.discrete_value_rule)
}
}

/// ## Rule
///
/// A Rule may have multiple states that affect agent behavior while it is
/// driving through the rule's zone. The possible states of a Rule must be
/// semantically coherent. The current state of a Rule is given by a
/// [RuleStateProvider]. States can be:
///
/// - range based ([RangeValueRule]).
/// - discrete ([DiscreteValueRule]).
///
/// ## RangeValueRule
///
/// [Range]s describe a numeric range based rule.
/// Ranges are closed and continuous, defined by a minimum and maximum quantity.
/// When only one extreme is formally defined, the other should take a
/// semantically correct value. For example, if a speed limit only specifies a
/// maximum value, the minimum value is typically zero.
pub struct RangeValueRule {
range_value_rule: cxx::UniquePtr<maliput_sys::api::rules::ffi::RangeValueRule>,
}

impl RangeValueRule {
/// Returns the Id of the rule as a string.
pub fn id(&self) -> String {
maliput_sys::api::rules::ffi::RangeValueRule_id(&self.range_value_rule)
}
/// Returns the type of the rule as a string.
/// Example: "right-of-way-rule-type-id", "direction-usage-rule-type-id"
pub fn type_id(&self) -> String {
maliput_sys::api::rules::ffi::RangeValueRule_type_id(&self.range_value_rule)
}
}
8 changes: 8 additions & 0 deletions maliput/tests/road_rulebook_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ fn road_rulebook_test_api() {
let road_network = common::create_loop_road_pedestrian_crosswalk_road_network_with_books();

let book = road_network.rulebook();
// Test get_discrete_value_rule method.
let expected_rule_id = String::from("Vehicle-Stop-In-Zone-Behavior Rule Type/WestToEastSouth");
let expected_type_id = String::from("Vehicle-Stop-In-Zone-Behavior Rule Type");
let rule = book.get_discrete_value_rule(&expected_rule_id);
assert_eq!(rule.id(), expected_rule_id);
assert_eq!(rule.type_id(), expected_type_id);

// Test get_range_value_rule method.
let expected_rule_id = String::from("Speed-Limit Rule Type/1_0_1_1");
let expected_type_id = String::from("Speed-Limit Rule Type");
let rule = book.get_range_value_rule(&expected_rule_id);
assert_eq!(rule.id(), expected_rule_id);
assert_eq!(rule.type_id(), expected_type_id);
}