Skip to content

Commit

Permalink
validate agains BASE if TYPE=derived and properly reset log
Browse files Browse the repository at this point in the history
  • Loading branch information
joanvallve committed Jul 4, 2024
1 parent 4effe6c commit 416c229
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/yaml_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ void checkSchema(const YAML::Node& node_schema,
{
throw std::runtime_error("YAML schema: In " + node_field + ", " + TYPE + " should be a string");
}
if (node_schema[TYPE].as<std::string>().find("derived") != std::string::npos)
auto type =
isSequenceSchema(node_schema) ? getTypeOfSequence(node_schema) : node_schema[TYPE].as<std::string>();
// If type=="derived" or "derived[]", 'BASE' of type string is required
if (type == "derived")
{
// If type=="derived" or "derived[]", 'base' of type string is required
if (not node_schema[BASE])
{
throw std::runtime_error("YAML schema: " + node_field + " of derived type does not contain " + BASE);
Expand All @@ -119,6 +121,8 @@ void checkSchema(const YAML::Node& node_schema,
{
throw std::runtime_error("YAML schema: In " + node_field + ", " + BASE + " should be a string");
}
// store type as BASE to check OPTIONS, VALUE & DEFAULT
type = node_schema[BASE].as<std::string>();
}
// Required 'doc' of type string
if (not node_schema[DOC])
Expand Down Expand Up @@ -150,9 +154,7 @@ void checkSchema(const YAML::Node& node_schema,
// OPTIONAL 'value'
if (node_schema[VALUE])
{
// check type
auto type =
isSequenceSchema(node_schema) ? getTypeOfSequence(node_schema) : node_schema[TYPE].as<std::string>();
// CHECK TYPE (take BASE if "derived")
// trivial type
if (isTrivialType(type))
{
Expand Down Expand Up @@ -194,9 +196,7 @@ void checkSchema(const YAML::Node& node_schema,
" value cannot be defined in mandatory field");
}

// check type
auto type =
isSequenceSchema(node_schema) ? getTypeOfSequence(node_schema) : node_schema[TYPE].as<std::string>();
// CHECK TYPE (take BASE if "derived")
// trivial type
if (isTrivialType(type))
{
Expand Down Expand Up @@ -229,9 +229,7 @@ void checkSchema(const YAML::Node& node_schema,
{
throw std::runtime_error("YAML schema: " + node_field + ", " + OPTIONS + " should be a sequence");
}
// check that all items have valid type
auto type =
isSequenceSchema(node_schema) ? getTypeOfSequence(node_schema) : node_schema[TYPE].as<std::string>();
// CHECK TYPE (take BASE if "derived")
for (auto n_i = 0; n_i < node_schema[OPTIONS].size(); n_i++)
{
// trivial type
Expand Down
1 change: 1 addition & 0 deletions src/yaml_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void YamlServer::setYaml(const YAML::Node _node_input)

bool YamlServer::applySchema(const std::string& name_schema)
{
log_.str("");
log_.clear();
std::string header1, header2, header3;

Expand Down
22 changes: 22 additions & 0 deletions test/gtest_apply_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ TEST(schema, nontrivial_options_default_value)
std::cout << "log: \n" << server.getLog() << std::endl;
}

TEST(schema, complex_case)
{
YamlServer server = YamlServer({ROOT_DIR + "/test/schema/folder_schema"}, ROOT_DIR + "/test/yaml/complex_case.yaml");

std::cout << "before: \n" << server.getNode() << std::endl;

server.addFolderSchema(ROOT_DIR + "/test/schema/complex_case");

EXPECT_TRUE(server.applySchema("Problem.schema"));

std::cout << "after Problem.schema: \n" << server.getNode() << std::endl;

std::cout << "log: \n" << server.getLog() << std::endl;

bool is2D = server.getNode()["problem"]["dimension"].as<int>() == 2;
EXPECT_TRUE(server.applySchema(is2D ? "Problem2d.schema" : "Problem3d.schema"));

std::cout << "after Problem3d.schema: \n" << server.getNode() << std::endl;

std::cout << "log: \n" << server.getLog() << std::endl;
}

int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
Expand Down
15 changes: 15 additions & 0 deletions test/schema/complex_case/MapBase.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type:
_mandatory: true
_type: string
_doc: Type of the Map used in the problem.

plugin:
_mandatory: true
_type: string
_doc: Name of the wolf plugin where the map type is implemented.

landmarks:
_mandatory: false
_type: derived[]
_base: LandmarkBase
_doc: Sequence of derived landmarks.
9 changes: 9 additions & 0 deletions test/schema/complex_case/MotionProvider.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
state_provider:
_mandatory: true
_type: bool
_doc: If the processor is used by the problem as provider of state.

state_provider_order:
_mandatory: $state_provider
_type: double
_doc: The order number of this processor when problem gets the state (only used if state_provider = true). Two processors cannot have the same priority (if so, when installing the second is increased).
5 changes: 5 additions & 0 deletions test/schema/complex_case/None.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type:
_mandatory: false
_options: ["None"]
_type: string
_doc: Nothing to say here
29 changes: 29 additions & 0 deletions test/schema/complex_case/Problem.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
problem:
tree_manager:
_type: derived
_base: TreeManagerBase.schema
_mandatory: true
_doc: Tree manager parameters
dimension:
_type: int
_mandatory: true
_options: [2, 3]
_doc: "Dimension of the problem. '2' for 2D or '3' for 3D"
map:
_type: derived
_base: MapBase.schema
_mandatory: false
_default:
type: MapBase
plugin: core
_doc: The map used in the wolf problem.
sensors:
_type: derived[]
_base: SensorBase.schema
_mandatory: true
_doc: A sequence of all the sensors.
processors:
_type: derived[]
_base: ProcessorBaseWithSensor.schema
_mandatory: true
_doc: A sequence of all the processors.
20 changes: 20 additions & 0 deletions test/schema/complex_case/Problem3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
follow: Problem.schema
problem:
first_frame:
P:
_mandatory: false
_type: StateP3d
_doc: "specification for the first frame position state"
O:
_mandatory: false
_type: StateO3d
_doc: "specification for the first frame orientation state"
V:
_mandatory: false
_type: StateV3d
_doc: "specification for the first frame linear velocity state"
dimension:
_type: int
_mandatory: true
_options: [3]
_doc: "Dimension of the problem: '3' for 3D"
22 changes: 22 additions & 0 deletions test/schema/complex_case/ProcessorBase.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
follow: TypeAndPlugin.schema

name:
_mandatory: true
_type: string
_doc: The processor's name. It has to be unique.

time_tolerance:
_mandatory: true
_type: double
_doc: Maximum time difference between a Keyframe time stamp and a particular Capture of this processor to allow assigning this Capture to the Keyframe. [s].

keyframe_vote:
voting_active:
_mandatory: true
_type: bool
_doc: If the processor is allowed to decide to create a frame.

apply_loss_function:
_mandatory: true
_type: bool
_doc: If the factors created by the processor have loss function.
6 changes: 6 additions & 0 deletions test/schema/complex_case/ProcessorBaseWithSensor.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
follow: ProcessorBase.schema

sensor_name:
_mandatory: true
_type: string
_doc: The name of the sensor corresponding to this processor.
23 changes: 23 additions & 0 deletions test/schema/complex_case/ProcessorMotion.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
follow: ProcessorBase.schema
follow: MotionProvider.schema
keyframe_vote:
max_time_span:
_mandatory: $voting_active
_type: double
_doc: Time threshold to create a new frame [s].
max_buff_length:
_mandatory: $voting_active
_type: unsigned int
_doc: Maximum size of the buffer of motions.
max_dist_traveled:
_mandatory: $voting_active
_type: double
_doc: Distance traveled threshold to create a new frame [m].
max_angle_turned:
_mandatory: $voting_active
_type: double
_doc: Angle turned threshold to create a new frame [rad].
unmeasured_perturbation_std:
_mandatory: true
_type: double
_doc: Noise (standard deviation) of the integrated movement in the not observed directions.
1 change: 1 addition & 0 deletions test/schema/complex_case/ProcessorOdom3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
follow: ProcessorMotion.schema
6 changes: 6 additions & 0 deletions test/schema/complex_case/SensorBase.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
follow: TypeAndPlugin.schema

name:
_mandatory: true
_type: string
_doc: The sensor's name. It has to be unique.
32 changes: 32 additions & 0 deletions test/schema/complex_case/SensorOdom3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
follow: SensorBase.schema

k_disp_to_disp:
_mandatory: true
_type: double
_doc: ratio of displacement variance to displacement, for odometry noise calculation.

k_disp_to_rot:
_mandatory: true
_type: double
_doc: ratio of displacement variance to rotation, for odometry noise calculation.

k_rot_to_rot:
_mandatory: true
_type: double
_doc: ratio of rotation variance to rotation, for odometry noise calculation.

min_disp_var:
_mandatory: true
_type: double
_doc: minimum displacement variance, for odometry noise calculation.

min_rot_var:
_mandatory: true
_type: double
_doc: minimum rotation variance, for odometry noise calculation.

states:
P:
follow: StateSensorP3d.schema
O:
follow: StateSensorO3d.schema
22 changes: 22 additions & 0 deletions test/schema/complex_case/StateO3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type:
_type: string
_mandatory: false
_value: StateQuaternion
_doc: "The type of the state for orientation in 3D: StateQuaternion. Not required to be filled by the user"

value:
_type: Vector4d
_mandatory: true
_doc: A vector containing the quaternion values (x, y, z, w)

prior:
mode:
_type: string
_mandatory: true
_options: ["fix", "factor", "initial_guess"]
_doc: Can be 'factor' to add an absolute factor (with covariance defined in 'factor_std'), 'fix' to set the values constant or 'initial_guess' to just set the values.

factor_std:
_type: Vector3d
_mandatory: $mode == 'factor'
_doc: A vector containing the stdev values of the noise of the factor, i.e. the sqrt of the diagonal elements of the covariance matrix [rad].
22 changes: 22 additions & 0 deletions test/schema/complex_case/StateP3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type:
_type: string
_mandatory: false
_value: StatePoint3d
_doc: "The type of the state for position in 3D: StatePoint3d. Not required to be filled by the user"

value:
_type: Vector3d
_mandatory: true
_doc: A vector containing the position (x, y, z) [m].

prior:
mode:
_type: string
_mandatory: true
_options: ["fix", "factor", "initial_guess"]
_doc: Can be 'factor' to add an absolute factor (with covariance defined in 'factor_std'), 'fix' to set the values constant or 'initial_guess' to just set the values.

factor_std:
_type: Vector3d
_mandatory: $mode == 'factor'
_doc: A vector containing the stdev values of the noise of the factor, i.e. the sqrt of the diagonal elements of the covariance matrix [m].
26 changes: 26 additions & 0 deletions test/schema/complex_case/StateSensorO3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
dynamic:
_type: bool
_mandatory: true
_doc: If the orientation is dynamic, i.e. it changes along time.

value:
_type: Vector4d
_mandatory: true
_doc: A vector containing the quaternion values (x, y, z, w)

prior:
mode:
_type: string
_mandatory: true
_options: ["fix", "factor", "initial_guess"]
_doc: Can be 'factor' to add an absolute factor (with covariance defined in 'factor_std'), 'fix' to set the values constant or 'initial_guess' to just set the values.

factor_std:
_type: Vector3d
_mandatory: $mode == 'factor'
_doc: A vector containing the stdev values of the noise of the factor, i.e. the sqrt of the diagonal elements of the covariance matrix [rad].

drift_std:
_type: Vector3d
_mandatory: false
_doc: A vector containing the stdev values of the noise of the drift factor per second (only if dynamic==true), i.e. the sqrt of the diagonal elements of the covariance matrix [rad/sqrt(s)].
26 changes: 26 additions & 0 deletions test/schema/complex_case/StateSensorP3d.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
dynamic:
_type: bool
_mandatory: true
_doc: If the position is dynamic, i.e. it changes along time.

value:
_type: Vector3d
_mandatory: true
_doc: A vector containing the position (x, y, z) [m].

prior:
mode:
_type: string
_mandatory: true
_options: ["fix", "factor", "initial_guess"]
_doc: Can be 'factor' to add an absolute factor (with covariance defined in 'factor_std'), 'fix' to set the values constant or 'initial_guess' to just set the values.

factor_std:
_type: Vector3d
_mandatory: $mode == 'factor'
_doc: A vector containing the stdev values of the noise of the factor, i.e. the sqrt of the diagonal elements of the covariance matrix [m].

drift_std:
_type: Vector3d
_mandatory: false
_doc: A vector containing the stdev values of the noise of the drift factor per second (only if dynamic==true) i.e. the sqrt of the diagonal elements of the covariance matrix [m/sqrt(s)].
Loading

0 comments on commit 416c229

Please sign in to comment.