예제 #1
0
    def _construct_jointjs_json(self):
        self.out_json["cells"] = []

        # Start out in the top left corner until we get a better inital layout
        x_pos = 10
        y_pos = 10
        y_starting_pos = 10

        for component, unit_attrs in self.unit_models.items():
            try:
                self._create_image_jointjs_json(
                    self.out_json, x_pos, y_pos, unit_attrs["name"],
                    icon_mapping(unit_attrs["type"]), unit_attrs["type"],
                    link_position_mapping[unit_attrs["type"]])
            except KeyError as e:
                self._logger.info(
                    f'Unable to find icon for {unit_attrs["type"]}. Using default icon'
                )
                self._create_image_jointjs_json(
                    self.out_json, x_pos, y_pos, unit_attrs["name"],
                    icon_mapping("default"), unit_attrs["type"],
                    link_position_mapping["default"])

            # If x_pos it greater than 700 then start another diagonal line
            if x_pos >= 700:
                x_pos = 100
                y_pos = y_starting_pos
                y_starting_pos += 100
            else:
                x_pos += 100
                y_pos += 100

        for name, ports_dict in self.edges.items():
            umst = self.unit_models[ports_dict["source"]]["type"]  # alias
            dest = ports_dict["dest"]
            if hasattr(ports_dict["source"], "vap_outlet"):
                # TODO Figure out how to denote different outlet types. Need to
                # deal with multiple input/output offsets
                for arc in list(self.arcs.values()):
                    if (self.ports[arc.dest] == dest
                            and arc.source == ports_dict["source"].vap_outlet):
                        source_anchor = "top"
                    else:
                        source_anchor = "bottom"
            else:
                source_anchor = "out"

            # The source_port and dest_port should be replaced by actual names in case there are multiple
            # inlets and outlets between the same two unit models
            source_port = "out"
            dest_port = "in"
            self._create_link_jointjs_json(self.out_json, source_port,
                                           dest_port,
                                           ports_dict["source"].getname(),
                                           dest.getname(), name,
                                           self.labels[name])
    def _construct_jointjs_json(self):
        self.out_json["cells"] = []
        x_pos = 10
        y_pos = 10
        y_starting_pos = 10

        for component, unit_attrs in self.unit_models.items():
            try:
                self.create_image_jointjs_json(
                    self.out_json, x_pos, y_pos, unit_attrs["name"],
                    icon_mapping(unit_attrs["type"]), unit_attrs["type"],
                    link_position_mapping[unit_attrs["type"]])
            except KeyError as e:
                print(
                    f'Unable to find icon for {unit_attrs["type"]}. Using default icon'
                )
                self.create_image_jointjs_json(
                    self.out_json, x_pos, y_pos, unit_attrs["name"],
                    icon_mapping("default"), unit_attrs["type"],
                    link_position_mapping["default"])
            if x_pos >= 700:
                x_pos = 100
                y_pos = y_starting_pos
                y_starting_pos += 100
            else:
                x_pos += 100
                y_pos += 100

        id_counter = 0
        for name, ports_dict in self.edges.items():
            umst = self.unit_models[ports_dict["source"]]["type"]  # alias
            dest = ports_dict["dest"]
            if hasattr(ports_dict["source"], "vap_outlet"):
                # TODO Figure out how to denote different outlet types. Need to
                #  deal with multiple input/output offsets
                for arc in list(self.arcs.values()):
                    if (self.ports[arc.dest] == dest
                            and arc.source == ports_dict["source"].vap_outlet):
                        source_anchor = "top"
                    else:
                        source_anchor = "bottom"
            else:
                source_anchor = "out"

            self.create_link_jointjs_json(self.out_json, "out", "in",
                                          ports_dict["source"].getname(),
                                          dest.getname(), name,
                                          self.labels[name])
예제 #3
0
    def _construct_model_json(self):
        self.out_json["model"]["id"] = self.name
        self.out_json["model"]["unit_models"] = {}
        self.out_json["model"]["arcs"] = {}

        for unit_model in self.unit_models.values():
            unit_name = unit_model["name"]
            unit_type = unit_model["type"]

            performance_contents = {}
            stream_contents = {}
            if unit_name in self.serialized_contents:
                performance_contents = self.serialized_contents[unit_name][
                    "performance_contents"].to_dict('index')
                stream_contents = self.serialized_contents[unit_name][
                    "stream_contents"].to_dict('index')

            self.out_json["model"]["unit_models"][unit_name] = {
                "type": unit_type,
                "image": "/images/icons/" + icon_mapping(unit_type),
                "performance_contents": performance_contents,
                "stream_contents": stream_contents
            }

        for edge, edge_info in self.edges.items():
            self.out_json["model"]["arcs"][edge] = \
                {"source": edge_info["source"].getname(),
                 "dest": edge_info["dest"].getname(),
                 "label": self.labels[edge]}
    def _construct_model_json(self):
        self.out_json["model"]["id"] = self.name
        self.out_json["model"]["unit_models"] = {}
        self.out_json["model"]["arcs"] = {}

        for unit_model in self.unit_models.values():
            self.out_json["model"]["unit_models"][unit_model["name"]] = {
                "type": unit_model["type"],
                "image": "/images/icons/" + icon_mapping(unit_model["type"])
            }

        for edge, edge_info in self.edges.items():
            self.out_json["model"]["arcs"][edge] = \
                {"source": edge_info["source"].getname(),
                 "dest": edge_info["dest"].getname(),
                 "label": self.labels[edge]}
예제 #5
0
def test_create_image_jointjs_json():
    out_json = {"model": {}}
    out_json["cells"] = []
    x_pos = 0
    y_pos = 0
    component_id = "M101"
    component_type = "mixer"
    image = icon_mapping(component_type)
    port_group = {
        "groups": {
            "in": {
                "position": {
                    "name": "left",
                    "args": {
                        "x": 2,
                        "y": 25,
                        "dx": 1,
                        "dy": 1
                    }
                },
                "attrs": {
                    "rect": {
                        "stroke": '#000000',
                        'stroke-width': 0,
                        "width": 0,
                        "height": 0
                    }
                },
                "markup": '<g><rect/></g>'
            }
        },
        "items": [{
            "group": "in",
            "id": "in"
        }, {
            "group": "out",
            "id": "out"
        }]
    }

    fss = FlowsheetSerializer()
    fss.create_image_jointjs_json(out_json, x_pos, y_pos, component_id, image,
                                  component_type, port_group)
    assert out_json == {
        'cells': [{
            'type': 'standard.Image',
            'position': {
                'x': 0,
                'y': 0
            },
            'size': {
                'width': 50,
                'height': 50
            },
            'angle': 0,
            'id': 'M101',
            'z': (1, ),
            'attrs': {
                'image': {
                    'xlinkHref': '/images/icons/mixer.svg'
                },
                'label': {
                    'text': 'M101'
                },
                'root': {
                    'title': 'mixer'
                }
            },
            'ports': {
                "groups": {
                    "in": {
                        "position": {
                            "name": "left",
                            "args": {
                                "x": 2,
                                "y": 25,
                                "dx": 1,
                                "dy": 1
                            }
                        },
                        "attrs": {
                            "rect": {
                                "stroke": '#000000',
                                'stroke-width': 0,
                                "width": 0,
                                "height": 0
                            }
                        },
                        "markup": '<g><rect/></g>'
                    }
                },
                "items": [{
                    "group": "in",
                    "id": "in"
                }, {
                    "group": "out",
                    "id": "out"
                }]
            }
        }],
        "model": {}
    }
예제 #6
0
def test_icon_mapping(test_input, expected):
    assert icon_mapping(test_input) == expected