RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
knowrob_wrapper.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright 2015 RAPP
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16  Author: Athanassios Kintsakis
17  contact: akintsakis@issel.ee.auth.gr
18 
19  ******************************************************************************/
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <sstream>
24 #include <ros/package.h>
25 #include <fstream>
26 
30 KnowrobWrapper::KnowrobWrapper(ros::NodeHandle nh) : nh_(nh) {
31  mysql_register_user_ontology_alias_client = nh_.serviceClient<rapp_platform_ros_communications::registerUserOntologyAliasSrv>("/rapp/rapp_mysql_wrapper/register_user_ontology_alias");
32  mysql_get_user_ontology_alias_client = nh_.serviceClient<rapp_platform_ros_communications::getUserOntologyAliasSrv>("/rapp/rapp_mysql_wrapper/get_user_ontology_alias");
33  //mysql_update_client = nh_.serviceClient<rapp_platform_ros_communications::updateDataSrv>("/rapp/rapp_mysql_wrapper/tbl_user_update_data");
34 }
35 
40  //rembember to remove path from here and put it to yaml
41  std::string ontologyDefaultPath = std::string("currentOntologyVersion.owl");
42  rapp_platform_ros_communications::ontologyLoadDumpSrv::Request dmp;
43  dmp.file_url = ontologyDefaultPath;
45 }
46 
52 bool checkIfStringVectorContainsString(std::vector<std::string> vec, std::string a) {
53  bool stringContained = false;
54  for (int i = 0; i < vec.size(); i++) {
55  if (vec.at(i) == a) {
56  stringContained = true;
57  break;
58  }
59  }
60  return stringContained;
61 }
62 
68 bool checkIfStringContainsString(std::string a, std::string b) {
69  std::size_t found = a.find(b);
70  if (found == std::string::npos) {
71  return false;
72  }
73  return true;
74 }
75 
81 std::string intToString(int a) {
82  std::ostringstream temp;
83  temp << a;
84  return temp.str();
85 }
86 
92 std::string SplitFilename(const std::string& str) {
93  size_t found;
94  found = str.find_last_of("/\\");
95  return str.substr(0, found);
96 }
97 
103 bool checkIfFileExists(const char *fileName) {
104  std::ifstream infile(fileName);
105  return infile.good();
106 }
107 
114 std::vector<std::string> split(std::string str, std::string sep) {
115  char* cstr = const_cast<char*> (str.c_str());
116  char* current;
117  std::vector<std::string> arr;
118  current = strtok(cstr, sep.c_str());
119  while (current != NULL) {
120  arr.push_back(current);
121  current = strtok(NULL, sep.c_str());
122  }
123  return arr;
124 }
125 
132 rapp_platform_ros_communications::createOntologyAliasSrv::Response KnowrobWrapper::create_ontology_alias(rapp_platform_ros_communications::createOntologyAliasSrv::Request req) {
133  rapp_platform_ros_communications::createOntologyAliasSrv::Response res;
134  try {
135  if (req.username == std::string("")) {
136  throw std::string("Error, empty username");
137  }
138  std::string currentAlias = get_ontology_alias(req.username);
139  // std::size_t found = currentAlias.find(std::string("FAIL"));
140  // if (found != std::string::npos) {
141  // throw currentAlias;
142  // }
143  res.ontology_alias = currentAlias;
144  res.success = true;
146  return res;
147  } catch (std::string error) {
148  res.success = false;
149  res.trace.push_back(error);
150  res.error = error;
151  return res;
152  }
153 }
154 
161 std::string KnowrobWrapper::get_ontology_alias(std::string username) {
162  std::string ontology_alias;
163  rapp_platform_ros_communications::getUserOntologyAliasSrv srv;
164  srv.request.username = username;
166  if (srv.response.success != true) {
167  throw std::string(std::string("FAIL: User not found, incorrect username?"));
168  } else {
169  ontology_alias = srv.response.ontology_alias;
170  if (ontology_alias == std::string("None")) {
171  ontology_alias = create_ontology_alias_for_new_user(username);
172  }
173  }
174  return ontology_alias;
175 }
176 
183 std::string KnowrobWrapper::create_ontology_alias_for_new_user(std::string username) {
184  std::string ontology_alias;
185  std::vector<std::string> instance_name;
186  std::string query = std::string("rdf_instance_from_class(knowrob:'Person") + std::string("',A)");
187  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
188  char status = results.getStatus();
189  if (status == 0) {
190  throw std::string("User was uninitialized (had no ontology alias), and initilization failed on ontology level");
191  }
192  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
193  it != results.end(); it++) {
194  json_prolog::PrologBindings bdg = *it;
195  instance_name.push_back(bdg["A"]);
196  }
197 
198  ontology_alias = instance_name[0];
199  std::vector<std::string> splitted = split(ontology_alias, std::string("#"));
200  ontology_alias = splitted[1];
201  rapp_platform_ros_communications::registerUserOntologyAliasSrv srv;
202  srv.request.username = username;
203  srv.request.ontology_alias = std::string(ontology_alias);
205  if (srv.response.success != true) {
206  std::string error = srv.response.trace[0];
207  query = std::string("rdf_retractall(knowrob:'") + ontology_alias + std::string("',rdf:type,knowrob:'Person") + std::string("')");
208  results = pl.query(query.c_str());
209  status = results.getStatus();
210  if (status == 0) {
211  error = error + std::string(" DB operation failed.. removing instance from ontology also failed...");
212  } else if (status == 3) {
213  error = error + std::string(" DB operation failed..Remove from ontology Success");
214  }
215  throw std::string(std::string("FAIL") + error);
216  }
218  return ontology_alias;
219 }
220 
226 rapp_platform_ros_communications::recordUserPerformanceCognitiveTestsSrv::Response KnowrobWrapper::record_user_cognitive_tests_performance(rapp_platform_ros_communications::recordUserPerformanceCognitiveTestsSrv::Request req) {
227  rapp_platform_ros_communications::recordUserPerformanceCognitiveTestsSrv::Response res;
228  if (req.test == std::string("") || req.score < 0 || req.score > 100 || req.timestamp < 1 || req.patient_ontology_alias == std::string("") || req.test == std::string("")) {
229  res.success = false;
230  res.trace.push_back("Error, one or more arguments not provided or out of range. Test score is >=0 and <=100 and timestamp is positive integers");
231  res.error = std::string("Error, one or more arguments not provided or out of range. Test score is >=0 and <=100 and timestamp is positive integers");
232  return res;
233  }
234  std::string timestamp = intToString(req.timestamp);
235  std::string score = intToString(req.score);
236 
237  std::string query = std::string("cognitiveTestPerformed(B,knowrob:'") + req.patient_ontology_alias + std::string("',knowrob:'") + req.test + std::string("','") + timestamp + std::string("','") + score + std::string("',knowrob:'Person',knowrob:'CognitiveTestPerformed')");
238  query = std::string("cognitiveTestPerformed(B,knowrob:'") + req.patient_ontology_alias + std::string("',knowrob:'") + req.test + std::string("','") + timestamp + std::string("','") + score + std::string("',knowrob:'Person',knowrob:'CognitiveTestPerformed')");
239  res.trace.push_back(query);
240  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
241 
242  char status = results.getStatus();
243  if (status == 0) {
244  res.success = false;
245  res.trace.push_back(std::string("Test performance entry insertion into ontology FAILED, either invalid test or patient alias"));
246  res.error = std::string("Test performance entry insertion into ontology FAILED, either invalid test or patient alias");
247  return res;
248  } else if (status == 3) {
249  res.success = true;
250  }
251  std::vector<std::string> query_ret_tests;
252  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
253  it != results.end(); it++) {
254  json_prolog::PrologBindings bdg = *it;
255  std::string temp_query_tests = bdg["B"];
256  query_ret_tests.push_back(temp_query_tests);
257  }
258  for (unsigned int i = 0; i < query_ret_tests.size(); i++) {
259  res.cognitive_test_performance_entry = (query_ret_tests[i]);
260  }
262  return res;
263 }
264 
271 rapp_platform_ros_communications::createCognitiveExerciseTestSrv::Response KnowrobWrapper::create_cognitve_tests(rapp_platform_ros_communications::createCognitiveExerciseTestSrv::Request req) {
272  rapp_platform_ros_communications::createCognitiveExerciseTestSrv::Response res;
273  try {
274  if (req.test_type == std::string("") || req.test_difficulty < 1 || req.test_path == std::string("") || req.test_subtype == std::string("") || req.test_id <0) {
275  throw std::string("Error, one or more arguments not provided or out of range. Test variation and test difficulty are positive integers >0");
276  }
277 
278  std::string path = ros::package::getPath("rapp_cognitive_exercise");
279  std::string temp_check_path = path + req.test_path;
280  const char * c = temp_check_path.c_str();
281  if (!checkIfFileExists(c)) {
282  throw std::string("Test file does not exist in provided file path");
283  }
284  std::string difficulty = intToString(req.test_difficulty);
285  std::string test_id = intToString(req.test_id);
286  std::string query = std::string("createCognitiveTest(knowrob:'") + req.test_type + std::string("',B,'") + difficulty + std::string("','") + req.test_path + std::string("',knowrob:'") + req.test_subtype + std::string("','") + test_id + std::string("')");
287  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
288  char status = results.getStatus();
289  if (status == 0) {
290  throw std::string("Test insertion into ontology FAILED, possible error is test type/subtype invalid");
291  } else if (status == 3) {
292  res.success = true;
293  }
294  std::vector<std::string> query_ret_tests;
295  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
296  it != results.end(); it++) {
297  json_prolog::PrologBindings bdg = *it;
298  std::string temp_query_tests = bdg["B"];
299  query_ret_tests.push_back(temp_query_tests);
300  }
301  for (unsigned int i = 0; i < query_ret_tests.size(); i++) {
302  res.test_name = (query_ret_tests[i]);
303  }
304  std::string tmp_test_name;
305  tmp_test_name.assign(res.test_name.c_str());
306  std::vector<std::string> test_created = split(tmp_test_name, std::string("#"));
307  if (test_created.size() == 2) {
308  for (unsigned int i = 0; i < req.supported_languages.size(); i++) {
309  query = std::string("rdf_assert(knowrob:'") + test_created[1] + std::string("',knowrob:supportedLanguages,knowrob:'") + req.supported_languages[i] + std::string("')");
310  results = pl.query(query.c_str());
311  }
312  }
314  return res;
315  } catch (std::string error) {
316  res.success = false;
317  res.trace.push_back(error);
318  res.error = error;
319  return res;
320  }
321 }
322 
329 rapp_platform_ros_communications::cognitiveTestsOfTypeSrv::Response KnowrobWrapper::cognitive_tests_of_type(rapp_platform_ros_communications::cognitiveTestsOfTypeSrv::Request req) {
330  rapp_platform_ros_communications::cognitiveTestsOfTypeSrv::Response res;
331  try {
332  if (req.test_type == std::string("")) {
333  throw std::string("Error, test_type empty");
334  }
335  if (req.test_language == std::string("")) {
336  throw std::string("Error, language empty");
337  }
338  std::string query = std::string("cognitiveTestsOfType(knowrob:'") + req.test_type + std::string("',B,Path,Dif,Sub,knowrob:'") + req.test_language + std::string("',Id)");
339  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
340  char status = results.getStatus();
341  if (status == 0) {
342  throw std::string("No tests of given type exist");
343  }
344  res.success = true;
345  std::vector<std::string> query_ret_tests;
346  std::vector<std::string> query_ret_scores;
347  std::vector<std::string> query_ret_difficulty;
348  std::vector<std::string> query_ret_file_paths;
349  std::vector<std::string> query_ret_subtypes;
350  std::vector<std::string> query_ret_ids;
351  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
352  it != results.end(); it++) {
353  json_prolog::PrologBindings bdg = *it;
354  std::string temp_query_tests = bdg["B"];
355  query_ret_tests.push_back(temp_query_tests);
356  std::string temp_query_file_paths = bdg["Path"];
357  query_ret_file_paths.push_back(temp_query_file_paths);
358  std::string temp_query_difficulty = bdg["Dif"];
359  query_ret_difficulty.push_back(temp_query_difficulty);
360  std::string temp_query_subtypes = bdg["Sub"];
361  query_ret_subtypes.push_back(temp_query_subtypes);
362  std::string temp_query_ids = bdg["Id"];
363  query_ret_ids.push_back(temp_query_ids);
364  }
365  for (unsigned int i = 0; i < query_ret_tests.size(); i++) {
366  res.tests.push_back(query_ret_tests[i]);
367  res.difficulty.push_back(query_ret_difficulty[i]);
368  res.file_paths.push_back(query_ret_file_paths[i]);
369  res.subtype.push_back(query_ret_subtypes[i]);
370  res.test_id.push_back(query_ret_ids[i]);
371  }
372  return res;
373  } catch (std::string error) {
374  res.success = false;
375  res.trace.push_back(error);
376  res.error = error;
377  return res;
378  }
379 }
380 
387 rapp_platform_ros_communications::userPerformanceCognitveTestsSrv::Response KnowrobWrapper::user_performance_cognitve_tests(rapp_platform_ros_communications::userPerformanceCognitveTestsSrv::Request req) {
388  rapp_platform_ros_communications::userPerformanceCognitveTestsSrv::Response res;
389  try {
390  if (req.ontology_alias == std::string("")) {
391  throw std::string("Error, ontology alias empty");
392  }
393  if (req.test_type == std::string("")) {
394  throw std::string("Error, test type empty");
395  }
396  std::string query = std::string("userCognitiveTestPerformance(knowrob:'") + req.ontology_alias + std::string("',knowrob:'") + req.test_type + std::string("',B,Dif,Timestamp,SC,P,SubType)");
397  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
398  char status = results.getStatus();
399  if (status == 0) {
400  throw std::string("No performance records exist for the user or invalid user or invalid test type");
401  }
402  res.success = true;
403  std::vector<std::string> query_ret_tests;
404  std::vector<std::string> query_ret_scores;
405  std::vector<std::string> query_ret_difficulty;
406  std::vector<std::string> query_ret_timestamps;
407  std::vector<std::string> query_ret_subtypes;
408  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
409  it != results.end(); it++) {
410  json_prolog::PrologBindings bdg = *it;
411  std::string temp_query_tests = bdg["B"];
412  query_ret_tests.push_back(temp_query_tests);
413  std::string temp_query_difficulty = bdg["Dif"];
414  query_ret_difficulty.push_back(temp_query_difficulty);
415  std::string temp_query_timestamps = bdg["Timestamp"];
416  query_ret_timestamps.push_back(temp_query_timestamps);
417  std::string temp_query_scores = bdg["SC"];
418  query_ret_scores.push_back(temp_query_scores);
419  std::string tmp_query_subtypes = bdg["SubType"];
420  query_ret_subtypes.push_back(tmp_query_subtypes);
421  }
422  for (unsigned int i = 0; i < query_ret_tests.size(); i++) {
423  res.tests.push_back(query_ret_tests[i]);
424  res.scores.push_back(query_ret_scores[i]);
425  res.difficulty.push_back(query_ret_difficulty[i]);
426  res.timestamps.push_back(query_ret_timestamps[i]);
427  res.subtypes.push_back(query_ret_subtypes[i]);
428  }
429  return res;
430  } catch (std::string error) {
431  res.success = false;
432  res.trace.push_back(error);
433  res.error = error;
434  return res;
435  }
436 }
437 
444 rapp_platform_ros_communications::clearUserPerformanceCognitveTestsSrv::Response KnowrobWrapper::clear_user_cognitive_tests_performance_records(rapp_platform_ros_communications::clearUserPerformanceCognitveTestsSrv::Request req) {
445  rapp_platform_ros_communications::clearUserPerformanceCognitveTestsSrv::Response res;
446  try {
447  if (req.username == std::string("")) {
448  throw std::string("Error, ontology alias empty");
449  }
450  std::string currentAlias = get_ontology_alias(req.username);
451  if (req.test_type == std::string("")) {
452  std::string query = std::string("rdf_has(P,knowrob:cognitiveTestPerformedPatient,knowrob:'") + currentAlias + std::string("'),rdf_retractall(P,L,S)");
453  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
454  char status = results.getStatus();
455  if (status == 0) {
456  throw std::string("No performance records exist for the user or invalid user or invalid test type");
457  } else if (status == 3) {
458  res.success = true;
459  }
460  } else {
461  std::string query = std::string("rdf_has(A,rdf:type,knowrob:'") + req.test_type + std::string("'),rdf_has(P,knowrob:cognitiveTestPerformedTestName,A),rdf_has(P,knowrob:cognitiveTestPerformedPatient,knowrob:'") + currentAlias + std::string("'),rdf_retractall(P,L,S)");
462  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
463  char status = results.getStatus();
464  if (status == 0) {
465  throw std::string("No performance records exist for the user or invalid user or invalid test type");
466  } else if (status == 3) {
467  res.success = true;
468  }
469  }
471  return res;
472  } catch (std::string error) {
473  res.success = false;
474  res.trace.push_back(error);
475  res.error = error;
476  return res;
477  }
478 }
479 
486 rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Response KnowrobWrapper::subclassesOfQuery(rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Request req) {
487  rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Response res;
488  try {
489  if (req.ontology_class == std::string("")) {
490  throw std::string("Error, empty ontology class");
491  }
492  std::string query = std::string("");
493  if (req.recursive == true) {
494  query = std::string("superclassesOf_withCheck(knowrob:'") + req.ontology_class + std::string("',A)");
495  } else {
496  query = std::string("direct_superclassesOf_withCheck(knowrob:'") + req.ontology_class + std::string("',A)");
497  }
498  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
499  char status = results.getStatus();
500  if (status == 0) {
501  throw std::string(std::string("Class: ") + req.ontology_class + std::string(" does not exist"));
502  }
503  res.success = true;
504  std::vector<std::string> query_ret;
505  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
506  it != results.end(); it++) {
507  json_prolog::PrologBindings bdg = *it;
508  std::string temp_query_result = bdg["A"];
509  if (!checkIfStringContainsString(temp_query_result, std::string("file:///"))) {
510  if (!checkIfStringVectorContainsString(query_ret, temp_query_result)) {
511  query_ret.push_back(temp_query_result);
512  }
513  }
514  }
515  for (unsigned int i = 0; i < query_ret.size(); i++) {
516  res.results.push_back(query_ret[i]);
517  }
518  return res;
519  } catch (std::string error) {
520  res.success = false;
521  res.trace.push_back(error);
522  res.error = error;
523  return res;
524  }
525 }
526 
533 rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Response KnowrobWrapper::superclassesOfQuery(rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Request req) {
534  rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Response res;
535  try {
536  if (req.ontology_class == std::string("")) {
537  throw std::string("Error, empty ontology class");
538  }
539  std::string query = std::string("");
540  if (req.recursive == true) {
541  query = std::string("subclassesOf_withCheck(knowrob:'") + req.ontology_class + std::string("',A)");
542  } else {
543  query = std::string("direct_subclassesOf_withCheck(knowrob:'") + req.ontology_class + std::string("',A)");
544  }
545  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
546  char status = results.getStatus();
547  if (status == 0) {
548  throw std::string(std::string("Class: ") + req.ontology_class + std::string(" does not exist"));
549  }
550  res.success = true;
551  std::vector<std::string> query_ret;
552  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
553  it != results.end(); it++) {
554  json_prolog::PrologBindings bdg = *it;
555  std::string temp_query_result = bdg["A"];
556  if (!checkIfStringContainsString(temp_query_result, std::string("file:///"))) {
557  if (!checkIfStringVectorContainsString(query_ret, temp_query_result)) {
558  query_ret.push_back(temp_query_result);
559  }
560  }
561  }
562  for (unsigned int i = 0; i < query_ret.size(); i++) {
563  res.results.push_back(query_ret[i]);
564  }
565  return res;
566  } catch (std::string error) {
567  res.success = false;
568  res.trace.push_back(error);
569  res.error = error;
570  return res;
571  }
572 }
573 
580 rapp_platform_ros_communications::ontologyIsSubSuperClassOfSrv::Response KnowrobWrapper::isSubSuperclassOfQuery(rapp_platform_ros_communications::ontologyIsSubSuperClassOfSrv::Request req) {
581  rapp_platform_ros_communications::ontologyIsSubSuperClassOfSrv::Response res;
582  try {
583  if (req.parent_class == std::string("")) {
584  throw std::string("Error, empty ontology class");
585  }
586  if (req.child_class == std::string("")) {
587  throw std::string("Error, empty other class");
588  }
589  std::string query = std::string("");
590  if (req.recursive == true) {
591  query = std::string("superclassesOf_withCheck(knowrob:'") + req.parent_class + std::string("',A)");
592  } else {
593  query = std::string("direct_superclassesOf_withCheck(knowrob:'") + req.parent_class + std::string("',A)");
594  }
595  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
596  char status = results.getStatus();
597  if (status == 0) {
598  throw std::string(std::string("Class: ") + req.parent_class + std::string(" does not exist"));
599  }
600  res.success = true;
601  std::vector<std::string> query_ret;
602  int logic = 0;
603  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
604  it != results.end(); it++) {
605  json_prolog::PrologBindings bdg = *it;
606  std::string temp_query_result = bdg["A"];
607  std::vector<std::string> seperator = split(temp_query_result, std::string("#"));
608  if (seperator[1] == req.child_class) {
609  logic = 1;
610  break;
611  }
612  }
613  if (logic == 0) {
614  res.result = false;
615  } else {
616  res.result = true;
617  }
618  return res;
619  } catch (std::string error) {
620  res.success = false;
621  res.trace.push_back(error);
622  res.error = error;
623  return res;
624  }
625 }
626 
633 rapp_platform_ros_communications::createInstanceSrv::Response KnowrobWrapper::createInstanceQuery(rapp_platform_ros_communications::createInstanceSrv::Request req) {
634  rapp_platform_ros_communications::createInstanceSrv::Response res;
635  try {
636  if (req.username == std::string("")) {
637  throw std::string("Error, empty username");
638  }
639  if (req.ontology_class == std::string("")) {
640  throw std::string("Error, empty ontology class");
641  }
642  std::string ontology_alias = get_ontology_alias(req.username);
643  std::vector<std::string> instance_name;
644  std::string query = std::string("instanceFromClass_withCheck_andAssign(knowrob:'") + req.ontology_class + std::string("',A,knowrob:'") + ontology_alias + std::string("')");
645  res.trace.push_back(query);
646  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
647  char status = results.getStatus();
648  if (status == 0) {
649  throw std::string(std::string("Class: ") + req.ontology_class + std::string(" does not exist probably.. or ontology_alias for user exists in the mysqlDatabase and not in the ontology"));
650  }
651  res.success = true;
652  for (json_prolog::PrologQueryProxy::iterator it = results.begin(); it != results.end(); it++) {
653  json_prolog::PrologBindings bdg = *it;
654  instance_name.push_back(bdg["A"]);
655  }
656  if (instance_name.size() == 1) {
657  instance_name = split(instance_name[0], "#");
658  if (instance_name.size() == 2) {
659  res.instance_name = (std::string("Created instance name is: ") + instance_name[1]);
660  } else {
661  throw std::string("Fatal Error, instance not created.. split to # error");
662  }
663  } else {
664  throw std::string("Fatal Error, instance not created.., retrieval error");
665  }
667  return res;
668  } catch (std::string error) {
669  res.success = false;
670  res.trace.push_back(error);
671  res.error = error;
672  return res;
673  }
674 }
675 
682 rapp_platform_ros_communications::ontologyLoadDumpSrv::Response KnowrobWrapper::dumpOntologyQuery(rapp_platform_ros_communications::ontologyLoadDumpSrv::Request req) {
683  rapp_platform_ros_communications::ontologyLoadDumpSrv::Response res;
684  try {
685  std::string path = getenv("HOME");
686  path = path + std::string("/rapp_platform_files/");
687  if (req.file_url.empty()) { // || req.file_url==std::string("")
688  throw std::string("Empty file path");
689  }
690  size_t pathDepth = std::count(req.file_url.begin(), req.file_url.end(), '/');
691  std::string str2("/");
692  std::size_t found = req.file_url.find(str2);
693  if (found != std::string::npos && pathDepth > 1) {
694  std::string folderFromPath = SplitFilename(req.file_url);
695  const char * c = folderFromPath.c_str();
696  if (!checkIfFileExists(c)) {
697  throw std::string("Path does not exist, invalid folder?");
698  }
699  }
700  req.file_url = path + req.file_url;
701  std::string query = std::string("rdf_save('") + req.file_url + std::string("')");
702  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
703  char status = results.getStatus();
704  if (status == 0) {
705  throw std::string("Ontology dump failed");
706  }
707  res.success = true;
708  return res;
709  } catch (std::string error) {
710  res.success = false;
711  res.trace.push_back(error);
712  res.error = error;
713  return res;
714  }
715 }
716 
723 rapp_platform_ros_communications::ontologyLoadDumpSrv::Response KnowrobWrapper::loadOntologyQuery(rapp_platform_ros_communications::ontologyLoadDumpSrv::Request req) {
724  rapp_platform_ros_communications::ontologyLoadDumpSrv::Response res;
725  try {
726  if (req.file_url.empty()) {
727  throw std::string("Empty file path");
728  }
729  std::string path = getenv("HOME");
730  path = path + std::string("/rapp_platform_files/");
731  req.file_url = path + req.file_url;
732  const char * c = req.file_url.c_str();
733  if (!checkIfFileExists(c)) {
734  throw std::string(std::string("File does not exist in provided file path: '")
735  + std::string(req.file_url) + std::string("'"));
736  }
737  std::string query = std::string("rdf_load('") + req.file_url + std::string("')");
738  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
739  char status = results.getStatus();
740  if (status == 0) {
741  throw std::string("Ontology load failed");
742  }
743  res.success = true;
744  return res;
745  } catch (std::string error) {
746  res.success = false;
747  res.trace.push_back(error);
748  res.error = error;
749  return res;
750  }
751 }
752 
759 rapp_platform_ros_communications::returnUserInstancesOfClassSrv::Response KnowrobWrapper::user_instances_of_class(rapp_platform_ros_communications::returnUserInstancesOfClassSrv::Request req) {
760  rapp_platform_ros_communications::returnUserInstancesOfClassSrv::Response res;
761  try {
762  if (req.username == std::string("")) {
763  throw std::string("Error, empty username");
764  }
765  if (req.ontology_class == std::string("")) {
766  throw std::string("Error, empty ontology class");
767  }
768  std::string ontology_alias = get_ontology_alias(req.username);
769  std::string query = std::string("rdf_has(knowrob:'") + ontology_alias + std::string("',knowrob:'belongsToUser',A)");
770  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
771  char status = results.getStatus();
772  if (status == 0) {
773  throw std::string("User has no instances");
774  }
775  res.success = true;
776  std::vector<std::string> query_ret;
777  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
778  it != results.end(); it++) {
779  json_prolog::PrologBindings bdg = *it;
780  std::string temp_query_result = bdg["A"];
781  if (!checkIfStringContainsString(temp_query_result, std::string("file:///"))) {
782  if (!checkIfStringVectorContainsString(query_ret, temp_query_result)) {
783  query_ret.push_back(temp_query_result);
784  }
785  }
786  }
787  for (unsigned int i = 0; i < query_ret.size(); i++) {
788  res.results.push_back(query_ret[i]);
789  }
790  return res;
791  } catch (std::string error) {
792  res.success = false;
793  res.trace.push_back(error);
794  res.error = error;
795  return res;
796  }
797 }
798 
804 rapp_platform_ros_communications::registerImageObjectToOntologySrv::Response KnowrobWrapper::register_image_object_to_ontology(rapp_platform_ros_communications::registerImageObjectToOntologySrv::Request req) {
805  rapp_platform_ros_communications::registerImageObjectToOntologySrv::Response res;
806  try {
807  if (req.user_ontology_alias == std::string("") || req.timestamp < 1 || req.image_path == std::string("") || req.object_ontology_class == std::string("")) {
808  res.success = false;
809  res.trace.push_back("Error, one or more arguments not provided or out of range");
810  res.error = std::string("Error, one or more arguments not provided or out of range");
811  return res;
812  }
813  //std::string ontology_alias = get_ontology_alias(req.username);
814  std::string timestamp = intToString(req.timestamp);
815 
816  std::string query = std::string("createObjectAndRegisterImage(Object,knowrob:'") + req.object_ontology_class + std::string("',knowrob:'") + req.user_ontology_alias + std::string("','") + timestamp + std::string("','") + req.image_path + std::string("','") + req.caffe_class + std::string("')");
817  //query = std::string("cognitiveTestPerformed(B,knowrob:'") + req.patient_ontology_alias + std::string("',knowrob:'") + req.test + std::string("','") + timestamp + std::string("','") + score + std::string("',knowrob:'Person',knowrob:'CognitiveTestPerformed')");
818  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
819  char status = results.getStatus();
820  if (status == 0) {
821  res.success = false;
822  res.trace.push_back(std::string("Test performance entry insertion into ontology FAILED, either invalid test or patient alias"));
823  res.error = std::string("Test performance entry insertion into ontology FAILED, either invalid test or patient alias");
824  return res;
825  } else if (status == 3) {
826  res.success = true;
827  }
828  std::vector<std::string> query_ret_tests;
829  for (json_prolog::PrologQueryProxy::iterator it = results.begin();
830  it != results.end(); it++) {
831  json_prolog::PrologBindings bdg = *it;
832  std::string temp_query_tests = bdg["Object"];
833  query_ret_tests.push_back(temp_query_tests);
834  }
835  for (unsigned int i = 0; i < query_ret_tests.size(); i++) {
836  res.object_entry = (query_ret_tests[i]);
837  }
839  return res;
840  } catch (std::string error) {
841  res.success = false;
842  res.trace.push_back(error);
843  res.error = error;
844  return res;
845  }
846 }
847 
853 rapp_platform_ros_communications::retractUserOntologyAliasSrv::Response KnowrobWrapper::retract_user_ontology_alias(rapp_platform_ros_communications::retractUserOntologyAliasSrv::Request req) {
854  rapp_platform_ros_communications::retractUserOntologyAliasSrv::Response res;
855  try {
856  if (req.ontology_alias == std::string("")) {
857  throw std::string("User ontology_alias not provided");
858  }
859  std::string query = std::string("rdf_retractall(knowrob:'"+req.ontology_alias+"',rdf:type,knowrob:'Person')");
860  json_prolog::PrologQueryProxy results = pl.query(query.c_str());
861  char status = results.getStatus();
862  if (status == 0) {
863  throw std::string("Retract failed at ontology level");
864  } else if (status == 3) {
865  res.success = true;
866  }
868  return res;
869  } catch (std::string error) {
870  res.success = false;
871  res.trace.push_back(error);
872  res.error = error;
873  return res;
874  }
875 }
std::string get_ontology_alias(std::string user_id)
Returns the ontology alias of the user.
rapp_platform_ros_communications::createInstanceSrv::Response createInstanceQuery(rapp_platform_ros_communications::createInstanceSrv::Request req)
Implements the createInstance ROS service.
rapp_platform_ros_communications::returnUserInstancesOfClassSrv::Response user_instances_of_class(rapp_platform_ros_communications::returnUserInstancesOfClassSrv::Request req)
Implements the returnUserInstancesOfClass ROS service.
rapp_platform_ros_communications::createCognitiveExerciseTestSrv::Response create_cognitve_tests(rapp_platform_ros_communications::createCognitiveExerciseTestSrv::Request req)
Implements the create_cognitve_tests ROS service.
rapp_platform_ros_communications::ontologyLoadDumpSrv::Response loadOntologyQuery(rapp_platform_ros_communications::ontologyLoadDumpSrv::Request req)
Implements the loadOntology ROS service.
rapp_platform_ros_communications::recordUserPerformanceCognitiveTestsSrv::Response record_user_cognitive_tests_performance(rapp_platform_ros_communications::recordUserPerformanceCognitiveTestsSrv::Request req)
Implements the record_user_cognitive_tests_performance ROS service.
rapp_platform_ros_communications::registerImageObjectToOntologySrv::Response register_image_object_to_ontology(rapp_platform_ros_communications::registerImageObjectToOntologySrv::Request req)
Implements the register_image_object_to_ontology ROS service.
KnowrobWrapper(ros::NodeHandle nh)
Default constructor.
rapp_platform_ros_communications::createOntologyAliasSrv::Response create_ontology_alias(rapp_platform_ros_communications::createOntologyAliasSrv::Request req)
Implements the create_ontology_alias ROS service.
ros::ServiceClient mysql_get_user_ontology_alias_client
rapp_platform_ros_communications::retractUserOntologyAliasSrv::Response retract_user_ontology_alias(rapp_platform_ros_communications::retractUserOntologyAliasSrv::Request req)
Implements the retract_user_ontology_alias ROS service.
json_prolog::Prolog pl
bool checkIfStringVectorContainsString(std::vector< std::string > vec, std::string a)
Check if a string is contained in a vector string.
rapp_platform_ros_communications::clearUserPerformanceCognitveTestsSrv::Response clear_user_cognitive_tests_performance_records(rapp_platform_ros_communications::clearUserPerformanceCognitveTestsSrv::Request req)
Implements the clear_user_cognitive_tests_performance_records ROS service.
bool checkIfFileExists(const char *fileName)
Checks if path exists.
bool checkIfStringContainsString(std::string a, std::string b)
Checks if the second string is contained within the first string.
std::string SplitFilename(const std::string &str)
Keeps only the final folder or file from a path.
rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Response subclassesOfQuery(rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Request req)
Implements the subclassesOf ROS service.
rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Response superclassesOfQuery(rapp_platform_ros_communications::ontologySubSuperClassesOfSrv::Request req)
Implements the superclassesOf ROS service.
void dump_ontology_now()
Dumps the ontology to default path.
ros::ServiceClient mysql_register_user_ontology_alias_client
rapp_platform_ros_communications::ontologyIsSubSuperClassOfSrv::Response isSubSuperclassOfQuery(rapp_platform_ros_communications::ontologyIsSubSuperClassOfSrv::Request req)
Implements the isSubSuperclass ROS service.
rapp_platform_ros_communications::userPerformanceCognitveTestsSrv::Response user_performance_cognitve_tests(rapp_platform_ros_communications::userPerformanceCognitveTestsSrv::Request req)
Implements the user_performance_cognitve_tests ROS service.
ros::NodeHandle nh_
rapp_platform_ros_communications::ontologyLoadDumpSrv::Response dumpOntologyQuery(rapp_platform_ros_communications::ontologyLoadDumpSrv::Request req)
Implements the dumpOntology ROS service.
rapp_platform_ros_communications::cognitiveTestsOfTypeSrv::Response cognitive_tests_of_type(rapp_platform_ros_communications::cognitiveTestsOfTypeSrv::Request req)
Implements the cognitive_tests_of_type ROS service.
std::string intToString(int a)
Converts integer to string.
std::vector< std::string > split(std::string str, std::string sep)
Splits string by delimiter.
std::string create_ontology_alias_for_new_user(std::string user_id)
Creates a new ontology alias for a user.