From: Alina Quereilhac Date: Thu, 17 Feb 2011 15:42:57 +0000 (+0100) Subject: graphical information added to boxes and testbed descriptions X-Git-Tag: nepi_v2~201 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=60905d8c857572ebd1c51a7b821318adc79b8b40;p=nepi.git graphical information added to boxes and testbed descriptions --- diff --git a/examples/design1.py b/examples/design1.py index 1765b215..3c6cb769 100644 --- a/examples/design1.py +++ b/examples/design1.py @@ -29,26 +29,10 @@ app = netns_desc.create("Application") app.set_attribute_value("command", "ping -qc10 10.0.0.2") app.connector("node").connect(node1.connector("apps")) -from nepi.util.parser.base import ExperimentParser -p = ExperimentParser() -data = p.to_data(exp_desc) -print data.data +xml = exp_desc.to_xml() exp_desc2 = ExperimentDescription() -p.from_data(exp_desc2, data) -data2 = p.to_data(exp_desc2) -print data2.data -print data.data == data2.data - -from nepi.util.parser._xml import XmlExperimentParser -p = XmlExperimentParser() -xml = p.to_xml(exp_desc) -print xml -exp_desc2 = ExperimentDescription() -p.from_xml(exp_desc2, xml) -xml2 = p.to_xml(exp_desc2) -print xml2 -print xml == xml2 - -#print experiment.xml_description +exp_desc2.from_xml(xml) +xml2 = exp_desc2.to_xml() +assert xml == xml2 diff --git a/src/nepi/core/description.py b/src/nepi/core/description.py index cc3cb076..baae03f7 100644 --- a/src/nepi/core/description.py +++ b/src/nepi/core/description.py @@ -4,6 +4,7 @@ from nepi.core.attributes import AttributesMap, Attribute from nepi.util import validation from nepi.util.guid import GuidGenerator +from nepi.util.graphical_info import GraphicalInfo from nepi.util.parser._xml import XmlExperimentParser import sys @@ -218,6 +219,8 @@ class Box(AttributesMap): # factory attributes for box construction self._factory_attributes = list() + self.graphical_info = GraphicalInfo(str(self._guid)) + for connector_type in factory.connector_types: connector = Connector(self, connector_type) self._connectors[connector_type.name] = connector @@ -428,6 +431,10 @@ class TestbedFactoriesProvider(object): def testbed_version(self): return self._testbed_version + @property + def factories(self): + return self._factories.values() + def factory(self, factory_id): return self._factories[factory_id] @@ -437,9 +444,6 @@ class TestbedFactoriesProvider(object): def remove_factory(self, factory_id): del self._factories[factory_id] - def list_factories(self): - return self._factories.keys() - class TestbedDescription(AttributesMap): def __init__(self, guid_generator, provider): super(TestbedDescription, self).__init__() @@ -447,6 +451,7 @@ class TestbedDescription(AttributesMap): self._guid = guid_generator.next() self._provider = provider self._boxes = dict() + self.graphical_info = GraphicalInfo(str(self._guid)) @property def guid(self): @@ -484,7 +489,6 @@ class TestbedDescription(AttributesMap): class ExperimentDescription(object): def __init__(self, guid = 0): self._guid_generator = GuidGenerator(guid) - # testbed design instances self._testbed_descriptions = dict() @property diff --git a/src/nepi/testbeds/netns/metadata_v01.py b/src/nepi/testbeds/netns/metadata_v01.py index a0faef37..bfa8caa0 100644 --- a/src/nepi/testbeds/netns/metadata_v01.py +++ b/src/nepi/testbeds/netns/metadata_v01.py @@ -46,7 +46,7 @@ def get_metadata(): "Connector from P2PInterface to Node", "node", 1, 1, ["netns_node_devs"]), - ("netns_p2pinterface_p2p", + ("netns_p2piface_p2p", "Connector to another P2PInterface", "p2p", 1, 0, ["netns_p2piface_p2p"]) diff --git a/src/nepi/util/parser/_xml.py b/src/nepi/util/parser/_xml.py index fa21d0aa..5f2e3d6e 100644 --- a/src/nepi/util/parser/_xml.py +++ b/src/nepi/util/parser/_xml.py @@ -31,6 +31,7 @@ class XmlExperimentParser(ExperimentParser): testbed_tag.setAttribute("testbed_id", str(testbed_id)) testbed_tag.setAttribute("testbed_version", str(testbed_version)) parent_tag.appendChild(testbed_tag) + self.graphical_info_data_to_xml(doc, testbed_tag, guid, data) self.attributes_data_to_xml(doc, testbed_tag, guid, data) elements_tag = doc.createElement("elements") testbed_tag.appendChild(elements_tag) @@ -43,15 +44,26 @@ class XmlExperimentParser(ExperimentParser): parent_tag.appendChild(element_tag) element_tag.setAttribute("factory_id", factory_id) element_tag.setAttribute("guid", str(guid)) + self.graphical_info_data_to_xml(doc, element_tag, guid, data) self.factory_attributes_data_to_xml(doc, element_tag, guid, data) self.attributes_data_to_xml(doc, element_tag, guid, data) self.traces_data_to_xml(doc, element_tag, guid, data) self.addresses_data_to_xml(doc, element_tag, guid, data) self.routes_data_to_xml(doc, element_tag, guid, data) self.connections_data_to_xml(doc, element_tag, guid, data) - + + def graphical_info_data_to_xml(self, doc, parent_tag, guid, data): + graphical_info_tag = doc.createElement("graphical_info") + parent_tag.appendChild(graphical_info_tag) + (x, y, width, height, label) = data.get_graphical_info_data(guid) + graphical_info_tag.setAttribute("x", str(x)) + graphical_info_tag.setAttribute("y", str(y)) + graphical_info_tag.setAttribute("width", str(width)) + graphical_info_tag.setAttribute("height", str(height)) + graphical_info_tag.setAttribute("label", str(label)) + def factory_attributes_data_to_xml(self, doc, parent_tag, guid, data): - factory_attributes_tag = doc.createElement("factory_attributes") + factory_attributes_tag = doc.createElement("factory_attributes") parent_tag.appendChild(factory_attributes_tag) for (name, value) in data.get_factory_attribute_data(guid): factory_attribute_tag = doc.createElement("factory_attribute") @@ -142,19 +154,36 @@ class XmlExperimentParser(ExperimentParser): testbed_id = str(tag.getAttribute("testbed_id")) testbed_version = str(tag.getAttribute("testbed_version")) data.add_testbed_data(testbed_guid, testbed_id, testbed_version) + self.graphical_info_data_from_xml(tag, testbed_guid, data) self.attributes_data_from_xml(tag, testbed_guid, data) def box_data_from_xml(self, tag, testbed_guid, data): guid = int(tag.getAttribute("guid")) factory_id = str(tag.getAttribute("factory_id")) data.add_box_data(guid, testbed_guid, factory_id) + self.graphical_info_data_from_xml(tag, guid, data) self.factory_attributes_data_from_xml(tag, guid, data) self.attributes_data_from_xml(tag, guid, data) self.traces_data_from_xml(tag, guid, data) self.addresses_data_from_xml(tag, guid, data) self.routes_data_from_xml(tag, guid, data) self.connections_data_from_xml(tag, guid, data) - + + def graphical_info_data_from_xml(self, tag, guid, data): + graphical_info_tag_list = tag.getElementsByTagName( + "graphical_info") + if len(graphical_info_tag_list) == 0: + return + + graphical_info_tag = graphical_info_tag_list[0] + if graphical_info_tag.nodeType == tag.ELEMENT_NODE: + x = int(graphical_info_tag.getAttribute("x")) + y = int(graphical_info_tag.getAttribute("y")) + width = int(graphical_info_tag.getAttribute("width")) + height = int(graphical_info_tag.getAttribute("height")) + label = str(graphical_info_tag.getAttribute("label")) + data.add_graphical_info_data(guid, x, y, width, height, label) + def factory_attributes_data_from_xml(self, tag, guid, data): factory_attributes_tag_list = tag.getElementsByTagName( "factory_attributes") diff --git a/src/nepi/util/parser/base.py b/src/nepi/util/parser/base.py index feef6ab8..15c18ad1 100644 --- a/src/nepi/util/parser/base.py +++ b/src/nepi/util/parser/base.py @@ -23,6 +23,17 @@ class ExperimentData(object): box_data["factory_id"] = factory_id self.data[guid] = box_data + def add_graphical_info_data(self, guid, x, y, width, height, label): + data = self.data[guid] + if not "graphical_info" in data: + data["graphical_info"] = dict() + graphical_info_data = data["graphical_info"] + graphical_info_data["x"] = x + graphical_info_data["y"] = y + graphical_info_data["width"] = width + graphical_info_data["height"] = height + graphical_info_data["label"] = label + def add_factory_attribute_data(self, guid, name, value): data = self.data[guid] if not "factory_attributes" in data: @@ -100,6 +111,17 @@ class ExperimentData(object): box_data = self.data[guid] return (box_data["testbed_guid"], box_data["factory_id"]) + def get_graphical_info_data(self, guid): + data = self.data[guid] + if not "graphical_info" in data: + return (0, 0, 0, 0, "") + graphical_info_data = data["graphical_info"] + return (graphical_info_data["x"], + graphical_info_data["y"], + graphical_info_data["width"], + graphical_info_data["height"], + graphical_info_data["label"]) + def get_factory_attribute_data(self, guid): data = self.data[guid] if not "factory_attributes" in data: @@ -165,9 +187,12 @@ class ExperimentParser(object): testbed_id = testbed_description.provider.testbed_id testbed_version = testbed_description.provider.testbed_version data.add_testbed_data(guid, testbed_id, testbed_version) + self.graphical_info_to_data(data, guid, + testbed_description.graphical_info) self.attributes_to_data(data, guid, testbed_description.attributes) for box in testbed_description.boxes: data.add_box_data(box.guid, guid, box.factory_id) + self.graphical_info_to_data(data, box.guid, box.graphical_info) self.factory_attributes_to_data(data, box.guid, box.factory_attributes) self.attributes_to_data(data, box.guid, box.attributes) @@ -177,6 +202,10 @@ class ExperimentParser(object): self.routes_to_data(data, box.guid, box.routes) return data + def graphical_info_to_data(self, data, guid, g_info): + data.add_graphical_info_data(guid, g_info.x, g_info.y, g_info.width, + g_info.height, g_info.label) + def factory_attributes_to_data(self, data, guid, attributes): for attribute in attributes: if attribute.modified: @@ -256,11 +285,21 @@ class ExperimentParser(object): self.factory_attributes_from_data(testbed_description, factory_id, guid, data) box = testbed_description.create(factory_id) + self.graphical_info_from_data(box, data) self.attributes_from_data(box, data) self.traces_from_data(box, data) self.addresses_from_data(box, data) self.routes_from_data(box, data) + def graphical_info_from_data(self, element, data): + (x, y, width, height, label) = data.get_graphical_info_data( + element.guid) + element.graphical_info.x = x + element.graphical_info.y = y + element.graphical_info.width = width + element.graphical_info.height = height + element.graphical_info.label = label + def factory_attributes_from_data(self, testbed_description, factory_id, guid, data): factory = testbed_description.provider.factory(factory_id)