Beispiel #1
0
class TestNodes(unittest.TestCase):
    def setUp(self):
        state = GameState(game_mode="Herz Solo", offensive_player=1, active=0)
        p_hand = ['EK_', 'EU_', 'G10', 'SA_', 'HK_', 'H7_', 'S7_', 'H10']
        p_id = 0
        self.node = Node(state, p_hand, p_id)
        self.node.add_child("EK_")

    def test_hand(self):
        result = self.node.children["EK_"].p_hand
        expected = set(['EU_', 'G10', 'SA_', 'HK_', 'H7_', 'S7_', 'H10'])
        self.assertEqual(result, expected)
Beispiel #2
0
    def parse_yaml_rules(self, nodelist, base_class=None):
        abstract_nodes = []
        nodes = []

        for node in nodelist:
            # name of the ast class and it's properties as dictionary
            class_name, properties = next(iter(node.items()))

            # no need to process empty nodes
            if properties is None:
                continue

            args = Argument()
            args.url = properties.get('url', None)
            args.class_name = class_name
            args.brief = properties.get('brief', '')
            args.description = properties.get('description', '')

            # yaml file has abstract classes and their subclasses with children as a property
            if 'children' in properties:
                # recursively parse all sub-classes of current abstract class
                child_abstract_nodes, child_nodes = self.parse_yaml_rules(
                    properties['children'], class_name)

                # append all parsed subclasses
                abstract_nodes.extend(child_abstract_nodes)
                nodes.extend(child_nodes)

                # classes like Ast which don't have base class
                # are not added (we print Ast class separately)
                if base_class is not None:
                    args.base_class = base_class
                    node = Node(args)
                    abstract_nodes.append(node)
                    nodes.insert(0, node)
                    if self.debug:
                        print('Abstract {}'.format(node))
            else:
                args.base_class = base_class if base_class else 'Ast'

                # check if we need token for the node
                # todo : we will have token for every node
                args.has_token = LanguageParser.is_token(class_name)

                # name of the node while printing back to NMODL
                args.nmodl_name = properties[
                    'nmodl'] if 'nmodl' in properties else None

                # create tree node and add to the list
                node = Node(args)
                nodes.append(node)

                if self.debug:
                    print('Class {}'.format(node))

                # now process all children specification
                if 'members' in properties:
                    for child in properties['members']:
                        args = self.parse_child_rule(child)
                        node.add_child(args)

        # update the abstract nodes
        for absnode in abstract_nodes:
            for node in nodes:
                if absnode.class_name == node.class_name:
                    node.is_abstract = True
                    break

        return abstract_nodes, nodes