5 from datetime
import datetime
6 from os.path
import expanduser
7 from collections
import OrderedDict
8 from app_error_exception
import AppError
11 from rapp_platform_ros_communications.srv
import (
12 ontologySubSuperClassesOfSrv,
13 ontologySubSuperClassesOfSrvRequest,
14 ontologySubSuperClassesOfSrvResponse,
15 createOntologyAliasSrv,
16 createOntologyAliasSrvRequest,
17 createOntologyAliasSrvResponse,
18 userPerformanceCognitveTestsSrv,
19 userPerformanceCognitveTestsSrvRequest,
20 userScoreHistoryForAllCategoriesSrv,
21 userScoreHistoryForAllCategoriesSrvResponse,
23 getUserLanguageSrvRequest,
24 getUserLanguageSrvResponse,
25 cognitiveTestsOfTypeSrv,
26 cognitiveTestsOfTypeSrvRequest,
27 cognitiveTestsOfTypeSrvResponse
30 from rapp_platform_ros_communications.msg
import (
43 serv_topic = rospy.get_param(
'rapp_knowrob_wrapper_user_performance_cognitve_tests')
44 userPerformanceReq=userPerformanceCognitveTestsSrvRequest()
45 userPerformanceReq.test_type=testType
46 userPerformanceReq.ontology_alias=userOntologyAlias
47 knowrob_service = rospy.ServiceProxy(serv_topic, userPerformanceCognitveTestsSrv)
48 return knowrob_service(userPerformanceReq)
57 serv_topic = rospy.get_param(
'rapp_knowrob_wrapper_create_ontology_alias')
58 knowrob_service = rospy.ServiceProxy(serv_topic, createOntologyAliasSrv)
59 createOntologyAliasReq = createOntologyAliasSrvRequest()
60 createOntologyAliasReq.username=username
61 createOntologyAliasResponse = knowrob_service(createOntologyAliasReq)
62 if(createOntologyAliasResponse.success!=
True):
63 raise AppError(createOntologyAliasResponse.error, createOntologyAliasResponse.trace)
64 return createOntologyAliasResponse.ontology_alias
72 serv_topic = rospy.get_param(
'rapp_knowrob_wrapper_subclasses_of_topic')
73 knowrob_service = rospy.ServiceProxy(serv_topic, ontologySubSuperClassesOfSrv)
74 testTypesReq = ontologySubSuperClassesOfSrvRequest()
75 testTypesReq.ontology_class=
"CognitiveTests"
76 testTypesResponse = knowrob_service(testTypesReq)
77 if(testTypesResponse.success!=
True):
78 testTypesResponse.trace.append(
"cannot load test categories from ontology")
79 raise AppError(testTypesResponse.error+
"cannot load test categories from ontology",testTypesResponse.trace)
81 for s
in testTypesResponse.results:
83 testTypesList.append(tmpList[1])
92 serv_topic = rospy.get_param(
'rapp_knowrob_wrapper_subclasses_of_topic')
93 knowrob_service = rospy.ServiceProxy(serv_topic, ontologySubSuperClassesOfSrv)
94 testTypesReq = ontologySubSuperClassesOfSrvRequest()
95 testTypesReq.ontology_class=
"HumanLanguage"
96 testTypesResponse = knowrob_service(testTypesReq)
97 if(testTypesResponse.success!=
True):
98 testTypesResponse.trace.append(
"cannot load test categories from ontology")
99 raise AppError(testTypesResponse.error+
"cannot load test categories from ontology",testTypesResponse.trace)
101 for s
in testTypesResponse.results:
103 testTypesList.append(tmpList[1])
114 serv_topic = rospy.get_param(
'rapp_knowrob_wrapper_cognitive_tests_of_type')
115 cognitiveTestsOfTypeSrvReq=cognitiveTestsOfTypeSrvRequest()
116 cognitiveTestsOfTypeSrvReq.test_type=testType
117 cognitiveTestsOfTypeSrvReq.test_language=userLanguage
118 knowrob_service = rospy.ServiceProxy(serv_topic, cognitiveTestsOfTypeSrv)
119 cognitiveTestsOfTypeResponse = knowrob_service(cognitiveTestsOfTypeSrvReq)
120 if(cognitiveTestsOfTypeResponse.success!=
True):
121 raise AppError(cognitiveTestsOfTypeResponse.error, cognitiveTestsOfTypeResponse.trace)
123 return cognitiveTestsOfTypeResponse
133 testSubTypePrefix=
"http://knowrob.org/kb/knowrob.owl#"
136 for i
in range(len(testsOfType.tests)):
137 if((chosenDif==
"" or chosenDif==testsOfType.difficulty[i])
and (testSubType==
"" or testSubTypePrefix+testSubType==testsOfType.subtype[i])):
138 tlist=[testsOfType.file_paths[i],testsOfType.difficulty[i],testsOfType.subtype[i], testsOfType.test_id[i]]
139 d[testsOfType.tests[i]]=[tlist]
150 serv_topic = rospy.get_param(
'rapp_mysql_wrapper_get_user_language_service_topic')
151 mysql_service = rospy.ServiceProxy(serv_topic, getUserLanguageSrv)
152 getUserLanguageSrvReq = getUserLanguageSrvRequest()
153 getUserLanguageSrvReq.username=username
154 getUserLanguageSrvResponse = mysql_service(getUserLanguageSrvReq)
155 if(getUserLanguageSrvResponse.success!=
True):
156 raise AppError(getUserLanguageSrvResponse.error, getUserLanguageSrvResponse.trace)
157 return getUserLanguageSrvResponse.user_language
167 if(
not testType==
""):
168 if(testType
not in validtestTypesList):
169 error=
"invalid test type, not contained in ontology subclasses of cognitive test types"
172 testTypesList.append(testType)
174 testTypesList=validtestTypesList
183 userPerfOrganizedByTimestamp=OrderedDict()
184 for i
in range(len(userPerf.tests)):
185 tlist=[userPerf.tests[i],userPerf.scores[i],userPerf.difficulty[i], userPerf.subtypes[i]]
186 userPerfOrganizedByTimestamp[int(userPerf.timestamps[i])]=[tlist]
187 userPerfOrganizedByTimestamp=OrderedDict(sorted(userPerfOrganizedByTimestamp.items(), key=
lambda t: t[0], reverse=
True))
188 return userPerfOrganizedByTimestamp
197 for frame
in traceback.extract_tb(sys.exc_info()[2]):
198 fname,lineno,fn,text = frame
199 print "Error in %s on line %d" % (fname, lineno)
200 error = error +
"Error in %s on line %d" % (fname, lineno)
201 trace.append(
"Error in %s on line %d" % (fname, lineno))
def traceError
Traces and returns the code line where an error occured.
def determineTestTypeListForReturningScoresOrHistory
Validates the provided test type or selects all test types from ontology if non provided.
def getTestTypesFromOntology
Queries the ontology and returns the cognitive test types available.
Exception compliant with the ros error and trace srvs.
def filterTestsbyDifficultyAndSubtype
Filters the tests by Difficulty and test subtype.
def getUserOntologyAlias
Gets the users ontology alias and if it doesnt exist it creates it.
def getTestLanguagesFromOntology
Queries the ontology and returns the cognitive test languages available.
def organizeUserPerformanceByTimestamp
Organizes the user's performance entries by timestamp.
def getUserLanguage
Queries the MySQL database through the MySQL wrapper and returns the user's language.
def getUserPerformanceRecordsForTestType
Gets the user cognitive test performance records for given test type.
def getCognitiveTestsOfType
Gets the cognitive tests of the given type and difficulty available in the ontology.