Ejemplo n.º 1
0
class GraphPrototypeRepository:

    defined_graph_prototypes = None

    def __init__(self):
        self.defined_graph_prototypes = ObservableDict()

    def create(self, graph_prototype):
        self.defined_graph_prototypes.append(graph_prototype)

    def update(self, graph_prototype):
        self.defined_graph_prototypes.update(graph_prototype)

    def delete(self, graph_prototype_specifications):
        graph_prototypes = self.get(graph_prototype_specifications)
        for graph_prototype in graph_prototypes:
            self.defined_graph_prototypes.delete(graph_prototype)

    def get(self, specifications):
        elements = []
        for prototype in self.defined_graph_prototypes.elements.values():
            if specifications.matches(prototype):
                elements.append(prototype)
        return elements

    def get_all(self):
        return self.defined_graph_prototypes.elements.values()
Ejemplo n.º 2
0
class SocketRepository:

    defined_sockets = None

    def __init__(self, identifier_factory):
        self.identifier_factory = identifier_factory
        self.defined_sockets = ObservableDict()

    def create_socket(self, specification):
        socket_description = specification.description
        parent_component = specification.parent_component
        socket_type = specification.socket_type

        socket_identifier = socket_description['name']
        component_uid = parent_component.get_unique_identifier()
        socket_name = component_uid + ":" + socket_identifier

        socket_uid = socket_name

        if socket_type == "in":
            socket = InSocketModel(socket_uid)
        else:
            socket = OutSocketModel(socket_uid)

        socket.parent_component = parent_component
        socket.description = socket_description

        self.defined_sockets.append(socket)
        return socket

    def update_socket(self, socket):
        self.defined_sockets.update(socket)
Ejemplo n.º 3
0
class ToolboxItemRepository:

    component_dir = '/home/michael/Projects/Mindblocks/packages'

    defined_toolbox_prototypes = None

    def __init__(self):
        self.defined_toolbox_prototypes = ObservableDict()

    def get(self, specifications):
        for element in self.defined_toolbox_prototypes.elements.values():
            if specifications.matches(element):
                return element

    def get_prototypes(self, specifications):
        if specifications.package_manifest is not None:
            return self.get_prototypes_by_package_manifest(
                specifications.package_manifest)

    def load_prototypes(self, package_manifest):
        component_files = package_manifest['files']
        module_path = package_manifest['module_name']

        prototypes = []

        for file_manifest in component_files:
            os_path = os.path.join(package_manifest['path'],
                                   file_manifest['name'])
            class_path = module_path + "." + file_manifest['name'].split(
                ".")[0]
            loaded_file = importlib.machinery.SourceFileLoader(
                "module", os_path).load_module()

            for component_manifest in file_manifest['components']:
                component_manifest['file_path'] = class_path
                component_manifest['package'] = package_manifest['package']
                component_class = getattr(loaded_file,
                                          component_manifest['name'])

                prototype = ToolboxItemModel()
                prototype.name = component_manifest['name']
                prototype.package = package_manifest['package']
                prototype.prototype_class = component_class
                prototype.attributes = component_class.default_attributes
                prototypes.append(prototype)

                self.defined_toolbox_prototypes.append(prototype)

        return prototypes
class ComputationUnitRepository:

    computation_units = None
    identifier_factory = None

    def __init__(self, identifier_factory):
        self.computation_units = ObservableDict()
        self.identifier_factory = identifier_factory

    def create(self, computation_unit_model):
        identifier = self.identifier_factory.get_next_identifier(
            name_string='computation_unit')
        computation_unit_model.set_unique_identifier(identifier)

        self.computation_units.append(computation_unit_model)

    def get(self, computation_unit_specifications):
        models = []
        for model in self.computation_units.get_elements():
            if computation_unit_specifications.matches(model):
                models.append(model)
        return models
Ejemplo n.º 5
0
class ComponentRepository:

    defined_components = None

    def __init__(self, identifier_factory, socket_repository,
                 module_repository, xml_helper):
        self.identifier_factory = identifier_factory
        self.socket_repository = socket_repository
        self.xml_helper = xml_helper
        self.module_repository = module_repository
        self.defined_components = ObservableDict()

    def create_component_with_sockets(self, component):
        prototype = self.module_repository.get_prototype_by_id(
            component.prototype_id)

        identifier = self.identifier_factory.get_next_identifier(
            name_string=prototype.get_name())
        component.set_unique_identifier(identifier)

        for in_socket_description in component.get_default_in_sockets():
            socket_specification = SocketSpecification()
            socket_specification.parent_component = component
            socket_specification.socket_type = "in"
            socket_specification.description = in_socket_description
            component.add_in_socket(
                self.socket_repository.create_socket(socket_specification))

        for out_socket_description in component.get_default_out_sockets():
            socket_specification = SocketSpecification()
            socket_specification.parent_component = component
            socket_specification.socket_type = "out"
            socket_specification.description = out_socket_description
            component.add_out_socket(
                self.socket_repository.create_socket(socket_specification))

        self.defined_components.append(component)
        return component

    def update_component(self, component):
        self.defined_components.update(component)

    def save_component(self, component, outfile):
        name = component.get_unique_identifier()
        print(self.xml_helper.get_header("component", {"name": name},
                                         indentation=2),
              file=outfile)

        print(self.xml_helper.get_header("class", indentation=3) +
              component.module_name + self.xml_helper.get_footer("class"),
              file=outfile)
        print(self.xml_helper.get_header("package", indentation=3) +
              component.module_package + self.xml_helper.get_footer("package"),
              file=outfile)

        for attribute in component.attributes:
            print(self.xml_helper.get_header("attribute", {"key": attribute},
                                             indentation=3) +
                  component.attributes[attribute] +
                  self.xml_helper.get_footer("attribute"),
                  file=outfile)

        for in_socket in component.get_in_sockets():
            out_socket_names = [
                e.origin.get_unique_identifier()
                for e in in_socket.get_edges_in()
            ]
            if out_socket_names:
                in_socket_name = in_socket.description['name']
                socket_string = self.xml_helper.get_header(
                    "socket", {"name": in_socket_name}, indentation=3)
                socket_string += ",".join(out_socket_names)
                socket_string += self.xml_helper.get_footer("socket")
                print(socket_string, file=outfile)

        print(self.xml_helper.get_footer("component", indentation=2),
              file=outfile)

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

        class_symbol = ""
        package_symbol = ""
        component_attributes = {}

        #TODO: This is a code smell
        edges = {}

        while symbol != "/component":
            symbol, attributes, next_index = self.xml_helper.pop_symbol(
                lines, start_index=next_index)
            if symbol == "class":
                class_symbol, _, next_index = self.xml_helper.pop_symbol(
                    lines, start_index=next_index, expect_value=True)
            elif symbol == "package":
                package_symbol, _, next_index = self.xml_helper.pop_symbol(
                    lines, start_index=next_index, expect_value=True)
            elif symbol == "attribute":
                value, _, next_index = self.xml_helper.pop_symbol(
                    lines, start_index=next_index, expect_value=True)
                component_attributes[attributes['key']] = value
            elif symbol == "socket":
                target, _, next_index = self.xml_helper.pop_symbol(
                    lines, start_index=next_index, expect_value=True)
                edges[attributes['name']] = target

        toolbox_item_specification = PrototypeSpecifications()
        toolbox_item_specification.package = package_symbol
        toolbox_item_specification.name = class_symbol

        #TODO: Stupid hack
        if 'canvas' in component_attributes:
            toolbox_item_specification.canvas = component_attributes['canvas']

        toolbox_item = self.module_repository.get_prototype(
            toolbox_item_specification)

        component = toolbox_item.prototype_class(None)
        component.prototype_id = toolbox_item.get_unique_identifier()
        component.update_attributes(toolbox_item.get_attributes())
        component.update_attributes(component_attributes)

        component = self.create_component_with_sockets(component)

        # TODO: Load all graphs, then load all components

        return component, next_index, edges
Ejemplo n.º 6
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.º 7
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
Ejemplo n.º 8
0
class ModuleRepository:

    modules = None
    component_dir = '/home/michael/Projects/Mindblocks/packages'

    prototype_repository = None
    graph_prototype_repository = None

    def __init__(self, prototype_repository, graph_prototype_repository):
        self.prototype_repository = prototype_repository
        self.graph_prototype_repository = graph_prototype_repository
        self.modules = ObservableDict()

    def load_basic_modules(self):
        for package_name in self.get_all_package_names():
            module = self.load_basic_module_by_package_name(package_name)
            self.modules.append(module)

    def load_basic_module_by_package_name(self, package_name):
        manifest = self.load_package_manifest(package_name)
        module = ModuleModel(manifest['name'])

        prototypes = self.prototype_repository.load_prototypes(manifest)
        module.extend_prototypes(prototypes)

        return module

    def get_prototype_by_id(self, id):
        for module in self.get_basic_modules(None):
            for prototype in module.components:
                print(prototype.get_unique_identifier())
                print(id)
                if prototype.get_unique_identifier() == id:
                    print(prototype)
                    return prototype

        print("NOT FOUND")

        for module in self.get_canvas_modules(None):
            for prototype in module.components:
                if prototype.get_unique_identifier() == id:
                    return prototype

        return None

    def get_prototype(self, specifications):
        basic_prototype = self.prototype_repository.get(specifications)

        if basic_prototype is not None:
            return basic_prototype

        graph_prototype_specifications = GraphPrototypeSpecifications()
        graph_prototype_specifications.graph_identifier = specifications.name
        graph_prototype_specifications.canvas_identifier = specifications.canvas

        graph_prototype = self.graph_prototype_repository.get(graph_prototype_specifications)[0]
        return graph_prototype

    def get_basic_modules(self, specifications):
        return list(self.modules.elements.values())

    def get_canvas_modules(self, specifications):
        prototypes = self.graph_prototype_repository.get_all()
        modules = {}

        for prototype in prototypes:
            if prototype.canvas_identifier not in modules:
                modules[prototype.canvas_identifier] = ModuleModel(prototype.canvas_identifier)

            modules[prototype.canvas_identifier].components.append(prototype)

        return list(modules.values())

    '''
    Logic for loading modules:
    '''
    def get_all_package_names(self):
        all_subitems = os.listdir(self.component_dir)
        filtered_subitems = [item for item in all_subitems if self.filter_name(item)]

        absolute_subitems = [(d,os.path.join(self.component_dir, d)) for d in filtered_subitems]
        subfolders = [d[0] for d in absolute_subitems if os.path.isdir(d[1])]

        return subfolders

    def filter_name(self, name):
        if name.startswith('.') or name.startswith('_'):
            return False

        return True

    def load_manifest(self, path):
        manifest_path = os.path.join(path, 'manifest.json')
        with open(manifest_path) as data_file:
            manifest = json.load(data_file)
            manifest['path'] = path
            return manifest

    def load_package_manifest(self, package_name):
        manifest_path = os.path.join(self.component_dir, package_name)
        manifest = self.load_manifest(manifest_path)
        manifest['package'] = package_name
        return manifest