def testLoadsLanguagesCorrect(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["output"] component_type.languages = ["test_language"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Add" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["left", "right"] component_type.languages = ["tensorflow", "python"] filename = "add_constants.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) spec = CreationComponentSpecifications() spec.name = "constant_1" component = self.setup_holder.component_repository.get(spec)[0] self.assertEqual("test_language", component.language) spec = CreationComponentSpecifications() spec.name = "adder" component = self.setup_holder.component_repository.get(spec)[0] self.assertEqual("python", component.language)
def testLoadsSimpleCanvasWithEdge(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["out"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Printer" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["in"] text = """<canvas name="main"> <component name="constant_1" type="Constant"></component> <component name="printer" type="Printer"></component> <edge><source socket=out>constant_1</source><target socket=in>printer</target></edge> </canvas>""" self.setup_holder.canvas_loader.load_canvas(text, 0) spec = CreationComponentSpecifications() spec.name = "constant_1" components = self.setup_holder.component_repository.get(spec) graph = components[0].graph self.assertEqual(1, len(graph.get_edges())) self.assertEqual(2, len(graph.get_vertices())) edge = graph.get_edges()[0] self.assertEqual("constant_1", edge.get_source_component_name()) self.assertEqual("printer", edge.get_target_component_name())
def testLoadsFromFile(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["output"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Add" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["left", "right"] filename = "add_constants.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) self.assertEqual(1, self.setup_holder.canvas_repository.count()) self.assertEqual(3, self.setup_holder.component_repository.count()) self.assertEqual( 2, list(self.setup_holder.graph_repository.elements.values()) [0].count_edges())
def testLoadsFullWithVariable(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["out"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Printer" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["in"] text = """<block> <configuration> <variable name="test_variable"><default_value>45</default_value></variable> </configuration> <canvas name="main"> <component name="constant_1" type="Constant"></component> <component name="printer" type="Printer"></component> <edge><source socket=out>constant_1</source><target socket=in>printer</target></edge> </canvas> </block>""" self.setup_holder.block_loader.load_block(text, 0) self.assertEqual(1, self.setup_holder.canvas_repository.count()) self.assertEqual(1, self.setup_holder.variable_repository.count())
def testLoadsSimpleComponent(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) def test_assign(dic): dic["value"] = "test_value" dic["value_type"] = "test_value" component_type.assign_default_value = test_assign text = """<component name="constant_1" type="Constant"><value>5.17</value><type>float</type></component>""" self.setup_holder.component_loader.load_component(text, 0) self.assertEqual(1, self.setup_holder.component_repository.count()) spec = CreationComponentSpecifications() spec.name = "constant_1" components = self.setup_holder.component_repository.get(spec) self.assertEqual(1, len(components)) self.assertEqual("constant_1", components[0].name) self.assertEqual("Constant", components[0].get_component_type_name()) self.assertEqual([("5.17", {})], components[0].get_value_dictionary()["value"]) self.assertEqual([("float", {})], components[0].get_value_dictionary()["type"])
def assign_component_type(self, model, specifications): if specifications.component_type_name is not None: type_specs = ComponentTypeSpecifications() type_specs.name = specifications.component_type_name component_types = self.component_type_repository.get(type_specs) if len(component_types) == 0: raise ComponentTypeNotFoundException( "Attempted to create component with undeclared type " + specifications.component_type_name + ".") component_type = component_types[0] model.component_type = component_type for out_socket_name in model.component_type.get_out_sockets(): out_socket = CreationComponentOutSocket(model, out_socket_name) out_socket.identifier = self.identifier_repository.create() out_socket.language = model.language model.add_out_socket(out_socket) for in_socket_name in model.component_type.get_in_sockets(): in_socket = CreationComponentInSocket(model, in_socket_name) in_socket.identifier = self.identifier_repository.create() in_socket.language = model.language model.add_in_socket(in_socket)
def testCreateWithUndeclaredTypeRaisesException(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" specs = CreationComponentSpecifications() specs.component_type_name = "TestType" with self.assertRaises(ComponentTypeNotFoundException): self.repository.create(specs)
def testEdgeLoadersRaisesExceptionForUnknownTargetSocket(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["out"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Printer" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["in"] text = """<block><canvas name="main"> <component name="constant_1" type="Constant"></component> <component name="printer" type="Printer"></component> <edge><source socket=output>constant_1</source><target socket=hahano>printer</target></edge> </canvas></block>""" with self.assertRaises(SocketNotFoundException): self.setup_holder.block_loader.load_block(text, 0)
def testLoadsFullBlockTwoCanvases(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["out"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Printer" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["in"] text = """<block> <canvas name="main"> <component name="constant_1" type="Constant"></component> <component name="printer" type="Printer"></component> <edge><source socket=out>constant_1</source><target socket=in>printer</target></edge> </canvas> <canvas name="secondary"> <component name="constant_1_s" type="Constant"></component> <component name="printer_s" type="Printer"></component> <edge><source socket=out>constant_1_s</source><target socket=in>printer_s</target></edge> </canvas> </block>""" self.setup_holder.block_loader.load_block(text, 0) self.assertEqual(2, self.setup_holder.canvas_repository.count()) spec = CreationComponentSpecifications() spec.name = "constant_1" component = self.setup_holder.component_repository.get(spec)[0] self.assertEqual("main", component.canvas.name) spec = CreationComponentSpecifications() spec.name = "constant_1_s" component = self.setup_holder.component_repository.get(spec)[0] self.assertEqual("secondary", component.canvas.name)
def testCreateWithComponentTypeName(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.component_type) self.assertEqual(component_type, element.component_type) self.assertEqual("TestType", element.get_component_type_name())
def testCreateAssignsLanguage(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) component_type.languages = ["test_language", "other_test_language"] specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.language) self.assertEqual("test_language", element.language)
def testCreateWithComponentTypeAssignsDefaultValue(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) def test_assign(dic): dic["test_field"] = "test_value" component_type.assign_default_value = test_assign specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.component_value) self.assertEqual({"test_field": "test_value"}, element.component_value)
def testCreateAssignsInSockets(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) component_type.in_sockets = ["in_1", "in_2"] specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.out_sockets) self.assertIsNotNone(element.in_sockets) self.assertEqual(0, element.count_out_sockets()) self.assertEqual(2, element.count_in_sockets()) self.assertIn("in_1", element.in_sockets) self.assertIn("in_1", element.in_sockets) self.assertIsNotNone(element.get_in_socket("in_1")) self.assertIsNotNone(element.get_in_socket("in_2"))