コード例 #1
0
ファイル: intersect.py プロジェクト: g11r/lgr-core
def intersect_lgrs(lgr1, lgr2):
    """
    Compute the intersection of 2 LGRs and returns a valid LGR.

    Note: Ranges have to be expanded before calling this function.

    :param lgr1: First LGR.
    :param lgr2: Second LGR.
    :return: New LGR: intersection of two inputs.
    """
    name = 'Intersection of %s and %s' % (lgr1.name, lgr2.name)

    lgr1.expand_ranges()
    lgr2.expand_ranges()

    # Note: We need to create a copy (copy.deepcopy) for some elements
    # otherwise they could reference the original objects.

    metadata = copy.deepcopy(intersect_metadata(lgr1.metadata, lgr2.metadata))
    lgr = LGR(name=name, metadata=metadata)

    # No need to copy references, they are new objects
    references = intersect_reference_manager(lgr1.reference_manager,
                                             lgr2.reference_manager)
    lgr.reference_manager = references

    first_cps = {c.cp for c in lgr1.repertoire}
    second_cps = {c.cp for c in lgr2.repertoire}

    # No need to copy char, they are new objects
    for cp in set.intersection(first_cps, second_cps):
        char1 = lgr1.get_char(cp)
        char2 = lgr2.get_char(cp)

        intersect_char(lgr, char1, char2)

    (actions, actions_xml) = intersect_actions(lgr1, lgr2)
    lgr.actions = copy.deepcopy(actions)
    lgr.actions_xml = actions_xml

    (rules, rules_xml) = intersect_rules(lgr1, lgr2)
    lgr.rules = copy.deepcopy(rules)
    lgr.rules_xml = rules_xml

    (classes, classes_xml) = intersect_classes(lgr1, lgr2)
    lgr.classes = copy.deepcopy(classes)
    lgr.classes_xml = classes_xml

    return lgr
コード例 #2
0
ファイル: union.py プロジェクト: j-bernard/lgr-core
def union_lgrs(lgr1, lgr2):
    """
    Compute the union of 2 LGRs and returns a valid LGR.

    Note: Ranges have to be expanded before calling this function.

    :param lgr1: First LGR.
    :param lgr2: Second LGR.
    :return: New LGR: union of two inputs.
    """
    name = 'Union of %s and %s' % (lgr1.name, lgr2.name)

    logger.debug("Union of %s", name)

    lgr1.expand_ranges()
    lgr2.expand_ranges()

    # Note: We need to create a copy (copy.deepcopy) for some elements
    # otherwise they could reference the original objects.

    metadata = copy.deepcopy(union_metadata(lgr1.metadata, lgr2.metadata))
    lgr = LGR(name=name, metadata=metadata)

    # No need to copy references, they are new objects
    references = union_reference_manager(lgr1.reference_manager,
                                         lgr2.reference_manager)
    lgr.reference_manager = references

    first_cps = {c.cp for c in lgr1.repertoire}
    second_cps = {c.cp for c in lgr2.repertoire}


    # No need to copy char, they are new objects

    # Compute union of all common code points
    for cp in set.intersection(first_cps, second_cps):
        char1 = lgr1.get_char(cp)
        char2 = lgr2.get_char(cp)

        union_char(lgr, char1, char2)

    # Append all other code points
    for cp in set.difference(first_cps, second_cps):
        char = lgr1.get_char(cp)

        lgr.add_cp(char.cp,
                   comment=char.comment,
                   #ref=char.references,
                   tag=char.tags,
                   when=char.when, not_when=char.not_when)

    for cp in set.difference(second_cps, first_cps):
        char = lgr2.get_char(cp)

        lgr.add_cp(char.cp,
                   comment=char.comment,
                   #ref=char.references,
                   tag=char.tags,
                   when=char.when, not_when=char.not_when)

    (actions, actions_xml) = union_actions(lgr1, lgr2)
    lgr.actions = copy.deepcopy(actions)
    lgr.actions_xml = actions_xml

    (rules, rules_xml) = union_rules(lgr1, lgr2)
    lgr.rules = copy.deepcopy(rules)
    lgr.rules_xml = rules_xml

    (classes, classes_xml) = union_classes(lgr1, lgr2)
    lgr.classes = copy.deepcopy(classes)
    lgr.classes_xml = classes_xml

    return lgr