Ejemplo n.º 1
0
    def post(self):
        result = None

        # get arguments
        idx = 1
        arguments = []
        arg = ''
        while arg is not None:
            arg = self.get_argument(u'%d_' % idx, None)
            if arg is not None:
                arguments.append(arg)
            idx += 1

        with SctpClientInstance() as sctp_client:
            keys = Keynodes(sctp_client)

            sc_session = logic.ScSession(self, sctp_client, keys)
            used_lang = sc_session.get_used_language()

            result = {}
            # get requested identifiers for arguments
            for addr_str in arguments:
                addr = ScAddr.parse_from_string(addr_str)
                if addr is None:
                    self.clear()
                    self.set_status(404)
                    self.finish('Can\'t parse sc-addr from argument: %s' %
                                addr_str)

                found = False

                idtf_value = logic.get_identifier_translated(
                    addr, used_lang, keys, sctp_client)
                if idtf_value:
                    result[addr_str] = idtf_value

            self.set_header("Content-Type", "application/json")
            self.finish(json.dumps(result))
Ejemplo n.º 2
0
    def post(self):
        result = None

        # get arguments
        idx = 1
        arguments = []
        arg = ''
        while arg is not None:
            arg = self.get_argument(u'%d_' % idx, None)
            if arg is not None:
                arguments.append(arg)
            idx += 1

        sctp_client = new_sctp_client()
        keys = Keynodes(sctp_client)
        
        sc_session = logic.ScSession(self, sctp_client, keys)
        used_lang = sc_session.get_used_language()
        

        result = {}
        # get requested identifiers for arguments
        for addr_str in arguments:
            addr = ScAddr.parse_from_string(addr_str)
            if addr is None:
                self.clear()
                self.set_status(404)
                self.finish('Can\'t parse sc-addr from argument: %s' % addr_str)
            
            found = False

            idtf_value = logic.get_identifier_translated(addr, used_lang, keys, sctp_client)
            if idtf_value:
                result[addr_str] = idtf_value
        
        sctp_client.shutdown()
        self.set_header("Content-Type", "application/json")
        self.finish(json.dumps(result))
Ejemplo n.º 3
0
    def post(self):
        result = '[]'
        with SctpClientInstance() as sctp_client:

            cmd_addr = ScAddr.parse_from_string(self.get_argument(u'cmd', None))
            # parse arguments
            first = True
            arg = None
            arguments = []
            idx = 0
            while first or (arg is not None):
                arg = ScAddr.parse_from_string(self.get_argument(u'%d_' % idx, None))
                if arg is not None:
                    # check if sc-element exist
                    if sctp_client.check_element(arg):
                        arguments.append(arg)
                    else:
                        return serialize_error(404, "Invalid argument: %s" % arg)
    
                first = False
                idx += 1
    
            if (len(arguments) > 0) and (cmd_addr is not None):
    
                keys = Keynodes(sctp_client)
    
                keynode_ui_rrel_commnad = keys[KeynodeSysIdentifiers.ui_rrel_commnad]
                keynode_ui_rrel_command_arguments = keys[KeynodeSysIdentifiers.ui_rrel_command_arguments]
                keynode_ui_nrel_command_result = keys[KeynodeSysIdentifiers.ui_nrel_command_result]
                keynode_ui_command_generate_instance = keys[KeynodeSysIdentifiers.ui_command_generate_instance]
                keynode_ui_command_initiated = keys[KeynodeSysIdentifiers.ui_command_initiated]
                keynode_ui_command_finished = keys[KeynodeSysIdentifiers.ui_command_finished]
                keynode_ui_nrel_command_result = keys[KeynodeSysIdentifiers.ui_nrel_command_result]
                keynode_ui_user = keys[KeynodeSysIdentifiers.ui_user]
                keynode_nrel_authors = keys[KeynodeSysIdentifiers.nrel_authors]
                keynode_question_initiated = keys[KeynodeSysIdentifiers.question_initiated]
                keynode_question = keys[KeynodeSysIdentifiers.question]
                keynode_system_element = keys[KeynodeSysIdentifiers.system_element]
                keynode_nrel_ui_nrel_command_lang_template = keys[KeynodeSysIdentifiers.nrel_ui_nrel_command_lang_template]
                keynode_languages = keys[KeynodeSysIdentifiers.languages]
                keynode_nrel_main_idtf = keys[KeynodeSysIdentifiers.nrel_main_idtf]
    
                # create command in sc-memory
                inst_cmd_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
                logic.append_to_system_elements(sctp_client, keynode_system_element, inst_cmd_addr)
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_command_generate_instance, inst_cmd_addr)
                logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
                inst_cmd_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, inst_cmd_addr, cmd_addr)
                logic.append_to_system_elements(sctp_client, keynode_system_element, inst_cmd_arc)
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_rrel_commnad, inst_cmd_arc)
                logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
                # create arguments
                args_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
                logic.append_to_system_elements(sctp_client, keynode_system_element, args_addr)
                args_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, inst_cmd_addr, args_addr)
                logic.append_to_system_elements(sctp_client, keynode_system_element, args_arc)
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_rrel_command_arguments, args_arc)
                logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
                idx = 1
                for arg in arguments:
                    arg_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, args_addr, arg)
                    logic.append_to_system_elements(sctp_client, keynode_system_element, arg_arc)
                    if arg_arc is None:
                        return serialize_error(self, 404, 'Error while create "create_instance" command')
    
                    idx_addr = sctp_client.find_element_by_system_identifier(str(u'rrel_%d' % idx))
                    if idx_addr is None:
                        return serialize_error(self, 404, 'Error while create "create_instance" command')
                    idx += 1
                    arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, idx_addr, arg_arc)
                    logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
                wait_time = 0
                wait_dt = 0.1
                
                # initialize command
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_command_initiated, inst_cmd_addr)
    
                cmd_finished = logic.check_command_finished(inst_cmd_addr, keynode_ui_command_finished, sctp_client)
                while cmd_finished is None:
                    time.sleep(wait_dt)
                    wait_time += wait_dt
                    if wait_time > tornado.options.options.event_wait_timeout:
                        return serialize_error(self, 404, 'Timeout waiting for "create_instance" command finished')
                    cmd_finished = logic.check_command_finished(inst_cmd_addr, keynode_ui_command_finished, sctp_client)
    
    
                # get command result
                cmd_result = sctp_client.iterate_elements(
                    SctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                    inst_cmd_addr,
                    ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                    ScElementType.sc_type_node | ScElementType.sc_type_const,
                    ScElementType.sc_type_arc_pos_const_perm,
                    keynode_ui_nrel_command_result
                )
                if cmd_result is None:
                    return serialize_error(self, 404, 'Can\'t find "create_instance" command result')
    
                cmd_result = cmd_result[0][2]
    
                # @todo support all possible commands
                # try to find question node
                question = sctp_client.iterate_elements(
                    SctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                    keynode_question,
                    ScElementType.sc_type_arc_pos_const_perm,
                    ScElementType.sc_type_node | ScElementType.sc_type_const,
                    ScElementType.sc_type_arc_pos_const_perm,
                    cmd_result
                )
                if question is None:
                    return serialize_error(self, 404, "Can't find question node")
    
                question = question[0][2]
    
                logic.append_to_system_elements(sctp_client, keynode_system_element, question)
                
                # generate main identifiers
                langs = logic.get_languages_list(keynode_languages, sctp_client)
                if langs:
                    templates = sctp_client.iterate_elements(
                        SctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                        cmd_addr,
                        ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                        ScElementType.sc_type_link,
                        ScElementType.sc_type_arc_pos_const_perm,
                        keynode_nrel_ui_nrel_command_lang_template
                    )
                    if templates:
                        generated = {}
                        identifiers = {}
                        
                        # get identifiers
                        for l in langs:
                            identifiers[str(l)] = {}
                            for a in arguments:
                                idtf_value = logic.get_identifier_translated(a, l, keys, sctp_client)
                                if idtf_value:
                                    identifiers[str(l)][str(a)] = idtf_value
                                    
                        
                        for t in templates:
                            input_arcs = sctp_client.iterate_elements(
                                                SctpIteratorType.SCTP_ITERATOR_3A_A_F,
                                                ScElementType.sc_type_node | ScElementType.sc_type_const | ScElementType.sc_type_node_class,
                                                ScElementType.sc_type_arc_pos_const_perm,
                                                t[2])
                            if input_arcs:
                                for arc in input_arcs:
                                    for l in langs:
                                        if not generated.has_key(str(l)) and arc[0] == l:
                                            lang_idtfs = identifiers[str(l)]
                                            # get content of link
                                            data = sctp_client.get_link_content(t[2]).decode('utf-8')
                                            if data:
                                                for idx in xrange(len(arguments)):
                                                    value = arguments[idx].to_id()
                                                    if lang_idtfs.has_key(str(arguments[idx])):
                                                        value = lang_idtfs[str(arguments[idx])]
                                                    data = data.replace(u'$ui_arg_%d' % (idx + 1), value)
                                                    
                                                
                                                # generate identifier
                                                idtf_link = sctp_client.create_link()
                                                sctp_client.set_link_content(idtf_link, str(data.encode('utf-8')))
                                                sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, l, idtf_link)
                                                
                                                bin_arc = sctp_client.create_arc(ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                                                                                 question, idtf_link)
                                                sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm,
                                                                       keynode_nrel_main_idtf, bin_arc)
                                                
                                                generated[str(l)] = True
    
                # create author
                sc_session = logic.ScSession(self, sctp_client, keys)
                user_node = sc_session.get_sc_addr()
                if not user_node:
                    return serialize_error(self, 404, "Can't resolve user node")
                
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_user, user_node)
                logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
                author_arc = sctp_client.create_arc(ScElementType.sc_type_arc_common | ScElementType.sc_type_const, question, user_node)
                logic.append_to_system_elements(sctp_client, keynode_system_element, author_arc)
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_nrel_authors, author_arc)
                logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
    
                # initiate question
                arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_question_initiated, question)
                logic.append_to_system_elements(sctp_client, keynode_system_element, arc)
    
                # first of all we need to wait answer to this question
                #print sctp_client.iterate_elements(SctpIteratorType.SCTP_ITERATOR_3F_A_A, keynode_question_initiated, 0, 0)
                
                result = { 'question': question.to_id() }
                
            self.set_header("Content-Type", "application/json")
            self.finish(json.dumps(result))