Example #1
0
def apply_rules(alpaca, playfield, x, y, rules, class_decls, checked_classes=None, verbose=False):
    """Given a set of rules and a set of superclasses (for a given state or
    class which is not given), try the rules; if none of them apply,
    recursively apply this function with the rules and superclasses for each
    given superclass.

    """
    if checked_classes is None:
        checked_classes = set()
    new_state_id = eval_rules(alpaca, playfield, x, y, rules, verbose=verbose)
    if new_state_id is not None:
        return new_state_id
    for class_decl in class_decls:
        assert class_decl.type == 'ClassDecl'
        class_id = class_decl.id
        if class_id in checked_classes:
            continue
        class_ast = find_class_defn(alpaca, class_id)
        rules = class_ast.rules
        classes = class_ast.classes
        checked_classes.add(class_id)
        new_state_id = apply_rules(
            alpaca, playfield, x, y, rules, classes,
            checked_classes=checked_classes, verbose=verbose
        )
        if new_state_id is not None:
            return new_state_id
    return None
Example #2
0
def evolve_playfield(playfield, new_pf, alpaca):
    # XXX TODO + 1, - 1's in here should reflect the maximum
    # neighbourhood used by any rule
    if playfield.min_y is None:
        return
    y = playfield.min_y - 1
    while y <= playfield.max_y + 1:
        x = playfield.min_x - 1
        while x <= playfield.max_x + 1:
            state_id = playfield.get(x, y)
            #print "state at (%d,%d): %s" % (x, y, state_id)
            state_ast = find_state_defn(alpaca, state_id)
            #print " => %r" % state_ast
            new_state_id = eval_rules(alpaca, playfield, x, y, state_ast.children[3])
            class_decls = state_ast.children[2]
            assert class_decls.type == 'MembershipDecls'
            for class_decl in class_decls.children:
                assert class_decl.type == 'ClassDecl'
                if new_state_id is not None:
                    break
                class_id = class_decl.value
                class_ast = find_class_defn(alpaca, class_id)
                new_state_id = eval_rules(alpaca, playfield, x, y, class_ast.children[0])
            if new_state_id is None:
                new_state_id = playfield.get(x, y)
            #print "new state: %s" % new_state_id
            new_pf.set(x, y, new_state_id)
            x += 1
        y += 1
Example #3
0
def apply_rules(alpaca, playfield, x, y, rules, class_decls):
    """Given a set of rules and a set of superclasses (for a given state or
    class which is not given), try the rules; if none of them apply,
    recursively apply this function with the rules and superclasses for each
    given superclass.

    """
    new_state_id = eval_rules(alpaca, playfield, x, y, rules)
    if new_state_id is not None:
        return new_state_id
    assert class_decls.type == "MembershipDecls"
    for class_decl in class_decls.children:
        assert class_decl.type == "ClassDecl"
        class_id = class_decl.value
        class_ast = find_class_defn(alpaca, class_id)
        rules = class_ast.children[0]
        classes = class_ast.children[1]
        new_state_id = apply_rules(alpaca, playfield, x, y, rules, classes)
        if new_state_id is not None:
            return new_state_id
    return None
Example #4
0
def apply_rules(alpaca,
                playfield,
                x,
                y,
                rules,
                class_decls,
                checked_classes=None,
                verbose=False):
    """Given a set of rules and a set of superclasses (for a given state or
    class which is not given), try the rules; if none of them apply,
    recursively apply this function with the rules and superclasses for each
    given superclass.

    """
    if checked_classes is None:
        checked_classes = set()
    new_state_id = eval_rules(alpaca, playfield, x, y, rules, verbose=verbose)
    if new_state_id is not None:
        return new_state_id
    for class_decl in class_decls:
        assert class_decl.type == 'ClassDecl'
        class_id = class_decl.id
        if class_id in checked_classes:
            continue
        class_ast = find_class_defn(alpaca, class_id)
        rules = class_ast.rules
        classes = class_ast.classes
        checked_classes.add(class_id)
        new_state_id = apply_rules(alpaca,
                                   playfield,
                                   x,
                                   y,
                                   rules,
                                   classes,
                                   checked_classes=checked_classes,
                                   verbose=verbose)
        if new_state_id is not None:
            return new_state_id
    return None