def post(self): with SctpClientInstance() as sctp_client: question_addr = ScAddr.parse_from_string( self.get_argument(u'question', None)) format_addr = ScAddr.parse_from_string( self.get_argument(u'format', None)) keys = Keynodes(sctp_client) keynode_nrel_answer = keys[ KeynodeSysIdentifiers.question_nrel_answer] keynode_nrel_translation = keys[ KeynodeSysIdentifiers.nrel_translation] keynode_nrel_format = keys[KeynodeSysIdentifiers.nrel_format] keynode_system_element = keys[KeynodeSysIdentifiers.system_element] # try to find answer for the question wait_time = 0 wait_dt = 0.1 answer = logic.find_answer(question_addr, keynode_nrel_answer, sctp_client) while answer is None: time.sleep(wait_dt) wait_time += wait_dt if wait_time > tornado.options.options.event_wait_timeout: return logic.serialize_error(self, 404, 'Timeout waiting for answer') answer = logic.find_answer(question_addr, keynode_nrel_answer, sctp_client) if answer is None: return logic.serialize_error(self, 404, 'Answer not found') answer_addr = answer[0][2] # try to find translation to specified format result_link_addr = logic.find_translation_with_format( answer_addr, format_addr, keynode_nrel_format, keynode_nrel_translation, sctp_client) # if link addr not found, then run translation of answer to specified format if result_link_addr is None: trans_cmd_addr = sctp_client.create_node( ScElementType.sc_type_node | ScElementType.sc_type_const) logic.append_to_system_elements(sctp_client, keynode_system_element, trans_cmd_addr) arc_addr = sctp_client.create_arc( ScElementType.sc_type_arc_pos_const_perm, trans_cmd_addr, answer_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) arc_addr = sctp_client.create_arc( ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_rrel_source_sc_construction], arc_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) arc_addr = sctp_client.create_arc( ScElementType.sc_type_arc_pos_const_perm, trans_cmd_addr, format_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) arc_addr = sctp_client.create_arc( ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_rrel_output_format], arc_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) # add into translation command set arc_addr = sctp_client.create_arc( ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_command_translate_from_sc], trans_cmd_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) # initialize command arc_addr = sctp_client.create_arc( ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_command_initiated], trans_cmd_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) # now we need to wait translation result wait_time = 0 translation = logic.find_translation_with_format( answer_addr, format_addr, keynode_nrel_format, keynode_nrel_translation, sctp_client) while translation is None: time.sleep(wait_dt) wait_time += wait_dt if wait_time > tornado.options.options.event_wait_timeout: return logic.serialize_error( self, 404, 'Timeout waiting for answer translation') translation = logic.find_translation_with_format( answer_addr, format_addr, keynode_nrel_format, keynode_nrel_translation, sctp_client) if translation is not None: result_link_addr = translation # if result exists, then we need to return it content if result_link_addr is not None: result = json.dumps({"link": result_link_addr.to_int()}) self.set_header("Content-Type", "application/json") self.finish(result)
def post(self): with SctpClientInstance() as sctp_client: question_addr = ScAddr.parse_from_string(self.get_argument(u'question', None)) format_addr = ScAddr.parse_from_string(self.get_argument(u'format', None)) keys = Keynodes(sctp_client) keynode_nrel_answer = keys[KeynodeSysIdentifiers.question_nrel_answer] keynode_nrel_translation = keys[KeynodeSysIdentifiers.nrel_translation] keynode_nrel_format = keys[KeynodeSysIdentifiers.nrel_format] keynode_system_element = keys[KeynodeSysIdentifiers.system_element] # try to find answer for the question wait_time = 0 wait_dt = 0.1 answer = logic.find_answer(question_addr, keynode_nrel_answer, sctp_client) while answer is None: time.sleep(wait_dt) wait_time += wait_dt if wait_time > tornado.options.options.event_wait_timeout: return logic.serialize_error(self, 404, 'Timeout waiting for answer') answer = logic.find_answer(question_addr, keynode_nrel_answer, sctp_client) if answer is None: return logic.serialize_error(self, 404, 'Answer not found') answer_addr = answer[0][2] # try to find translation to specified format result_link_addr = logic.find_translation_with_format(answer_addr, format_addr, keynode_nrel_format, keynode_nrel_translation, sctp_client) # if link addr not found, then run translation of answer to specified format if result_link_addr is None: trans_cmd_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const) logic.append_to_system_elements(sctp_client, keynode_system_element, trans_cmd_addr) arc_addr = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, trans_cmd_addr, answer_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) arc_addr = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_rrel_source_sc_construction], arc_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) arc_addr = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, trans_cmd_addr, format_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) arc_addr = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_rrel_output_format], arc_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) # add into translation command set arc_addr = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_command_translate_from_sc], trans_cmd_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) # initialize command arc_addr = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keys[KeynodeSysIdentifiers.ui_command_initiated], trans_cmd_addr) logic.append_to_system_elements(sctp_client, keynode_system_element, arc_addr) # now we need to wait translation result wait_time = 0 translation = logic.find_translation_with_format(answer_addr, format_addr, keynode_nrel_format, keynode_nrel_translation, sctp_client) while translation is None: time.sleep(wait_dt) wait_time += wait_dt if wait_time > tornado.options.options.event_wait_timeout: return logic.serialize_error(self, 404, 'Timeout waiting for answer translation') translation = logic.find_translation_with_format(answer_addr, format_addr, keynode_nrel_format, keynode_nrel_translation, sctp_client) if translation is not None: result_link_addr = translation # if result exists, then we need to return it content if result_link_addr is not None: result = json.dumps({"link": result_link_addr.to_int()}) self.set_header("Content-Type", "application/json") self.finish(result)