Skip to content

Commit a0eeb32

Browse files
feat: Initialize RESTART database with cognitive domain, test, exercise, and user types
1 parent c7339a3 commit a0eeb32

File tree

5 files changed

+117
-59
lines changed

5 files changed

+117
-59
lines changed

src/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,43 @@ int main()
1616
coco::mongo_db db;
1717
coco::coco cc(db);
1818
auto &rst = cc.add_module<restart::restart>(cc);
19+
cc.init();
20+
21+
try
22+
{
23+
[[maybe_unused]] auto &usr = cc.get_type("CognitiveDomain");
24+
}
25+
catch (const std::exception &e)
26+
{
27+
LOG_WARN("Initializing RESTART database");
28+
29+
// Create the types for RESTART
30+
[[maybe_unused]] auto &cd_type = cc.create_type("CognitiveDomain", {}, json::json{{"name", {"type", "string"}}}, json::json());
31+
[[maybe_unused]] auto &ct_type = cc.create_type("CognitiveTest", {}, json::json{{"name", {"type", "string"}}, {"domain", {{"type", "item"}, {"domain", "CognitiveDomain"}}}}, json::json());
32+
[[maybe_unused]] auto &ce_type = cc.create_type("CognitiveExercise", {}, json::json{{"name", {"type", "string"}}, {"duration", {{"type", "int"}, {"min", 0}, {"max", 60}}}, {"domain", {{"type", "item"}, {"domain", "CognitiveTest"}}}}, json::json());
33+
[[maybe_unused]] auto &usr_type = cc.create_type("User", {}, json::json{{"name", {"type", "string"}}}, json::json());
34+
[[maybe_unused]] auto &t_done_type = cc.create_type("TestDone", {}, json::json{{"user", {{"type", "item"}, {"domain", "User"}}}, {"test", {{"type", "item"}, {"domain", "CognitiveTest"}}}, {"score", {{"type", "int"}, {"min", 0}, {"max", 6}}}}, json::json());
35+
[[maybe_unused]] auto &ex_done_type = cc.create_type("ExerciseDone", {}, json::json{{"user", {{"type", "item"}, {"domain", "User"}}}, {"exercise", {{"type", "item"}, {"domain", "CognitiveExercise"}}}, {"done", {{"type", "int"}, {"min", 0}, {"default", 0}}}, {"level", {{"type", "int"}, {"min", 0}, {"max", 6}}}, {"score", {{"type", "float"}, {"min", 0}, {"max", 1}}}}, json::json());
36+
[[maybe_unused]] auto &robot_type = cc.create_type("Robot", {}, json::json{{"name", {"type", "string"}}}, json::json{{"current_command", {{"type", "symbol"}, {"values", {"welcome", "rot", "training", "goodbye"}}}}, {"current_modality", {{"type", "symbol"}, {"values", std::vector<json::json>{"formal", "informal"}}}}, {"command_completed", {{"type", "symbol"}, {"values", {"welcome", "rot", "training", "goodbye"}}}}, {"current_user", {{"type", "item"}, {"domain", "User"}}}, {"current_exercise", {{"type", "item"}, {"domain", "CognitiveExercise"}}}, {"current_level", {{"type", "int"}, {"min", 0}, {"max", 10}}}, {"current_score", {{"type", "float"}, {"min", 0}, {"max", 1}}}});
37+
// Create the initial robot item
38+
[[maybe_unused]] auto &robot = cc.create_item(robot_type, json::json{{"name", "RESTART"}});
39+
40+
cc.create_reactive_rule("robot_session", restart::start_session_rule);
41+
cc.create_reactive_rule("robot_rot", restart::start_rot_rule);
42+
{
43+
std::ifstream file("src/start_training.clp");
44+
std::stringstream buffer;
45+
buffer << file.rdbuf();
46+
cc.create_reactive_rule("start_training", buffer.str());
47+
}
48+
{
49+
std::ifstream file("src/exercise_done.clp");
50+
std::stringstream buffer;
51+
buffer << file.rdbuf();
52+
cc.create_reactive_rule("exercise_done", buffer.str());
53+
}
54+
cc.create_reactive_rule("robot_end_session", restart::end_session_rule);
55+
}
1956

2057
coco::coco_server srv(cc);
2158
srv.add_module<coco::server_noauth>(srv);

src/restart.cpp

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,6 @@ namespace restart
1919
LOG_TRACE(enqueue_exercise_deffunction);
2020
[[maybe_unused]] auto build_enqueue_exercise_df_err = Build(get_env(), enqueue_exercise_deffunction);
2121
assert(build_enqueue_exercise_df_err == BE_NO_ERROR);
22-
23-
try
24-
{
25-
[[maybe_unused]] auto &usr = cc.get_type("CognitiveDomain");
26-
}
27-
catch (const std::exception &e)
28-
{
29-
LOG_WARN("Initializing RESTART database");
30-
31-
[[maybe_unused]] auto &cd_type = cc.create_type("CognitiveDomain", {}, json::json{{"name", {"type", "string"}}}, json::json());
32-
// auto &memory_cd = cc.create_item(cd_type, json::json{{"name", "Memory"}});
33-
// auto &attention_cd = cc.create_item(cd_type, json::json{{"name", "Attention"}});
34-
// auto &executive_functions_cd = cc.create_item(cd_type, json::json{{"name", "ExecutiveFunctions"}});
35-
// auto &general_cd = cc.create_item(cd_type, json::json{{"name", "General"}});
36-
37-
[[maybe_unused]] auto &ct_type = cc.create_type("CognitiveTest", {}, json::json{{"name", {"type", "string"}}, {"domain", {{"type", "item"}, {"domain", "CognitiveDomain"}}}}, json::json());
38-
// [[maybe_unused]] auto &moca_ct = cc.create_item(ct_type, json::json{{"name", "MoCA"}, {"domain", general_cd.get_id()}});
39-
// [[maybe_unused]] auto &attention_matrices_ct = cc.create_item(ct_type, json::json{{"name", "AttentionMatrices"}, {"domain", attention_cd.get_id()}});
40-
// [[maybe_unused]] auto &trial_making_test_a_ct = cc.create_item(ct_type, json::json{{"name", "TrialMakingTestA"}, {"domain", executive_functions_cd.get_id()}});
41-
// [[maybe_unused]] auto &trial_making_test_b_ct = cc.create_item(ct_type, json::json{{"name", "TrialMakingTestB"}, {"domain", executive_functions_cd.get_id()}});
42-
// [[maybe_unused]] auto &trial_making_test_b_a_ct = cc.create_item(ct_type, json::json{{"name", "TrialMakingTestBA"}, {"domain", executive_functions_cd.get_id()}});
43-
// [[maybe_unused]] auto &semantic_fluency_ct = cc.create_item(ct_type, json::json{{"name", "SemanticFluency"}, {"domain", executive_functions_cd.get_id()}});
44-
// [[maybe_unused]] auto &phonological_fluency_ct = cc.create_item(ct_type, json::json{{"name", "PhonologicalFluency"}, {"domain", executive_functions_cd.get_id()}});
45-
// [[maybe_unused]] auto &modified_wisconsin_card_sorting_test_ct = cc.create_item(ct_type, json::json{{"name", "ModifiedWisconsinCardSortingTest"}, {"domain", executive_functions_cd.get_id()}});
46-
// [[maybe_unused]] auto &short_story_ct = cc.create_item(ct_type, json::json{{"name", "ShortStory"}, {"domain", memory_cd.get_id()}});
47-
48-
[[maybe_unused]] auto &ce_type = cc.create_type("CognitiveExercise", {}, json::json{{"name", {"type", "string"}}, {"duration", {{"type", "int"}, {"min", 0}, {"max", 60}}}, {"domain", {{"type", "item"}, {"domain", "CognitiveTest"}}}}, json::json());
49-
// [[maybe_unused]] auto &visual_memory_ce = cc.create_item(ce_type, json::json{{"name", "VisualMemory"}, {"duration", 5}, {"domain", memory_cd.get_id()}});
50-
// [[maybe_unused]] auto &attention_ce = cc.create_item(ce_type, json::json{{"name", "Attention"}, {"duration", 5}, {"domain", attention_cd.get_id()}});
51-
// [[maybe_unused]] auto &executive_functions_ce = cc.create_item(ce_type, json::json{{"name", "ExecutiveFunctions"}, {"duration", 5}, {"domain", executive_functions_cd.get_id()}});
52-
53-
[[maybe_unused]] auto &usr_type = cc.create_type("User", {}, json::json{{"name", {"type", "string"}}}, json::json());
54-
[[maybe_unused]] auto &t_done_type = cc.create_type("TestDone", {}, json::json{{"user", {{"type", "item"}, {"domain", "User"}}}, {"test", {{"type", "item"}, {"domain", "CognitiveTest"}}}, {"score", {{"type", "int"}, {"min", 0}, {"max", 6}}}}, json::json());
55-
[[maybe_unused]] auto &ex_done_type = cc.create_type("ExerciseDone", {}, json::json{{"user", {{"type", "item"}, {"domain", "User"}}}, {"exercise", {{"type", "item"}, {"domain", "CognitiveExercise"}}}, {"done", {{"type", "int"}, {"min", 0}, {"default", 0}}}, {"level", {{"type", "int"}, {"min", 0}, {"max", 6}}}, {"score", {{"type", "float"}, {"min", 0}, {"max", 1}}}}, json::json());
56-
57-
auto &robot_type = cc.create_type("Robot", {}, json::json{{"name", {"type", "string"}}}, json::json{{"current_command", {{"type", "symbol"}, {"values", {"welcome", "rot", "training", "goodbye"}}}}, {"current_modality", {{"type", "symbol"}, {"values", std::vector<json::json>{"formal", "informal"}}}}, {"command_completed", {{"type", "symbol"}, {"values", {"welcome", "rot", "training", "goodbye"}}}}, {"current_user", {{"type", "item"}, {"domain", "User"}}}, {"current_exercise", {{"type", "item"}, {"domain", "CognitiveExercise"}}}, {"current_level", {{"type", "int"}, {"min", 0}, {"max", 10}}}, {"current_score", {{"type", "float"}, {"min", 0}, {"max", 1}}}});
58-
[[maybe_unused]] auto &robot = cc.create_item(robot_type, json::json{{"name", "RESTART"}});
59-
60-
cc.create_reactive_rule("robot_session", start_session_rule);
61-
cc.create_reactive_rule("robot_rot", start_rot_rule);
62-
{
63-
std::ifstream file("src/start_training.clp");
64-
std::stringstream buffer;
65-
buffer << file.rdbuf();
66-
cc.create_reactive_rule("start_training", buffer.str());
67-
}
68-
{
69-
std::ifstream file("src/exercise_done.clp");
70-
std::stringstream buffer;
71-
buffer << file.rdbuf();
72-
cc.create_reactive_rule("exercise_done", buffer.str());
73-
}
74-
cc.create_reactive_rule("robot_end_session", end_session_rule);
75-
76-
// create_user("TestUser", json::json{{"MoCA", 2}, {"AttentionMatrices", 1}, {"TrialMakingTestA", 4}, {"TrialMakingTestB", 3}, {"TrialMakingTestBA", 0}, {"SemanticFluency", 2}, {"PhonologicalFluency", 4}, {"ModifiedWisconsinCardSortingTest", 1}, {"ShortStory", 3}});
77-
}
7822
}
7923

8024
std::vector<domain> restart::get_domains() noexcept

src/restart_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ namespace restart
121121
std::unique_ptr<network::response> restart_server::new_user(const network::request &req)
122122
{
123123
auto &body = static_cast<const network::json_request &>(req).get_body();
124-
if (!body.is_object() || !body.contains("name") || !body["name"].is_string())
124+
if (!body.is_object() || !body.contains("name") || !body["name"].is_string() || !body.contains("tests") || !body["tests"].is_object())
125125
return std::make_unique<network::json_response>(json::json({{"message", "Invalid request"}}), network::status_code::bad_request);
126126

127127
std::string name = body["name"];
128128
json::json tests;
129-
for (const auto &[test_name, test_score] : body.as_object())
129+
for (const auto &[test_name, test_score] : body["tests"].as_object())
130130
if (test_name != "name")
131131
tests[test_name] = test_score.get<int>();
132132
try

tests/test.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import sys
2+
import requests
3+
import logging
4+
5+
logger = logging.getLogger(__name__)
6+
logger.setLevel(logging.DEBUG)
7+
8+
handler = logging.StreamHandler(sys.stdout)
9+
handler.setLevel(logging.DEBUG)
10+
logger.addHandler(handler)
11+
12+
13+
def create_items(session: requests.Session, url: str) -> list[str]:
14+
# Create cognitive domain items
15+
cognitive_domains = ['Memory', 'Attention', 'ExecutiveFunction', 'General']
16+
cognitive_domain_ids = []
17+
for domain in cognitive_domains:
18+
response = session.post(
19+
url + '/cognitive_domains', json={'name': domain})
20+
if response.status_code != 201:
21+
logger.error(f'Failed to create CognitiveDomain item for {domain}')
22+
return
23+
cognitive_domain_ids.append(response.text)
24+
25+
# Create cognitive test items
26+
cognitive_tests = [
27+
{'name': 'MoCa', 'domain': 'General'},
28+
{'name': 'Matrici_Attentive', 'domain': 'Attention'},
29+
{'name': 'Trial_Making_Test_A', 'domain': 'ExecutiveFunction'},
30+
{'name': 'Trial_Making_Test_B', 'domain': 'ExecutiveFunction'},
31+
{'name': 'Trial_Making_Test_B_A', 'domain': 'ExecutiveFunction'},
32+
{'name': 'Fluenza_semantica', 'domain': 'ExecutiveFunction'},
33+
{'name': 'Fluenza_fonologica', 'domain': 'ExecutiveFunction'},
34+
{'name': 'Modified_Winsconsin_Card_Sorting_Test',
35+
'domain': 'ExecutiveFunction'},
36+
{'name': 'Breve_racconto', 'domain': 'Memory'}
37+
]
38+
cognitive_test_ids = []
39+
for test in cognitive_tests:
40+
response = session.post(
41+
url + '/cognitive_tests', json=test)
42+
if response.status_code != 201:
43+
logger.error(
44+
f'Failed to create CognitiveTest item for {test["name"]}')
45+
return
46+
cognitive_test_ids.append(response.text)
47+
48+
# Create exercise type items
49+
exercise_types = [
50+
{'name': 'memoria visiva', 'duration': 5, 'domain': 'Memory'},
51+
{'name': 'Att_Attenzione divisa', 'duration': 5, 'domain': 'Attention'},
52+
{'name': 'fluenza verbale', 'duration': 5, 'domain': 'ExecutiveFunction'}
53+
]
54+
exercise_type_ids = []
55+
for exercise in exercise_types:
56+
response = session.post(
57+
url + '/cognitive_exercises', json=exercise)
58+
if response.status_code != 201:
59+
logger.error(
60+
f'Failed to create ExerciseType item for {exercise["name"]}')
61+
return
62+
exercise_type_ids.append(response.text)
63+
64+
# Create user items
65+
response = session.post(
66+
url + '/users', json={'name': 'User1', 'tests': {'MoCa': 1, 'Matrici_Attentive': 1, 'Trial_Making_Test_A': 1, 'Trial_Making_Test_B': 1, 'Trial_Making_Test_B_A': 1, 'Fluenza_semantica': 1, 'Fluenza_fonologica': 1, 'Modified_Winsconsin_Card_Sorting_Test': 1, 'Breve_racconto': 1}})
67+
if response.status_code != 201:
68+
logger.error('Failed to create User item')
69+
return
70+
user_id = response.text
71+
72+
73+
if __name__ == '__main__':
74+
url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080'
75+
session = requests.Session()
76+
77+
create_items(session, url)

0 commit comments

Comments
 (0)