Ejemplo n.º 1
0
Archivo: api.py Proyecto: fiks/sc-web
def scAddrs(request):
    result = "[]"
    if request.is_ajax():
        sctp_client = newSctpClient()

        # parse arguments
        first = True
        arg = None
        arguments = []
        idx = 0
        while first or (arg is not None):
            arg_str = u'%d_' % idx
            arg = ScAddr.parse_from_string(request.GET.get(arg_str, None))
            if arg is not None:
                arguments.append(arg)
            first = False
            idx += 1

        res = {}
        for idtf in arguments:
            addr = sctp_client.find_element_by_system_identifier(idtf)
            if addr is not None:
                res[idtf] = addr

        result = json.dumps(res)

    return HttpResponse(result, 'application/json')
Ejemplo n.º 2
0
Archivo: api.py Proyecto: fiks/sc-web
def get_identifier(request):
    result = None
    if request.is_ajax():
        lang_code = ScAddr.parse_from_string(request.GET.get(u'language', None))

        if lang_code is None:
            print "Invalid sc-addr of language"
            return HttpResponse(None)

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

        sctp_client = newSctpClient()

        keys = Keynodes(sctp_client)
        keynode_ui_nrel_idtf_language_relation = keys[KeynodeSysIdentifiers.ui_nrel_idtf_language_relation]

        # first of all we need to resolve language relation
        lang_relation_keynode = sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
            lang_code,
            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_idtf_language_relation)
        if lang_relation_keynode is None:
            print "Can't resolve keynode for language '%s'" % str(lang_code)
            return HttpResponse(None)

        lang_relation_keynode = lang_relation_keynode[0][2]

        result = {}
        # get requested identifiers for arguments
        for addr_str in arguments:
            addr = ScAddr.parse_from_string(addr_str)
            if addr is None:
                print "Can't parse sc-addr from argument: %s" % addr_str
                return HttpResponse(None)

            identifier = sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                addr,
                ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                ScElementType.sc_type_link,
                ScElementType.sc_type_arc_pos_const_perm,
                lang_relation_keynode)
            idtf_value = None
            if identifier is not None:
                idtf_addr = identifier[0][2]

                # get identifier value
                idtf_value = sctp_client.get_link_content(idtf_addr)
                idtf_value = idtf_value.decode('utf-8')

                result[addr_str] = idtf_value

        result = json.dumps(result)

    return HttpResponse(result, 'application/json')
Ejemplo n.º 3
0
Archivo: api.py Proyecto: fiks/sc-web
def doCommand(request):
    result = "[]"
    if request.is_ajax():
        #result = u'[{"type": "node", "id": "1", "identifier": "node1"},' \
        #		 u'{"type": "arc", "id": "2", "begin": "1", "end": "3"},' \
        #		 u'{"type": "node", "id": "3", "identifier": "node2"}]'
        sctp_client = newSctpClient()

        cmd_addr = ScAddr.parse_from_string(request.GET.get(u'cmd', None))
        output_addr = ScAddr.parse_from_string(request.GET.get(u'output', None))
        # parse arguments
        first = True
        arg = None
        arguments = []
        idx = 0
        while first or (arg is not None):
            arg = ScAddr.parse_from_string(request.GET.get(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 HttpResponse(None, 'application/json')

            first = False
            idx += 1

        if (len(arguments) > 0) and (cmd_addr is not None) and (output_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_user_command_question = keys[KeynodeSysIdentifiers.ui_user_command_question]
            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_author = keys[KeynodeSysIdentifiers.nrel_author]
            keynode_ui_nrel_user_answer_formats = keys[KeynodeSysIdentifiers.ui_nrel_user_answer_formats]
            keynode_nrel_translation = keys[KeynodeSysIdentifiers.nrel_translation]
            keynode_nrel_answer = keys[KeynodeSysIdentifiers.question_nrel_answer]
            keynode_question_initiated = keys[KeynodeSysIdentifiers.question_initiated]
            keynode_question = keys[KeynodeSysIdentifiers.question]


            # create command in sc-memory
            inst_cmd_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_command_generate_instance,
                inst_cmd_addr)

            inst_cmd_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, inst_cmd_addr, cmd_addr)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_rrel_commnad, inst_cmd_arc)

            # create arguments
            args_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            args_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, inst_cmd_addr, args_addr)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_rrel_command_arguments,
                args_arc)

            idx = 1
            for arg in arguments:
                arg_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, args_addr, arg)
                if arg_arc is None:
                    return HttpResponse(None, 'application/json')

                idx_addr = sctp_client.find_element_by_system_identifier(str(u'rrel_%d' % idx))
                if idx_addr is None:
                    return HttpResponse(None, 'application/json')
                idx += 1
                sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, idx_addr, arg_arc)

            # initialize command
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_command_initiated,
                inst_cmd_addr)
            cmd_finished = checkCommandFinished(inst_cmd_addr, keynode_ui_command_finished, sctp_client)
            while cmd_finished is None:
                time.sleep(0.1)
                cmd_finished = checkCommandFinished(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 HttpResponse(None, 'application/json')

            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 HttpResponse(None, 'application/json')

            question = question[0][2]

            # create author
            user_node = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_user, user_node)

            author_arc = sctp_client.create_arc(ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                question, user_node)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_nrel_author, author_arc)

            # create output formats set
            output_formats_node = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, output_formats_node, output_addr)

            format_arc = sctp_client.create_arc(ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                question, output_formats_node)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_nrel_user_answer_formats,
                format_arc)

            # initiate question
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_question_initiated, question)

            # 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)

            answer = findAnswer(question, keynode_nrel_answer, sctp_client)
            while answer is None:
                time.sleep(0.1)
                answer = findAnswer(question, keynode_nrel_answer, sctp_client)

            answer_addr = answer[0][2]
            translation = findTranslation(answer_addr, keynode_nrel_translation, sctp_client)
            while translation is None:
                time.sleep(0.1)
                translation = findTranslation(answer_addr, keynode_nrel_translation, sctp_client)

            # get output string
            translation_addr = translation[0][2]
            result = sctp_client.get_link_content(translation_addr);

    return HttpResponse(result, 'application/json')