예제 #1
0
파일: sc2scg.py 프로젝트: Alexandra-H/pyUI
    def translate_impl(self, _input, _output):
        """Translator implementation
        @param _input:    input data set
        @type _input:    sc_global_addr
        @param _output:    output window (must be created)
        @type _output:    sc_global_addr
        
        @return: list of errors each element of list is a tuple(object, error)
        @rtype: list
        """
        errors = []
        
        # FIXME:    think about multiply windows for one sc-element
        objs = objects.ScObject._sc2Objects(_output)
        
        assert len(objs) > 0
        sheet = objs[0]
        assert type(sheet) is objects.ObjectSheet
        
        session = core.Kernel.session()
        
        trans_objs = []
        result_objs = []
        
        
        # creating list of element to translate
        it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a,
                                                               _input,
                                                               sc.SC_A_CONST | sc.SC_POS,
                                                               0), True)
        list_of_addrs = []
        while not it.is_over():
            trans_objs.append(it.value(2))
            list_of_addrs.append(str(it.value(2).this))
            it.next()
            
        # getting objects on sheet
        childs = sheet.getChilds()
        sc_scg = {}
        object_types = {}
        for obj in childs:
            addr = obj._getScAddr()
            if addr is not None:
                s_addr = str(addr.this)    
                sc_scg[s_addr] = obj
                object_types[s_addr] = session.get_type(addr)
                list_of_addrs.append(s_addr)
        
        ignore_list = []
        process_pairs = []

        # translating binary and noorient pairs and store types
        for obj in trans_objs:
            _type = session.get_type(obj)
            object_types[str(obj.this)] = _type
            
            if str(obj.this) in ignore_list: 
                continue
            
            # checking for sheaf nodes
            if _type & sc.SC_NODE and sc_utils.isNodeSheaf(session, obj):
                res = get_pair_binary(session, obj, list_of_addrs)
                if res is not None:
                    for idx in xrange(len(res[0])):
                        if idx != 0 and idx != 4:
                            ignore_list.append(str(res[0][idx].this))
                        
                    #ignore_list.extend(res[0])
                    # creating pair
                    _const = "const"
                    if _type & sc.SC_VAR:
                        _const = "var"
                    elif _type & sc.SC_METAVAR:
                        _const = "meta"
                    
                    _orient = "-"
                    if res[1]:
                        _orient = "orient"
                    type_name = "pair/-/-/%s/%s" % (_orient, _const)
                                        
                    # creating pair
                    scg_obj = scg_alphabet.createSCgPair(type_name)
                    scg_obj._setScAddr(obj)
                    sc_scg[str(obj.this)] = scg_obj
                    
                    # append additional sc-addrs
                    for addr in res[0]:
                        if addr.this != obj.this:
                            scg_obj.additionalScAddrs.append(addr)
                    
                    #scg_obj.setWasInMemory(True)
                    
                    # appending to sheet
                    #sheet.addChild(scg_obj)
                    result_objs.append(scg_obj)
                    
                    process_pairs.append( (res[0][2], res[0][0], res[0][4]) )
                
        
        # translating objects
        for obj in trans_objs:
            if sc_scg.has_key(str(obj.this)) or (str(obj.this) in ignore_list):
                continue
            
            _type = object_types[str(obj.this)]
                        
            # checking pairs
            if _type & sc.SC_ARC:
                beg = session.get_beg(obj)
                end = session.get_end(obj)
                if (beg is not None) and (end is not None):
                    if (str(beg.this) in list_of_addrs) and (str(end.this) in list_of_addrs):
                        process_pairs.append((obj, beg, end)) 
                    else:
                        continue    # skipping dead (haven't begin or end element) pairs
                         
                    
            # translating sc-element to scg-object
            scg_obj = translate_obj(session, obj, object_types[str(obj.this)])
            
            sc_scg[str(obj.this)] = scg_obj
            # translating identificators
#            idtf = session.get_idtf(obj)
#            if not sc_utils.isSystemId(idtf):
#                scg_obj.setText(idtf)
            
            #scg_obj.setWasInMemory(True)
            
            # adding to window
            #sheet.addChild(scg_obj)
            result_objs.append(scg_obj)
            
        # linking pairs
        for pair, beg, end in process_pairs:
            scg_pair = sc_scg[str(pair.this)]
            if sc_scg.has_key(str(beg.this)) and sc_scg.has_key(str(end.this)):
                scg_beg = sc_scg[str(beg.this)]
                scg_end = sc_scg[str(end.this)]
                assert scg_end is not None and scg_beg is not None
                scg_pair.setBegin(scg_beg)
                scg_pair.setEnd(scg_end)
            
        # get all pairs from sheet and link them to begin and end object if they are present in translated set
        for obj in sheet.getChilds():
            if isinstance(obj, scg_objects.SCgPair):
                addr = obj._getScAddr()
                if addr is not None:
                    beg_addr = session.get_beg(addr)
                    end_addr = session.get_end(addr)
                    
                    if (beg_addr is not None) and sc_scg.has_key(str(beg_addr.this)):
                        obj.setBegin(sc_scg[str(beg_addr.this)])
                    if (end_addr is not None) and sc_scg.has_key(str(end_addr.this)):
                        obj.setEnd(sc_scg[str(end_addr.this)])
        
        
        # append into sheet
        for obj in result_objs:
            obj.setWasInMemory(True)
            sheet.addChild(obj)
        
        return errors
       
            
예제 #2
0
def searchBinPairsFromNode(_session, _beg, _const):
    """Searches full data for all binary orient pairs with specified begin element
    @param _session:    session to work with
    @type _session: MThreadSession
    @param _beg:    begin element of pair
    @type _beg:    sc_global_addr
    @param _const:    pair constant type
    @type _const:    int
        
    @return:    list of tuple(rel, end)
    @rtype:     list
    
    @raise RuntimeWarning:    if structure of binary orient pair is wrong
    
    template:
                x ? rel
                |
                v
        O =============> ? (node to find)
     _beg                
    """

    res = []
    # finding sheaf node
    it1 = _session.create_iterator(
        _session.sc_constraint_new(sc_constants.CONSTR_5_a_a_f_a_f,
                                   sc.SC_NODE | _const,
                                   sc.SC_ARC | sc.SC_POS | _const, _beg,
                                   sc.SC_ARC | sc.SC_POS | _const,
                                   keynodes.n_1), True)
    while not it1.is_over():
        # check if founded node is sheaf
        sheaf_node = it1.value(0)
        if sc_utils.isNodeSheaf(_session, sheaf_node):
            #finding relation node
            list1 = searchPosArcTo(_session, sheaf_node)
            rel_node = None
            for el in list1:
                idtf = str(sc_utils.cp1251ToUtf8(_session.get_idtf(el)))
                if not idtf.__eq__("stype_sheaf"):
                    rel_node = el

            # finding end node
            it2 = _session.create_iterator(
                _session.sc_constraint_new(sc_constants.CONSTR_5_f_a_a_a_f,
                                           sheaf_node,
                                           sc.SC_ARC | sc.SC_POS | _const,
                                           sc.SC_NODE,
                                           sc.SC_ARC | sc.SC_POS | _const,
                                           keynodes.n_2), True)
            end_el = None
            while not it2.is_over():
                if end_el is None:
                    end_el = it2.value(2)
                else:
                    raise RuntimeWarning("Invalid binary orient pair")
                it2.next()

            if end_el is not None: res.append((rel_node, end_el))

        it1.next()

    return res
예제 #3
0
    def translate_impl(self, _input, _output):
        """Translator implementation
        @param _input:    input data set
        @type _input:    sc_global_addr
        @param _output:    output window (must be created)
        @type _output:    sc_global_addr
        
        @return: list of errors each element of list is a tuple(object, error)
        @rtype: list
        """
        errors = []
        
        # FIXME:    think about multiply windows for one sc-element
        objs = objects.ScObject._sc2Objects(_output)
        
        assert len(objs) > 0
        sheet = objs[0]
        assert type(sheet) is objects.ObjectSheet
        
        session = core.Kernel.session()
        
        trans_objs = []
        
        
        # creating list of element to translate
        it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a,
                                                               _input,
                                                               sc.SC_A_CONST | sc.SC_POS,
                                                               0), True)
        list_of_addrs = []
        while not it.is_over():
            trans_objs.append(it.value(2))
            list_of_addrs.append(str(it.value(2).this))
            it.next()
            
        # getting objects on sheet
        childs = sheet.getChilds()
        sc_scg = {}
        object_types = {}
        for obj in childs:
            addr = obj._getScAddr()
            if addr is not None:
                s_addr = str(addr.this)    
                sc_scg[s_addr] = obj
                object_types[s_addr] = session.get_type(addr)
                list_of_addrs.append(s_addr)
        
        ignore_list = []
        process_pairs = []

        # translating binary and noorient pairs and store types
        for obj in trans_objs:
            _type = session.get_type(obj)
            object_types[str(obj.this)] = _type
            
            if str(obj.this) in ignore_list: 
                continue
            
            # checking for sheaf nodes
            if _type & sc.SC_NODE and sc_utils.isNodeSheaf(session, obj):
                res = get_pair_binary(session, obj, list_of_addrs)
                if res is not None:
                    for idx in xrange(len(res[0])):
                        if idx != 0 and idx != 4:
                            ignore_list.append(str(res[0][idx].this))
                        
                    #ignore_list.extend(res[0])
                    # creating pair
                    _const = "const"
                    if _type & sc.SC_VAR:
                        _const = "var"
                    elif _type & sc.SC_METAVAR:
                        _const = "meta"
                    
                    _orient = "-"
                    if res[1]:
                        _orient = "orient"
                    type_name = "pair/-/-/%s/%s" % (_orient, _const)
                                        
                    # creating pair
                    scg_obj = scg_alphabet.createSCgPair(type_name)
                    scg_obj._setScAddr(obj)
                    sc_scg[str(obj.this)] = scg_obj
                    
                    # append additional sc-addrs
                    for addr in res[0]:
                        if addr.this != obj.this:
                            scg_obj.additionalScAddrs.append(addr)
                    
                    scg_obj.setWasInMemory(True)
                    # appending to sheet
                    sheet.addChild(scg_obj)
                    
                    process_pairs.append( (res[0][2], res[0][0], res[0][4]) )
                
        
        # translating objects
        for obj in trans_objs:
            if sc_scg.has_key(str(obj.this)) or (str(obj.this) in ignore_list):
                continue
            
            _type = object_types[str(obj.this)]
                        
            # checking pairs
            if _type & sc.SC_ARC:
                beg = session.get_beg(obj)
                end = session.get_end(obj)
                if (beg is not None) and (end is not None):
                    if (str(beg.this) in list_of_addrs) and (str(end.this) in list_of_addrs):
                        process_pairs.append((obj, beg, end)) 
                    else:
                        continue    # skipping dead (haven't begin or end element) pairs
                         
                    
            # translating sc-element to scg-object
            scg_obj = translate_obj(session, obj, object_types[str(obj.this)])
            
            sc_scg[str(obj.this)] = scg_obj
            # translating identificators
            idtf = session.get_idtf(obj)
            if not sc_utils.isSystemId(idtf):
                scg_obj.setText(idtf)
            
            scg_obj.setWasInMemory(True)
            
            # adding to window
            sheet.addChild(scg_obj)
            
        # linking pairs
        for pair, beg, end in process_pairs:
            scg_pair = sc_scg[str(pair.this)]
            if sc_scg.has_key(str(beg.this)) and sc_scg.has_key(str(end.this)):
                scg_beg = sc_scg[str(beg.this)]
                scg_end = sc_scg[str(end.this)]
                assert scg_end is not None and scg_beg is not None
                scg_pair.setBegin(scg_beg)
                scg_pair.setEnd(scg_end)
            
        # get all pairs from sheet and link them to begin and end object if they are present in translated set
        for obj in sheet.getChilds():
            if isinstance(obj, scg_objects.SCgPair):
                addr = obj._getScAddr()
                if addr is not None:
                    beg_addr = session.get_beg(addr)
                    end_addr = session.get_end(addr)
                    
                    if (beg_addr is not None) and sc_scg.has_key(str(beg_addr.this)):
                        obj.setBegin(sc_scg[str(beg_addr.this)])
                    if (end_addr is not None) and sc_scg.has_key(str(end_addr.this)):
                        obj.setEnd(sc_scg[str(end_addr.this)])
        
        return errors
       
            
예제 #4
0
def searchBinPairsFromNode(_session, _beg, _const):
    """Searches full data for all binary orient pairs with specified begin element
    @param _session:    session to work with
    @type _session: MThreadSession
    @param _beg:    begin element of pair
    @type _beg:    sc_global_addr
    @param _const:    pair constant type
    @type _const:    int
        
    @return:    list of tuple(rel, end)
    @rtype:     list
    
    @raise RuntimeWarning:    if structure of binary orient pair is wrong
    
    template:
                x ? rel
                |
                v
        O =============> ? (node to find)
     _beg                
    """
    
    res = []
    # finding sheaf node
    it1 = _session.create_iterator(_session.sc_constraint_new(sc_constants.CONSTR_5_a_a_f_a_f,
                                                           sc.SC_NODE | _const,
                                                           sc.SC_ARC | sc.SC_POS | _const,
                                                           _beg,
                                                           sc.SC_ARC | sc.SC_POS | _const,
                                                           keynodes.n_1), True)
    while not it1.is_over():
        # check if founded node is sheaf
        sheaf_node = it1.value(0)
        if sc_utils.isNodeSheaf(_session, sheaf_node):
            #finding relation node
            list1 = searchPosArcTo(_session, sheaf_node)
            rel_node = None
            for el in list1:
                idtf = str(sc_utils.cp1251ToUtf8(_session.get_idtf(el)))
                if not idtf.__eq__("stype_sheaf"):
                    rel_node=el
                                
            # finding end node
            it2 = _session.create_iterator(_session.sc_constraint_new(sc_constants.CONSTR_5_f_a_a_a_f,
                                                                   sheaf_node,
                                                                   sc.SC_ARC | sc.SC_POS | _const,
                                                                   sc.SC_NODE,
                                                                   sc.SC_ARC | sc.SC_POS | _const,
                                                                   keynodes.n_2), True)
            end_el = None
            while not it2.is_over():
                if end_el is None:
                    end_el = it2.value(2)
                else:
                    raise RuntimeWarning("Invalid binary orient pair")
                it2.next()
                
            if end_el is not None:   res.append((rel_node, end_el))
            
        it1.next()
   
    return res