def test_create_nonexistent_cognitive_test_xml_path_nonexistent(self): subclasses_of_service = rospy.get_param(\ "rapp_knowrob_wrapper_create_cognitve_tests") rospy.wait_for_service(subclasses_of_service) test_service = rospy.ServiceProxy(\ subclasses_of_service, createCognitiveExerciseTestSrv) req = createCognitiveExerciseTestSrvRequest() req.test_type="ArithmeticCts" req.test_difficulty=1 req.test_path="1/1cognitiveTests/additionsTest1.xml" req.test_subtype="BasicArithmeticCts" response = test_service(req) self.assertEqual(response.success, False) self.assertEqual(response.error, 'Test file does not exist in provided file path')
def test_create_nonexistent_cognitive_test(self): subclasses_of_service = rospy.get_param(\ "rapp_knowrob_wrapper_create_cognitve_tests") rospy.wait_for_service(subclasses_of_service) test_service = rospy.ServiceProxy(\ subclasses_of_service, createCognitiveExerciseTestSrv) req = createCognitiveExerciseTestSrvRequest() req.test_type="1ArithmeticCts" req.test_difficulty=1 req.test_path="/cognitiveTests/ArithmeticCts_BasicArithmeticCts_diff1_id_0.xml" req.test_subtype="BasicArithmeticCts" supportedLanguages=['en','el'] req.supported_languages=supportedLanguages response = test_service(req) self.assertEqual(response.success, False) self.assertEqual(response.error, 'Test insertion into ontology FAILED, possible error is test type/subtype invalid')
def test_create_nonexistent_cognitive_test(self): subclasses_of_service = rospy.get_param(\ "rapp_knowrob_wrapper_create_cognitve_tests") rospy.wait_for_service(subclasses_of_service) test_service = rospy.ServiceProxy(\ subclasses_of_service, createCognitiveExerciseTestSrv) req = createCognitiveExerciseTestSrvRequest() req.test_type="1ArithmeticCts" req.test_difficulty=1 req.test_path="/cognitiveTests/ArithmeticCts_BasicArithmeticCts_diff1.xml" req.test_subtype="BasicArithmeticCts" supportedLanguages=['en','el'] req.supported_languages=supportedLanguages response = test_service(req) self.assertEqual(response.success, False) self.assertEqual(response.error, 'Test insertion into ontology FAILED, possible error is test type/subtype invalid')
def testCreator(self,req): res=cognitiveTestCreatorSrvResponse() fname=req.inputFile d=dict() questions=dict() answers=dict() correctAnswers=dict() listQuestions=[] listAnswers=[] listCorrectAnswers=[] supportedLanguages=[] flag=False questionsStart=False count=0 try: with open(fname) as f: content = f.readlines() for s in content: if (":" in s): s=s.strip() tmp=s.split(":") if(tmp[0] == "Difficulty" or tmp[0]=="variationID"): if (not self.is_int(tmp[1])): res.trace.append("error, difficulty or variation ID is not an integer") flag=False if(tmp[0] == "language"): if(not len(questions)==len(answers)==len(correctAnswers)): flag=False res.trace.append("error, test is broken, questions-answers-correctAnswers not equal in size") break supportedLanguages.append(tmp[1]) listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() questionsStart=True flag=True count=0 elif(questionsStart): if(tmp[0] == "question"): count=count+1; tmp[0]=tmp[0]+str(count) #print tmp[1] questions[tmp[0]]=tmp[1] elif(tmp[0]=="answers"): tmp[0]=tmp[0]+str(count) answers[tmp[0]]=tmp[1] elif(tmp[0]=="correctAnswer"): tmp[0]=tmp[0]+str(count) if(tmp[1] not in answers["answers"+str(count)]): flag=False res.trace.append("Correct Answer not in answers in Question "+str(count) +" in test "+fname ) break correctAnswers[tmp[0]]=tmp[1] else: flag=False res.trace.append("field other than questions,answers,correctAnswer found") break else: d[tmp[0]]=[tmp[1]] if(not len(questions)==len(answers)==len(correctAnswers)): flag=False res.trace.append("error, test is broken, questions-answers-correctAnswers not equal in size") else: listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() xmlFileName="" if(flag): xmlFileName=d["testType"][0]+"_"+d["testSubType"][0]+"_"+"diff"+d["difficulty"][0] root = ET.Element("cognitiveTest") ET.SubElement(root, "testType").text = d["testType"][0] ET.SubElement(root, "testSubType").text = d["testSubType"][0] Languages=ET.SubElement(root,"Languages") for l in range(1,len(listQuestions)): questions=listQuestions[l] answers=listAnswers[l] correctAnswers=listCorrectAnswers[l] currentLanguage=ET.SubElement(Languages, supportedLanguages[l-1]) for i in range(1,count+1): nm="Q"+str(i) Q=ET.SubElement(currentLanguage, "Question", name=nm) nm="question"+str(i) ET.SubElement(Q, "body").text = questions[nm].decode('UTF-8') nm="answers"+str(i) answs=answers[nm].split(",") for j in answs: A=ET.SubElement(Q, "answer") ET.SubElement(A, "body").text = j.decode('UTF-8') nm="correctAnswer"+str(i) corrAnswer=correctAnswers[nm] ET.SubElement(Q, "correctAnswer").text = corrAnswer.decode('UTF-8') tree = ET.ElementTree(root) rospack = rospkg.RosPack() localPackagePath=rospack.get_path('rapp_cognitive_exercise') inNodeName="/cognitiveTests/"+xmlFileName+".xml" localPackagePath=localPackagePath+inNodeName tree.write(localPackagePath,encoding="UTF-8",xml_declaration=True) serv_topic = rospy.get_param('rapp_knowrob_wrapper_create_cognitve_tests') createTestReq=createCognitiveExerciseTestSrvRequest() createTestReq.test_type=d["testType"][0] createTestReq.test_difficulty=int(d["difficulty"][0]) createTestReq.test_subtype=d["testSubType"][0] createTestReq.test_path=inNodeName createTestReq.supported_languages=supportedLanguages knowrob_service = rospy.ServiceProxy(serv_topic, createCognitiveExerciseTestSrv) createCognitiveTestResponse = knowrob_service(createTestReq) if(createCognitiveTestResponse.success!=True): res.trace.extend(createCognitiveTestResponse.trace) res.error=createCognitiveTestResponse.error res.success=False os.remove(localPackagePath) return res ontologyName=createCognitiveTestResponse.test_name tmp=ontologyName.split("#") ontologyName=tmp[1] tree = ET.parse(localPackagePath) root = tree.getroot() ET.SubElement(root, "name").text = ontologyName self.indent(root) os.remove(localPackagePath) tree.write(localPackagePath,encoding="UTF-8",xml_declaration=True) res.success=True else: res.error="test "+fname +" is broken" res.success=False except IndexError: #print "test "+fname +" is broken" res.error="IndexError.. test "+fname +" is broken" res.success=False except IOError: #print "IO Error, cannot open test file or write xml file" res.error="IO Error, cannot open test file or write xml file" res.success=False return res
def testCreator(self, req): res = cognitiveTestCreatorSrvResponse() fname = req.inputFile d = dict() questions = dict() answers = dict() correctAnswers = dict() listQuestions = [] listAnswers = [] listCorrectAnswers = [] supportedLanguages = [] flag = False questionsStart = False count = 0 try: with open(fname) as f: content = f.readlines() for s in content: if (":" in s): s = s.strip() tmp = s.split(":") #print tmp[1] if (tmp[0] == "Difficulty" or tmp[0] == "variationID"): if (not self.is_int(tmp[1])): res.trace.append( "error, difficulty or variation ID is not an integer" ) flag = False if (tmp[0] == "language"): if (not len(questions) == len(answers) == len(correctAnswers)): flag = False res.trace.append( "error, test is broken, questions-answers-correctAnswers not equal in size" ) break #print "tt "+tmp[1] supportedLanguages.append(tmp[1]) listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() questionsStart = True flag = True count = 0 elif (questionsStart): if (tmp[0] == "question"): count = count + 1 tmp[0] = tmp[0] + str(count) #print tmp[1] questions[tmp[0]] = tmp[1] elif (tmp[0] == "answers"): tmp[0] = tmp[0] + str(count) answers[tmp[0]] = tmp[1] elif (tmp[0] == "correctAnswer"): tmp[0] = tmp[0] + str(count) if (tmp[1] not in answers["answers" + str(count)]): flag = False res.trace.append( "Correct Answer not in answers in Question " + str(count) + " in test " + fname) break correctAnswers[tmp[0]] = tmp[1] else: flag = False res.trace.append( "field other than questions,answers,correctAnswer found" ) break else: d[tmp[0]] = [tmp[1]] if (not len(questions) == len(answers) == len(correctAnswers)): flag = False res.trace.append( "error, test is broken, questions-answers-correctAnswers not equal in size" ) else: #print questions listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() xmlFileName = "" #print "reading finished" if (flag): #res.trace.append("test seems good") #xmlFileName=d["testType"][0]+"_"+d["testSubType"][0]+"_"+"diff"+d["difficulty"][0]+"_"+"var"+d["variationID"][0]+".xml" xmlFileName = d["testType"][0] + "_" + d["testSubType"][ 0] + "_" + "diff" + d["difficulty"][0] #print xmlFileName root = ET.Element("cognitiveTest") #ET.SubElement(root, "name").text = ontologyName ET.SubElement(root, "testType").text = d["testType"][0] #ET.SubElement(root, "variationID").text = d["variationID"][0] #ET.SubElement(root, "difficulty").text = d["difficulty"][0] ET.SubElement(root, "testSubType").text = d["testSubType"][0] #ET.SubElement(root, "Questions") Languages = ET.SubElement(root, "Languages") #print "count "+str(count) #print len(listQuestions) for l in range(1, len(listQuestions)): #print supportedLanguages[l-1] questions = listQuestions[l] #print questions answers = listAnswers[l] #print answers correctAnswers = listCorrectAnswers[l] #print correctAnswers currentLanguage = ET.SubElement(Languages, supportedLanguages[l - 1]) for i in range(1, count + 1): nm = "Q" + str(i) #ET.SubElement(Questions, "Question", name=nm) Q = ET.SubElement(currentLanguage, "Question", name=nm) nm = "question" + str(i) #print nm #print questions[nm] #print questions ET.SubElement(Q, "body").text = questions[nm].decode( 'UTF-8') #[0] nm = "answers" + str(i) answs = answers[nm].split(",") for j in answs: A = ET.SubElement(Q, "answer") ET.SubElement(A, "body").text = j.decode('UTF-8') nm = "correctAnswer" + str(i) corrAnswer = correctAnswers[nm] ET.SubElement( Q, "correctAnswer").text = corrAnswer.decode('UTF-8') tree = ET.ElementTree(root) rospack = rospkg.RosPack() #get the cognitive test_id #serv_topic = rospy.get_param('rapp_knowrob_wrapper_cognitive_tests_of_type') #if(not serv_topic): #rospy.logerror("rapp_knowrob_wrapper_cognitive_tests_of_type not found") #res.trace.append("rapp_knowrob_wrapper_cognitive_tests_of_type not found") #res.error="rapp_knowrob_wrapper_cognitive_tests_of_type not found" #res.success=False #return res #cognitiveTestsOfTypeSrvReq=cognitiveTestsOfTypeSrvRequest() #cognitiveTestsOfTypeSrvReq.test_type=d["testType"][0] #knowrob_service = rospy.ServiceProxy(serv_topic, cognitiveTestsOfTypeSrv) #cognitiveTestsOfTypeResponse = knowrob_service(cognitiveTestsOfTypeSrvReq) #calculatedVariationID=1 #if(cognitiveTestsOfTypeResponse.success==True): #maxidlist=[] #for test_i in range(len(cognitiveTestsOfTypeResponse.subtype)): #tmp=cognitiveTestsOfTypeResponse.subtype[test_i].split("#") #tmpSubtype=tmp[1] #if (tmpSubtype==d["testSubType"][0] and cognitiveTestsOfTypeResponse.difficulty[test_i]==d["difficulty"][0]): #maxidlist.append(cognitiveTestsOfTypeResponse.variation[test_i]) ##print maxidlist #if (len(maxidlist)>0): #maxidlist=map(int, maxidlist) #calculatedVariationID=max(maxidlist)+1 ##print calculatedVariationID ############## localPackagePath = rospack.get_path('rapp_cognitive_exercise') #inNodeName="/cognitiveTests/"+xmlFileName+"_var"+str(calculatedVariationID)+".xml" inNodeName = "/cognitiveTests/" + xmlFileName + ".xml" localPackagePath = localPackagePath + inNodeName tree.write(localPackagePath, encoding="UTF-8", xml_declaration=True) serv_topic = rospy.get_param( 'rapp_knowrob_wrapper_create_cognitve_tests') if (not serv_topic): rospy.logerror( "rapp_knowrob_wrapper_create_cognitve_tests not found") res.trace.append( "rapp_knowrob_wrapper_create_cognitve_tests not found") res.error = "rapp_knowrob_wrapper_create_cognitve_tests not found" res.success = False os.remove(localPackagePath) return res createTestReq = createCognitiveExerciseTestSrvRequest() createTestReq.test_type = d["testType"][0] #createTestReq.test_variation=calculatedVariationID#int(d["variationID"][0]) createTestReq.test_difficulty = int(d["difficulty"][0]) createTestReq.test_subtype = d["testSubType"][0] createTestReq.test_path = inNodeName createTestReq.supported_languages = supportedLanguages knowrob_service = rospy.ServiceProxy( serv_topic, createCognitiveExerciseTestSrv) createCognitiveTestResponse = knowrob_service(createTestReq) if (createCognitiveTestResponse.success != True): res.trace.extend(createCognitiveTestResponse.trace) res.error = createCognitiveTestResponse.error res.success = False os.remove(localPackagePath) return res ontologyName = createCognitiveTestResponse.test_name #print ontologyName tmp = ontologyName.split("#") ontologyName = tmp[1] tree = ET.parse(localPackagePath) root = tree.getroot() ET.SubElement(root, "name").text = ontologyName self.indent(root) os.remove(localPackagePath) #localPackagePath=localPackagePath+"_var"+str(calculatedVariationID)+".xml" tree.write(localPackagePath, encoding="UTF-8", xml_declaration=True) res.success = True else: #print "test "+fname +" is broken" res.error = "test " + fname + " is broken" res.success = False #except IndexError: #print "test "+fname +" is broken" # res.error="IndexError.. test "+fname +" is broken" #res.success=False except IOError: #print "IO Error, cannot open test file or write xml file" res.error = "IO Error, cannot open test file or write xml file" res.success = False return res
def testCreator(self, req): res = cognitiveTestCreatorSrvResponse() fname = req.inputFile d = dict() questions = dict() answers = dict() correctAnswers = dict() listQuestions = [] listAnswers = [] listCorrectAnswers = [] supportedLanguages = [] flag = False questionsStart = False count = 0 try: with open(fname) as f: content = f.readlines() for s in content: if ":" in s: s = s.strip() tmp = s.split(":") # print tmp[1] if tmp[0] == "Difficulty" or tmp[0] == "variationID": if not self.is_int(tmp[1]): res.trace.append("error, difficulty or variation ID is not an integer") flag = False if tmp[0] == "language": if not len(questions) == len(answers) == len(correctAnswers): flag = False res.trace.append( "error, test is broken, questions-answers-correctAnswers not equal in size" ) break # print "tt "+tmp[1] supportedLanguages.append(tmp[1]) listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() questionsStart = True flag = True count = 0 elif questionsStart: if tmp[0] == "question": count = count + 1 tmp[0] = tmp[0] + str(count) # print tmp[1] questions[tmp[0]] = tmp[1] elif tmp[0] == "answers": tmp[0] = tmp[0] + str(count) answers[tmp[0]] = tmp[1] elif tmp[0] == "correctAnswer": tmp[0] = tmp[0] + str(count) if tmp[1] not in answers["answers" + str(count)]: flag = False res.trace.append( "Correct Answer not in answers in Question " + str(count) + " in test " + fname ) break correctAnswers[tmp[0]] = tmp[1] else: flag = False res.trace.append("field other than questions,answers,correctAnswer found") break else: d[tmp[0]] = [tmp[1]] if not len(questions) == len(answers) == len(correctAnswers): flag = False res.trace.append("error, test is broken, questions-answers-correctAnswers not equal in size") else: # print questions listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() xmlFileName = "" # print "reading finished" if flag: # res.trace.append("test seems good") # xmlFileName=d["testType"][0]+"_"+d["testSubType"][0]+"_"+"diff"+d["difficulty"][0]+"_"+"var"+d["variationID"][0]+".xml" xmlFileName = d["testType"][0] + "_" + d["testSubType"][0] + "_" + "diff" + d["difficulty"][0] # print xmlFileName root = ET.Element("cognitiveTest") # ET.SubElement(root, "name").text = ontologyName ET.SubElement(root, "testType").text = d["testType"][0] # ET.SubElement(root, "variationID").text = d["variationID"][0] # ET.SubElement(root, "difficulty").text = d["difficulty"][0] ET.SubElement(root, "testSubType").text = d["testSubType"][0] # ET.SubElement(root, "Questions") Languages = ET.SubElement(root, "Languages") # print "count "+str(count) # print len(listQuestions) for l in range(1, len(listQuestions)): # print supportedLanguages[l-1] questions = listQuestions[l] # print questions answers = listAnswers[l] # print answers correctAnswers = listCorrectAnswers[l] # print correctAnswers currentLanguage = ET.SubElement(Languages, supportedLanguages[l - 1]) for i in range(1, count + 1): nm = "Q" + str(i) # ET.SubElement(Questions, "Question", name=nm) Q = ET.SubElement(currentLanguage, "Question", name=nm) nm = "question" + str(i) # print nm # print questions[nm] # print questions ET.SubElement(Q, "body").text = questions[nm].decode("UTF-8") # [0] nm = "answers" + str(i) answs = answers[nm].split(",") for j in answs: A = ET.SubElement(Q, "answer") ET.SubElement(A, "body").text = j.decode("UTF-8") nm = "correctAnswer" + str(i) corrAnswer = correctAnswers[nm] ET.SubElement(Q, "correctAnswer").text = corrAnswer.decode("UTF-8") tree = ET.ElementTree(root) rospack = rospkg.RosPack() # get the cognitive test_id # serv_topic = rospy.get_param('rapp_knowrob_wrapper_cognitive_tests_of_type') # if(not serv_topic): # rospy.logerror("rapp_knowrob_wrapper_cognitive_tests_of_type not found") # res.trace.append("rapp_knowrob_wrapper_cognitive_tests_of_type not found") # res.error="rapp_knowrob_wrapper_cognitive_tests_of_type not found" # res.success=False # return res # cognitiveTestsOfTypeSrvReq=cognitiveTestsOfTypeSrvRequest() # cognitiveTestsOfTypeSrvReq.test_type=d["testType"][0] # knowrob_service = rospy.ServiceProxy(serv_topic, cognitiveTestsOfTypeSrv) # cognitiveTestsOfTypeResponse = knowrob_service(cognitiveTestsOfTypeSrvReq) # calculatedVariationID=1 # if(cognitiveTestsOfTypeResponse.success==True): # maxidlist=[] # for test_i in range(len(cognitiveTestsOfTypeResponse.subtype)): # tmp=cognitiveTestsOfTypeResponse.subtype[test_i].split("#") # tmpSubtype=tmp[1] # if (tmpSubtype==d["testSubType"][0] and cognitiveTestsOfTypeResponse.difficulty[test_i]==d["difficulty"][0]): # maxidlist.append(cognitiveTestsOfTypeResponse.variation[test_i]) ##print maxidlist # if (len(maxidlist)>0): # maxidlist=map(int, maxidlist) # calculatedVariationID=max(maxidlist)+1 ##print calculatedVariationID ############## localPackagePath = rospack.get_path("rapp_cognitive_exercise") # inNodeName="/cognitiveTests/"+xmlFileName+"_var"+str(calculatedVariationID)+".xml" inNodeName = "/cognitiveTests/" + xmlFileName + ".xml" localPackagePath = localPackagePath + inNodeName tree.write(localPackagePath, encoding="UTF-8", xml_declaration=True) serv_topic = rospy.get_param("rapp_knowrob_wrapper_create_cognitve_tests") if not serv_topic: rospy.logerror("rapp_knowrob_wrapper_create_cognitve_tests not found") res.trace.append("rapp_knowrob_wrapper_create_cognitve_tests not found") res.error = "rapp_knowrob_wrapper_create_cognitve_tests not found" res.success = False os.remove(localPackagePath) return res createTestReq = createCognitiveExerciseTestSrvRequest() createTestReq.test_type = d["testType"][0] # createTestReq.test_variation=calculatedVariationID#int(d["variationID"][0]) createTestReq.test_difficulty = int(d["difficulty"][0]) createTestReq.test_subtype = d["testSubType"][0] createTestReq.test_path = inNodeName createTestReq.supported_languages = supportedLanguages knowrob_service = rospy.ServiceProxy(serv_topic, createCognitiveExerciseTestSrv) createCognitiveTestResponse = knowrob_service(createTestReq) if createCognitiveTestResponse.success != True: res.trace.extend(createCognitiveTestResponse.trace) res.error = createCognitiveTestResponse.error res.success = False os.remove(localPackagePath) return res ontologyName = createCognitiveTestResponse.test_name # print ontologyName tmp = ontologyName.split("#") ontologyName = tmp[1] tree = ET.parse(localPackagePath) root = tree.getroot() ET.SubElement(root, "name").text = ontologyName self.indent(root) os.remove(localPackagePath) # localPackagePath=localPackagePath+"_var"+str(calculatedVariationID)+".xml" tree.write(localPackagePath, encoding="UTF-8", xml_declaration=True) res.success = True else: # print "test "+fname +" is broken" res.error = "test " + fname + " is broken" res.success = False # except IndexError: # print "test "+fname +" is broken" # res.error="IndexError.. test "+fname +" is broken" # res.success=False except IOError: # print "IO Error, cannot open test file or write xml file" res.error = "IO Error, cannot open test file or write xml file" res.success = False return res
def testCreator(self,req): res=cognitiveTestCreatorSrvResponse() fname=req.inputFile d=dict() questions=dict() answers=dict() correctAnswers=dict() listQuestions=[] listAnswers=[] listCorrectAnswers=[] supportedLanguages=[] flag=False questionsStart=False count=0 try: with open(fname) as f: content = f.readlines() for s in content: if (":" in s): s=s.strip() tmp=s.split(":") if(tmp[0] == "Difficulty" or tmp[0]=="variationID"): if (not self.is_int(tmp[1])): res.trace.append("error, difficulty or variation ID is not an integer") flag=False if(tmp[0] == "language"): if(not len(questions)==len(answers)==len(correctAnswers)): flag=False res.trace.append("error, test is broken, questions-answers-correctAnswers not equal in size") break supportedLanguages.append(tmp[1]) listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() questionsStart=True flag=True count=0 elif(questionsStart): if(tmp[0] == "question"): count=count+1; tmp[0]=tmp[0]+str(count) #print tmp[1] questions[tmp[0]]=tmp[1] elif(tmp[0]=="answers"): tmp[0]=tmp[0]+str(count) answers[tmp[0]]=tmp[1] elif(tmp[0]=="correctAnswer"): tmp[0]=tmp[0]+str(count) if(tmp[1] not in answers["answers"+str(count)]): flag=False res.trace.append("Correct Answer not in answers in Question "+str(count) +" in test "+fname ) break correctAnswers[tmp[0]]=tmp[1] else: flag=False res.trace.append("field other than questions,answers,correctAnswer found") break else: d[tmp[0]]=[tmp[1]] if(not len(questions)==len(answers)==len(correctAnswers)): flag=False res.trace.append("error, test is broken, questions-answers-correctAnswers not equal in size") else: listQuestions.append(questions.copy()) questions.clear() listAnswers.append(answers.copy()) answers.clear() listCorrectAnswers.append(correctAnswers.copy()) correctAnswers.clear() xmlFileName="" if(flag): #xmlFileName=d["testType"][0]+"_"+d["testSubType"][0]+"_"+"diff"+d["difficulty"][0] root = ET.Element("cognitiveTest") ET.SubElement(root, "testType").text = d["testType"][0] ET.SubElement(root, "testSubType").text = d["testSubType"][0] Languages=ET.SubElement(root,"Languages") for l in range(1,len(listQuestions)): questions=listQuestions[l] answers=listAnswers[l] correctAnswers=listCorrectAnswers[l] currentLanguage=ET.SubElement(Languages, supportedLanguages[l-1]) for i in range(1,count+1): nm="Q"+str(i) Q=ET.SubElement(currentLanguage, "Question", name=nm) nm="question"+str(i) ET.SubElement(Q, "body").text = questions[nm].decode('UTF-8') nm="answers"+str(i) answs=answers[nm].split(",") for j in answs: A=ET.SubElement(Q, "answer") ET.SubElement(A, "body").text = j.decode('UTF-8') nm="correctAnswer"+str(i) corrAnswer=correctAnswers[nm] ET.SubElement(Q, "correctAnswer").text = corrAnswer.decode('UTF-8') tree = ET.ElementTree(root) rospack = rospkg.RosPack() localPackagePath=rospack.get_path('rapp_cognitive_exercise') test_id=self.determineCognitiveTestId(d["testType"][0], d["testSubType"][0], d["difficulty"][0]) xmlFileName=d["testType"][0]+"_"+d["testSubType"][0]+"_"+"diff"+d["difficulty"][0]+"_id_"+str(test_id) inNodeName="/cognitiveTests/"+xmlFileName+".xml" localPackagePath=localPackagePath+inNodeName tree.write(localPackagePath,encoding="UTF-8",xml_declaration=True) serv_topic = rospy.get_param('rapp_knowrob_wrapper_create_cognitve_tests') createTestReq=createCognitiveExerciseTestSrvRequest() createTestReq.test_type=d["testType"][0] createTestReq.test_difficulty=int(d["difficulty"][0]) createTestReq.test_subtype=d["testSubType"][0] createTestReq.test_path=inNodeName createTestReq.supported_languages=supportedLanguages createTestReq.test_id=supportedLanguages=test_id knowrob_service = rospy.ServiceProxy(serv_topic, createCognitiveExerciseTestSrv) createCognitiveTestResponse = knowrob_service(createTestReq) if(createCognitiveTestResponse.success!=True): res.trace.extend(createCognitiveTestResponse.trace) res.error=createCognitiveTestResponse.error res.success=False os.remove(localPackagePath) return res ontologyName=createCognitiveTestResponse.test_name tmp=ontologyName.split("#") ontologyName=tmp[1] tree = ET.parse(localPackagePath) root = tree.getroot() ET.SubElement(root, "name").text = ontologyName self.indent(root) os.remove(localPackagePath) tree.write(localPackagePath,encoding="UTF-8",xml_declaration=True) res.success=True else: res.error="test "+fname +" is broken" res.success=False except IndexError: #print "test "+fname +" is broken" res.error="IndexError.. test "+fname +" is broken" CognitiveExerciseHelperFunctions.traceError(res.error,res.trace) res.success=False except IOError: #print "IO Error, cannot open test file or write xml file" res.error="IO Error, cannot open test file or write xml file" CognitiveExerciseHelperFunctions.traceError(res.error,res.trace) res.success=False except AppError as e: AppError.passErrorToRosSrv(e,res) return res