def filter_rules(rules, nodeset_size_limit):
    """
    Filter rules if their graphs exceed nodeset_size_limit.
    :param rules: Set of Rule objects
    :param nodeset_size_limit: Maximum size of graph node set
    :return: Filtered set of Rule objects
    """

    if nodeset_size_limit > 0:
        # The maximum nodeset size constraint is applied after all rules are created
        # This is to prevent restricting the space of possible rules

        return filter(lambda x: len(x.source_side) <= nodeset_size_limit, rules)

    else:
        return rules