Skip to content

Commit

Permalink
Add task composer taskflow unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Armstrong committed Jun 24, 2023
1 parent 5a1bcf3 commit eedbca1
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 183 deletions.
1 change: 0 additions & 1 deletion tesseract_task_composer/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ add_library(
${PROJECT_NAME}
src/task_composer_data_storage.cpp
src/task_composer_executor.cpp
src/task_composer_future.cpp
src/task_composer_graph.cpp
src/task_composer_input.cpp
src/task_composer_node.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,11 @@ class TaskComposerExecutor

/**
* @brief Execute the provided node
* @details It will call one of the pure virtual methods below based on the node type
* @param node The node to execute
* @param task_input The task input provided to every task
* @return The future associated with execution
*/
virtual TaskComposerFuture::UPtr run(const TaskComposerNode& node, TaskComposerInput& task_input);

/**
* @brief Execute the provided task graph
* @param task_graph The task graph to execute
* @param task_input The task input provided to every task
* @return The future associated with execution
*/
virtual TaskComposerFuture::UPtr run(const TaskComposerGraph& task_graph, TaskComposerInput& task_input) = 0;

/**
* @brief Execute the provided task pipeline
* @param task_pipeline The task pipeline to execute
* @param task_input The task input provided to every task
* @return The future associated with execution
*/
virtual TaskComposerFuture::UPtr run(const TaskComposerPipeline& task_pipeline, TaskComposerInput& task_input) = 0;

/**
* @brief Execute the provided task
* @param task_graph The task to execute
* @param task_input The task input provided to task
* @return The future associated with execution
*/
virtual TaskComposerFuture::UPtr run(const TaskComposerTask& task, TaskComposerInput& task_input) = 0;
virtual TaskComposerFuture::UPtr run(const TaskComposerNode& node, TaskComposerInput& task_input) = 0;

/** @brief Queries the number of workers (example: number of threads) */
virtual long getWorkerCount() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#include <memory>
TESSERACT_COMMON_IGNORE_WARNINGS_POP

#include <tesseract_common/serialization.h>

namespace tesseract_planning
{
/**
Expand Down Expand Up @@ -87,18 +85,7 @@ class TaskComposerFuture
* @return A copy, for example to allow access from multiple thread
*/
virtual TaskComposerFuture::UPtr copy() const = 0;

bool operator==(const TaskComposerFuture& rhs) const;
bool operator!=(const TaskComposerFuture& rhs) const;

private:
friend struct tesseract_common::Serialization;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version); // NOLINT
};
} // namespace tesseract_planning

#include <boost/serialization/export.hpp>
BOOST_CLASS_EXPORT_KEY2(tesseract_planning::TaskComposerFuture, "TaskComposerFuture")
#endif // TESSERACT_TASK_COMPOSER_TASK_COMPOSER_FUTURE_H
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace tesseract_planning
{
enum class TaskComposerNodeType
{
NODE,
TASK,
PIPELINE,
GRAPH
Expand All @@ -55,7 +56,7 @@ class TaskComposerNode
using ConstUPtr = std::unique_ptr<const TaskComposerNode>;

TaskComposerNode(std::string name = "TaskComposerNode",
TaskComposerNodeType type = TaskComposerNodeType::TASK,
TaskComposerNodeType type = TaskComposerNodeType::NODE,
bool conditional = false);
explicit TaskComposerNode(std::string name, TaskComposerNodeType type, const YAML::Node& config);
virtual ~TaskComposerNode() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <fstream>
TESSERACT_COMMON_IGNORE_WARNINGS_POP

Expand Down
14 changes: 0 additions & 14 deletions tesseract_task_composer/core/src/task_composer_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ TaskComposerExecutor::TaskComposerExecutor(std::string name) : name_(std::move(n

const std::string& TaskComposerExecutor::getName() const { return name_; }

TaskComposerFuture::UPtr TaskComposerExecutor::run(const TaskComposerNode& node, TaskComposerInput& task_input)
{
if (node.getType() == TaskComposerNodeType::TASK)
return run(static_cast<const TaskComposerTask&>(node), task_input);

if (node.getType() == TaskComposerNodeType::PIPELINE)
return run(static_cast<const TaskComposerPipeline&>(node), task_input);

if (node.getType() == TaskComposerNodeType::GRAPH)
return run(static_cast<const TaskComposerGraph&>(node), task_input);

throw std::runtime_error("TaskComposerExecutor, unsupported node type!");
}

bool TaskComposerExecutor::operator==(const TaskComposerExecutor& rhs) const { return (name_ == rhs.name_); }

bool TaskComposerExecutor::operator!=(const TaskComposerExecutor& rhs) const { return !operator==(rhs); }
Expand Down
46 changes: 0 additions & 46 deletions tesseract_task_composer/core/src/task_composer_future.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ class TaskflowTaskComposerExecutor : public TaskComposerExecutor
TaskflowTaskComposerExecutor(TaskflowTaskComposerExecutor&&) = delete;
TaskflowTaskComposerExecutor& operator=(TaskflowTaskComposerExecutor&&) = delete;

TaskComposerFuture::UPtr run(const TaskComposerGraph& task_graph, TaskComposerInput& task_input) override final;

TaskComposerFuture::UPtr run(const TaskComposerPipeline& task_pipeline, TaskComposerInput& task_input) override final;

TaskComposerFuture::UPtr run(const TaskComposerTask& task, TaskComposerInput& task_input) override final;
TaskComposerFuture::UPtr run(const TaskComposerNode& node, TaskComposerInput& task_input) override final;

long getWorkerCount() const override final;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#ifndef TESSERACT_TASK_COMPOSER_TASKFLOW_TASK_COMPOSER_FUTURE_H
#define TESSERACT_TASK_COMPOSER_TASKFLOW_TASK_COMPOSER_FUTURE_H

#include <tesseract_common/macros.h>
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#include <memory>
#include <vector>
TESSERACT_COMMON_IGNORE_WARNINGS_POP

#include <tesseract_task_composer/core/task_composer_future.h>

namespace tf
Expand Down Expand Up @@ -63,15 +69,7 @@ class TaskflowTaskComposerFuture : public TaskComposerFuture

TaskComposerFuture::UPtr copy() const override final;

bool operator==(const TaskflowTaskComposerFuture& rhs) const;
bool operator!=(const TaskflowTaskComposerFuture& rhs) const;

private:
friend struct tesseract_common::Serialization;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version); // NOLINT

/** @brief This is the future return from taskflow executor.run */
std::shared_future<void> future_;

Expand All @@ -80,7 +78,4 @@ class TaskflowTaskComposerFuture : public TaskComposerFuture
};
} // namespace tesseract_planning

#include <boost/serialization/export.hpp>
#include <boost/serialization/tracking.hpp>
BOOST_CLASS_EXPORT_KEY2(tesseract_planning::TaskflowTaskComposerFuture, "TaskflowTaskComposerFuture")
#endif // TESSERACT_TASK_COMPOSER_TASKFLOW_TASK_COMPOSER_FUTURE_H
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,24 @@ TaskflowTaskComposerExecutor::TaskflowTaskComposerExecutor(std::string name, con

TaskflowTaskComposerExecutor::~TaskflowTaskComposerExecutor() = default;

TaskComposerFuture::UPtr TaskflowTaskComposerExecutor::run(const TaskComposerGraph& task_graph,
TaskComposerInput& task_input)
TaskComposerFuture::UPtr TaskflowTaskComposerExecutor::run(const TaskComposerNode& node, TaskComposerInput& task_input)
{
auto taskflow = convertToTaskflow(task_graph, task_input, *this);
std::shared_future<void> f = executor_->run(*(taskflow->front()));

// std::ofstream out_data;
// out_data.open(tesseract_common::getTempPath() + "task_composer_example.dot");
// taskflow.top->dump(out_data); // dump the graph including dynamic tasks
// out_data.close();

return std::make_unique<TaskflowTaskComposerFuture>(f, std::move(taskflow));
}

TaskComposerFuture::UPtr TaskflowTaskComposerExecutor::run(const TaskComposerPipeline& task_pipeline,
TaskComposerInput& task_input)
{
auto taskflow = convertToTaskflow(task_pipeline, task_input, *this);
std::shared_future<void> f = executor_->run(*(taskflow->front()));
std::shared_ptr<std::vector<std::unique_ptr<tf::Taskflow>>> taskflow;
if (node.getType() == TaskComposerNodeType::TASK)
taskflow = convertToTaskflow(static_cast<const TaskComposerTask&>(node), task_input, *this);
else if (node.getType() == TaskComposerNodeType::PIPELINE)
taskflow = convertToTaskflow(static_cast<const TaskComposerPipeline&>(node), task_input, *this);
else if (node.getType() == TaskComposerNodeType::GRAPH)
taskflow = convertToTaskflow(static_cast<const TaskComposerGraph&>(node), task_input, *this);
else
throw std::runtime_error("TaskComposerExecutor, unsupported node type!");

// std::ofstream out_data;
// out_data.open(tesseract_common::getTempPath() + "task_composer_example.dot");
// taskflow.top->dump(out_data); // dump the graph including dynamic tasks
// out_data.close();

return std::make_unique<TaskflowTaskComposerFuture>(f, std::move(taskflow));
}

TaskComposerFuture::UPtr TaskflowTaskComposerExecutor::run(const TaskComposerTask& task, TaskComposerInput& task_input)
{
auto taskflow = convertToTaskflow(task, task_input, *this);
std::shared_future<void> f = executor_->run(*(taskflow->front()));

// std::ofstream out_data;
// out_data.open(tesseract_common::getTempPath() + "task_composer_example.dot");
// taskflow.top->dump(out_data); // dump the graph including dynamic tasks
// out_data.close();

return std::make_unique<TaskflowTaskComposerFuture>(f, std::move(taskflow));
}

Expand All @@ -117,7 +97,6 @@ bool TaskflowTaskComposerExecutor::operator==(const TaskflowTaskComposerExecutor
{
bool equal = true;
equal &= (num_threads_ == rhs.num_threads_);
equal &= (executor_ == rhs.executor_);
equal &= TaskComposerExecutor::operator==(rhs);
return equal;
}
Expand Down Expand Up @@ -176,7 +155,7 @@ TaskflowTaskComposerExecutor::convertToTaskflow(const TaskComposerGraph& task_gr
->emplace([task, &task_input, &task_executor] { task->run(task_input, task_executor); })
.name(pair.second->getName());
}
if (pair.second->getType() == TaskComposerNodeType::PIPELINE)
else if (pair.second->getType() == TaskComposerNodeType::PIPELINE)
{
auto pipeline = std::static_pointer_cast<const TaskComposerPipeline>(pair.second);
if (edges.size() > 1 && pipeline->isConditional())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@
* limitations under the License.
*/

#include <tesseract_common/macros.h>
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#include <boost/serialization/nvp.hpp>
#if (BOOST_VERSION >= 107400) && (BOOST_VERSION < 107500)
#include <boost/serialization/library_version_type.hpp>
#endif
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <boost/serialization/shared_ptr.hpp>
TESSERACT_COMMON_IGNORE_WARNINGS_POP

#include <tesseract_task_composer/taskflow/taskflow_task_composer_future.h>

namespace tesseract_planning
Expand Down Expand Up @@ -83,25 +71,4 @@ TaskComposerFuture::UPtr TaskflowTaskComposerFuture::copy() const
return clone;
}

bool TaskflowTaskComposerFuture::operator==(const tesseract_planning::TaskflowTaskComposerFuture& rhs) const
{
bool equal = true;
equal &= future_.valid() == rhs.future_.valid();
return equal;
}

bool TaskflowTaskComposerFuture::operator!=(const tesseract_planning::TaskflowTaskComposerFuture& rhs) const
{
return !operator==(rhs);
}

template <class Archive>
void TaskflowTaskComposerFuture::serialize(Archive& ar, const unsigned int /*version*/)
{
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(TaskComposerFuture);
}

} // namespace tesseract_planning
#include <tesseract_common/serialization.h>
TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(tesseract_planning::TaskflowTaskComposerFuture)
BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_planning::TaskflowTaskComposerFuture)
24 changes: 24 additions & 0 deletions tesseract_task_composer/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,27 @@ target_code_coverage(
add_gtest_discover_tests(${PROJECT_NAME}_core_unit)
add_dependencies(run_tests ${PROJECT_NAME}_core_unit)
add_dependencies(${PROJECT_NAME}_core_unit ${PROJECT_NAME})

# Taskflow Tests
add_executable(${PROJECT_NAME}_taskflow_unit ${PROJECT_NAME}_taskflow_unit.cpp)
target_link_libraries(
${PROJECT_NAME}_taskflow_unit
PRIVATE GTest::GTest
GTest::Main
${PROJECT_NAME}
${PROJECT_NAME}_nodes
${PROJECT_NAME}_taskflow)
target_include_directories(${PROJECT_NAME}_taskflow_unit PUBLIC "$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/examples>")
target_compile_options(${PROJECT_NAME}_taskflow_unit PRIVATE ${TESSERACT_COMPILE_OPTIONS_PRIVATE}
${TESSERACT_COMPILE_OPTIONS_PUBLIC})
target_clang_tidy(${PROJECT_NAME}_taskflow_unit ENABLE ${TESSERACT_ENABLE_CLANG_TIDY})
target_cxx_version(${PROJECT_NAME}_taskflow_unit PRIVATE VERSION ${TESSERACT_CXX_VERSION})
target_code_coverage(
${PROJECT_NAME}_taskflow_unit
PRIVATE
ALL
EXCLUDE ${COVERAGE_EXCLUDE}
ENABLE ${TESSERACT_ENABLE_CODE_COVERAGE})
add_gtest_discover_tests(${PROJECT_NAME}_taskflow_unit)
add_dependencies(run_tests ${PROJECT_NAME}_taskflow_unit)
add_dependencies(${PROJECT_NAME}_taskflow_unit ${PROJECT_NAME})
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ TEST(TesseractTaskComposerCoreUnit, TaskComposerNodeTests) // NOLINT
auto node = std::make_unique<TaskComposerNode>();
// Default
EXPECT_EQ(node->getName(), "TaskComposerNode");
EXPECT_EQ(node->getType(), TaskComposerNodeType::TASK);
EXPECT_EQ(node->getType(), TaskComposerNodeType::NODE);
EXPECT_FALSE(node->getUUID().is_nil());
EXPECT_FALSE(node->getUUIDString().empty());
EXPECT_TRUE(node->getParentUUID().is_nil());
Expand Down
Loading

0 comments on commit eedbca1

Please sign in to comment.