11#include " restart.hpp"
22#include " coco.hpp"
3- #include " coco_item .hpp"
3+ #include " coco_type .hpp"
44#include " logging.hpp"
55#include < fstream>
66#include < sstream>
@@ -88,8 +88,7 @@ namespace restart
8888 std::string restart::create_domain (std::string_view name, bool infere)
8989 {
9090 std::lock_guard<std::recursive_mutex> lock (get_mtx ());
91- auto &cd_type = get_coco ().get_type (" CognitiveDomain" );
92- auto &cd = get_coco ().create_item (cd_type, json::json{{" name" , name.data ()}});
91+ auto &cd = get_coco ().create_item (get_coco ().get_type (" CognitiveDomain" ), json::json{{" name" , name.data ()}});
9392
9493 if (infere)
9594 Run (get_env (), -1 );
@@ -155,20 +154,13 @@ namespace restart
155154 std::lock_guard<std::recursive_mutex> lock (get_mtx ());
156155 std::vector<test> tests;
157156 for (auto &ct : get_coco ().get_items (get_coco ().get_type (" CognitiveTest" )))
158- tests.emplace_back (ct.get ().get_id (), ct.get ().get_properties ()[" name" ].get <std::string>(), ct.get ().get_properties ()[" domain" ].get <std::string>());
157+ tests.emplace_back (ct.get ().get_id (), ct.get ().get_properties ()[" name" ].get <std::string>(), get_coco (). get_item ( ct.get ().get_properties ()[" domain" ].get <std::string>()). get_id ());
159158 return tests;
160159 }
161160 std::string restart::create_test (std::string_view name, std::string_view domain, bool infere)
162161 {
163162 std::lock_guard<std::recursive_mutex> lock (get_mtx ());
164- auto &ct_type = get_coco ().get_type (" CognitiveTest" );
165- std::string query = " (find-fact ((?cd CognitiveDomain)) (eq ?cd:name \" " + std::string (domain) + " \" ))" ;
166- CLIPSValue cd_fact;
167- Eval (get_env (), query.c_str (), &cd_fact);
168- if (cd_fact.value == nullptr )
169- throw std::invalid_argument (" Cognitive domain not found: " + std::string (domain));
170- auto &cd_itm = get_coco ().get_item (cd_fact.lexemeValue ->contents );
171- auto &test = get_coco ().create_item (ct_type, json::json{{" name" , name.data ()}, {" domain" , cd_itm.get_id ()}});
163+ auto &test = get_coco ().create_item (get_coco ().get_type (" CognitiveTest" ), json::json{{" name" , name.data ()}, {" domain" , get_domain (domain).get_id ()}});
172164
173165 if (infere)
174166 Run (get_env (), -1 );
@@ -181,27 +173,50 @@ namespace restart
181173 std::lock_guard<std::recursive_mutex> lock (get_mtx ());
182174 std::vector<exercise> exercises;
183175 for (auto &ce : get_coco ().get_items (get_coco ().get_type (" CognitiveExercise" )))
184- exercises.emplace_back (ce.get ().get_id (), ce.get ().get_properties ()[" name" ].get <std::string>(), ce.get ().get_properties ()[" domain" ].get <std::string>(), ce.get ().get_properties ()[" duration" ].get <int64_t >());
176+ exercises.emplace_back (ce.get ().get_id (), ce.get ().get_properties ()[" name" ].get <std::string>(), get_coco (). get_item ( ce.get ().get_properties ()[" domain" ].get <std::string>()). get_id (), ce.get ().get_properties ()[" duration" ].get <int64_t >());
185177 return exercises;
186178 }
187179 std::string restart::create_exercise (std::string_view name, std::string_view domain, int duration, bool infere)
188180 {
189181 std::lock_guard<std::recursive_mutex> lock (get_mtx ());
190- auto &ce_type = get_coco ().get_type (" CognitiveExercise" );
191- std::string query = " (find-fact ((?cd CognitiveDomain)) (eq ?cd:name \" " + std::string (domain) + " \" ))" ;
192- CLIPSValue cd_fact;
193- Eval (get_env (), query.c_str (), &cd_fact);
194- if (cd_fact.value == nullptr )
195- throw std::invalid_argument (" Cognitive domain not found: " + std::string (domain));
196- auto &cd_itm = get_coco ().get_item (cd_fact.lexemeValue ->contents );
197- auto &exercise = get_coco ().create_item (ce_type, json::json{{" name" , name.data ()}, {" domain" , cd_itm.get_id ()}, {" duration" , duration}});
182+ auto &exercise = get_coco ().create_item (get_coco ().get_type (" CognitiveExercise" ), json::json{{" name" , name.data ()}, {" domain" , get_domain (domain).get_id ()}, {" duration" , duration}});
198183
199184 if (infere)
200185 Run (get_env (), -1 );
201186
202187 return exercise.get_id ();
203188 }
204189
190+ coco::item &restart::get_domain (std::string_view domain)
191+ {
192+ std::string query = " (find-fact ((?cd CognitiveDomain_name)) (eq ?cd:name \" " + std::string (domain) + " \" ))" ;
193+ CLIPSValue cd_fact;
194+ Eval (get_env (), query.c_str (), &cd_fact);
195+ Fact *f;
196+ switch (cd_fact.header ->type )
197+ {
198+ case SYMBOL_TYPE:
199+ if (strcmp (cd_fact.lexemeValue ->contents , " FALSE" ) == 0 )
200+ throw std::invalid_argument (" Cognitive domain not found: " + std::string (domain));
201+ break ;
202+ case FACT_ADDRESS_TYPE:
203+ f = cd_fact.factValue ;
204+ break ;
205+ case MULTIFIELD_TYPE:
206+ if (cd_fact.multifieldValue ->length == 0 )
207+ throw std::invalid_argument (" Cognitive domain not found: " + std::string (domain));
208+ f = cd_fact.multifieldValue ->contents [0 ].factValue ;
209+ break ;
210+ default :
211+ throw std::invalid_argument (" Unexpected result type from find-fact for domain: " + std::string (domain));
212+ }
213+ CLIPSValue cd_id;
214+ GetFactSlot (f, " item_id" , &cd_id);
215+ auto &cd_itm = get_coco ().get_item (cd_id.lexemeValue ->contents );
216+ assert (cd_itm.get_type ().get_name () == " CognitiveDomain" );
217+ return cd_itm;
218+ }
219+
205220 domain::domain (std::string_view id, std::string_view name) noexcept : id(id), name(name) {}
206221 json::json domain::to_json () const noexcept { return json::json{{" id" , id}, {" name" , name}}; }
207222
0 commit comments