Esempio n. 1
0
    def load_component(self,
                       text,
                       start_index=0,
                       canvas_id=None,
                       graph_id=None):
        next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
            text, start_index=start_index)

        if next_symbol != "component":
            print("ERROR")
            exit()

        component_specifications = CreationComponentSpecifications()
        component_specifications.canvas_id = canvas_id
        component_specifications.graph_id = graph_id
        for key, value in attributes.items():
            component_specifications.add(key, value)

        component = self.component_repository.create(component_specifications)

        value_lines = {}
        current_value_line_id = None
        next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
            text, start_index=pointer)
        while next_symbol != "/component":
            if next_symbol == "mark":
                socket_name = attributes["socket"]
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
                    text, start_index=pointer)
                component.place_mark(next_symbol, socket_name)
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
                    text, start_index=pointer)
            elif next_symbol == "socket":
                socket_type = attributes["type"]
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
                    text, start_index=pointer)
                if socket_type == "in":
                    in_socket = CreationComponentInSocket(
                        component, next_symbol)
                    component.add_in_socket(in_socket)
                elif socket_type == "out":
                    out_socket = CreationComponentOutSocket(
                        component, next_symbol)
                    component.add_out_socket(out_socket)
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
                    text, start_index=pointer)
            elif current_value_line_id is None:
                current_value_line_id = next_symbol
                current_value_line_attributes = attributes
                if not current_value_line_id in value_lines:
                    value_lines[current_value_line_id] = []
            elif next_symbol == "/" + current_value_line_id:
                current_value_line_id = None
            else:
                value_lines[current_value_line_id] += [
                    (next_symbol, current_value_line_attributes)
                ]

            next_symbol, attributes, pointer = self.xml_helper.pop_symbol(
                text, start_index=pointer)

        for key, value in value_lines.items():
            component.add_value_lines(key, value)

        return component, pointer
Esempio n. 2
0
    def load_edge(self, text, start_index, graph_id=None):
        next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=start_index)

        if next_symbol != "edge":
            print("ERROR")
            exit()

        source_socket = None
        target_socket = None

        cast = None
        if "cast" in attributes:
            cast = attributes["cast"]

        dropout_rate = None

        while next_symbol != "/edge":
            next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)
            if next_symbol == "source":
                socket_name = attributes["socket"]
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)

                component_name = next_symbol
                component_spec = CreationComponentSpecifications()
                component_spec.graph_id = graph_id
                component_spec.name = component_name
                components = self.component_repository.get(component_spec)

                if len(components) == 0:
                    raise ComponentNotFoundException("Attempted edge creation with undeclared source component " + component_name)

                component = components[0]

                try:
                    source_socket = component.get_out_socket(socket_name)
                except SocketNotFoundException:
                    raise SocketNotFoundException("Attempted edge creation with non-existant socket "+socket_name+" for source component "+component_name)

                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)
            elif next_symbol == "target":
                socket_name = attributes["socket"]
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)

                component_name = next_symbol
                component_spec = CreationComponentSpecifications()
                component_spec.graph_id = graph_id
                component_spec.name = component_name
                components = self.component_repository.get(component_spec)

                if len(components) == 0:
                    raise ComponentNotFoundException("Attempted edge creation with undeclared target component " + component_name)

                component = components[0]

                try:
                    target_socket = component.get_in_socket(socket_name)
                except SocketNotFoundException:
                    raise SocketNotFoundException("Attempted edge creation with non-existant socket "+socket_name+" for target component "+component_name)

                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)
            elif next_symbol == "dropout":
                dropout_attributes = attributes
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)
                dropout_rate = next_symbol
                next_symbol, attributes, pointer = self.xml_helper.pop_symbol(text, start_index=pointer)

        edge = self.graph_repository.add_edge(source_socket, target_socket)

        if dropout_rate is not None:
            edge.add_value_line("dropout_rate", (dropout_rate, dropout_attributes))

        if cast is not None:
            edge.add_value_line("cast", (cast, {}))

        return edge, pointer