Files
ros2-assignments/doc/nodes/FinalGradeDeterminator.md

4.1 KiB

FinalGradeDeterminator (assignments::one::final_grade_determinator)

Overview

The FinalGradeDeterminator node collects exam results for student-course combinations, triggers grade calculation when enough results are gathered, and stores final grades in the database. It interacts with ROS2 publishers, subscribers, and service clients to manage the grading workflow.

Implementation Details

Parameters

  • grade_collection_amount (int, default: 5): Number of exam results required before triggering grade calculation for a student-course combination.

Constructor

FinalGradeDeterminator()
  • Initializes ROS2 node with name final_grade_determinator
  • Declares and retrieves grade_collection_amount parameter
  • Sets up DatabaseManager
  • Creates publisher for student course management
  • Subscribes to exam results topic
  • Initializes service client for grade calculation

Core Functions

void exam_results_callback(const g2_2025_interfaces::msg::Exam::SharedPtr msg)

  • Updates internal map with received exam result for student-course combo
  • Checks if enough results have been collected
  • Triggers grade calculation request when threshold is met

void grade_calculator_request(StudentCourse combo)

  • Waits for grade calculator service to be available
  • Sends async request with collected exam grades for the student-course combination
  • Uses callback to handle service response

void grade_calculator_response(rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future, StudentCourse studentCourseCombo)

  • Verifies database connection
  • Publishes final student message to ROS2 topic
  • Logs final grade information
  • Stores final course result in database

Unit Tests

Unit tests for FinalGradeDeterminator are implemented in src/g2_2025_grade_calculator_pkg/test/FinalGradeDeterminator.test.cpp using Google Test and ROS2 test utilities. The tests use a mock database manager and a mock grade calculator service to simulate the node's interactions.

Test Cases

1. ConstructorTest

Description: Verifies that the node can be constructed without errors.

  • Input: Construct a FinalGradeDeterminator node.
  • Expected Output: No exceptions are thrown. The node is created successfully.

2. ExamCollectionTest

Description: Sends the required number of exam results and checks that a grade calculation request is triggered, a student message is published, and the results are stored.

  • Input:
    • Student: "Test Student"
    • Course: "Test Course"
    • Grades published: [80, 85, 90, 75, 95] (5 exam results, which is the default required amount)
  • Expected Output:
    • A grade calculation service request is made with the correct student, course, and grades.
    • A student message is published with the correct student and course.
    • The final grade is calculated as the average: (80 + 85 + 90 + 75 + 95) / 5 = 85.

3. PartialExamCollectionTest

Description: Sends fewer than the required number of exam results and checks that no grade calculation or student message occurs.

  • Input:
    • Student: "Test Student"
    • Course: "Test Course"
    • Grades published: [80, 85, 90] (only 3 exam results, less than required)
  • Expected Output:
    • No grade calculation service request is made.
    • No student message is published.

4. MultipleStudentsTest

Description: Simulates multiple students submitting exam results and verifies that each student triggers independent grade calculation and messaging.

  • Input:
    • Students: "Alice", "Bob"
    • Course: "Test Course"
    • Each student receives grades: [80, 85, 90, 75, 95] (5 exam results per student)
  • Expected Output:
    • Two grade calculation service requests are made, one for each student, with the correct grades.
    • Two student messages are published, one for each student.
    • The final grade for each student is calculated as the average: (80 + 85 + 90 + 75 + 95) / 5 = 85.

These tests ensure that the node correctly collects exam results, triggers grade calculation at the right time, publishes the appropriate messages, and interacts with the database as expected.