Ejemplo n.º 1
0
class GraphRepository:

    defined_graphs = None
    xml_helper = None

    def __init__(self, identifier_factory, component_repository, xml_helper):
        self.identifier_factory = identifier_factory
        self.xml_helper = xml_helper
        self.component_repository = component_repository
        self.defined_graphs = ObservableDict()

    def define_create_observer(self, observer):
        self.defined_graphs.define_observer(observer, 'append')

    def define_update_observer(self, observer):
        self.defined_graphs.define_observer(observer, 'update')

    def define_delete_observer(self, observer):
        self.defined_graphs.define_observer(observer, 'delete')

    def create(self, graph_model):
        if graph_model.get_unique_identifier() is None:
            identifier = self.identifier_factory.get_next_identifier(
                name_string='graph')
            graph_model.set_unique_identifier(identifier)

        self.defined_graphs.append(graph_model)
        return graph_model

    def add_vertex_to_graph(self, graph, vertex):
        graph.add_vertex(vertex)

    def add_edge_to_graph(self, graph, origin, destination):
        edge = graph.add_edge(origin, destination)
        self.update_graph(graph)
        return edge

    def save_graph(self, graph, outfile):
        name = graph.get_unique_identifier()

        print(self.xml_helper.get_header("graph", {"name": name},
                                         indentation=1),
              file=outfile)
        for component in graph.topological_walk(components_only=True):
            self.component_repository.save_component(component, outfile)
        print(self.xml_helper.get_footer("graph", indentation=1), file=outfile)

    def update_graph(self, graph):
        self.defined_graphs.update(graph)

    def load_next_graph(self, lines, canvas_name, start_index=0):
        symbol, attributes, next_index = self.xml_helper.pop_symbol(
            lines, start_index=start_index)
        name = attributes["name"]

        graph = GraphModel(name)
        graph.canvas_identifier = canvas_name
        self.create(graph)

        symbol, attributes, _ = self.xml_helper.pop_symbol(
            lines, start_index=next_index)
        while symbol != "/graph":
            component, next_index, edges = self.component_repository.load_next_component(
                lines, start_index=next_index)
            graph.add_component_with_sockets(component)

            #TODO: This is a code smell
            for target_socket_name, source in edges.items():
                print(source)
                source = source.split(":")
                source_component = graph.get_vertex_by_name(source[0])
                source_socket_name = source[1]

                source_socket = source_component.get_out_socket_by_name(
                    source_socket_name)
                target_socket = component.get_in_socket_by_name(
                    target_socket_name)

                graph.add_edge(source_socket, target_socket)

            symbol, attributes, _ = self.xml_helper.pop_symbol(
                lines, start_index=next_index)

        self.update_graph(graph)

        symbol, attributes, next_index = self.xml_helper.pop_symbol(
            lines, start_index=next_index)
        return graph, next_index

    def unify_graphs(self, graph_1, graph_2):
        if graph_1 == graph_2:
            return True

        for vertex in graph_2.get_vertices():
            graph_1.append_vertex(vertex)
            vertex.set_graph(graph_1)

        for edge in graph_2.get_edges():
            graph_1.append_edge(edge)
            edge.set_graph(graph_1)

        self.defined_graphs.delete(graph_2)
Ejemplo n.º 2
0
class CanvasRepository:

    defined_canvases = None
    graph_repository = None
    xml_helper = None

    def __init__(self, identifier_factory, graph_repository, xml_helper):
        self.identifier_factory = identifier_factory
        self.graph_repository = graph_repository
        self.xml_helper = xml_helper
        self.defined_canvases = ObservableDict()

    def create_canvas(self):
        identifier = self.identifier_factory.get_next_identifier(
            name_string='canvas')
        canvas = CanvasModel(identifier)
        self.defined_canvases.append(canvas)
        return canvas

    def define_create_observer(self, observer):
        self.defined_canvases.define_observer(observer, 'append')

    def define_update_observer(self, observer):
        self.defined_canvases.define_observer(observer, 'update')

    def get_canvas_by_identifier(self, unique_identifier):
        return self.defined_canvases.get(unique_identifier)

    def get_all_canvases(self):
        return [c[1] for c in self.defined_canvases.as_list()]

    def update_canvas(self, canvas):
        self.defined_canvases.update(canvas)

    def save_all_canvases(self, outfile):
        for canvas in self.defined_canvases.as_list():
            self.save_canvas(canvas[1], outfile)

    def save_canvas(self, canvas, outfile):
        name = canvas.get_unique_identifier()

        print(self.xml_helper.get_header("view", {"name": name}), file=outfile)
        for graph in canvas.get_defined_graphs():
            self.graph_repository.save_graph(graph, outfile)
        print(self.xml_helper.get_footer("view"), file=outfile)

    def load_canvases(self, infile):
        lines = infile.read()
        canvases = []
        index = 0
        while len(lines) - 1 > index:
            canvas, index = self.load_next_canvas(lines, start_index=index)
            canvases.append(canvas)
            self.defined_canvases.append(canvas)

        return canvases

    def load_next_canvas(self, lines, start_index=0):
        symbol, attributes, next_index = self.xml_helper.pop_symbol(
            lines, start_index=start_index)
        name = attributes["name"]

        canvas = CanvasModel(name)

        symbol, attributes, _ = self.xml_helper.pop_symbol(
            lines, start_index=next_index)
        while symbol != "/view":
            graph, next_index = self.graph_repository.load_next_graph(
                lines, name, start_index=next_index)
            canvas.append_graph(graph)
            symbol, attributes, _ = self.xml_helper.pop_symbol(
                lines, start_index=next_index)
            print(symbol)

        self.update_canvas(canvas)

        symbol, attributes, next_index = self.xml_helper.pop_symbol(
            lines, start_index=next_index)
        return canvas, next_index