Exemplo n.º 1
0
    def _OutputState(self, state, index_as_child, graph_parent,
                     previous_states):
        state_content = list()
        for h in state.event_handlers:
            if h.state_from != h.state_to:
                continue
            event_type = h.event_type
            if event_type == 'Q_ENTRY_SIG':
                event_type = 'entry'
            elif event_type == 'Q_EXIT_SIG':
                event_type = 'exit'
            else:
                assert event_type.endswith('_SIG')
                event_type = event_type[:-len('_SIG')]

            state_content.append(('%s' % event_type) +
                                 ('[%s]' %
                                  h.condition if h.condition else '') + '/')
            for statement in h.statements:
                for line in statement.split('\n'):
                    state_content.append('  ' + line)
            state_content.append('')

        full_node_name = (self.state_name_to_node_name[state.parent_state_name]
                          + ':' if state.parent_state_name != 'global' else
                          '') + 'n%d' % index_as_child
        self.state_name_to_node_name[state.state_name] = full_node_name
        h = 100
        w = 200
        x0 = 259
        y0 = 255
        previous_states_with_name = [
            s for s in previous_states if s.name == state.state_name
        ]
        if previous_states_with_name:
            print('For state %s using cached coordinates' % state.state_name)
            h = previous_states_with_name[0].height * qm.divider
            w = previous_states_with_name[0].width * qm.divider
            x0 = previous_states_with_name[0].x * qm.divider
            y0 = previous_states_with_name[0].y * qm.divider
        else:
            print('For state %s using DUMB coordinates' % state.state_name)

        if state.child_states:
            group_node = create_graphml.add_group_node(
                graph_parent, state.state_name, '\n'.join(state_content),
                full_node_name, h, w, x0, y0)
            parent = create_graphml.create_graph(group_node,
                                                 full_node_name + ':')

            child_index = 0
            for child_state in state.child_states:
                self._OutputState(child_state, child_index, parent,
                                  previous_states)
                child_index += 1
        else:
            create_graphml.add_simple_node(graph_parent, state.state_name,
                                           '\n'.join(state_content),
                                           full_node_name, h, w, x0, y0)
Exemplo n.º 2
0
    def WriteToFile(self,
                    filename: str,
                    previous_filename: Optional[str] = None):
        previous_filename = previous_filename or filename
        previous_states = []
        if os.path.isfile(previous_filename):
            # previous_filename = os.path.splitext(previous_filename)[0]
            previous_states, minx, miny = graphmltoqm.get_states_from_graphml(
                previous_filename)

        graphml_root_node = create_graphml.prepare_graphml()
        self.graph = create_graphml.create_graph(graphml_root_node, 'G')
        create_graphml.add_start_state(self.graph, "n0")
        self.state_name_to_node_name = {}
        self.edge_id = 1

        child_index = 1
        for state in self.state_machine.states['global'].child_states:
            self._OutputState(state, child_index, self.graph, previous_states)
            child_index += 1

        create_graphml.add_edge(
            self.graph, "e0", "n0",
            self.state_name_to_node_name[self.state_machine.initial_state],
            self.state_machine.initial_code, 0, 0, 0, 0, [])

        for state_name in self.state_machine.states:
            state = self.state_machine.states[state_name]
            for h in state.event_handlers:
                if not h.state_to:
                    continue
                if h.state_from == h.state_to:
                    continue
                self._OutputEdge(h)

        create_graphml.get_constructor_code_comment(
            self.graph, 'constructor_code',
            self.state_machine.constructor_code)
        create_graphml.get_constructor_fields_comment(
            self.graph, 'constructor_fields',
            self.state_machine.constructor_fields)
        create_graphml.get_event_fields_comment(
            self.graph, 'event_fields', self.state_machine.event_fields)
        create_graphml.get_h_code_comment(self.graph, "raw_h_code",
                                          self.state_machine.raw_h_code)
        create_graphml.get_state_fields_comment(
            self.graph, "state_fields", self.state_machine.state_fields)

        create_graphml.finish_graphml(graphml_root_node)
        xml_tree = etree.ElementTree(graphml_root_node)
        xml_tree.write(filename, xml_declaration=True, encoding="UTF-8")
Exemplo n.º 3
0
    def _OutputDiagram(self):
        root_node = create_graphml.prepare_graphml()
        self.graph = create_graphml.create_graph(root_node)
        self.node_ids = {}
        self.node_id = 0
        self.edge_id = 0

        for state_name in self.states:
            self._OutputState(self.states[state_name])

        for state_name in self.states:
            state = self.states[state_name]
            for name in state.handlers:
                h = state.handlers[name]
                if not h.target_state_name:
                    continue
                if h.state_name == h.target_state_name:
                    continue
                self._OutputEdge(h)

        create_graphml.finish_graphml(root_node)
        xml_tree = etree.ElementTree(root_node)
        xml_tree.write("test.graphml", xml_declaration=True, encoding="UTF-8")