def test_remove_feature(temp_device, feature_dict, layer_dict): temp_device.add_layer(Layer(json_data=layer_dict)) feature = Feature(json_data=feature_dict, device_ref=temp_device) temp_device.add_feature(feature) temp_device.remove_feature(feature.ID) assert feature not in temp_device.features assert temp_device.G.has_node(feature.ID) is False
def test_remove_connection(temp_device, layer_dict, component_dict): temp_device.add_layer(Layer(json_data=layer_dict)) component1 = Component(json_data=component_dict, device_ref=temp_device) temp_device.add_component(component1) component2 = Component(json_data=component_dict, device_ref=temp_device) component2.ID = "c2" component2.name = "c2" temp_device.add_component(component2) connection1 = Connection(device_ref=temp_device) connection1.ID = "connection1" source_target = Target() source_target.component = component1.ID source_target.port = "1" connection1.source = source_target sink_target = Target() sink_target.component = component2.ID sink_target.port = "1" connection1.sinks.append(sink_target) temp_device.add_connection(connection1) temp_device.remove_connection(connection1.ID) assert connection1 not in temp_device.connections assert temp_device.G.has_edge(component1.ID, component2.ID) is False
def test_remove_component(temp_device, component_dict, layer_dict): temp_device.add_layer(Layer(json_data=layer_dict)) component = Component(json_data=component_dict, device_ref=temp_device) temp_device.add_component(component) temp_device.remove_component(component.ID) assert component not in temp_device.components assert temp_device.G.has_node(component.ID) is False
def test_to_parchmint_v1_x( device_dict, connection_dict, component_dict, layer_dict, feature_dict, valve1_dict, valve2_dict, ): device = Device() device.name = "dev1" device.xspan = 100000 device.yspan = 50000 device.add_layer(Layer(json_data=layer_dict)) device.add_feature(Feature(json_data=feature_dict, device_ref=device)) device.add_component(Component(json_data=component_dict, device_ref=device)) valve1 = Component(json_data=valve1_dict, device_ref=device) valve2 = Component(json_data=valve2_dict, device_ref=device) device.add_component(valve1) device.add_component(valve2) con1 = Connection(json_data=connection_dict, device_ref=device) device.add_connection(con1) device.map_valve(valve1, con1, ValveType.NORMALLY_OPEN) device.map_valve(valve2, con1, ValveType.NORMALLY_CLOSED) assert device.to_parchmint_v1_x() == device_dict
def parse_from_json(self, json) -> None: """Returns the json dict Returns: dict: dictionary that can be used in json.dumps() """ self.name = json["name"] # First always add the layers for layer in json["layers"]: self.add_layer(Layer(layer)) # Loop through the components for component in json["components"]: self.add_component(Component(component, self)) for connection in json["connections"]: self.add_connection(Connection(connection, self)) if "params" in json.keys(): self.params = Params(json["params"]) if self.params.exists("xspan"): self.xspan = self.params.get_param("xspan") elif self.params.exists("width"): self.xspan = self.params.get_param("width") elif self.params.exists("x-span"): self.xspan = self.params.get_param("x-span") if self.params.exists("yspan"): self.yspan = self.params.get_param("yspan") elif self.params.exists("length"): self.yspan = self.params.get_param("length") elif self.params.exists("y-span"): self.yspan = self.params.get_param("y-span")
def test_connection_to_parchmint_v1_x( layer_dict, connection_target_dict, connection_path_dict, params_dict, feature_dict, connection_dict, ): layer = Layer(json_data=layer_dict) device = Device() device.add_layer(layer) feat = Feature(json_data=feature_dict, device_ref=device) device.add_feature(feat) c = Connection() c.ID = "con1" c.name = "con1" c.source = Target(json_data=connection_target_dict) c.sinks.append(Target(json_data=connection_target_dict)) c.sinks.append(Target(json_data=connection_target_dict)) c.paths.append(ConnectionPath(json_data=connection_path_dict, device_ref=device)) c.paths.append(ConnectionPath(json_data=connection_path_dict, device_ref=device)) c.layer = layer c.entity = "CHANNEL" c.params = Params(json_data=params_dict) assert c.to_parchmint_v1_x() == connection_dict
def feature_dict(params_dict, layer_dict): layer = Layer(json_data=layer_dict) ret = { "id": "feat1", "type": "UNION", "macro": "TYPE1", "layerID": layer.ID, "params": params_dict, } return ret
def component_dict(port_dict, layer_dict, params_dict): layer = Layer(json_data=layer_dict) ret = { "name": "c1", "id": "c1", "entity": "MIXER", "layers": [layer.ID], "ports": [port_dict], "params": params_dict, "x-span": 1000, "y-span": 5000, } return ret
def test_get_connections_for_edge(temp_device, layer_dict, component_dict): temp_device.add_layer(Layer(json_data=layer_dict)) component1 = Component(json_data=component_dict, device_ref=temp_device) temp_device.add_component(component1) component2 = Component(json_data=component_dict, device_ref=temp_device) component2.ID = "c2" component2.name = "c2" temp_device.add_component(component2) connection1 = Connection(device_ref=temp_device) connection1.ID = "connection1" source_target = Target() source_target.component = component1.ID source_target.port = "1" connection1.source = source_target sink_target = Target() sink_target.component = component2.ID sink_target.port = "1" connection1.sinks.append(sink_target) temp_device.add_connection(connection1) connection2 = Connection(device_ref=temp_device) connection2.ID = "connection2" source_target = Target() source_target.component = component1.ID source_target.port = "2" connection2.source = source_target sink_target = Target() sink_target.component = component2.ID sink_target.port = "2" connection2.sinks.append(sink_target) temp_device.add_connection(connection2) assert temp_device.get_connections_for_edge(component1, component2) == [ connection1, connection2, ] assert temp_device.get_connections_for_edge(component2, component1) == []
def test_add_connection(temp_device, component_dict, pathless_connection_dict, connection_dict, layer_dict): # Ideal scenario for this test, has all the components already present in the graph before inserting the connection temp_device.add_layer(Layer(json_data=layer_dict)) component1 = Component(json_data=component_dict, device_ref=temp_device) component2 = Component(json_data=component_dict, device_ref=temp_device) component2.ID = "c2" component2.name = "c2" connection1 = Connection(json_data=pathless_connection_dict, device_ref=temp_device) connection1.ID = "connection1" source_target = Target() source_target.component = component1.ID source_target.port = "1" connection1.source = source_target sink_target = Target() sink_target.component = component2.ID sink_target.port = "1" connection1.sinks.append(sink_target) # First test to see if this raises an exception when there are no source and sink components added to the device with pytest.raises(Exception): temp_device.add_connection(connection1) # Now add the source and see if it raises an exception with pytest.raises(Exception): temp_device.add_component(component1) temp_device.add_connection(connection1) # Now add the sink and see if it goes through without raising an exception temp_device.add_component(component2) temp_device.add_connection(connection1) assert connection1 in temp_device.connections assert temp_device.G.has_edge(component1.ID, component2.ID)
def parse_from_json(self, json_data) -> None: """Returns the json dict Returns: dict: dictionary that can be used in json.dumps() """ self.name = json_data["name"] # First always add the layers if "layers" in json_data.keys(): for layer in json_data["layers"]: self.add_layer(Layer(layer)) else: print("no layers found") # Loop through the components if "components" in json_data.keys(): for component in json_data["components"]: self.add_component(Component(json_data=component, device_ref=self)) else: print("no components found") if "connections" in json_data.keys(): for connection in json_data["connections"]: self.add_connection(Connection(json_data=connection, device_ref=self)) else: print("no connections found") if "params" in json_data.keys(): self.params = Params(json_data=json_data["params"]) if self.params.exists("xspan"): self.xspan = self.params.get_param("xspan") elif self.params.exists("width"): self.xspan = self.params.get_param("width") elif self.params.exists("x-span"): self.xspan = self.params.get_param("x-span") if self.params.exists("yspan"): self.yspan = self.params.get_param("yspan") elif self.params.exists("length"): self.yspan = self.params.get_param("length") elif self.params.exists("y-span"): self.yspan = self.params.get_param("y-span") else: print("no params found") if "valveMap" in json_data.keys(): valve_map = json_data["valveMap"] for key, value in valve_map.items(): self._valve_map[self.get_component(key)] = self.get_connection(value) if "valveTypeMap" in json_data.keys(): valve_type_map = json_data["valveTypeMap"] for key, value in valve_type_map.items(): if value is ValveType.NORMALLY_OPEN: self._valve_type_map[ self.get_component(key) ] = ValveType.NORMALLY_OPEN else: self._valve_type_map[ self.get_component(key) ] = ValveType.NORMALLY_CLOSED
def layer(layer_dict): return Layer(json_data=layer_dict)
def test_add_feature(temp_device, feature_dict, layer_dict): temp_device.add_layer(Layer(json_data=layer_dict)) feature = Feature(json_data=feature_dict, device_ref=temp_device) temp_device.add_feature(feature) assert feature in temp_device.features