Skip to content

Commit

Permalink
handle enums conversions is assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Mar 7, 2024
1 parent ec2e696 commit 6337feb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
14 changes: 9 additions & 5 deletions include/behaviortree_cpp/scripting/operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <memory>
#include <string>
#include <vector>
#include <utility>

#include "behaviortree_cpp/scripting/any_types.hpp"
#include "behaviortree_cpp/scripting/script_parser.hpp"
Expand Down Expand Up @@ -555,11 +554,16 @@ struct ExprAssignment : ExprBase
{
*dst_ptr = converter(str);
}
else
else if(dst_ptr->isNumber())
{
auto msg = StrCat(errorPrefix(), "\nThe right operand is a string but"
"can't find the corresponding "
"convertFromString<T>().");
auto num_value = StringToDouble(value, env);
*dst_ptr = Any(num_value);
}
else {
auto msg = StrCat(errorPrefix(),
"\nThe right operand is a string, "
"can't convert to ",
demangle(dst_ptr->type()));
throw RuntimeError(msg);
}
}
Expand Down
55 changes: 55 additions & 0 deletions tests/gtest_postconditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,59 @@ TEST(PostConditions, Issue601)
ASSERT_EQ(tree.rootBlackboard()->get<std::string>("test"), "halted");
}

enum BatteryStatus
{
BATTERY_OK,
LOW_BATTERY
};

class BatteryCheck : public BT::SyncActionNode
{
public:
BatteryCheck(const std::string& name, const BT::NodeConfig& config) :
BT::SyncActionNode(name, config){}

static BT::PortsList providedPorts() {
return { InputPort<int>("health")};
}

BT::NodeStatus tick() override {
int health = getInput<int>("health").value();
return health > 10 ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
}
};

TEST(PostConditions, OnFailure)
{
const std::string xml_text = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="Main">
<Sequence>
<Script code="battery_voltage := 7"/>
<Script code="battery_status := BATTERY_OK"/>
<SubTree ID="Sub" _autoremap="true"
fault_status="LOW_BATTERY"
health="{battery_voltage}" />
</Sequence>
</BehaviorTree>
<BehaviorTree ID="Sub">
<BatteryCheck health="{health}"
_onFailure="battery_status := fault_status" />
</BehaviorTree>
</root>)";

BehaviorTreeFactory factory;
factory.registerNodeType<BatteryCheck>("BatteryCheck");
factory.registerScriptingEnums<BatteryStatus>();

factory.registerBehaviorTreeFromText(xml_text);

auto tree = factory.createTree("Main");
const auto status = tree.tickWhileRunning();

ASSERT_EQ(status, NodeStatus::FAILURE);
ASSERT_EQ(tree.rootBlackboard()->get<BatteryStatus>("battery_status"), LOW_BATTERY);
}


1 change: 0 additions & 1 deletion tests/gtest_preconditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ class SimpleOutput : public BT::SyncActionNode
setOutput("output", true);
return BT::NodeStatus::SUCCESS;
}

};

TEST(Preconditions, Remapping)
Expand Down
33 changes: 32 additions & 1 deletion tests/script_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,38 @@ TEST(ParserTest, NotInitializedComparison)
EXPECT_ANY_THROW(GetResult("x += 1"));
}

TEST(ParserTest, Enums)
TEST(ParserTest, EnumsBasic)
{
BT::Ast::Environment environment = {BT::Blackboard::create(), {}};

auto GetResult = [&environment](const char* text) -> BT::Any {
return GetScriptResult(environment, text);
};

enum Color
{
RED = 1,
BLUE = 3,
GREEN = 5
};

environment.enums = std::make_shared<BT::EnumsTable>();
environment.enums->insert( {"RED", RED} );
environment.enums->insert( {"BLUE", BLUE} );
environment.enums->insert( {"GREEN", GREEN} );
GetResult("A:=RED");
GetResult("B:=RED");
GetResult("C:=BLUE");

EXPECT_EQ(GetResult("A==B").cast<int>(), 1);
EXPECT_EQ(GetResult("A!=C").cast<int>(), 1);

EXPECT_EQ(GetResult("A").cast<Color>(), RED);
EXPECT_EQ(GetResult("B").cast<Color>(), RED);
EXPECT_EQ(GetResult("C").cast<Color>(), BLUE);
}

TEST(ParserTest, EnumsXML)
{
BT::BehaviorTreeFactory factory;

Expand Down

0 comments on commit 6337feb

Please sign in to comment.